"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 prettify_1 = require("../../../prettify");
const output_format_enum_1 = require("../../../cli-entities/output-format-enum");
const type_safety_utils_1 = require("../../../uilogic/type-safety-utils");
class AssetVolumeList extends command_base_exec_1.CommandBaseExec {
    constructor() {
        super(...arguments);
        this._outputFormat = output_format_enum_1.OutputFormatEnum.auto;
    }
    captureArgs() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(AssetVolumeList);
            let partial = {};
            if (flags.enterprise)
                partial.enterprise = flags.enterprise;
            this._outputFormat = (_a = type_safety_utils_1.TypeSafetyUtils.toEnum(output_format_enum_1.OutputFormatEnum, flags.output)) !== null && _a !== void 0 ? _a : output_format_enum_1.OutputFormatEnum.auto;
            return [partial, false];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const ent = rootMap.enterprises.find(e => e.name === partial.enterprise);
            if (!ent)
                throw new Error(`Enterprise "${partial.enterprise}" not found.`);
            const aCntx = yield this.establishAssetContext(ent, true);
            const asset = ent.assets.find(a => a.name === aCntx[0]);
            if (!asset)
                throw new Error(`Asset "${aCntx[0]}" not found in enterprise "${partial.enterprise}".`);
            return { enterprise: partial.enterprise, reservedFolders: (_a = asset.reservedFolders) !== null && _a !== void 0 ? _a : [], assetName: asset.name };
        });
    }
    buildCandidate_interactively(partial, rootMap, takeDefaults) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const ent = rootMap.enterprises.find(e => e.name === partial.enterprise);
            if (!ent)
                throw new Error(`Enterprise "${partial.enterprise}" not found.`);
            // const aCntx = await this.establishAssetContext(ent, true);
            // const asset = ent.assets.find(a => a.name === aCntx[0]);
            let [assetName] = yield this.promptForAsset(ent.assets, partial.assetName);
            let asset = ent.assets.find(a => a.name == assetName);
            if (!asset)
                throw new Error(`Asset "${partial.assetName}" not found in enterprise "${partial.enterprise}".`);
            return { enterprise: partial.enterprise, reservedFolders: (_a = asset.reservedFolders) !== null && _a !== void 0 ? _a : [], assetName: asset.name };
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push("-e", candidate.enterprise);
        cmd.push("-o", this._outputFormat);
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (candidate.reservedFolders.length > 0) {
                this._ui.userMessage(this.renderAs(candidate.reservedFolders, this._outputFormat));
            }
            else {
                this._ui.userMessage(`No volume mounts defined for asset [${candidate.assetName}] in enterprise [${candidate.enterprise}].`);
            }
        });
    }
    renderAs(object, format) {
        switch (format) {
            case output_format_enum_1.OutputFormatEnum.json:
                return JSON.stringify(object, undefined, 2);
            case output_format_enum_1.OutputFormatEnum.yaml:
                return YAML.stringify(object, { indent: 2 });
            case output_format_enum_1.OutputFormatEnum.pretty:
            case output_format_enum_1.OutputFormatEnum.auto:
            default:
                return (0, prettify_1.prettify)(object);
        }
    }
    promptForAsset(assets, current) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let selected = current;
            let changed = false;
            if (assets.length === 0) {
                throw new Error("This enterprise has no assets. You must create an asset to run this command.");
            }
            if (selected != undefined) {
                let found = assets.find(x => x.name === selected);
                if (found == undefined) {
                    selected = undefined;
                }
            }
            if (selected == undefined) {
                if (assets.length === 1) {
                    selected = assets[0].name;
                }
                else {
                    let choices = assets.map(a => { return { name: a.name, value: a.name }; });
                    selected = (yield this._ui.promptSelect("asset", "Asset: ", choices));
                }
                changed = Boolean(current != undefined);
            }
            return [selected, changed];
        });
    }
}
exports.default = AssetVolumeList;
AssetVolumeList.description = 'Renders the currently declared mount paths for the asset.';
AssetVolumeList.aliases = ['asset:vol:list', 'asset:volumes:list'];
AssetVolumeList.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    enterprise: core_1.Flags.string({ char: 'e', description: 'The name of the enterprise to which the asset belongs (required only if more than one enterprise exists).' }),
    asset: core_1.Flags.string({ char: 'a', required: false, description: "The name of the asset whose volumes to list." }),
    name: core_1.Flags.string({ char: 'n', required: false, description: "Filter volumes within the asset by mount name." }),
    path: core_1.Flags.string({ char: 'p', required: false, description: "Filter volumes within the asset by mount path." }),
    output: core_1.Flags.string({ char: 'o', required: false, default: output_format_enum_1.OutputFormatEnum.auto, options: [output_format_enum_1.OutputFormatEnum.pretty, output_format_enum_1.OutputFormatEnum.json, output_format_enum_1.OutputFormatEnum.yaml, output_format_enum_1.OutputFormatEnum.auto], description: "Output format of the actual settings data document." }),
    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.' }),
};
AssetVolumeList.args = {};
AssetVolumeList.examples = [
    { description: "List all volume mounts for the current asset.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "List volumes for a specific enterprise asset.", command: "<%= config.bin %> <%= command.id %> -e myEnterprise" },
    { description: "Export as JSON.", command: "<%= config.bin %> <%= command.id %> -o json" },
    { description: "Non-interactive (scripted) invocation.", command: "<%= config.bin %> <%= command.id %> -e myEnterprise --silent" },
];
//# sourceMappingURL=list.js.map