import type { AppConfig, Calibration, PolyMap, PolyWarp, Vec2, RenderStyle } from './types'; export const DEFAULT_RENDER: RenderStyle = { cutColor: '#00e5ff', rapidColor: '#ff9800', lineWidth: 1.5 }; function isFiniteNumber(n: unknown): n is number { return typeof n === 'number' && Number.isFinite(n); } function isVec2Array(v: unknown): v is Vec2[] { return Array.isArray(v) && v.every((p) => Array.isArray(p) && p.length === 2 && p.every(isFiniteNumber)); } function isNumArray(v: unknown, len: number): v is number[] { return Array.isArray(v) && v.length === len && v.every(isFiniteNumber); } function isVec2(v: unknown): v is Vec2 { return Array.isArray(v) && v.length === 2 && v.every(isFiniteNumber); } function termCount(degree: number): number { return ((degree + 1) * (degree + 2)) / 2; } function parsePolyMap(m: unknown, degree: number): PolyMap | null { if (!m || typeof m !== 'object') return null; const o = m as Record; const norm = o.norm as Record | undefined; if (!norm || !isVec2(norm.off) || !isVec2(norm.scl)) return null; const n = termCount(degree); if (!isNumArray(o.cu, n) || !isNumArray(o.cv, n)) return null; return { degree, norm: { off: norm.off as Vec2, scl: norm.scl as Vec2 }, cu: o.cu as number[], cv: o.cv as number[] }; } function parseWarp(w: unknown): PolyWarp | null { if (!w || typeof w !== 'object') return null; const o = w as Record; // Require a positive integer degree: a negative/zero/fractional degree makes termCount // collapse to 0, which would vacuously accept empty coefficient arrays (a garbage warp). if (!isFiniteNumber(o.degree) || !Number.isInteger(o.degree) || o.degree < 1) return null; const fwd = parsePolyMap(o.fwd, o.degree); const inv = parsePolyMap(o.inv, o.degree); if (!fwd || !inv) return null; return { degree: o.degree, fwd, inv }; } function parseCalibration(c: unknown): Calibration | null { if (!c || typeof c !== 'object') return null; const obj = c as Record; if (!isVec2Array(obj.imagePoints) || !isVec2Array(obj.machinePoints)) return null; if (obj.imagePoints.length !== obj.machinePoints.length || obj.imagePoints.length < 4) return null; const warp = parseWarp(obj.warp); if (!warp) return null; return { imagePoints: obj.imagePoints, machinePoints: obj.machinePoints, warp }; } function parseRenderStyle(r: unknown): RenderStyle { const obj = (r && typeof r === 'object' ? r : {}) as Record; return { cutColor: typeof obj.cutColor === 'string' ? obj.cutColor : DEFAULT_RENDER.cutColor, rapidColor: typeof obj.rapidColor === 'string' ? obj.rapidColor : DEFAULT_RENDER.rapidColor, lineWidth: isFiniteNumber(obj.lineWidth) ? obj.lineWidth : DEFAULT_RENDER.lineWidth, }; } export async function loadConfig(url: string): Promise { const res = await fetch(url); if (!res.ok) throw new Error(`Failed to load ${url}: ${res.status}`); const raw = (await res.json()) as Record; return { streamUrl: typeof raw.streamUrl === 'string' ? raw.streamUrl : '', calibration: parseCalibration(raw.calibration), renderDefaults: parseRenderStyle(raw.renderDefaults), }; }