"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var profile_commands_exports = {};
__export(profile_commands_exports, {
  ActiveProfileCommand: () => ActiveProfileCommand,
  ListProfilesCommand: () => ListProfilesCommand,
  UseProfileCommand: () => UseProfileCommand
});
module.exports = __toCommonJS(profile_commands_exports);
var import_chalk = __toESM(require("chalk"));
var consts = __toESM(require("../consts"));
var errors = __toESM(require("../errors"));
var import_global_command = require("./global-command");
class ActiveProfileCommand extends import_global_command.GlobalCommand {
  async run() {
    let activeProfileName = await this.globalCache.get("activeProfile");
    if (!activeProfileName) {
      this.logger.debug(`No active profile set, defaulting to ${consts.defaultProfileName}`);
      activeProfileName = consts.defaultProfileName;
      await this.globalCache.set("activeProfile", activeProfileName);
    }
    const profile = await this.readProfileFromFS(activeProfileName);
    const profileEntry = { name: activeProfileName, ...profile };
    this.logger.log("Active profile:");
    this.logger.json(profileEntry);
  }
}
class ListProfilesCommand extends import_global_command.GlobalCommand {
  async run() {
    const profiles = await this.readProfilesFromFS();
    const profileEntries = Object.entries(profiles).map(([name, profile]) => ({ name, ...profile }));
    if (!profileEntries.length) {
      this.logger.log("No profiles found");
      return;
    }
    const activeProfileName = await this.globalCache.get("activeProfile");
    this.logger.log(`Active profile: '${import_chalk.default.bold(activeProfileName)}'`);
    this.logger.json(profileEntries);
  }
}
class UseProfileCommand extends import_global_command.GlobalCommand {
  async run() {
    const logSuccess = (profileName) => this.logger.success(`Now using profile "${profileName}"`);
    if (this.argv.profileToUse) {
      const profile2 = await this.readProfileFromFS(this.argv.profileToUse);
      await this.globalCache.set("activeProfile", this.argv.profileToUse);
      await _updateGlobalCache({ globalCache: this.globalCache, profileName: this.argv.profileToUse, profile: profile2 });
      logSuccess(this.argv.profileToUse);
      return;
    }
    const profiles = await this.readProfilesFromFS();
    const choices = Object.entries(profiles).map(([profileName, _]) => ({
      title: profileName,
      description: "",
      value: profileName
    }));
    const selectedProfile = await this.prompt.select("Select the profile you want to use.", { choices });
    if (!selectedProfile) {
      this.logger.log("No profile selected, aborting.");
      return;
    }
    const profile = profiles[selectedProfile];
    if (!profile) throw new errors.BotpressCLIError("The selected profile could not be read");
    await this.globalCache.set("activeProfile", selectedProfile);
    await _updateGlobalCache({ globalCache: this.globalCache, profileName: selectedProfile, profile });
    logSuccess(selectedProfile);
  }
}
const _updateGlobalCache = async (props) => {
  await props.globalCache.set("activeProfile", props.profileName);
  await props.globalCache.set("apiUrl", props.profile.apiUrl);
  await props.globalCache.set("token", props.profile.token);
  await props.globalCache.set("workspaceId", props.profile.workspaceId);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ActiveProfileCommand,
  ListProfilesCommand,
  UseProfileCommand
});
//# sourceMappingURL=profile-commands.js.map
