//#region imports
import { config } from 'tnp-core/lib-prod';
import { crossPlatformPath, Helpers__NS__info, UtilsTerminal__NS__clearConsole, UtilsTerminal__NS__confirm, UtilsTerminal__NS__drawBigText, UtilsTerminal__NS__isVerboseModeTaon, UtilsTerminal__NS__pressAnyKeyToContinueAsync, UtilsTerminal__NS__select } from 'tnp-core/lib-prod';
import { chalk, ___NS__isBoolean, ___NS__startCase } from 'tnp-core/lib-prod';
export class BaseCliWorkerTerminalUI {
    worker;
    constructor(worker) {
        this.worker = worker;
    }
    //#region protected methods / text for header
    async headerText() {
        return ___NS__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) {
            UtilsTerminal__NS__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 || {};
        Helpers__NS__info(`
      Service ${chalk.bold.red(this.worker.serviceID)}` +
            ` (version: ${this.worker.serviceVersion}) started..
      Check info here ${this.worker.getWorkerInfoGuiUrl(options)}
      Worker started by ${chalk.bold(config.frameworkName)}
      (cwd: ${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 = ___NS__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 UtilsTerminal__NS__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 (!UtilsTerminal__NS__isVerboseModeTaon()) {
                UtilsTerminal__NS__clearConsole();
            }
            const choices = this.getWorkerTerminalActions(options);
            if (this.headerDisplayed) {
                await this.infoMessageBelowHeader();
            }
            else {
                this.headerDisplayed = true;
                if (this.showWorkerInfoScreen) {
                    await this.header();
                    await UtilsTerminal__NS__pressAnyKeyToContinueAsync({
                        message: `\nWelcome to ${chalk.bold(this.worker.serviceID) +
                            chalk.bold('@') +
                            chalk.bold(this.worker.serviceVersion)} Terminal UI.` +
                            `\n
              Press any key to continue...`,
                    });
                }
                UtilsTerminal__NS__clearConsole();
                await this.infoMessageBelowHeader();
            }
            const choice = await UtilsTerminal__NS__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);
    }
}
//# sourceMappingURL=c:/Users/darek/projects/npm/taon-dev/tnp-helpers/dist-nocutsrc-prod/lib/base/classes/base-cli-worker/base-cli-worker-terminal-ui.js.map