"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCliWorkerTerminalUI = void 0;
//#region imports
const lib_1 = require("tnp-core/lib");
const lib_2 = require("tnp-core/lib");
const lib_3 = require("tnp-core/lib");
class BaseCliWorkerTerminalUI {
    worker;
    constructor(worker) {
        this.worker = worker;
    }
    //#region protected methods / text for header
    async headerText() {
        return lib_3._.startCase(this.worker.serviceID);
    }
    //#endregion
    //#region protected methods / text header style
    textHeaderStyle() {
        return 'block';
    }
    //#endregion
    //#region protected methods / header text align
    headerTextAlign() {
        return 'left';
    }
    //#endregion
    //#region protected methods / header
    /**
     * override whole terminal header
     */
    async header() {
        //#region @backendFunc
        const headerText = await this.headerText();
        if (headerText) {
            lib_2.UtilsTerminal.drawBigText(await this.headerText(), {
                align: this.headerTextAlign(),
                style: this.textHeaderStyle(),
            });
        }
        //#endregion
    }
    //#endregion
    //#region protected methods / info message below header
    async infoMessageBelowHeader(options) {
        //#region @backendFunc
        options = options || {};
        lib_2.Helpers.info(`
      Service ${lib_3.chalk.bold.red(this.worker.serviceID)}` +
            ` (version: ${this.worker.serviceVersion}) started..
      Check info here ${this.worker.getWorkerInfoGuiUrl(options)}
      Worker started by ${lib_3.chalk.bold(lib_1.config.frameworkName)}
      (cwd: ${(0, lib_2.crossPlatformPath)(process.cwd())})
        `);
        //#endregion
    }
    //#endregion
    //#region protected methods / get back action
    get backAction() {
        return {
            back: {
                name: 'Back',
            },
        };
    }
    //#endregion
    //#region protected methods / choose action
    get chooseAction() {
        return {
            emptyAction: {
                name: ' -- choose any action below --',
                action: async () => { },
            },
        };
    }
    //#endregion
    //#region protected methods / worker terminal actions
    getWorkerTerminalActions(options) {
        //#region @backendFunc
        options = options || {};
        options.chooseAction = lib_3._.isBoolean(options.chooseAction)
            ? options.chooseAction
            : true;
        return {
            ...(options.chooseAction ? this.chooseAction : {}),
            openBrowser: {
                name: 'Open browser with service info',
                action: async () => {
                    const openInBrowser = require('open');
                    openInBrowser(this.worker.getWorkerInfoGuiUrl());
                },
            },
            exit: {
                name: options.exitIsOnlyReturn
                    ? '< Return to previous menu'
                    : `Shut down service`,
                action: async () => {
                    if (options.exitIsOnlyReturn) {
                        return true; // false will keep loop running
                    }
                    if (await lib_2.UtilsTerminal.confirm({
                        defaultValue: false,
                        message: 'Are you sure you want to shut down service?',
                    })) {
                        await this.worker.kill({
                            reason: 'user kill from menu',
                        });
                        process.exit(0);
                    }
                },
            },
        };
        //#endregion
    }
    //#endregion
    //#region protected methods / info screen
    headerDisplayed = false;
    showWorkerInfoScreen = true;
    async infoScreen(options) {
        options = options || {};
        while (true) {
            if (!lib_2.UtilsTerminal.isVerboseModeTaon()) {
                lib_2.UtilsTerminal.clearConsole();
            }
            const choices = this.getWorkerTerminalActions(options);
            if (this.headerDisplayed) {
                await this.infoMessageBelowHeader();
            }
            else {
                this.headerDisplayed = true;
                if (this.showWorkerInfoScreen) {
                    await this.header();
                    await lib_2.UtilsTerminal.pressAnyKeyToContinueAsync({
                        message: `\nWelcome to ${lib_3.chalk.bold(this.worker.serviceID) +
                            lib_3.chalk.bold('@') +
                            lib_3.chalk.bold(this.worker.serviceVersion)} Terminal UI.` +
                            `\n
              Press any key to continue...`,
                    });
                }
                lib_2.UtilsTerminal.clearConsole();
                await this.infoMessageBelowHeader();
            }
            const choice = await lib_2.UtilsTerminal.select({
                choices,
                question: 'Choose action',
            });
            const action = choices[choice].action;
            const result = await action();
            if (choice === 'exit' && result) {
                break;
            }
        }
    }
    //#endregion
    //#region protected methods / display special worker ready message
    displaySpecialWorkerReadyMessage() {
        console.log(this.worker.SPECIAL_WORKER_READY_MESSAGE);
    }
}
exports.BaseCliWorkerTerminalUI = BaseCliWorkerTerminalUI;
//# sourceMappingURL=base-cli-worker-terminal-ui.js.map