"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const command_base_exec_tenant_1 = require("../../command-base-exec-tenant");
const prettify_1 = require("../../prettify");
const cloud_provider_contextualizer_1 = require("../../uilogic/cloud-provider-contextualizer");
class SecretStoreForget extends command_base_exec_tenant_1.CommandBaseExecTenant {
    bootstrap() {
        super.bootstrap();
        cloud_provider_contextualizer_1.CloudProviderContextualizer.bootstrap(this.ioc);
    }
    // --------------------------------------------------------------------
    // Lazy-loaded properties
    // --------------------------------------------------------------------
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(SecretStoreForget);
            let partial = {};
            if (flags.name)
                partial.name = flags.name;
            return [partial, false];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.name)
                throw new Error("A name must be set");
            let existings = yield this._codiacApi.KvpStoreDef.readMany({ all: true }, { throwNotFound: false });
            let found = existings.find(r => r.name === partial.name);
            if (!found)
                throw new Error(`No secret store record currently exists with name: [${partial.name}]. Either it may not be captured or you might need to check your spelling.`);
            return found;
        });
    }
    buildCandidate_interactively(partial, takeDefaults, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let existings = yield this._codiacApi.KvpStoreDef.readMany({ all: true }, { throwNotFound: false });
            if (existings.length === 0)
                throw new Error(`Cannot forget: No secret stores are defined yet.`);
            let confirmed;
            let candidate;
            while (!confirmed) {
                let choices = existings.map((x, index) => { return { name: `${x.name} (${x.type})`, value: index }; });
                let selectedIndex = (yield this._ui.promptSelect("condemned", "Secret Store: ", choices));
                candidate = existings[selectedIndex];
                this._ui.userMessage((0, prettify_1.prettify)(candidate));
                this._ui.userMessage(`You are about to remove the record for ${candidate.type} secret store ${this._styler.bold(this._styler.hilite(candidate.name))} [Tenant: ${this._userCntx.currentTenantCode}].`);
                confirmed = yield this._ui.promptConfirm("confirm", "Continue?", false);
            }
            if (candidate === undefined)
                throw new Error("No secret store was defined.");
            return candidate;
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push("--name", candidate.name);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let result = yield this._codiacApi.KvpStoreDef.tryDelete({ id: candidate.id });
            if (result.success) {
                this._ui.userMessage(`Successfully removed ${candidate.type} secret store [${this._styler.hilite(candidate.name)}]`);
            }
            else if (result.error) {
                this._ui.userMessage(`${this._styler.colorCritical("Error")} removing ${candidate.type} secret store [${candidate.name}]: ${result.error.name} ${(_a = result.error.message) !== null && _a !== void 0 ? _a : result.error.stack}`);
            }
        });
    }
}
exports.default = SecretStoreForget;
SecretStoreForget.description = 'Removes the reference to the given secret store for the tenant.  Does NOT delete the store itself.';
SecretStoreForget.aliases = [];
SecretStoreForget.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    name: core_1.Flags.string({ char: 'n', required: false, description: "The name of the secret store to forget." }),
    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.' }),
};
SecretStoreForget.args = {};
//# sourceMappingURL=forget.js.map