"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("reflect-metadata");
const core_1 = require("@oclif/core");
const ops_1 = require("../../ops");
const command_base_exec_tenant_1 = require("../../command-base-exec-tenant");
const uilogic_1 = require("../../uilogic");
class TenantCreate extends command_base_exec_tenant_1.CommandBaseExecTenant {
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(TenantCreate);
            if (this._projCfg && this._projCfg.tenantCode !== undefined && this._projCfg.tenantCode !== ops_1.DxConstants.UNREGISTERED_TENANT_CODE) {
                throw new Error(`Cannot create a new tenant from within a project folder with a tenant already declared.`);
            }
            return [{}, false];
        });
    }
    buildCandidate_silently(_partial, _rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(TenantCreate);
            if (!flags.name)
                throw new Error(`Tenant name (--name / -n) 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);
            yield contextor.establishPrincipalContext(this._userCntx);
            const newContext = yield contextor.tryRegisterNewTenant(this._userCntx, flags.name, flags.code);
            if (!newContext)
                throw new Error(`Unable to create a new tenant.`);
            if (newContext.currentToken)
                this._codiacApi.setUserToken(newContext.currentToken);
            this.bindUserInIoc(newContext);
            return { newContext };
        });
    }
    buildCandidate_interactively(_partial, _takeDefaults, _rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(TenantCreate);
            const contextor = new uilogic_1.TenantContextualizer(this.cliEnviro, this._projCfg, this.authClient, this.projCfgRepo, this._ui, this._styler, this.logger);
            yield contextor.establishPrincipalContext(this._userCntx);
            const newContext = yield contextor.tryRegisterNewTenant(this._userCntx, flags.name, flags.code);
            if (!newContext)
                throw new Error(`Unable to create a new tenant.`);
            if (newContext.currentToken)
                this._codiacApi.setUserToken(newContext.currentToken);
            this.bindUserInIoc(newContext);
            return { newContext };
        });
    }
    buildCommandString(candidate, cmd) {
        if (candidate.newContext.currentTenantCode)
            cmd.push("-c", candidate.newContext.currentTenantCode);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this._userCntx = candidate.newContext;
            if (this._userCntx.currentTenantCode) {
                this._ui.userMessage(`Created and switched to tenant [${this._styler.hilite(this._userCntx.currentTenantCode)}]`);
            }
            else {
                this._ui.userMessage(`Tenant created; current user context is tenant-less.`);
            }
        });
    }
}
exports.default = TenantCreate;
TenantCreate.description = 'Registers a new tenant with the current user as the administrator.';
TenantCreate.args = {
    name: core_1.Args.string({ required: false, description: 'The name of the tenant to create.' })
};
TenantCreate.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    name: core_1.Flags.string({ char: 'n', description: "Full name of the tenant organization." }),
    code: core_1.Flags.string({ char: 'c', description: "(Optional, not recommended) Proposed human-readable unique identifier code for the tenant organization. This command will fail if the given tenant code is already taken." }),
    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.' }),
};
TenantCreate.examples = [
    { description: "Create a tenant interactively.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Create a tenant with a given name.", command: "<%= config.bin %> <%= command.id %> -n 'Acme Corp'" },
    { description: "Non-interactive invocation.", command: "<%= config.bin %> <%= command.id %> -n 'Acme Corp' -c acme --silent" },
    { description: "Renders the equivalent silent command without executing.", command: "<%= config.bin %> <%= command.id %> -n 'Acme Corp' -c acme --to-script" },
];
//# sourceMappingURL=create.js.map