"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 output_format_enum_1 = require("../../cli-entities/output-format-enum");
const type_safety_utils_1 = require("../../uilogic/type-safety-utils");
class HostView 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(HostView);
            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 = {
                enterprise: partial.enterprise,
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : output_format_enum_1.OutputFormatEnum.pretty
            };
            return candidate;
        });
    }
    buildCandidate_interactively(partial, rootMap, takeDefaults) {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let candidate = {
                enterprise: partial.enterprise,
                outputFormat: (_a = partial.outputFormat) !== null && _a !== void 0 ? _a : output_format_enum_1.OutputFormatEnum.pretty
            };
            return candidate;
        });
    }
    buildCommandString(candidate, cmd) {
        // the domain argument
        // if (candidate.domainName) cmd.push("\"".concat(candidate.domainName, "\""));
        // 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 result = yield this.genRelayClient.Call.getIngressMap(req);
            if (result.error)
                throw result.error;
            if (result.output == undefined)
                throw new Error("No ingress map was found."); // <-- this case should never exist; it would be an empty object if nothing existed, not an undefined.
            // let output = JSON.stringify(result.output, undefined, ' ');
            // let output = prettify(result.output);
            let output = this.renderAs(result.output, candidate.outputFormat);
            console.log(output);
        });
    }
    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:
            default:
                output = (0, prettify_1.prettify)(object);
                break;
        }
        return output;
    }
}
exports.default = HostView;
HostView.description = 'Retrieves the overall map of domain routes for the given enterprise, resulting from the current host mapping strategies, environments, and assets.';
HostView.examples = [
    '<%= config.bin %> <%= command.id %>',
];
HostView.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.pretty, 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.pretty] }),
    // 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." })
};
HostView.args = {
    domain: core_1.Args.string({ description: 'domain name to filter on (without any wildcards), eg: `yoursite.com`' }),
};
//# sourceMappingURL=view.js.map