"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.Main = void 0;
const Logger_1 = __importDefault(require("../core/libs/common/Logger"));
const plugin_help_1 = require("@oclif/plugin-help");
const update_1 = __importStar(require("./utils/update"));
const config_1 = __importDefault(require("./utils/config"));
const command_1 = __importDefault(require("./extends/command"));
const env_1 = require("./utils/env");
const base_1 = __importDefault(require("./implements/base"));
const EventTracker_1 = __importDefault(require("./adapters/EventTracker"));
const core_1 = require("../core");
let initSpinner;
Logger_1.default.Config = {
    appenders: ['stdout'],
    logPath: config_1.default.CacheRoot,
};
const logger = new Logger_1.default('Index');
logger.info('Init', 'config', 'end', config_1.default);
class Main extends command_1.default {
    static run(argv, options) {
        return super.run(argv, options || __dirname);
    }
    async init() {
        const [id, ...argv] = this.argv;
        await this.config.runHook('init', Object.assign({ id,
            argv }, { spinner: initSpinner }));
        return super.init();
    }
    async run() {
        const [id, ...argv] = this.argv;
        this.parse(Object.assign({ strict: false, '--': false }, this.ctor));
        process.env.COMMAND_ID = id;
        process.env.COMMAND_ARGV = argv.toString();
        initSpinner === null || initSpinner === void 0 ? void 0 : initSpinner.stop();
        process.env.ENTER_BIZ = Date.now().toString();
        const base = new base_1.default();
        base.eventTracker('command_start', { tags: { command: id } });
        let resultType = '';
        let err;
        try {
            await this.config.runCommand(id, argv);
            resultType = 'success';
        }
        catch (e) {
            resultType = 'fail';
            err = e;
        }
        base.eventTracker(`command_end`, {
            tags: { command: id, resultType },
        });
        EventTracker_1.default.prototype[core_1.EventTrackerCmdEnum.End]();
        if (err) {
            throw err;
        }
    }
    _helpOverride() {
        initSpinner === null || initSpinner === void 0 ? void 0 : initSpinner.stop();
        if (['-v', '-V', '--version', 'version'].includes(this.argv[0])) {
            return this._version();
        }
        if (['-h', 'help'].includes(this.argv[0])) {
            return true;
        }
        if (['-a', '--all'].includes(this.argv[0])) {
            return true;
        }
        if (this.argv.length === 0) {
            return true;
        }
        for (const arg of this.argv) {
            if (arg === '--help') {
                return true;
            }
            if (arg === '--') {
                return false;
            }
        }
        return false;
    }
    _help() {
        initSpinner === null || initSpinner === void 0 ? void 0 : initSpinner.stop();
        const HelpClass = (0, plugin_help_1.getHelpClass)(this.config);
        const help = new HelpClass(this.config);
        help.showHelp(this.argv);
        return this.exit(0);
    }
}
exports.Main = Main;
async function run(spinner, argv = process.argv.slice(2), options) {
    initSpinner = spinner;
    initSpinner.start();
    await (0, env_1.setEnv)();
    await (0, update_1.writeUpdateData)(undefined, false);
    await (0, update_1.writeFGData)();
    const command = argv[0];
    switch (command) {
        case 'create':
            if (process.env.DEBUG_TEMPLATES_ROOT) {
                await update_1.default.writeDebugTemplateMeta();
            }
            break;
    }
    process.env.ENTER_OCLIF = Date.now().toString();
    return Main.run(argv, options);
}
exports.run = run;
const { showError } = require('./utils/error');
process.on('uncaughtException', showError);
process.on('unhandledRejection', showError);
