"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const contracts_1 = require("../../apis/codiac-api/contracts");
const command_base_exec_tenant_1 = require("../../command-base-exec-tenant");
const prettify_1 = require("../../prettify");
const cloud_resource_configurator_factory_1 = require("../../uilogic/cloud-resource-configurator-factory");
const cloud_provider_contextualizer_1 = require("../../uilogic/cloud-provider-contextualizer");
const image_registry_contextualizer_1 = require("../../uilogic/image-registry-contextualizer");
const image_contextualizer_1 = require("../../uilogic/image-contextualizer");
// Has to be module-scoped bc the autocomplete redefines "this", so we can't use it inside searchPorts().
let ports = [];
class ImageRegistryForget extends command_base_exec_tenant_1.CommandBaseExecTenant {
    bootstrap() {
        super.bootstrap();
        cloud_provider_contextualizer_1.CloudProviderContextualizer.bootstrap(this.ioc);
        this.ioc.bind(image_registry_contextualizer_1.ImageRegistryContextualizer).toSelf();
        this.ioc.bind(image_contextualizer_1.ImageContextualizer).toSelf();
    }
    // --------------------------------------------------------------------
    // Lazy-loaded properties
    get uiRegistry() {
        if (this._uiRegistry_backer === undefined) {
            this._uiRegistry_backer = this.ioc.get(image_registry_contextualizer_1.ImageRegistryContextualizer);
        }
        return this._uiRegistry_backer;
    }
    // --------------------------------------------------------------------
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(ImageRegistryForget);
            let partial = {};
            if (flags.registry)
                partial.code = flags.registry;
            return [partial, false];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.code)
                throw new Error("A registry code must be set");
            let registries = yield this._codiacApi.ImageRegistry.readMany({ all: true }, { throwNotFound: false });
            let found = registries.find(r => r.code === partial.code);
            if (!found)
                throw new Error(`No ImageRegistry record currently exists with code: [${partial.code}]. You might need to check your spelling or capture your image registry.`);
            return found;
        });
    }
    buildCandidate_interactively(partial, takeDefaults, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            cloud_resource_configurator_factory_1.RegistryConfiguratorFactory.bootstrap(this.ioc);
            let registries = yield this._codiacApi.ImageRegistry.readMany({ all: true }, { throwNotFound: false });
            if (registries.length === 0)
                throw new Error(`Cannot forget: No image registries are defined yet.`);
            let confirmed;
            let candidate;
            while (!confirmed) {
                candidate = (yield this.uiRegistry.promptForExistingRegistry(contracts_1.PackageType.container, false, registries));
                this._ui.userMessage((0, prettify_1.prettify)(candidate));
                this._ui.userMessage(`You are about to remove the record for ${candidate.type} image registry ${this._styler.bold(this._styler.hilite(candidate.code))} [Tenant: ${this._userCntx.currentTenantCode}].`);
                confirmed = yield this._ui.promptConfirm("confirm", "Continue?", false);
            }
            if (candidate === undefined)
                throw new Error("No ImageRegistry was defined.");
            return candidate;
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push("--registry", candidate.code);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let result = yield this._codiacApi.ImageRegistry.tryDelete({ id: candidate.id });
            if (result.success) {
                this._ui.userMessage(`Successfully removed ${candidate.type} image registry [${this._styler.hilite(candidate.code)}]`);
            }
            else if (result.error) {
                this._ui.userMessage(`${this._styler.colorCritical("Error")} removing ${candidate.type} image registry [${candidate.code}]: ${result.error.name} ${(_a = result.error.message) !== null && _a !== void 0 ? _a : result.error.stack}`);
            }
        });
    }
}
exports.default = ImageRegistryForget;
ImageRegistryForget.description = 'Removes the reference to the given container registry for the tenant.  Does NOT delete the registry itself.';
ImageRegistryForget.aliases = [];
ImageRegistryForget.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    registry: core_1.Flags.string({ char: 'r', required: false, description: "The container registry 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.' }),
};
ImageRegistryForget.args = {};
//# sourceMappingURL=forget.js.map