"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PROMPT_ME_DESIGNATOR = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const YAML = require("yaml");
const command_base_enterprise_1 = require("../../../command-base-enterprise");
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");
// const { selectFiles, COMPLETED, CANCELLED } = require('select-files-cli');  // gd js wo ts...
// const { selectFiles, COMPLETED, CANCELLED } = require('../../uilogic/file-selector');  // gd js wo ts...
/** The value provided as input that indicates
 * a user's desire to be prompted with a list from which choose the value(s),
 * instead of entering them manually or taking a default. */
exports.PROMPT_ME_DESIGNATOR = "?";
class AssetVolumeList extends command_base_enterprise_1.CommandBaseEnterprise {
    run() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(AssetVolumeList);
            (_a = this._relayContainer) === null || _a === void 0 ? void 0 : _a.ready$.subscribe((ready) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                var _b;
                try {
                    this._userCntx = yield this.establishTenantContext(this._userCntx);
                    let rootMap = yield this.getRootMap();
                    const interactionMode = this.getInteractionMode(flags.silent, flags.echo, flags['to-script']);
                    let enterprise = (yield this.establishEnterpriseContext(rootMap, flags.enterprise, interactionMode)).enterprise;
                    _.remove(rootMap.enterprises, e => e.name != enterprise.name); // trim down to match legacy conditions
                    let aCntx = yield this.establishAssetContext(enterprise, true);
                    let asset = enterprise.assets.find(a => a.name == aCntx[0]);
                    let outputFormat = (_b = type_safety_utils_1.TypeSafetyUtils.toEnum(output_format_enum_1.OutputFormatEnum, flags.output)) !== null && _b !== void 0 ? _b : output_format_enum_1.OutputFormatEnum.auto;
                    if (asset.reservedFolders && asset.reservedFolders.length > 0) {
                        // let tbl = cli.table(asset.reservedFolders, [ "name", "path"])
                        let output = this.renderAs(asset.reservedFolders, outputFormat !== null && outputFormat !== void 0 ? outputFormat : output_format_enum_1.OutputFormatEnum.pretty);
                        this._ui.userMessage(output);
                    }
                    else {
                        this._ui.userMessage(`No volume mounts defined for asset [${asset.name}] in enterprise [${asset.enterprise}].`);
                    }
                }
                catch (err) {
                    this.handleError(err);
                }
                finally {
                    yield this.closeConnections();
                    return;
                }
            }));
        });
    }
    renderAs(object, format) {
        let output = "";
        switch (format) {
            case output_format_enum_1.OutputFormatEnum.json:
                output = JSON.stringify(object, undefined, 2);
                break;
            case output_format_enum_1.OutputFormatEnum.yaml:
                output = YAML.stringify(object, { indent: 2 });
                break;
            case output_format_enum_1.OutputFormatEnum.pretty:
                output = (0, prettify_1.prettify)(object);
                break;
            case output_format_enum_1.OutputFormatEnum.auto:
                output = (0, prettify_1.prettify)(object); // TODO:  output as a table instead.
                break;
        }
        return output;
    }
}
exports.default = AssetVolumeList;
AssetVolumeList.description = 'Renders the currently declared mount paths for the asset.';
AssetVolumeList.aliases = ['asset:vol:list', 'asset:volumes:list'];
AssetVolumeList.dependsOn = ["name"];
AssetVolumeList.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    enterprise: core_1.Flags.string({ description: '(required only if more than one enterprise exists) The name of the enterprise to which the asset belongs.' }),
    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({ char: 'y', dependsOn: AssetVolumeList.dependsOn, required: false, description: `(Optional: defaults to false) Prevents confirmations of user-values that are remembered from prior runs. Requires: ${AssetVolumeList.dependsOn}` })
};
//# sourceMappingURL=list.js.map