"use strict";
/*
 * @adonisjs/ace
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnknownFlagException = exports.InvalidCommandException = exports.InvalidFlagException = exports.MissingArgumentException = void 0;
const cliui_1 = require("@poppinss/cliui");
const utils_1 = require("@poppinss/utils");
/**
 * Raised when a required argument is missing
 */
class MissingArgumentException extends utils_1.Exception {
    /**
     * A required argument is missing
     */
    static invoke(name, command) {
        const exception = new this(`Missing required argument "${name}"`, 500, 'E_MISSING_ARGUMENT');
        exception.argumentName = name;
        exception.command = command;
        return exception;
    }
    /**
     * Handle itself
     */
    handle(error) {
        cliui_1.logger.error(cliui_1.logger.colors.red(`Missing required argument "${error.argumentName}"`));
    }
}
exports.MissingArgumentException = MissingArgumentException;
/**
 * Raised when an the type of a flag is not as one of the excepted type
 */
class InvalidFlagException extends utils_1.Exception {
    /**
     * Flag type validation failed.
     */
    static invoke(prop, expected, command) {
        let article = 'a';
        if (expected === 'number') {
            expected = 'numeric';
        }
        if (expected === 'array') {
            article = 'an';
            expected = 'array of strings';
        }
        if (expected === 'numArray') {
            article = 'an';
            expected = 'array of numbers';
        }
        const exception = new this(`"${prop}" flag expects ${article} "${expected}" value`, 500, 'E_INVALID_FLAG');
        exception.flagName = prop;
        exception.command = command;
        exception.expectedType = expected;
        return exception;
    }
    /**
     * Handle itself
     */
    handle(error) {
        cliui_1.logger.error(cliui_1.logger.colors.red(`Expected "--${error.flagName}" to be a valid "${error.expectedType}"`));
    }
}
exports.InvalidFlagException = InvalidFlagException;
/**
 * Raised when command is not registered with kernel
 */
class InvalidCommandException extends utils_1.Exception {
    constructor() {
        super(...arguments);
        this.suggestions = [];
    }
    static invoke(commandName, suggestions) {
        const exception = new this(`"${commandName}" is not a registered command`, 500, 'E_INVALID_COMMAND');
        exception.commandName = commandName;
        exception.suggestions = suggestions;
        return exception;
    }
    /**
     * Handle itself
     */
    handle(error) {
        cliui_1.logger.error(`"${error.commandName}" command not found`);
        if (!error.suggestions.length) {
            return;
        }
        cliui_1.logger.log('');
        const suggestionLog = (0, cliui_1.sticker)().heading('Did you mean one of these?');
        error.suggestions.forEach((commandName) => {
            suggestionLog.add(cliui_1.logger.colors.yellow(commandName));
        });
        suggestionLog.render();
    }
}
exports.InvalidCommandException = InvalidCommandException;
/**
 * Raised when an unknown flag is defined
 */
class UnknownFlagException extends utils_1.Exception {
    /**
     * Unknown flag
     */
    static invoke(prop) {
        const exception = new this(`Unknown flag "${prop}"`, 500, 'E_INVALID_FLAG');
        return exception;
    }
    /**
     * Handle itself
     */
    handle(error) {
        cliui_1.logger.error(cliui_1.logger.colors.red(error.message));
    }
}
exports.UnknownFlagException = UnknownFlagException;
