/* eslint-disable import/default */
import { Command, Flags, ux } from "@oclif/core";
import log4js from "log4js";
import locatorService from "../../services/locator.service.js";
import { obfuscateLicense } from "../../utils/license-obfuscate.js";
export class Configure extends Command {
    static description = "Configure your io.Connect CLI with correct credentials.";
    static examples = [
        {
            command: "<%= config.bin %> <%= command.id %>",
            description: "Starts an interactive flow to configure your io.Connect CLI."
        },
        {
            command: "<%= config.bin %> <%= command.id %> --token <license-token>",
            description: "Configures your io.Connect CLI with the provided license token."
        },
        {
            command: "<%= config.bin %> <%= command.id %> --mode <auto-or-remote-or-offline>",
            description: "Configures your io.Connect CLI mode to use when fetching app templates."
        },
        {
            command: "<%= config.bin %> <%= command.id %> --help",
            description: "Shows help for the configure command."
        }
    ];
    static flags = {
        mode: Flags.string({
            char: "m",
            description: "The mode to use for fetching the templates - either from a remote git repository or from an embedded collection.",
            multiple: false,
            options: ["auto", "remote", "offline"],
            required: false,
        }),
        token: Flags.string({
            char: "t",
            description: "Your io.Connect Browser license key.",
            multiple: false,
            required: false,
        })
    };
    static usage = "configure";
    async run() {
        const logger = log4js.getLogger("Command: Configure");
        const { flags } = await this.parse(Configure);
        logger.info(`Configuring your io.Connect CLI with cli flags: mode: ${flags.mode}, token: ${flags.token ? obfuscateLicense(flags.token) : "no token"}.}`);
        const token = await this.getToken(flags.token);
        const mode = await this.getMode(flags.mode);
        logger.info(`Configuring your io.Connect CLI with mode: ${mode} and token: ${token ? obfuscateLicense(token) : "no token"}.`);
        await locatorService.configurationService.setCliConfig({ mode, token });
        ux.info(`Successfully configured your io.Connect CLI.`);
    }
    async getMode(flagsMode) {
        const mode = (flagsMode || await locatorService.promptingService.select({
            choices: [
                {
                    description: "[Recommended] Will try to fetch the latest templates from a remote repo and fallback to am embedded collection upon remote failure.",
                    name: "auto",
                    value: "auto",
                },
                {
                    description: "The app templates will be fetched from a remote repository.",
                    name: "remote",
                    value: "remote",
                },
                {
                    description: "The app templates will be fetched from an embedded collection. No internet connection is required.",
                    name: "offline",
                    value: "offline",
                }
            ],
            message: "Select CLI mode."
        }));
        return mode;
    }
    async getToken(flagsToken) {
        const existingToken = await locatorService.configurationService.getLicenseKey();
        const keyMessage = existingToken ? `[***${obfuscateLicense(existingToken)}] IO.Connect Browser license key:` : "IO.Connect Browser license key:";
        const token = flagsToken || await locatorService.promptingService.input({ message: keyMessage }) || existingToken;
        return token;
    }
}
