"use client"; import Image from "next/image"; import Link from "next/link"; import { Search, X } from "lucide-react"; import { useMemo, useState } from "react"; import { Input } from "@/components/ui/input"; import type { Product } from "@/lib/headless/types"; export function PredictiveSearch({ products }: { products: Product[] }) { const [query, setQuery] = useState(""); const normalized = query.trim().toLocaleLowerCase("es"); const matches = useMemo(() => normalized.length < 2 ? [] : products.filter((product) => [product.name, product.category, product.collection, product.brand].join(" ").toLocaleLowerCase("es").includes(normalized)).slice(0, 6), [normalized, products]); return (
setQuery(event.target.value)} placeholder="Busca juguetes, mochilas, personajes…" className="h-[52px] border-white/80 bg-white pl-[52px] pr-12 shadow-xl" /> {query && } {matches.length > 0 && (
{matches.map((product) => ( setQuery("")}>
{product.name}{product.category} · {product.collection}
))}
)}
); }