"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandBaseExec = void 0;
const tslib_1 = require("tslib");
require("reflect-metadata");
const command_base_enterprise_1 = require("./command-base-enterprise");
const config_ui_utils_1 = require("./uilogic/config-ui-utils");
const _ = require("lodash");
const ops_1 = require("./ops");
class CommandBaseExec extends command_base_enterprise_1.CommandBaseEnterprise {
    constructor() {
        var _a, _b;
        super(...arguments);
        /** CLI Command-line string to rerun this execution as a non-TTY script. */
        this.cmd = [this.config.bin, ...((_b = (_a = this.id) === null || _a === void 0 ? void 0 : _a.split(":")) !== null && _b !== void 0 ? _b : [])];
    }
    run() {
        var _a;
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                this.bootstrap();
                yield this.readStdin();
                (_a = this._relayContainer) === null || _a === void 0 ? void 0 : _a.ready$.subscribe((ready) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                    try {
                        // 0. SET DEFAULTS
                        this._userCntx = yield this.establishTenantContext(this._userCntx);
                        // 1. CAPTURE ARGS
                        let [partial, interactionMode, takeDefaults] = yield this.captureArgs(); // TODO: Do this before setting any context.  (not required bc there's no overriding tenant context, but better design nonetheless).
                        // In silent mode, prevent user interaction and tui styling
                        if (interactionMode === config_ui_utils_1.InteractionModes.silent) {
                            this.ioc.rebind(ops_1.INTERFACES_CLI.IUserInputProvider).to(ops_1.ThrowingUserInputProvider);
                            this.__ui = undefined; // invoke a new lazy load  | new ThrowingUserInputProvider(this.logger);
                            this.ioc.rebind(ops_1.INTERFACES_CLI.IOutputStyler).to(ops_1.NoStyleOutputStyler);
                            this.__styler = undefined; // invoke a new lazy load  | new NoStyleOutputStyler();
                        }
                        // 2. ACQUIRE INPUT
                        let rootMap = yield this.getRootMap(); // we're gonna load the entire thing, so we can use it to get everything else.
                        let enterprise = (yield this.establishEnterpriseContext(rootMap, partial.enterprise, interactionMode)).enterprise;
                        partial.enterprise = enterprise.name;
                        // TEMPORARY: Trim this down for now to the current enterprise to match legacy conditions
                        //            (in case any legacy code is doing something stupid like a rootmap.enterprises[0])
                        _.remove(rootMap.enterprises, e => e.name != enterprise.name);
                        let candidate;
                        if (interactionMode == config_ui_utils_1.InteractionModes.silent) {
                            candidate = yield this.buildCandidate_silently(partial, rootMap);
                        }
                        else {
                            candidate = yield this.buildCandidate_interactively(partial, rootMap, takeDefaults);
                        }
                        // In case we're hanging waiting for piped-in data that doesnt exist... 
                        if (!process.stdin.destroyed)
                            process.stdin.destroy();
                        if (candidate == undefined) {
                            this.logger.notice(`Operation cancelled. No work was done.`);
                            this._ui.userMessage(`Operation cancelled. No work was done.`);
                            process.exit(0);
                        }
                        else {
                            // 3. RENDER COMMAND STRING
                            if (interactionMode != undefined && [config_ui_utils_1.InteractionModes.echo, config_ui_utils_1.InteractionModes.toscript].includes(interactionMode)) {
                                this.cmd = this.buildCommandString(candidate, this.cmd);
                                this._ui.userMessage(this.cmd.join(" "));
                            }
                            if (interactionMode != config_ui_utils_1.InteractionModes.toscript) {
                                // 4. EXECUTE THE MAIN TASK
                                yield this.executeTask(candidate);
                            }
                        }
                    }
                    catch (err) {
                        this.handleError(err);
                        // dont rethrow - err handling in the observable isnt yet user-friendly.
                    }
                    finally {
                        yield this.closeConnections();
                        return;
                    }
                }));
            }
            catch (err) {
                this.handleError(err);
            }
            finally {
                yield this.closeConnections();
                return;
            }
        });
    }
}
exports.CommandBaseExec = CommandBaseExec;
//# sourceMappingURL=command-base-exec.js.map