"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 errors_exports = {};
__export(errors_exports, {
  AbortedOperationError: () => AbortedOperationError,
  BotpressCLIError: () => BotpressCLIError,
  ExclusiveBotFeatureError: () => ExclusiveBotFeatureError,
  ExclusiveIntegrationFeatureError: () => ExclusiveIntegrationFeatureError,
  HTTPError: () => HTTPError,
  InvalidPackageReferenceError: () => InvalidPackageReferenceError,
  NoBotsFoundError: () => NoBotsFoundError,
  NoBundleFoundError: () => NoBundleFoundError,
  NoWorkspacesFoundError: () => NoWorkspacesFoundError,
  NotLoggedInError: () => NotLoggedInError,
  ParamRequiredError: () => ParamRequiredError,
  ProjectDefinitionNotFoundError: () => ProjectDefinitionNotFoundError,
  UnsupportedProjectType: () => UnsupportedProjectType
});
module.exports = __toCommonJS(errors_exports);
var client = __toESM(require("@botpress/client"));
var import_axios = __toESM(require("axios"));
var import_verror = require("verror");
var consts = __toESM(require("./consts"));
const isUnknownApiError = (e) => client.isApiError(e) && e.type === "Unknown";
const isKnownApiError = (e) => client.isApiError(e) && e.type !== "Unknown";
class BotpressCLIError extends import_verror.VError {
  static wrap(thrown, message) {
    const err = BotpressCLIError.map(thrown);
    return new BotpressCLIError(err, message ?? "");
  }
  static map(thrown) {
    if (thrown instanceof BotpressCLIError) {
      return thrown;
    }
    if (isUnknownApiError(thrown)) {
      const cause = thrown.error?.cause;
      if (cause && typeof cause === "object" && "code" in cause && cause.code === "ECONNREFUSED") {
        return new HTTPError(500, "The connection was refused by the server");
      }
      const unknownMessage = "An unknown API error occurred";
      const actualTrimmedMessage = thrown.message.trim();
      if (!actualTrimmedMessage) {
        return new HTTPError(500, unknownMessage);
      }
      const inner = new HTTPError(500, actualTrimmedMessage);
      return new BotpressCLIError(inner, unknownMessage);
    }
    if (isKnownApiError(thrown)) {
      return HTTPError.fromApi(thrown);
    }
    if (import_axios.default.isAxiosError(thrown)) {
      return HTTPError.fromAxios(thrown);
    }
    if (thrown instanceof Error) {
      const { message } = thrown;
      return new BotpressCLIError(message);
    }
    return new BotpressCLIError(String(thrown));
  }
  constructor(first, second) {
    if (typeof first === "string") {
      super(first);
      return;
    }
    super(first, second);
  }
}
class ExclusiveBotFeatureError extends BotpressCLIError {
  constructor() {
    const message = "This feature is only available for bots. This project is an integration or interface.";
    super(message);
  }
}
class ExclusiveIntegrationFeatureError extends BotpressCLIError {
  constructor() {
    const message = "This feature is only available for integration. This project is a bot or interface.";
    super(message);
  }
}
class HTTPError extends BotpressCLIError {
  constructor(status, message) {
    super(message);
    this.status = status;
  }
  static fromAxios(e) {
    const message = this._axiosMsg(e);
    return new HTTPError(e.response?.status, message);
  }
  static fromApi(e) {
    const { message, code } = e;
    return new HTTPError(code, message);
  }
  static _axiosMsg(e) {
    let message = e.message;
    if (e.response?.statusText) {
      message += `
  ${e.response?.statusText}`;
    }
    if (e.response?.status && e.request?.method && e.request?.path) {
      message += `
  (${e.response?.status}) ${e.request.method} ${e.request.path}`;
    }
    if (e.response?.data?.message) {
      message += `
  ${e.response?.data?.message}`;
    }
    return message;
  }
}
class NoBundleFoundError extends BotpressCLIError {
  constructor() {
    const message = "No bundle found. Please run `bp bundle` first.";
    super(message);
  }
}
class NoBotsFoundError extends BotpressCLIError {
  constructor() {
    const message = `No Bot found in your Workspace. Please create one first at ${consts.defaultBotpressAppUrl}.`;
    super(message);
  }
}
class NoWorkspacesFoundError extends BotpressCLIError {
  constructor() {
    const message = "No Workspace found. Please create one first.";
    super(message);
  }
}
class NotLoggedInError extends BotpressCLIError {
  constructor() {
    const message = "Not logged in. Please run `bp login` first.";
    super(message);
  }
}
class ParamRequiredError extends BotpressCLIError {
  constructor(param) {
    const message = `${param} is required.`;
    super(message);
  }
}
class InvalidPackageReferenceError extends BotpressCLIError {
  constructor(ref) {
    const message = `Invalid package reference "${ref}".`;
    super(message);
  }
}
class UnsupportedProjectType extends BotpressCLIError {
  constructor() {
    const message = "Unsupported project type.";
    super(message);
  }
}
class ProjectDefinitionNotFoundError extends BotpressCLIError {
  constructor(workdir) {
    const message = `No project definition found at "${workdir}".`;
    super(message);
  }
}
class AbortedOperationError extends BotpressCLIError {
  constructor() {
    super("Aborted");
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AbortedOperationError,
  BotpressCLIError,
  ExclusiveBotFeatureError,
  ExclusiveIntegrationFeatureError,
  HTTPError,
  InvalidPackageReferenceError,
  NoBotsFoundError,
  NoBundleFoundError,
  NoWorkspacesFoundError,
  NotLoggedInError,
  ParamRequiredError,
  ProjectDefinitionNotFoundError,
  UnsupportedProjectType
});
//# sourceMappingURL=errors.js.map
