"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvironmentContextualizer = void 0;
const tslib_1 = require("tslib");
const _ = require("lodash");
/** UI dialogue runner to acquire a valid environment under the current tenant.  */
class EnvironmentContextualizer {
    constructor(_codiacApi, _ui, logger) {
        this._codiacApi = _codiacApi;
        this._ui = _ui;
        this.logger = logger;
    }
    establishEnvironment(requestedEnv) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                // let output: Pick<Environment, "id" | "code" | "enterpriseCode" | "cabinets" | "clusterDefs"> | undefined;
                let output;
                // let projxn = { pathsIncluded: ["id", "code", "enterpriseCode", "cabinets.name", "clusterDefs"], pathsExcluded: [] };
                let envs = yield this._codiacApi.Environment.readMany({ all: true }, { /* projection: projxn, */ throwNotFound: false });
                if (envs.length === 0)
                    throw new Error(`No environments exist.  You must create before continuing.`);
                envs = _.sortBy(envs, e => e.sequence, e => e.code);
                if (requestedEnv !== undefined) {
                    output = envs.find(e => e.code === requestedEnv);
                }
                if (output === undefined) {
                    let choices = envs.map(e => { return { name: `${e.enterpriseCode}/${e.code}`, value: e.code }; });
                    let envCode = (yield this._ui.promptSelect("env", "Select a target environment", choices));
                    output = envs.find(e => e.code === envCode);
                }
                return output;
            }
            catch (err) {
                throw err;
            }
        });
    }
}
exports.EnvironmentContextualizer = EnvironmentContextualizer;
/** Prefix for user list item that triggers the creation of a new entity.  */
EnvironmentContextualizer.PREFIX_FOR_CREATE = "(create new) ";
EnvironmentContextualizer.CLI_COMMAND = "codiac";
//# sourceMappingURL=environment-contextualizer.js.map