// Browser-safe. THE ONLY implementation of the worksheet's time-ledger math. // Mirrors manual-roast-planner.html's worksheet box 6 (lines 1-9, totals A/C/D) // and box 7 (the four sanity checks) exactly. Imported by both server and browser. import { parseDuration, parseRangeMidpoint } from "./time.js"; import { YELLOW_RATIO, SANITY_BANDS } from "./reference-data.js"; /** * @param {import("./fields.js").Plan} plan * @returns ledger result — every time value in seconds, null when not computable. */ export function computeLedger(plan) { const f = plan.fields ?? {}; const isBlend = f["2.1"] === "blend"; const warnings = []; const l1 = parseRangeMidpoint(isBlend ? f["2.4"] : f["1.4"]); const l2 = parseDuration(f["1.6"]) ?? 0; const l3 = parseDuration(f["5.6"]) ?? 0; const l4 = parseDuration(f["6.4"]) ?? 0; const A = l1 === null ? null : l1 + l2 + l3 + l4; const yellow = A === null ? null : Math.round(A * YELLOW_RATIO); const maillard = A === null ? null : A - yellow; const l7 = parseRangeMidpoint(f["4.3"]); const l8 = parseDuration(isBlend ? f["2.5"] : f["3.2"]) ?? 0; const l9 = parseDuration(f["1.7"]) ?? 0; const C = l7 === null ? null : l7 + l8 + l9; // rows 5 and 6 (yellow/Maillard) are NEVER summed here const D = A === null || C === null ? null : A + C; if (l1 === null) warnings.push("No cultivar/blend anchor (field 1.4 or 2.4) — first crack cannot be computed."); if (l7 === null) warnings.push("No development base (field 4.3) — development cannot be computed."); const pct = (part) => (D && D > 0 && part !== null ? (part / D) * 100 : null); const checks = { drying: passCheck(pct(yellow), SANITY_BANDS.drying), maillard: passCheck(pct(maillard), SANITY_BANDS.maillard), dtr: passCheck(pct(C), SANITY_BANDS.dtr), ceiling: { valueS: C, maxS: SANITY_BANDS.ceilingS, pass: C === null ? null : C < SANITY_BANDS.ceilingS, }, }; return { isBlend, lines: { l1, l2, l3, l4, l7, l8, l9 }, A, yellow, maillard, C, D, checks, warnings, }; } function passCheck(pct, band) { return { pct, lo: band.lo, hi: band.hi, pass: pct === null ? null : pct >= band.lo && pct <= band.hi, }; }