/**
 * text.js - Text processing utilities
 */

export function simpleHash(text) {
  let hash = 0;
  for (let i = 0; i < text.length; i++) {
    const char = text.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash;
  }
  return Math.abs(hash).toString(36);
}

export function levenshteinDistance(a, b) {
  if (a.length === 0) return b.length;
  if (b.length === 0) return a.length;
  
  const matrix = [];
  
  for (let i = 0; i <= b.length; i++) {
    matrix[i] = [i];
  }
  
  for (let j = 0; j <= a.length; j++) {
    matrix[0][j] = j;
  }
  
  for (let i = 1; i <= b.length; i++) {
    for (let j = 1; j <= a.length; j++) {
      if (b.charAt(i - 1) === a.charAt(j - 1)) {
        matrix[i][j] = matrix[i - 1][j - 1];
      } else {
        matrix[i][j] = Math.min(
          matrix[i - 1][j - 1] + 1,
          matrix[i][j - 1] + 1,
          matrix[i - 1][j] + 1
        );
      }
    }
  }
  
  return matrix[b.length][a.length];
}

export function tokenize(text) {
  const tokens = [];
  // English words
  const english = text.match(/[a-zA-Z]+/g) || [];
  tokens.push(...english.map(t => t.toLowerCase()));
  // Chinese characters
  const chinese = text.match(/[\u4e00-\u9fff]/g) || [];
  tokens.push(...chinese);
  // Numbers
  const numbers = text.match(/\d+/g) || [];
  tokens.push(...numbers);
  return tokens;
}
