import { existsSync, readFileSync } from 'node:fs'; import { FOOD_GROUPS, type FoodGroup } from '../groups.ts'; // Groups two models of different families agreed on for records whose source gives no category (20 FD-09). // `scripts/food-assign-groups.mjs` writes the file, one {"key":"SRC-06|","group":"…"} per line; // CALSA_GROUP_ASSIGNMENTS names it. Without the variable nothing is assigned. let table: Map | undefined; export function assignedGroup(src: string, recordId: string): FoodGroup | null { if (!table) { table = new Map(); const path = process.env.CALSA_GROUP_ASSIGNMENTS; if (path && existsSync(path)) for (const line of readFileSync(path, 'utf8').split('\n')) { if (!line) continue; const { key, group } = JSON.parse(line) as { key: string; group: string }; if ((FOOD_GROUPS as readonly string[]).includes(group)) table.set(key, group as FoodGroup); } } return table.get(`${src}|${recordId}`) ?? null; }