"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const command_base_exec_tenant_1 = require("../command-base-exec-tenant");
const codiac_constants_1 = require("../codiac-constants");
const prettify_1 = require("../prettify");
class Whereami extends command_base_exec_tenant_1.CommandBaseExecTenant {
    captureArgs() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { flags } = yield this.parse(Whereami);
            return [{}, false];
        });
    }
    buildCandidate_silently(partial, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return {};
        });
    }
    buildCandidate_interactively(partial, takeDefaults, rootMap) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return {};
        });
    }
    buildCommandString(candidate, cmd) {
        cmd.push("--silent");
        return cmd;
    }
    executeTask(candidate) {
        var _a, _b, _c;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let bindings = {
                tenant: (_b = (_a = this._projCfg) === null || _a === void 0 ? void 0 : _a.tenantCode) !== null && _b !== void 0 ? _b : this._userCntx.currentTenantCode
            };
            if ((_c = this._projUserCfg) === null || _c === void 0 ? void 0 : _c.enterprise) {
                bindings.enterprise = this._projUserCfg.enterprise.enterpriseCode;
                if (this._projUserCfg.enterprise.cabinet) {
                    let environment = yield this.getEnvironmentFor(this._projUserCfg.enterprise.cabinet.cabinet);
                    bindings.environment = environment || "[unassigned]";
                    bindings.cabinet = this._projUserCfg.enterprise.cabinet.cabinet;
                }
                if (this._projUserCfg.enterprise.assetBindings && this._projUserCfg.enterprise.assetBindings.length > 0) {
                    bindings.assets = [];
                    for (let assetBinding of this._projUserCfg.enterprise.assetBindings) {
                        bindings.assets.push({
                            asset: assetBinding.incarnation.assetName,
                            image: assetBinding.image.name
                        });
                    }
                }
            }
            this._ui.userMessage((0, prettify_1.prettify)(bindings));
        });
    }
    getEnvironmentFor(cabinet) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let environment;
            try {
                let result = yield this._codiacApi.Cabinet.tryReadMany({ name: { $eq: cabinet } });
                if (result.error)
                    throw result.error;
                else if (result.output && result.output.length > 0) {
                    environment = result.output[0].environment;
                }
            }
            catch (err) {
                this.logger.debug(`Error retrieving environment for cabinet [${cabinet}, tenant: ${this._userCntx.currentTenantCode}]: ${err.name || ""} ${err.message || ""}`, err);
                environment = codiac_constants_1.ERROR_INDICATOR;
            }
            return environment;
        });
    }
}
exports.default = Whereami;
Whereami.description = 'Renders the current cabinet context the image project is pointed to.  ie:  Tenant, Enterprise, Environment, Cabinet.';
Whereami.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    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.' }),
};
Whereami.args = {};
Whereami.examples = [
    { description: "Show current project context.", command: "<%= config.bin %> <%= command.id %>" },
    { description: "Non-interactive invocation.", command: "<%= config.bin %> <%= command.id %> --silent" },
    { description: "Renders the equivalent silent command without executing.", command: "<%= config.bin %> <%= command.id %> --to-script" },
];
//# sourceMappingURL=whereami.js.map