"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 interface_commands_exports = {};
__export(interface_commands_exports, {
  DeleteInterfaceCommand: () => DeleteInterfaceCommand,
  GetInterfaceCommand: () => GetInterfaceCommand,
  ListInterfacesCommand: () => ListInterfacesCommand
});
module.exports = __toCommonJS(interface_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 GetInterfaceCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const parsedRef = (0, import_package_ref.parsePackageRef)(this.argv.interfaceRef);
    if (!parsedRef) {
      throw new errors.InvalidPackageReferenceError(this.argv.interfaceRef);
    }
    if (parsedRef.type === "path") {
      throw new errors.BotpressCLIError("Cannot get local interface");
    }
    try {
      const intrface = await api.findPublicOrPrivateInterface(parsedRef);
      if (intrface) {
        this.logger.success(`Interface ${import_chalk.default.bold(this.argv.interfaceRef)}:`);
        this.logger.json(intrface);
        return;
      }
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not get interface ${this.argv.interfaceRef}`);
    }
    throw new errors.BotpressCLIError(`Interface ${this.argv.interfaceRef} not found`);
  }
}
class ListInterfacesCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const privateLister = (req) => api.client.listInterfaces({ nextToken: req.nextToken });
    const publicLister = (req) => api.client.listPublicInterfaces({ nextToken: req.nextToken });
    try {
      const [privateInterfaces, publicInterfaces] = await Promise.all([
        api.listAllPages(privateLister, (r) => r.interfaces),
        api.listAllPages(publicLister, (r) => r.interfaces)
      ]);
      const interfaces = import_lodash.default.uniqBy([...privateInterfaces, ...publicInterfaces], (i) => i.id);
      this.logger.success("Interfaces:");
      this.logger.json(interfaces);
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, "Could not list interfaces");
    }
  }
}
class DeleteInterfaceCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const parsedRef = (0, import_package_ref.parsePackageRef)(this.argv.interfaceRef);
    if (!parsedRef) {
      throw new errors.InvalidPackageReferenceError(this.argv.interfaceRef);
    }
    if (parsedRef.type === "path") {
      throw new errors.BotpressCLIError("Cannot delete local interface");
    }
    let intrface;
    try {
      intrface = await api.findPublicOrPrivateInterface(parsedRef);
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not get interface ${this.argv.interfaceRef}`);
    }
    if (!intrface) {
      throw new errors.BotpressCLIError(`Interface ${this.argv.interfaceRef} not found`);
    }
    try {
      await api.client.deleteInterface({ id: intrface.id });
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not delete interface ${this.argv.interfaceRef}`);
    }
    this.logger.success(`Interface ${import_chalk.default.bold(this.argv.interfaceRef)} deleted`);
    return;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DeleteInterfaceCommand,
  GetInterfaceCommand,
  ListInterfacesCommand
});
//# sourceMappingURL=interface-commands.js.map
