/**
 * Standard exit codes for the CLI.
 *
 * Values start at 10 to avoid collisions with Unix signal numbers (1-9)
 * and shell conventions (e.g. 2 = misuse of builtins, 9 = SIGKILL/OOM).
 * Range 10-125 is safe — below shell-reserved 126-128 and signal-death
 * codes 128+N.
 *
 * 0 and 1 are kept as universal success/failure codes.
 */
export var ExitCode;
(function (ExitCode) {
    ExitCode[ExitCode["SUCCESS"] = 0] = "SUCCESS";
    ExitCode[ExitCode["GENERAL_ERROR"] = 1] = "GENERAL_ERROR";
    ExitCode[ExitCode["VALIDATION_ERROR"] = 10] = "VALIDATION_ERROR";
    ExitCode[ExitCode["AUTH_ERROR"] = 11] = "AUTH_ERROR";
    ExitCode[ExitCode["NOT_FOUND"] = 12] = "NOT_FOUND";
    ExitCode[ExitCode["PERMISSION_ERROR"] = 13] = "PERMISSION_ERROR";
    ExitCode[ExitCode["NETWORK_ERROR"] = 14] = "NETWORK_ERROR";
    ExitCode[ExitCode["FILE_ERROR"] = 15] = "FILE_ERROR";
    ExitCode[ExitCode["USER_CANCELLED"] = 16] = "USER_CANCELLED";
    ExitCode[ExitCode["BUILD_FAILED"] = 17] = "BUILD_FAILED";
    ExitCode[ExitCode["SECURITY_ERROR"] = 18] = "SECURITY_ERROR";
    ExitCode[ExitCode["PAYMENT_REQUIRED"] = 19] = "PAYMENT_REQUIRED";
    ExitCode[ExitCode["UPGRADE_REQUIRED"] = 20] = "UPGRADE_REQUIRED";
})(ExitCode || (ExitCode = {}));
/**
 * Human-readable descriptions for each exit code.
 * This is the single source of truth consumed by the schema generator and AI help.
 */
export const exitCodeDescriptions = {
    [ExitCode.SUCCESS]: 'Success',
    [ExitCode.GENERAL_ERROR]: 'General error',
    [ExitCode.VALIDATION_ERROR]: 'Validation error (invalid arguments or options)',
    [ExitCode.AUTH_ERROR]: 'Authentication error (login required or credentials invalid)',
    [ExitCode.NOT_FOUND]: 'Resource not found (project, file, deployment, etc.)',
    [ExitCode.PERMISSION_ERROR]: 'Permission denied (insufficient access rights)',
    [ExitCode.NETWORK_ERROR]: 'Network error (API unreachable or timeout)',
    [ExitCode.FILE_ERROR]: 'File system error (file read/write failed)',
    [ExitCode.USER_CANCELLED]: 'User cancelled (operation aborted by user)',
    [ExitCode.BUILD_FAILED]: 'Build failed',
    [ExitCode.SECURITY_ERROR]: 'Security error (malware detected)',
    [ExitCode.PAYMENT_REQUIRED]: 'Payment required (plan upgrade needed)',
    [ExitCode.UPGRADE_REQUIRED]: 'Upgrade required (CLI version too old)',
};
/**
 * Standard error codes for the CLI
 */
