"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const open_1 = __importDefault(require("open"));
const inquirer_1 = __importDefault(require("inquirer"));
const chalk_1 = __importDefault(require("chalk"));
const terminal_link_1 = __importDefault(require("terminal-link"));
const core_1 = require("../../core");
const ora_1 = __importDefault(require("ora"));
const AppName = {
    darwin: 'google chrome',
    linux: 'google-chrome',
    win32: 'chrome',
};
function getMsg(pre, msg) {
    return pre ? `${pre}(${msg})` : msg;
}
class UI extends core_1.UIModule {
    [core_1.UICmdEnum.pick](list, message) {
        return inquirer_1.default
            .prompt([
            {
                type: 'list',
                name: 'pick',
                message,
                choices: list,
            },
        ])
            .then((ans) => ans.pick);
    }
    [core_1.UICmdEnum.showInfo](info) {
        const msg = getMsg(info.title, info.content);
        if (info.type) {
            switch (info.type) {
                case 'error':
                    console.error(chalk_1.default.red(msg));
                    break;
                case 'info':
                    console.log(msg);
                    break;
                case 'warn':
                    console.warn(chalk_1.default.yellow(msg));
            }
        }
        else {
            console.log(msg);
        }
    }
    [core_1.UICmdEnum.confirm](info) {
        return inquirer_1.default
            .prompt([
            {
                name: 'confirm',
                message: getMsg(info.title, info.content),
                default: info.default,
                type: 'confirm',
            },
        ])
            .then((ans) => ans.confirm);
    }
    [core_1.UICmdEnum.input](info) {
        return inquirer_1.default
            .prompt([
            {
                name: 'input',
                type: 'input',
                message: getMsg(info.label, info.placeholder),
                validate(input) {
                    if (info.checker) {
                        return info.checker(input);
                    }
                    return true;
                },
                default: info.default,
            },
        ])
            .then((ans) => ans.input);
    }
    [core_1.UICmdEnum.openPage](url, options) {
        if (options === null || options === void 0 ? void 0 : options.forceChrome) {
            const opt = {
                wait: true,
            };
            if (process.platform in AppName) {
                opt.app = [AppName[process.platform]];
            }
            (0, open_1.default)(url, opt).catch((error) => {
                if (error.message.includes('Exited with code')) {
                    throw new Error('Chrome is not installed, the simulator requires Chrome');
                }
                throw error;
            });
        }
        else {
            (0, open_1.default)(url);
        }
        return Promise.resolve();
    }
    [core_1.UICmdEnum.link](url, tooltip) {
        return (0, terminal_link_1.default)(tooltip || '', url, {
            fallback(text, url) {
                return url;
            },
        });
    }
    [core_1.UICmdEnum.dialog](info) {
        console.log('info', info);
    }
    [core_1.UICmdEnum.showLoading](info) {
        const loading = (0, ora_1.default)({
            text: info.text,
        });
        loading.start();
        return {
            hideLoading: () => loading.stop(),
        };
    }
    async [core_1.UICmdEnum.multiPick](list, message) {
        const ans = await inquirer_1.default.prompt([
            {
                type: 'checkbox',
                name: 'multiSelect',
                message,
                choices: list,
            },
        ]);
        return ans.multiSelect;
    }
}
exports.default = UI;
