"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("reflect-metadata");
const core_1 = require("@oclif/core");
const command_base_exec_tenant_1 = require("../../command-base-exec-tenant");
const uilogic_1 = require("../../uilogic");
const ops_1 = require("../../ops");
class TenantSwitch extends command_base_exec_tenant_1.CommandBaseExecTenant {
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(TenantSwitch);
            if (this._projCfg && this._projCfg.tenantCode !== undefined && this._projCfg.tenantCode !== ops_1.DxConstants.UNREGISTERED_TENANT_CODE) {
                throw new Error(`Current tenant [${this._styler.hilite(this._projCfg.tenantCode)}]; cannot switch tenant from this folder; it exists within a workspace declared for that tenant (see the nearest codiac.json file).`);
            }
            return [{}, false];
        });
    }
    buildCandidate_silently(_partial, _rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args } = yield this.parse(TenantSwitch);
            if (!args.code)
                throw new Error(`Tenant code argument is required in silent mode.`);
            const contextor = new uilogic_1.TenantContextualizer(this.cliEnviro, this._projCfg, this.authClient, this.projCfgRepo, this._ui, this._styler, this.logger);
            const newContext = yield contextor.trySwitchTenant(this._userCntx, args.code);
            if (!newContext)
                throw new Error(`Failed to switch to tenant [${args.code}].`);
            return { newContext };
        });
    }
    buildCandidate_interactively(_partial, _takeDefaults, _rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args } = yield this.parse(TenantSwitch);
            const contextor = new uilogic_1.TenantContextualizer(this.cliEnviro, this._projCfg, this.authClient, this.projCfgRepo, this._ui, this._styler, this.logger);
            let newContext;
            if (args.code) {
                newContext = yield contextor.trySwitchTenant(this._userCntx, args.code);
            }
            newContext = newContext !== null && newContext !== void 0 ? newContext : yield contextor.trySelectFromMyTenants(this._userCntx, false);
            if (!newContext)
                return undefined;
            return { newContext };
        });
    }
    buildCommandString(candidate, cmd) {
        if (candidate.newContext.currentTenantCode)
            cmd.push(candidate.newContext.currentTenantCode);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this._userCntx = candidate.newContext;
            this.bindUserInIoc(this._userCntx);
            if (this._userCntx.currentTenantCode) {
                this._ui.userMessage(`Switched to tenant [${this._styler.hilite(this._userCntx.currentTenantCode)}]`);
            }
            else {
                this._ui.userMessage(`Current user context is tenant-less.`);
            }
        });
    }
}
exports.default = TenantSwitch;
TenantSwitch.description = "Sets a different tenant context from the current user's list of tenants.";
TenantSwitch.args = {
    code: core_1.Args.string({ required: false, description: 'The unique string identifying the tenant to switch to.' })
};
TenantSwitch.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    silent: core_1.Flags.boolean({ exclusive: ["echo", "to-script"], description: 'Executes without any user interaction; fails on missing or invalid arguments.' }),
    echo: core_1.Flags.boolean({ exclusive: ["to-script", "silent"], description: 'Renders the equivalent non-interactive command for future use before executing.' }),
    "to-script": core_1.Flags.boolean({ exclusive: ["echo", "silent"], description: 'Renders the equivalent non-interactive command without executing it.' }),
};
TenantSwitch.examples = [
    { description: "Switch tenant interactively.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Switch to a specific tenant.", command: "<%= config.bin %> <%= command.id %> my-tenant-code" },
    { description: "Non-interactive invocation.", command: "<%= config.bin %> <%= command.id %> my-tenant-code --silent" },
    { description: "Renders the equivalent silent command without executing.", command: "<%= config.bin %> <%= command.id %> my-tenant-code --to-script" },
];
//# sourceMappingURL=switch.js.map