"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.showError = void 0;
const errors_1 = require("@oclif/errors");
const Error_1 = require("../../core/libs/common/Error");
const request_error_1 = require("../../core/libs/common/request-error");
const compile_error_1 = require("../../core/libs/common/compile-error");
const core_1 = require("../../core");
const env_1 = require("./env");
function processError(err) {
    if ((0, request_error_1.isRequestError)(err)) {
        return [
            err.message,
            `Code: ${err.code}`,
            err.advise ? `Advise: ${err.advise}` : '',
            `LogID: ${err.logId}`,
        ]
            .filter(Boolean)
            .join('\n');
    }
    if ((0, compile_error_1.isCompileError)(err)) {
        return [
            err.message,
            `Code: ${err.code}`,
            err.logId ? `LogID: ${err.logId}` : '',
        ]
            .filter(Boolean)
            .join('\n');
    }
    return `${err.message}\nCode: ${err.name || 'UNKNOWN_ERROR'}${err.advise ? `\nAdvise: ${err.advise}` : ''}`;
}
function showError(err) {
    if (env_1.isDev) {
        (0, core_1.logger)().debug(err.stack);
    }
    const errorMessage = processError(err);
    (0, errors_1.error)(errorMessage, { exit: false });
    (0, core_1.logger)().error('Common', 'running', 'fail', errorMessage);
    const exitCode = !err.code || typeof err.code === 'string'
        ? Error_1.CoreErrorCode.UNKNOWN
        : err.code;
    setTimeout(() => {
        process.exit(exitCode);
    });
}
exports.showError = showError;
