"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const YAML = require("yaml");
const render = require("../../uilogic/render-methods");
const config_ui_utils_1 = require("../../uilogic/config-ui-utils");
const command_base_exec_tenant_1 = require("../../command-base-exec-tenant");
const uilogic_1 = require("../../uilogic");
const codiac_constants_1 = require("../../codiac-constants");
const type_safety_utils_1 = require("../../uilogic/type-safety-utils");
class ImageRegistryList extends command_base_exec_tenant_1.CommandBaseExecTenant {
    constructor() {
        super(...arguments);
        this.adminOnly = true;
    }
    captureArgs() {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args, flags } = yield this.parse(ImageRegistryList);
            let partial = {};
            if (flags.output)
                partial.outputFormat = uilogic_1.OutputFormatsEnum[flags.output];
            // ---------------------------------------------------------------
            // TODO: move everything from here down into the base class
            // ---------------------------------------------------------------
            const interactionMode = (this.pipedInput) ? config_ui_utils_1.InteractionModes.silent : (_b = (_a = undefined !== null && undefined !== void 0 ? undefined : (flags.echo ? config_ui_utils_1.InteractionModes.echo : undefined)) !== null && _a !== void 0 ? _a : (flags["to-script"] ? config_ui_utils_1.InteractionModes.toscript : undefined)) !== null && _b !== void 0 ? _b : (flags.silent ? config_ui_utils_1.InteractionModes.silent : undefined);
            const takeDefaults = flags['take-defaults'];
            return [partial, interactionMode, takeDefaults];
        });
    }
    // /** Throws on invalid */
    // private validate(partial: Partial<IAuthRoleDef>): void {
    //   if (partial.name == undefined || _.isEmpty(partial.name)) throw new Error("Missing name");
    //   let rxName = /^[a-zA-Z][a-zA-Z0-9-]{2,50}$/;
    //   if (!rxName.test(partial.name)) throw new Error("Invalid name: must be 3- to 50-character string of alphanumerics and hyphens, starting with a letter.");
    // }
    buildCandidate_silently(partial) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let candidate = {
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : uilogic_1.OutputFormatsEnum.text
            };
            // candidate.all = true;
            // candidate.tenantCode = <any>this._userCntx.currentTenantCode;  // any cast bc that custom filter isnt set up right - it shd take a string (to deserialize), but it thinks it wants an object. 
            return candidate;
        });
    }
    buildCandidate_interactively(partial, takeDefaults) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return yield this.buildCandidate_silently(partial);
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push("--output", candidate.outputFormat);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            // if ([OutputFormatsEnum.json, OutputFormatsEnum.yaml].includes(candidate.outputFormat) === false) this._ui.startSpinner("Retrieving");
            let crit = _.omit(candidate, "outputFormat");
            if (_.isEmpty(crit)) {
                crit.all = true;
            }
            let list = yield this._codiacApi.ImageRegistry.readMany(crit, { throwNotFound: false });
            // let doneMsg = "Found ".concat((list.length).toString());
            // if ([OutputFormatsEnum.json, OutputFormatsEnum.yaml].includes(candidate.outputFormat) === false) this._ui.stopSpinner(doneMsg);
            // RENDER
            let output = "";
            switch (candidate.outputFormat) {
                case uilogic_1.OutputFormatsEnum.json:
                    output = JSON.stringify(list, undefined, 2);
                    this._ui.userMessage(output);
                    break;
                case uilogic_1.OutputFormatsEnum.yaml:
                    output = YAML.stringify(list, { indent: 2 });
                    this._ui.userMessage(output);
                    break;
                case uilogic_1.OutputFormatsEnum.text:
                default:
                    let listForText = list.map(r => r.provider.concat(" ", this._styler.hilite(r.code), " ", (type_safety_utils_1.TypeSafetyUtils.toBoolean(r.pullRequiresAuth) ? codiac_constants_1.CliConstants.UTF8_KEY + " " : ""), this._styler.dim(`(${r.url})`)));
                    output = render.renderStringList(listForText);
                    this._ui.userMessage(output);
                    break;
            }
        });
    }
}
exports.default = ImageRegistryList;
ImageRegistryList.description = 'Retrieves all existing references to the container registries storing the published versions of asset images.';
ImageRegistryList.aliases = [];
ImageRegistryList.examples = [
    { description: "Simple text output", command: '<%= config.bin %> <%= command.id %>' },
    { description: "Output as json:", command: '<%= config.bin %> <%= command.id %> -o json' },
    { description: "Pipe to a yaml file:", command: '<%= config.bin %> <%= command.id %> -o yaml > registries.yaml' }
];
ImageRegistryList.flags = {
    output: core_1.Flags.string({ char: 'o', default: uilogic_1.OutputFormatsEnum.text, description: "format in which to render the data.", options: [uilogic_1.OutputFormatsEnum.json, uilogic_1.OutputFormatsEnum.yaml, uilogic_1.OutputFormatsEnum.text] }),
    // TODO: Move these into the base class
    silent: core_1.Flags.boolean({ exclusive: ["echo", "to-script"], description: 'Non-Interactive mode; executes without any user interaction and fails on any missing or invalid arguments.' }),
    echo: core_1.Flags.boolean({ exclusive: ["to-script", "silent"], description: 'Echo interaction mode; renders the equivalent non-interactive cli command for future use before final execution.' }),
    "to-script": core_1.Flags.boolean({ exclusive: ["echo", "silent"], description: 'Toscript interaction mode; renders the equivalent non-interactive cli command WITHOUT any execution.' }),
    "take-defaults": core_1.Flags.boolean({ exclusive: ["silent"], description: "Setting this to true prevents prompting for confirmation on parameters that were passed in or were automatically set to default values (Irrelevant in --silent mode).  This behavior does NOT apply to scenarios that the command needs to override invalid user input; such cases will always prompt for confirmation.  eg: when no argument was provided for file or type, and only one exists, the system will render the autoselected file unless this flag is set." })
};
ImageRegistryList.args = {};
//# sourceMappingURL=list.js.map