"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 plugin_commands_exports = {};
__export(plugin_commands_exports, {
  DeletePluginCommand: () => DeletePluginCommand,
  GetPluginCommand: () => GetPluginCommand,
  ListPluginsCommand: () => ListPluginsCommand
});
module.exports = __toCommonJS(plugin_commands_exports);
var import_chalk = __toESM(require("chalk"));
var import_lodash = __toESM(require("lodash"));
var errors = __toESM(require("../errors"));
var import_package_ref = require("../package-ref");
var import_global_command = require("./global-command");
class GetPluginCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const parsedRef = (0, import_package_ref.parsePackageRef)(this.argv.pluginRef);
    if (!parsedRef) {
      throw new errors.InvalidPackageReferenceError(this.argv.pluginRef);
    }
    if (parsedRef.type === "path") {
      throw new errors.BotpressCLIError("Cannot get local plugin");
    }
    try {
      const plugin = await api.findPublicOrPrivatePlugin(parsedRef);
      if (plugin) {
        this.logger.success(`Plugin ${import_chalk.default.bold(this.argv.pluginRef)}:`);
        this.logger.json(plugin);
        return;
      }
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not get plugin ${this.argv.pluginRef}`);
    }
    throw new errors.BotpressCLIError(`Plugin ${this.argv.pluginRef} not found`);
  }
}
class ListPluginsCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const { name, versionNumber: version } = this.argv;
    const privateLister = (req) => api.client.listPlugins({ nextToken: req.nextToken, name, version });
    const publicLister = (req) => api.client.listPublicPlugins({ nextToken: req.nextToken, name, version });
    try {
      const [privatePlugins, publicPlugins] = await Promise.all([
        api.listAllPages(privateLister, (r) => r.plugins),
        api.listAllPages(publicLister, (r) => r.plugins)
      ]);
      const plugins = import_lodash.default.uniqBy([...privatePlugins, ...publicPlugins], (p) => p.id);
      this.logger.success("Plugins:");
      this.logger.json(plugins);
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, "Could not list plugins");
    }
  }
}
class DeletePluginCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const parsedRef = (0, import_package_ref.parsePackageRef)(this.argv.pluginRef);
    if (!parsedRef) {
      throw new errors.InvalidPackageReferenceError(this.argv.pluginRef);
    }
    if (parsedRef.type === "path") {
      throw new errors.BotpressCLIError("Cannot delete local plugin");
    }
    let plugin;
    try {
      plugin = await api.findPublicOrPrivatePlugin(parsedRef);
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not get plugin ${this.argv.pluginRef}`);
    }
    if (!plugin) {
      throw new errors.BotpressCLIError(`Plugin ${this.argv.pluginRef} not found`);
    }
    try {
      await api.client.deletePlugin({ id: plugin.id });
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not delete plugin ${this.argv.pluginRef}`);
    }
    this.logger.success(`Plugin ${import_chalk.default.bold(this.argv.pluginRef)} deleted`);
    return;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DeletePluginCommand,
  GetPluginCommand,
  ListPluginsCommand
});
//# sourceMappingURL=plugin-commands.js.map
