"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const caporal_1 = tslib_1.__importDefault(require("caporal"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const terminal_kit_1 = require("terminal-kit");
const Codegen_1 = require("./Codegen");
async function generate(_, originalArgs, _logger) {
    // eslint-disable-next-line no-restricted-syntax
    for (const key in originalArgs) {
        if (typeof originalArgs[key] === 'boolean') {
            if (originalArgs[key] === true) {
                if (/no[A-Z]/.exec(key)) {
                    // eslint-disable-next-line no-param-reassign
                    originalArgs[key] = !originalArgs[key];
                }
            }
            else {
                // eslint-disable-next-line no-param-reassign
                delete originalArgs[key];
            }
        }
    }
    const args = originalArgs;
    const configPath = path_1.default.isAbsolute(args.config) ? args.config : path_1.default.join(process.cwd(), args.config);
    // eslint-disable-next-line import/no-dynamic-require, global-require
    const config = await Promise.resolve(require(configPath));
    if (args.force !== undefined) {
        config.force = args.force;
    }
    if (args.optimize !== undefined) {
        config.optimize = args.optimize;
    }
    let progress;
    let lastItem;
    function progressCallback(n, m, label, item) {
        if (!progress) {
            progress = terminal_kit_1.terminal.progressBar({
                width: Math.min(process.stdout.columns - 10, 120),
                title: 'Processing input structs',
                eta: true,
                percent: true,
                syncMode: false,
                items: m,
                inline: true,
            });
        }
        if (lastItem) {
            progress.itemDone(lastItem);
            lastItem = undefined;
        }
        lastItem = item ? item.Type.TypeName : label;
        if (lastItem) {
            progress.startItem(lastItem);
        }
        progress.update({ progress: n / m, items: m });
    }
    await Codegen_1.codegen(config, { progressCallback });
    if (progress) {
        progress.stop();
    }
    terminal_kit_1.terminal('\n');
}
const { version } = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../package.json'), 'utf8'));
const cli = caporal_1.default.version(version).description('Il2Js codegen tool');
cli
    .command('generate', 'Generates code using the provided configuration')
    .option('--config <config>', 'Path to configuration file', caporal_1.default.STRING, path_1.default.join(process.cwd(), '.il2js.config.js'))
    .option('--force', 'Force build without caching')
    .option('--optimize', 'Enable optimization', caporal_1.default.BOOLEAN)
    .option('--no-optimize', 'Disable optimization', caporal_1.default.BOOLEAN)
    .action(generate);
cli.parse(process.argv);
//# sourceMappingURL=Cli.js.map