"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandBaseExecTenant = 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");
/** Base class for commands involving entities at tenant-level scope.   */
class CommandBaseExecTenant 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);
                        // let enterprise = (await this.establishEnterpriseContext()).enterprise;  // <--- captureArgs should come BEFORE THIS!!!  We need to honor the user's enterprise arg
                        // 1. CAPTURE ARGS
                        let [partial, interactionMode, takeDefaults] = yield this.captureArgs();
                        // 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 candidate;
                        if (interactionMode == config_ui_utils_1.InteractionModes.silent) {
                            candidate = yield this.buildCandidate_silently(partial, rootMap);
                        }
                        else {
                            candidate = yield this.buildCandidate_interactively(partial, takeDefaults, rootMap);
                        }
                        // // In case we're hanging waiting for piped-in data that doesnt exist... 
                        // if (!process.stdin.destroyed) {
                        //   process.stdin.destroy();  // <-- Commented out bc this prevents any user input during executeTask() (csp login needed it)
                        //   // process.stdin.end();
                        // }
                        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 {
                        // In case we're hanging waiting for piped-in data that doesnt exist... 
                        if (!process.stdin.destroyed) {
                            process.stdin.destroy(); // <-- Commented out bc this prevents any user input during executeTask() (csp login needed it)
                            // process.stdin.end();  // Commented out bc end() was not enough to prevent hanging
                        }
                        yield this.closeConnections();
                        return;
                    }
                }));
            }
            catch (err) {
                this.handleError(err);
            }
            finally {
                // await this.closeConnections();  // COMMENTED OUT BC: this happens immediately, before the execution of the main command handler, because the body of this try block is only declaring an async rxjs handler, not awaiting its execution.
                return;
            }
        });
    }
}
exports.CommandBaseExecTenant = CommandBaseExecTenant;
//# sourceMappingURL=command-base-exec-tenant.js.map