// app/api/options/lens/route.ts import { NextResponse } from "next/server"; import { directusFetch } from "@/lib/directus"; /** * For fiber, co2-galvo, uv → f-theta scan lenses * For co2-gantry → focusing lenses */ function collectionForTarget(target?: string): { coll: string } | null { switch (target) { case "settings_fiber": case "settings_co2gal": case "settings_uv": return { coll: "laser_scan_lens" }; case "settings_co2gan": return { coll: "laser_focusing_lens" }; default: return null; } } function bestLensLabel(it: any): string { // Try common fields, then derive something readable if (it.name) return String(it.name); if (it.model) return String(it.model); if (it.focal_length_mm) return `F${it.focal_length_mm} mm`; return String(it.submission_id ?? it.id ?? "lens"); } export async function GET(req: Request) { try { const { searchParams } = new URL(req.url); const q = searchParams.get("q")?.trim() || ""; const limit = Number(searchParams.get("limit") || "500"); const target = searchParams.get("target") || undefined; const cfg = collectionForTarget(target); if (!cfg) return NextResponse.json({ error: "missing/invalid target" }, { status: 400 }); const url = `/items/${cfg.coll}?limit=${limit}${q ? `&search=${encodeURIComponent(q)}` : ""}`; const { data } = await directusFetch<{ data: any[] }>(url); const out = (data || []) .map((it) => ({ id: String(it.submission_id ?? it.id), label: bestLensLabel(it), })) .sort((a, b) => a.label.localeCompare(b.label)); return NextResponse.json({ data: out }); } catch (e: any) { return NextResponse.json({ error: e?.message || "lens error" }, { status: 500 }); } }