"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandBaseTenantless = void 0;
const tslib_1 = require("tslib");
require("reflect-metadata");
const cli_common_1 = require("./cli-common");
const argument_manager_1 = require("./cli-common/argument-manager");
const ops_1 = require("./ops");
const codiac_relay_contracts_1 = require("@codiac.io/codiac-relay-contracts");
const data_1 = require("./ops/data");
const bootstrapper_for_tenantless_commands_1 = require("./bootstrapper-for-tenantless-commands");
/** For commands that do not require enterprise operations (and thus have no dependency on codiac api or relay).
 */
class CommandBaseTenantless extends cli_common_1.commandBase {
    // constructor(argv: string[], config: Config, requiresProjectScope?: boolean) {
    //   super(argv, config, requiresProjectScope);
    // }
    /** Registers types with ioc.
     * No business logic is performed by this method. */
    bootstrap() {
        bootstrapper_for_tenantless_commands_1.BootstrapperForTenantlessCommands.bootstrap(this.ioc, this);
    }
    // --- PROJECT SCOPE -----------------------------------------------------------
    // TODO: Try to get rid of this one.
    /** NOTE:  This property MUST ONLY be accessed from within a project folder.
   * It requires project scoped objects
   * such as: `projectRoot`, `projectConfig`, `projectUserConfig`, and `ProjectContextRepo`.
   *
   * It lazy-loads them, and its methods will fail without them.
  */
    get _ops() {
        if (this.__ops === undefined) {
            this.__ops = new ops_1.LocalOps(this.ioc); // this ctor is trivial, no effect on perf. Lazy loading here is just for back-compat.
        }
        return this.__ops;
    }
    get exec() {
        if (this.__exec === undefined) {
            this.__exec = new argument_manager_1.ArgumentManager(this.id, this._projCfg, this.projCfgRepo, this.logger, this._ui);
        }
        return this.__exec;
    }
    /** Deprecated: use ioc instead.  This getter Lazyloads from ioc. */
    get projCfgRepo() {
        if (this.__projCfgRepo === undefined) {
            this.__projCfgRepo = this.ioc.get(data_1.ProjectContextRepo);
        }
        return this.__projCfgRepo;
    }
    /** Deprecated: use ioc instead.  This getter Lazyloads from ioc. */
    get _projCfg() {
        if (this.__projCfg === undefined) {
            this.__projCfg = this.ioc.get(codiac_relay_contracts_1.ProjectConfig);
        }
        return this.__projCfg;
    }
    /** Deprecated: use ioc instead.  This getter Lazyloads from ioc. */
    get _projUserCfg() {
        if (this.__projUserCfg === undefined) {
            this.__projUserCfg = this.ioc.get(codiac_relay_contracts_1.ProjectUserConfig);
        }
        return this.__projUserCfg;
    }
    /** Lazyloads the project root folder path by walking up the current path to find a project config file. */
    get _projectRoot() {
        if (this.__projectRoot === undefined) {
            this.__projectRoot = ops_1.CliPathWalker.getProjectRoot(undefined, true);
        }
        return this.__projectRoot;
    }
    /** Currently just coincides with projectRoot. */
    get _dockerRoot() {
        return this._projectRoot;
    }
    // private _dockerRoot: string|undefined;
    // --- END PROJECT SCOPE -----------------------------------------------------------
    acquireDependencyToTarget(operationName, flagOnly, depName) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                try {
                    let name = depName;
                    if (flagOnly) {
                        let names = yield this._projCfg.submodules.map(sm => sm.folderName);
                        if (names.length > 0 && (!name || names.indexOf(name) < 0)) {
                            if (names.length == 1) {
                                this._ui.userMessage("(Hit Enter to confirm, CTRL-C to cancel)");
                                let confirm = yield this._ui.prompt("target", `${operationName} only (${this._styler.hilite(names[0])})`, false);
                                if (confirm == "") {
                                    name = names[0];
                                }
                            }
                            else {
                                // let result = await inquirer.prompt({ type: 'list', name: 'name', message: `Please select a sourced dependency to ${operationName}:`, choices: names });
                                name = yield this._ui.promptSelect('name', `Please select a sourced dependency to ${operationName}:`, names);
                            }
                            if (name) {
                                this.logger.debug(`${operationName}: ${name}`);
                            }
                            else {
                                this.logger.detail(`Operation cancelled.  No work was done.`);
                                this._ui.userMessage(`Operation cancelled.  No work was done.`);
                                this.exit(0);
                            }
                        }
                        else {
                            this.logger.notice(`Operation cancelled: No dependencies are currently sourced.  No work was done.`);
                            this._ui.userMessage(`Operation cancelled: No dependencies are currently sourced.  No work was done.`);
                            this.exit(0);
                        }
                    }
                    resolve(name);
                }
                catch (err) {
                    reject(err);
                }
            }));
        });
    }
}
exports.CommandBaseTenantless = CommandBaseTenantless;
//# sourceMappingURL=command-base-tenantless.js.map