import { t } from '../i18n' import { X } from 'lucide-react' import { useMemo, useState } from 'react' import { normalizeText } from '../lib/utils' interface Props { value: string[] onChange: (tags: string[]) => void suggestions: string[] placeholder?: string /** Texte affiché pour une valeur enregistrée (ex. genre traduit) */ display?: (value: string) => string /** Valeur enregistrée à partir du texte saisi (ex. nom de référence d'un genre) */ normalize?: (input: string) => string } /** Saisie de tags libres avec suggestions tapables. */ export default function TagInput({ value, onChange, suggestions, placeholder, display = (v) => v, normalize = (v) => v }: Props) { const [text, setText] = useState('') const add = (raw: string) => { const typed = raw.trim().replace(/,$/, '').trim() if (!typed) return const tag = normalize(typed) if (!value.some((v) => normalizeText(v) === normalizeText(tag))) onChange([...value, tag]) setText('') } const filtered = useMemo(() => { const q = normalizeText(text) const taken = new Set(value.map((v) => normalizeText(display(v)))) return suggestions.filter((s) => !taken.has(normalizeText(s)) && (!q || normalizeText(s).includes(q))).slice(0, 12) }, [text, value, suggestions]) return (