"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 integration_commands_exports = {};
__export(integration_commands_exports, {
  DeleteIntegrationCommand: () => DeleteIntegrationCommand,
  GetIntegrationCommand: () => GetIntegrationCommand,
  ListIntegrationsCommand: () => ListIntegrationsCommand
});
module.exports = __toCommonJS(integration_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 GetIntegrationCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const parsedRef = (0, import_package_ref.parsePackageRef)(this.argv.integrationRef);
    if (!parsedRef) {
      throw new errors.InvalidPackageReferenceError(this.argv.integrationRef);
    }
    if (parsedRef.type === "path") {
      throw new errors.BotpressCLIError("Cannot get local integration");
    }
    try {
      const integration = await api.findPublicOrPrivateIntegration(parsedRef);
      if (integration) {
        this.logger.success(`Integration ${import_chalk.default.bold(this.argv.integrationRef)}:`);
        this.logger.json(integration);
        return;
      }
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not get integration ${this.argv.integrationRef}`);
    }
    throw new errors.BotpressCLIError(`Integration ${this.argv.integrationRef} not found`);
  }
}
class ListIntegrationsCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const { dev, name, versionNumber: version } = this.argv;
    const privateLister = (req) => api.client.listIntegrations({ nextToken: req.nextToken, dev, name, version });
    const dummyLister = async () => ({ integrations: [], meta: {} });
    const publicLister = dev ? dummyLister : (req) => api.client.listPublicIntegrations({ nextToken: req.nextToken, name, version });
    try {
      const privateIntegrations = await api.listAllPages(privateLister, (r) => r.integrations);
      const publicIntegrations = await api.listAllPages(publicLister, (r) => r.integrations);
      const integrations = import_lodash.default.uniqBy([...privateIntegrations, ...publicIntegrations], (i) => i.id);
      this.logger.success("Integrations:");
      this.logger.json(integrations);
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, "Could not list integrations");
    }
  }
}
class DeleteIntegrationCommand extends import_global_command.GlobalCommand {
  async run() {
    const api = await this.ensureLoginAndCreateClient(this.argv);
    const parsedRef = (0, import_package_ref.parsePackageRef)(this.argv.integrationRef);
    if (!parsedRef) {
      throw new errors.InvalidPackageReferenceError(this.argv.integrationRef);
    }
    if (parsedRef.type === "path") {
      throw new errors.BotpressCLIError("Cannot delete local integration");
    }
    let integrationId;
    if (parsedRef.type === "id") {
      integrationId = parsedRef.id;
    } else {
      const integration = await this._findIntegration(api, parsedRef);
      integrationId = integration.id;
    }
    try {
      await api.client.deleteIntegration({ id: integrationId });
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not delete integration ${this.argv.integrationRef}`);
    }
    this.logger.success(`Integration ${import_chalk.default.bold(this.argv.integrationRef)} deleted`);
    return;
  }
  _findIntegration = async (api, parsedRef) => {
    let integration;
    try {
      integration = await api.findPrivateIntegration(parsedRef);
    } catch (thrown) {
      throw errors.BotpressCLIError.wrap(thrown, `Could not get integration ${this.argv.integrationRef}`);
    }
    if (!integration) {
      const publicIntegration = await api.findPublicIntegration(parsedRef);
      if (publicIntegration) {
        throw new errors.BotpressCLIError(`Integration ${this.argv.integrationRef} does not belong to your workspace`);
      }
      throw new errors.BotpressCLIError(`Integration ${this.argv.integrationRef} not found`);
    }
    return integration;
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DeleteIntegrationCommand,
  GetIntegrationCommand,
  ListIntegrationsCommand
});
//# sourceMappingURL=integration-commands.js.map
