"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const YAML = require("yaml");
const command_base_exec_1 = require("../../command-base-exec");
const output_format_enum_1 = require("../../cli-entities/output-format-enum");
const type_safety_utils_1 = require("../../uilogic/type-safety-utils");
const prettify_1 = require("../../prettify");
const list_1 = require("../filestore/list");
const list_2 = require("./list");
class PvcGet extends command_base_exec_1.CommandBaseExec {
    captureArgs() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(PvcGet);
            const partial = {};
            if (flags.enterprise)
                partial.enterprise = flags.enterprise;
            if (flags.name)
                partial.name = flags.name;
            if (flags.output)
                partial.outputFormat = type_safety_utils_1.TypeSafetyUtils.toEnum(output_format_enum_1.OutputFormatEnum, flags.output);
            const takeDefaults = (_a = flags['take-defaults']) !== null && _a !== void 0 ? _a : false;
            return [partial, takeDefaults];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.name || partial.name.trim() === "")
                throw new Error("--name is required.");
            return {
                enterprise: partial.enterprise,
                name: partial.name,
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : output_format_enum_1.OutputFormatEnum.auto,
            };
        });
    }
    buildCandidate_interactively(partial, rootMap, takeDefaults) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!partial.name || partial.name.trim() === "") {
                const all = yield this._codiacApi.FileStoreDef.readMany({ all: true });
                const pvcList = all.filter(list_2.isPvcFileStore);
                if (pvcList.length === 0) {
                    this._ui.userMessage("There are no PVC filestores defined.");
                    this.exit(0);
                    return undefined;
                }
                const choices = pvcList.map(x => ({ name: list_1.FileStoreUiLogic.displayNameFor(x, this._styler, false), value: x.name }));
                partial.name = (yield this._ui.promptSelect("pvc", "Select a PVC filestore:", choices, false, false));
            }
            return this.buildCandidate_silently(partial, rootMap);
        });
    }
    buildCommandString(candidate, cmd) {
        if (candidate.enterprise)
            cmd.push("--enterprise", candidate.enterprise);
        cmd.push("--name", candidate.name);
        if (candidate.outputFormat)
            cmd.push("-o", candidate.outputFormat);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const matches = yield this._codiacApi.FileStoreDef.readMany({ name: { $eq: candidate.name } }, { throwNotFound: false });
            const pvcMatches = matches.filter(list_2.isPvcFileStore);
            if (pvcMatches.length === 0) {
                this._ui.userMessage(`No PVC filestore named [${candidate.name}] was found.`);
                this.exit(1);
                return;
            }
            if (pvcMatches.length > 1) {
                this._ui.userMessage(this._styler.colorWarning(`Multiple PVC filestores match name [${candidate.name}]; rendering all matches.`));
            }
            const output = this.renderAs(pvcMatches, candidate.outputFormat);
            console.log(output);
        });
    }
    renderAs(list, format) {
        switch (format) {
            case output_format_enum_1.OutputFormatEnum.json:
                return JSON.stringify(list.length === 1 ? list[0] : list, undefined, 2);
            case output_format_enum_1.OutputFormatEnum.yaml:
                return YAML.stringify(list.length === 1 ? list[0] : list, { indent: 2 });
            case output_format_enum_1.OutputFormatEnum.pretty:
            case output_format_enum_1.OutputFormatEnum.auto:
            default:
                return (0, prettify_1.prettify)(list.length === 1 ? list[0] : list);
        }
    }
}
exports.default = PvcGet;
PvcGet.description = 'Renders the FileStoreDef record for a single PVC-backed filestore. Useful for inspecting how a PVC will be provisioned (storage class, access modes, size, static vs dynamic binding).';
PvcGet.aliases = ['pvc:get'];
PvcGet.dependsOn = ["enterprise", "name"];
PvcGet.examples = [
    '<%= config.bin %> <%= command.id %> --name shared-files',
    '<%= config.bin %> <%= command.id %> --name pvc-blob -o yaml',
    '<%= config.bin %> <%= command.id %> --name app-documents-share --silent -o json',
];
PvcGet.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    enterprise: core_1.Flags.string({ char: 'e', required: false, description: 'Code name identifying the enterprise (optional if there is only one).' }),
    name: core_1.Flags.string({ char: 'n', required: false, description: 'Name of the PVC filestore to look up.' }),
    output: core_1.Flags.string({ char: 'o', default: output_format_enum_1.OutputFormatEnum.auto, description: 'Format in which to render the data.', options: [output_format_enum_1.OutputFormatEnum.json, output_format_enum_1.OutputFormatEnum.yaml, output_format_enum_1.OutputFormatEnum.auto] }),
    silent: core_1.Flags.boolean({ dependsOn: PvcGet.dependsOn, 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)." }),
};
//# sourceMappingURL=get.js.map