import { meshkeeApiBase, websiteDomain } from "@/lib/config"; export async function GET(request: Request) { const url = new URL(request.url); const query = url.searchParams.get("q")?.trim() ?? ""; if (query.length <= 2) { return Response.json({ items: [] }); } const encoded = encodeURIComponent(query); const base = `${meshkeeApiBase}/tenants/${encodeURIComponent(websiteDomain)}`; try { const storeResponse = await fetch(`${base}/store-items?name=${encoded}&pageSize=8`, { cache: "no-store", }); const storeData = (await storeResponse.json()) as { items?: unknown[] }; if (storeData.items?.length) { return Response.json(storeData); } const catalogResponse = await fetch(`${base}/products?name=${encoded}&pageSize=8`, { cache: "no-store", }); const catalogData = (await catalogResponse.json()) as { items?: Array<{ id: string; nameFa?: string | null; title?: string | null; slug?: string | null; thumbnailUrl?: string | null; image?: string | null; store?: { minPrice?: number | null; maxPrice?: number | null }; }>; }; return Response.json({ items: (catalogData.items ?? []).map((item) => ({ id: item.id, productId: item.id, productNameFa: item.nameFa, productTitle: item.title, productImage: item.thumbnailUrl || item.image, price: item.store?.minPrice ?? item.store?.maxPrice ?? null, })), }); } catch { return Response.json({ items: [] }); } }