export var ErrorCode;
(function (ErrorCode) {
    // Validation errors
    ErrorCode["VALIDATION_FAILED"] = "VALIDATION_FAILED";
    ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
    ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
    ErrorCode["INVALID_OPTION"] = "INVALID_OPTION";
    ErrorCode["UNKNOWN_COMMAND"] = "UNKNOWN_COMMAND";
    // Authentication errors
    ErrorCode["AUTH_REQUIRED"] = "AUTH_REQUIRED";
    ErrorCode["AUTH_FAILED"] = "AUTH_FAILED";
    ErrorCode["AUTH_EXPIRED"] = "AUTH_EXPIRED";
    ErrorCode["PERMISSION_DENIED"] = "PERMISSION_DENIED";
    // Project/Configuration errors
    ErrorCode["PROJECT_NOT_FOUND"] = "PROJECT_NOT_FOUND";
    ErrorCode["PROJECT_INVALID"] = "PROJECT_INVALID";
    ErrorCode["CONFIG_NOT_FOUND"] = "CONFIG_NOT_FOUND";
    ErrorCode["CONFIG_INVALID"] = "CONFIG_INVALID";
    // Resource errors
    ErrorCode["RESOURCE_NOT_FOUND"] = "RESOURCE_NOT_FOUND";
    ErrorCode["RESOURCE_ALREADY_EXISTS"] = "RESOURCE_ALREADY_EXISTS";
    ErrorCode["RESOURCE_CONFLICT"] = "RESOURCE_CONFLICT";
    // Organization/Region errors
    ErrorCode["ORG_REQUIRED"] = "ORG_REQUIRED";
    ErrorCode["ORG_NOT_FOUND"] = "ORG_NOT_FOUND";
    ErrorCode["REGION_REQUIRED"] = "REGION_REQUIRED";
    ErrorCode["REGION_NOT_FOUND"] = "REGION_NOT_FOUND";
    ErrorCode["NO_REGIONS_AVAILABLE"] = "NO_REGIONS_AVAILABLE";
    // Network/API errors
    ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
    ErrorCode["API_ERROR"] = "API_ERROR";
    ErrorCode["TIMEOUT"] = "TIMEOUT";
    // File system errors
    ErrorCode["FILE_NOT_FOUND"] = "FILE_NOT_FOUND";
    ErrorCode["FILE_READ_ERROR"] = "FILE_READ_ERROR";
    ErrorCode["FILE_WRITE_ERROR"] = "FILE_WRITE_ERROR";
    ErrorCode["DIRECTORY_NOT_FOUND"] = "DIRECTORY_NOT_FOUND";
    // Runtime errors
    ErrorCode["RUNTIME_ERROR"] = "RUNTIME_ERROR";
    ErrorCode["INTERNAL_ERROR"] = "INTERNAL_ERROR";
    ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
    // User cancellation
    ErrorCode["USER_CANCELLED"] = "USER_CANCELLED";
    // Build failed error
    ErrorCode["BUILD_FAILED"] = "BUILD_FAILED";
    // Integration errors
    ErrorCode["INTEGRATION_FAILED"] = "INTEGRATION_FAILED";
    // Security errors
    ErrorCode["MALWARE_DETECTED"] = "MALWARE_DETECTED";
    // Upgrade of the software is required to continue
    ErrorCode["UPGRADE_REQUIRED"] = "UPGRADE_REQUIRED";
    // Payment required - user needs to upgrade their plan
    ErrorCode["PAYMENT_REQUIRED"] = "PAYMENT_REQUIRED";
})(ErrorCode || (ErrorCode = {}));
/**
 * Map error codes to exit codes
 */
