"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 config_ui_utils_1 = require("../../uilogic/config-ui-utils");
const prettify_1 = require("../../prettify");
const _ = require("lodash");
const delete_1 = require("./delete");
const output_format_enum_1 = require("../../cli-entities/output-format-enum");
const type_safety_utils_1 = require("../../uilogic/type-safety-utils");
class HostList extends command_base_exec_1.CommandBaseExec {
    captureArgs() {
        var _a, _b;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { args, flags } = yield this.parse(HostList);
            let partial = {};
            if (flags.enterprise)
                partial.enterprise = flags.enterprise;
            if (flags.output)
                partial.outputFormat = type_safety_utils_1.TypeSafetyUtils.toEnum(output_format_enum_1.OutputFormatEnum, 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];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let candidate = {
                all: true,
                enterprise: partial.enterprise,
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : output_format_enum_1.OutputFormatEnum.auto
            };
            return candidate;
        });
    }
    buildCandidate_interactively(partial, rootMap, takeDefaults) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            // TODO: Replace this with interaction for filters
            partial.all = true;
            let candidate = {
                enterprise: partial.enterprise,
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : output_format_enum_1.OutputFormatEnum.auto
            };
            if (partial.all)
                candidate.all = partial.all;
            return candidate;
        });
    }
    buildCommandString(candidate, cmd) {
        // the domain argument
        // if (candidate.domainName) cmd.push("\"".concat(candidate.domainName, "\""));
        if (candidate.enterprise)
            cmd.push("--enterprise", candidate.enterprise);
        if (candidate.outputFormat)
            cmd.push("-o", candidate.outputFormat);
        // TODO: Capture takeDefaults
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let req = Object.assign({}, _.omit(candidate, "outputFormat"));
            let list = yield this._codiacApi.ScopedHostNameConfig.readMany(req);
            let output = this.renderAs(list, candidate.outputFormat, x => delete_1.HostNameUiLogic.displayNameFor(x, this._styler));
            console.log(output);
        });
    }
    renderAs(list, format, textFormatter) {
        let output = "";
        switch (format) {
            case output_format_enum_1.OutputFormatEnum.json:
                output = JSON.stringify(list, undefined, 2);
                break;
            case output_format_enum_1.OutputFormatEnum.yaml:
                output = YAML.stringify(list, { indent: 2 });
                break;
            case output_format_enum_1.OutputFormatEnum.pretty:
            case output_format_enum_1.OutputFormatEnum.auto:
            default:
                if (textFormatter) {
                    output = list.map(x => textFormatter(x)).join("\n");
                }
                else {
                    output = (0, prettify_1.prettify)(list);
                }
                break;
        }
        return output;
    }
}
exports.default = HostList;
HostList.description = 'Retrieves the list of host mapping strategies defined on a given enterprise.';
HostList.examples = [
    '<%= config.bin %> <%= command.id %>',
];
HostList.flags = {
    // scope
    // target
    enterprise: core_1.Flags.string({ char: 'e', description: '(optional if there is only one) The enterprise in which the domains have been mapped.' }),
    // cluster
    // cabinet
    // domain
    // strategy
    // output list, table, json, yaml (wd love to have jmespath and/or a template and/or a projection)
    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] }),
    // 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." })
};
HostList.args = {
    domain: core_1.Args.string({ description: 'domain name to filter on (without any wildcards), eg: `yoursite.com`' }),
};
//# sourceMappingURL=list.js.map