"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 validate_bot_exports = {};
__export(validate_bot_exports, {
  validateBotDefinition: () => validateBotDefinition
});
module.exports = __toCommonJS(validate_bot_exports);
var errors = __toESM(require("../errors"));
var utils = __toESM(require("../utils"));
const PLUGIN_PREFIX_SEP = "#";
const validateBotDefinition = (b) => {
  const { actions, events, states } = b;
  const invalidActionNames = _nonCamelCaseKeys(actions ?? {});
  if (invalidActionNames.length) {
    throw new errors.BotpressCLIError(
      `The following action names are not in camelCase: ${invalidActionNames.join(", ")}`
    );
  }
  const invalidEventNames = _nonCamelCaseKeys(events ?? {});
  if (invalidEventNames.length) {
    throw new errors.BotpressCLIError(`The following event names are not in camelCase: ${invalidEventNames.join(", ")}`);
  }
  const invalidStateNames = _nonCamelCaseKeys(states ?? {});
  if (invalidStateNames.length) {
    throw new errors.BotpressCLIError(`The following state names are not in camelCase: ${invalidStateNames.join(", ")}`);
  }
  for (const [pluginName, plugin] of Object.entries(b.plugins ?? {})) {
    const integrationDependencies = plugin.definition.integrations ?? {};
    for (const dep of Object.values(integrationDependencies)) {
      if (!_hasIntegrationDependency(b, dep)) {
        throw new errors.BotpressCLIError(
          `Plugin "${pluginName}" has a dependency on integration "${dep.name}@${dep.version}", but it is not present in the bot definition. Please install it.`
        );
      }
    }
    const interfaceDependencies = plugin.definition.interfaces ?? {};
    for (const [interfaceAlias, dep] of Object.entries(interfaceDependencies)) {
      const interfaceImpl = plugin.interfaces[interfaceAlias];
      if (!interfaceImpl) {
        throw new errors.BotpressCLIError(
          `Plugin "${pluginName}" has a dependency on interface "${dep.name}@${dep.version}" (aliased as "${interfaceAlias}"), but the bot does not specify an implementation for it.`
        );
      }
      if (!_hasIntegrationDependency(b, interfaceImpl)) {
        throw new errors.BotpressCLIError(
          `Integration "${interfaceImpl.name}@${interfaceImpl.version}" is not installed in the bot, but specified as an implementation for interface "${dep.name}@${dep.version}"`
        );
      }
    }
  }
};
const _nonCamelCaseKeys = (obj) => Object.keys(obj).filter((key) => {
  const tokens = key.split(PLUGIN_PREFIX_SEP, 2);
  return !utils.casing.is.camelCase(tokens.at(-1));
});
const _hasIntegrationDependency = (b, dep) => {
  const integrationInstances = Object.entries(b.integrations ?? {}).map(([_k, v]) => v);
  return integrationInstances.some(
    (integration) => integration.name === dep.name && integration.version === dep.version
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  validateBotDefinition
});
//# sourceMappingURL=validate-bot.js.map