export function getExitCode(errorCode) {
    switch (errorCode) {
        // Validation errors
        case ErrorCode.VALIDATION_FAILED:
        case ErrorCode.MISSING_ARGUMENT:
        case ErrorCode.INVALID_ARGUMENT:
        case ErrorCode.INVALID_OPTION:
        case ErrorCode.UNKNOWN_COMMAND:
        case ErrorCode.PROJECT_INVALID:
        case ErrorCode.CONFIG_INVALID:
            return ExitCode.VALIDATION_ERROR;
        // Authentication errors
        case ErrorCode.AUTH_REQUIRED:
        case ErrorCode.AUTH_FAILED:
        case ErrorCode.AUTH_EXPIRED:
            return ExitCode.AUTH_ERROR;
        // Permission errors
        case ErrorCode.PERMISSION_DENIED:
            return ExitCode.PERMISSION_ERROR;
        // Not found errors
        case ErrorCode.PROJECT_NOT_FOUND:
        case ErrorCode.CONFIG_NOT_FOUND:
        case ErrorCode.RESOURCE_NOT_FOUND:
        case ErrorCode.ORG_NOT_FOUND:
        case ErrorCode.REGION_NOT_FOUND:
        case ErrorCode.FILE_NOT_FOUND:
        case ErrorCode.DIRECTORY_NOT_FOUND:
        case ErrorCode.NO_REGIONS_AVAILABLE:
            return ExitCode.NOT_FOUND;
        // Network/API errors
        case ErrorCode.NETWORK_ERROR:
        case ErrorCode.API_ERROR:
        case ErrorCode.TIMEOUT:
            return ExitCode.NETWORK_ERROR;
        // File system errors
        case ErrorCode.FILE_READ_ERROR:
        case ErrorCode.FILE_WRITE_ERROR:
            return ExitCode.FILE_ERROR;
        // User cancellation
        case ErrorCode.USER_CANCELLED:
            return ExitCode.USER_CANCELLED;
        // Build errors
        case ErrorCode.BUILD_FAILED:
            return ExitCode.BUILD_FAILED;
        // Integration errors
        case ErrorCode.INTEGRATION_FAILED:
            return ExitCode.NETWORK_ERROR;
        // Security errors
        case ErrorCode.MALWARE_DETECTED:
            return ExitCode.SECURITY_ERROR;
        // Payment required - user needs to upgrade their plan
        case ErrorCode.PAYMENT_REQUIRED:
            return ExitCode.PAYMENT_REQUIRED;
        // Upgrade required - CLI version too old
        case ErrorCode.UPGRADE_REQUIRED:
            return ExitCode.UPGRADE_REQUIRED;
        // Resource conflicts and other errors
        case ErrorCode.RESOURCE_ALREADY_EXISTS:
        case ErrorCode.RESOURCE_CONFLICT:
        case ErrorCode.ORG_REQUIRED:
        case ErrorCode.REGION_REQUIRED:
        case ErrorCode.RUNTIME_ERROR:
        case ErrorCode.INTERNAL_ERROR:
        case ErrorCode.NOT_IMPLEMENTED:
        default:
            return ExitCode.GENERAL_ERROR;
    }
}
/**
 * Format error in JSON structure
 */
export function formatErrorJSON(error) {
    const exitCode = error.exitCode ?? getExitCode(error.code);
    const output = {
        error: {
            code: error.code,
            message: error.message,
            exitCode,
        },
    };
    if (error.details) {
        output.error.details = error.details;
    }
    if (error.suggestions && error.suggestions.length > 0) {
        output.error.suggestions = error.suggestions;
    }
    return JSON.stringify(output, null, 2);
}
/**
 * Format error for human-readable output
 */
export function formatErrorHuman(error) {
    let output = `error: ${error.message}`;
    if (error.details && Object.keys(error.details).length > 0) {
        output += '\n\nDetails:';
        for (const [key, value] of Object.entries(error.details)) {
            output += `\n  ${key}: ${JSON.stringify(value)}`;
        }
    }
    if (error.suggestions && error.suggestions.length > 0) {
        output += '\n\nSuggestions:';
        for (const suggestion of error.suggestions) {
            output += `\n  - ${suggestion}`;
        }
    }
    return output;
}
/**
 * Exit the process with a structured error
 */
export function exitWithError(error, logger, errorFormat = 'text') {
    if (errorFormat === 'json') {
        console.error(formatErrorJSON(error));
    }
    else {
        logger.error(formatErrorHuman(error));
    }
    const exitCode = error.exitCode ?? getExitCode(error.code);
    process.exit(exitCode);
}
/**
 * Create a structured error from a simple message and code
 */
export function createError(code, message, details, suggestions) {
    return { code, message, details, suggestions };
}
//# sourceMappingURL=errors.js.map