const DANGEROUS_PATTERNS = [
  /ignore\s+(all\s+)?(previous|above|prior)\s+(instructions?|commands?|prompts?)/i,
  /forget\s+(everything|all|previous)/i,
  /system\s*:\s*/i,
  /new\s+instructions?\s*:/i,
  /you\s+are\s+now/i,
  /pretend\s+to\s+be/i,
  /jailbreak/i,
  /DAN\s+mode/i,
  /developer\s+mode/i,
  /\[SYSTEM\]/i,
  /\[ADMIN\]/i,
  /\[ROOT\]/i,
  /<\|endoftext\|>/i,
  /<\|im_start\|>/i,
  /<\|im_end\|>/i
];
const SUSPICIOUS_PATTERNS = [
  /\{\{.*?\}\}/,
  // Template injection patterns
  /\$\{.*?\}/,
  // String interpolation
  /<script/i,
  // Script tags
  /javascript:/i,
  // JavaScript protocol
  /on\w+\s*=/i
  // Event handlers
];
function sanitizeUserInput(input, options = {}) {
  const {
    maxLength = 1e3,
    allowHtml = false,
    strict = false
  } = options;
  if (!input || typeof input !== "string") {
    throw new Error("Input must be a non-empty string");
  }
  let sanitized = input.trim();
  if (sanitized.length > maxLength) {
    sanitized = sanitized.substring(0, maxLength);
  }
  for (const pattern of DANGEROUS_PATTERNS) {
    if (pattern.test(sanitized)) {
      throw new Error("Input contains potentially malicious content");
    }
  }
  if (strict) {
    for (const pattern of SUSPICIOUS_PATTERNS) {
      if (pattern.test(sanitized)) {
        throw new Error("Input contains suspicious patterns");
      }
    }
  }
  if (!allowHtml) {
    sanitized = sanitized.replace(/<[^>]*>/g, "");
  }
  sanitized = sanitized.replace(/\r/g, " ");
  sanitized = sanitized.split("\n").map((line) => line.replace(/[ \t]+/g, " ").trim()).join("\n").replace(/\n{3,}/g, "\n\n");
  sanitized = sanitized.replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]/g, "");
  sanitized = sanitized.replace(/\\n\\n/g, " ").replace(/\\r/g, "");
  return sanitized;
}
function validatePromptSafety(input) {
  try {
    const sanitized = sanitizeUserInput(input, { strict: true });
    return {
      isSafe: true,
      sanitized
    };
  } catch (error) {
    return {
      isSafe: false,
      reason: error instanceof Error ? error.message : "Unknown validation error"
    };
  }
}
function sanitizeRefinementInstruction(instruction) {
  return sanitizeUserInput(instruction, {
    maxLength: 500,
    allowHtml: false,
    strict: true
  });
}
function sanitizeContentInput(content) {
  return sanitizeUserInput(content, {
    maxLength: 5e3,
    allowHtml: false,
    strict: false
  });
}
export {
  sanitizeContentInput,
  sanitizeRefinementInstruction,
  sanitizeUserInput,
  validatePromptSafety
};
