import { NextResponse } from 'next/server' import { getCatalog } from '@/lib/catalog-cache' import { readOverrides } from '@/lib/overrides' import type { CatalogItem } from '@/data/mock-catalog' function applyOverrides(items: CatalogItem[]): CatalogItem[] { const overrides = readOverrides() return items .map((item) => { const ov = overrides[item.id] if (!ov) return item return { ...item, featured: ov.featured ?? item.featured, category: ov.categoryOverride ?? item.category, categoryLabel: ov.categoryLabelOverride ?? item.categoryLabel, categories: ov.categoryOverride ? [ov.categoryOverride, ...(item.categories ?? [item.category]).slice(1)] : (item.categories ?? [item.category]), categoryLabels: ov.categoryLabelOverride ? [ov.categoryLabelOverride, ...(item.categoryLabels ?? [item.categoryLabel]).slice(1)] : (item.categoryLabels ?? [item.categoryLabel]), showColors: ov.showColors != null ? ov.showColors : item.showColors, colorMin: ov.colorMin ?? item.colorMin, colorMax: ov.colorMax !== undefined ? ov.colorMax : item.colorMax, chromeSurchargePerColor: ov.chromeSurchargePerColor ?? item.chromeSurchargePerColor, disabledColors: ov.disabledColors?.length ? ov.disabledColors : item.disabledColors, quantityUnit: ov.quantityUnit ?? item.quantityUnit, description: ov.descriptionOverride ?? item.description, variations: item.variations .filter((v) => !(ov.hiddenVariationIds ?? []).includes(v.id)), modifiers: item.modifiers .filter((m) => !(ov.hiddenModifierIds ?? []).includes(m.id)) .map((m) => { const minOverride = ov.modifierMinSelected?.[m.id] return minOverride !== undefined ? { ...m, minSelected: minOverride } : m }), } }) .filter((item) => !(overrides[item.id]?.hidden)) .sort((a, b) => { const featDiff = (b.featured ? 1 : 0) - (a.featured ? 1 : 0) if (featDiff !== 0) return featDiff const aOrder = overrides[a.id]?.sortOrder ?? 0 const bOrder = overrides[b.id]?.sortOrder ?? 0 return aOrder - bOrder }) } export async function GET() { try { const { items: rawItems } = await getCatalog() const items = applyOverrides(rawItems) return NextResponse.json({ items, source: 'square' }) } catch (err) { console.error('[catalog] error:', err) return NextResponse.json({ items: [], source: 'error' }, { status: 500 }) } }