"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var keywords_exports = {};
__export(keywords_exports, {
  createKeywordsCheckFn: () => createKeywordsCheckFn
});
module.exports = __toCommonJS(keywords_exports);
const WORD_CHAR_CLASS = "[\\p{L}\\p{N}_]";
const isWordChar = (() => {
  const wordCharRegex = new RegExp(WORD_CHAR_CLASS, "u");
  return (char) => {
    if (!char) return false;
    return wordCharRegex.test(char);
  };
})();
const keywordsCheck = (text, config) => {
  const { keywords } = config;
  const sanitizedKeywords = keywords.map((k) => k.replace(/[.,!?;:]+$/, ""));
  const keywordEntries = sanitizedKeywords.map((sanitized) => ({
    sanitized,
    escaped: sanitized.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
  })).filter(({ sanitized }) => sanitized.length > 0);
  if (keywordEntries.length === 0) {
    return {
      guardrailName: "keywords",
      tripwireTriggered: false,
      info: {
        matchedKeywords: []
      }
    };
  }
  const keywordPatterns = keywordEntries.map(({ sanitized, escaped }) => {
    const keywordChars = Array.from(sanitized);
    const firstChar = keywordChars[0];
    const lastChar = keywordChars[keywordChars.length - 1];
    const needsLeftBoundary = isWordChar(firstChar);
    const needsRightBoundary = isWordChar(lastChar);
    const leftBoundary = needsLeftBoundary ? `(?<!${WORD_CHAR_CLASS})` : "";
    const rightBoundary = needsRightBoundary ? `(?!${WORD_CHAR_CLASS})` : "";
    return `${leftBoundary}${escaped}${rightBoundary}`;
  });
  const patternText = `(?:${keywordPatterns.join("|")})`;
  const pattern = new RegExp(patternText, "giu");
  const matches = [];
  let match;
  const seen = /* @__PURE__ */ new Set();
  while ((match = pattern.exec(text)) !== null) {
    const matchedText = match[0];
    if (!seen.has(matchedText.toLowerCase())) {
      matches.push(matchedText);
      seen.add(matchedText.toLowerCase());
    }
  }
  const tripwireTriggered = matches.length > 0;
  return {
    guardrailName: "keywords",
    tripwireTriggered,
    info: {
      matchedKeywords: matches
    }
  };
};
const createKeywordsCheckFn = (config) => (input) => keywordsCheck(input, config);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createKeywordsCheckFn
});
//# sourceMappingURL=keywords.js.map