/**
 * AI utility functions
 */

/**
 * Sanitize user input for AI prompts to prevent prompt injection
 */
export function sanitizeForAIPrompt(input: string, maxLength: number = 200): string {
  // 1. Normalize Unicode (NFKC form collapses homoglyphs)
  let sanitized = input.normalize('NFKC');

  // 2. Remove zero-width characters
  sanitized = sanitized.replace(/[\u200B-\u200D\uFEFF\u00AD]/g, '');

  // 3. Expanded blocklist patterns
  const dangerousPatterns = [
    /ignore\s*(all|previous|above)?\s*instruction/gi,
    /system\s*prompt/gi,
    /you\s*are\s*(now|a)/gi,
    /pretend\s*(to\s*be|you)/gi,
    /act\s*as/gi,
    /disregard/gi,
    /forget\s*(everything|all|previous)/gi,
    /new\s*instruction/gi,
    /override/gi,
    /\[system\]/gi,
    /<\|im_start\|>/gi,
    /<\|im_end\|>/gi,
    /```[\s\S]*?```/g,  // Code blocks that might contain injection
    /"""/g,  // Triple quotes
    /---+/g,  // Horizontal rules/delimiters
  ];

  for (const pattern of dangerousPatterns) {
    sanitized = sanitized.replace(pattern, '[filtered]');
  }

  return sanitized.slice(0, maxLength);
}
