{"version":3,"sources":["../../src/hooks/use-content-prediction.ts","../../src/utils/hash.ts"],"sourcesContent":["'use client'\n\nimport { useEffect, useRef, useState } from 'react'\nimport TrieSearch from 'trie-search'\n\nimport { CompletionPrediction, GetCompletionContentPredictionFn } from '@/types'\nimport { stringHash } from '@/utils/hash'\n\n\nconst DEFAULT_CACHE_SIZE = 100\n\n// Create a singleton instance of the trie with a reasonable cache size\nconst predictionCache = new TrieSearch<CompletionPrediction>([], {\n  min: 1, // Match from the first character\n  ignoreCase: false, // Case sensitive matching\n  maxCacheSize: DEFAULT_CACHE_SIZE,\n  idFieldOrFunction: 'text' // Use the prediction text itself for identification\n})\n\nexport interface UseContentPredictionOptions {\n  debounceTime?: number\n  generateId?: (inputText: string, predictionText: string) => string\n}\n\nexport interface UseContentPredictionResult {\n  prediction: CompletionPrediction | null\n  setInputText: (text: string) => void\n  clearPrediction: () => void\n}\n\nconst DEFAULT_DEBOUNCE_TIME_MS = 300\n\n/**\n * A hook that manages content predictions for text input, providing debounced API calls\n * and prediction state management. It handles:\n * - Debouncing prediction requests to prevent excessive API calls\n * - Managing prediction state and cleanup\n * - Aborting in-flight requests when new ones are made\n * - Generating content-based IDs for predictions\n * - Caching predictions using a trie to prevent duplicate requests\n *   and optimize for backspace operations\n *\n * @param getContentPredictionFn The function to fetch predictions\n * @param options Configuration options including debounce time, prediction function, and ID generator\n * @returns Object containing the current prediction, input text setter, and prediction clearer\n */\nfunction useContentPrediction(getContentPredictionFn: GetCompletionContentPredictionFn, options: UseContentPredictionOptions): UseContentPredictionResult {\n  const {\n    debounceTime = DEFAULT_DEBOUNCE_TIME_MS,\n    // By default, generate a deterministic ID based on input + prediction text\n    // The ID is not used for the trie search (which uses text directly), but is required for:\n    // 1. Accessibility (ARIA attributes like aria-activedescendant)\n    // 2. DOM identification for the prediction element\n    // 3. Consistent referencing between component renders\n    generateId = (inputText, predictionText) => `p-${stringHash(`${inputText}${predictionText}`)}`\n  } = options\n\n  const [inputText, setInputText] = useState<string>('')\n  const [prediction, setPrediction] = useState<CompletionPrediction | null>(null)\n  // Tracks if a prediction fetch is currently in progress to prevent returning\n  // predictions until the fetch completes\n  const [isFetching, setIsFetching] = useState<boolean>(false)\n  const lastFetchTimeRef = useRef<number>(0)\n\n  async function getContentPrediction(text: string): Promise<CompletionPrediction | null> {\n    const cachedResults = predictionCache.search(text)\n    if (cachedResults.length > 0) {\n      return cachedResults[0] || null\n    }\n\n    try {\n      const predictionText = await getContentPredictionFn(text)\n      if (!predictionText) return null\n\n      const prediction: CompletionPrediction = {\n        id: generateId(text, predictionText),\n        text: predictionText\n      }\n\n      // Cache the prediction using explicit mapping\n      predictionCache.map(text, prediction)\n\n      if (!isFetching) {\n        return prediction\n      }\n      else {\n        return null\n      }\n    }\n    catch {\n      return null\n    }\n  }\n\n  function clearPrediction(): void {\n    setPrediction(null)\n  }\n\n  useEffect(() => {\n    const now = Date.now()\n    if (now - lastFetchTimeRef.current < debounceTime || !inputText) {\n      return\n    }\n    setIsFetching(true)\n    const timer = setTimeout(async () => {\n      const prediction = await getContentPrediction(inputText)\n      if (prediction) {\n        setPrediction(prediction)\n      }\n      lastFetchTimeRef.current = Date.now()\n      setIsFetching(false)\n    }, debounceTime)\n\n    return () => {\n      clearTimeout(timer)\n      setIsFetching(false)\n    }\n  }, [inputText, debounceTime])\n\n  return {\n    prediction,\n    setInputText,\n    clearPrediction,\n  }\n}\n\nexport { useContentPrediction }\n","/**\n * A string hashing function that creates a deterministic\n * string hash using the Web Crypto API.\n *\n * @param text The text to hash\n * @returns A string hash that can be used as an ID\n */\nexport function stringHash(text: string): string {\n  // Return a default value for empty strings\n  if (text.length === 0) return 'h-0'\n\n  // Use TextEncoder to convert the string to UTF-8 bytes\n  const encoder = new TextEncoder()\n  const data = encoder.encode(text)\n\n  // Constants for the FNV-1a hash algorithm:\n  // FNV_PRIME: A prime number used to mix bits (16777619 in decimal)\n  // FNV_OFFSET_BASIS: The initial hash value (2166136261 in decimal)\n  const FNV_PRIME = 0x01000193\n  const FNV_OFFSET_BASIS = 0x811c9dc5\n\n  // This implements the FNV-1a hash algorithm, which:\n  // 1. XORs each byte with the current hash value\n  // 2. Multiplies by the FNV prime to mix bits\n  // 3. Uses unsigned 32-bit integer arithmetic (>>> 0)\n  const hash = [...data].reduce((h, byte) => {\n    return ((h ^ byte) * FNV_PRIME) >>> 0 // Ensures 32-bit unsigned integer\n  }, FNV_OFFSET_BASIS)\n\n  // Base36 is used for alphanumeric representation (0-9, a-z)\n  const BASE36 = 36\n\n  // Use absolute value and convert to base36 string for shorter representation\n  return `h-${Math.abs(hash).toString(BASE36)}`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA4C;AAC5C,yBAAuB;;;ACIhB,SAAS,WAAW,MAAsB;AAE/C,MAAI,KAAK,WAAW,EAAG,QAAO;AAG9B,QAAM,UAAU,IAAI,YAAY;AAChC,QAAM,OAAO,QAAQ,OAAO,IAAI;AAKhC,QAAM,YAAY;AAClB,QAAM,mBAAmB;AAMzB,QAAM,OAAO,CAAC,GAAG,IAAI,EAAE,OAAO,CAAC,GAAG,SAAS;AACzC,YAAS,IAAI,QAAQ,cAAe;AAAA,EACtC,GAAG,gBAAgB;AAGnB,QAAM,SAAS;AAGf,SAAO,KAAK,KAAK,IAAI,IAAI,EAAE,SAAS,MAAM,CAAC;AAC7C;;;ADzBA,IAAM,qBAAqB;AAG3B,IAAM,kBAAkB,IAAI,mBAAAA,QAAiC,CAAC,GAAG;AAAA,EAC/D,KAAK;AAAA;AAAA,EACL,YAAY;AAAA;AAAA,EACZ,cAAc;AAAA,EACd,mBAAmB;AAAA;AACrB,CAAC;AAaD,IAAM,2BAA2B;AAgBjC,SAAS,qBAAqB,wBAA0D,SAAkE;AACxJ,QAAM;AAAA,IACJ,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMf,aAAa,CAACC,YAAW,mBAAmB,KAAK,WAAW,G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