import { FIELD_IDS, blankPlan } from "/shared/fields.js?v=__ASSET_VERSION__";
import { computeLedger } from "/shared/ledger.js?v=__ASSET_VERSION__";
import {
formatDuration,
formatSigned,
parseRangeMidpoint,
} from "/shared/time.js?v=__ASSET_VERSION__";
import {
CULTIVARS,
findCultivar,
findProcess,
findRoastLevel,
} from "/shared/reference-data.js?v=__ASSET_VERSION__";
import { buildPlanCurve, pointsToPathD, tToX, tempToY, curveWarnings } from "/shared/curve.js?v=__ASSET_VERSION__";
import { initPrefillPanel } from "./prefill-ui.js?v=__ASSET_VERSION__";
import { initAlogPanel } from "./alog-ui.js?v=__ASSET_VERSION__";
import { renderReportCurve } from "/shared/report.js?v=__ASSET_VERSION__";
import { initPrint } from "./print.js?v=__ASSET_VERSION__";
import { initLotPicker } from "./lot-picker.js?v=__ASSET_VERSION__";
import { api, protectedFetch, csrfToken } from "./api.js?v=__ASSET_VERSION__";
import { applyAvatar, initSideNav } from "./nav.js?v=__ASSET_VERSION__";
import { wireWhyPanels } from "./why-panels.js?v=__ASSET_VERSION__";
import { initFieldHelp } from "./field-help.js?v=__ASSET_VERSION__";
import { initPlanChat } from "./plan-chat-ui.js?v=__ASSET_VERSION__";
import { showToast } from "./toast.js?v=__ASSET_VERSION__";
const FIELD_ID_SET = new Set(FIELD_IDS);
const STORAGE_PREFIX = "roastPlannerPlan.v2";
let storageKey = null;
let remotePlanId = null;
let draftSyncedAt = null;
// The custom name of the currently loaded plan (server column, not part of the plan JSONB —
// see roast_plans.name). Header display prefers this over worksheet field 0.1 when set.
let currentPlanName = "";
let lastDrawerOpener = null;
let deferredInstallPrompt = null;
let lotPicker = null;
const BAND_DOMAIN = [60, 220]; // shared °C domain for every band-track in the Machine Plan section
const MACHINE_PROFILE_KEY_PREFIX = "roastPlannerMachineProfile.v1";
const machineProfile = null;
function renderMachineProfileNote() {
document.getElementById("machine-profile-note").textContent = "Plans use your inputs. Roaster selection and roast history do not change timing or temperature suggestions.";
}
// Temperature fields are always stored in state.plan as canonical °C — this is purely a
// display-layer preference. tempC/actualBt/expectedBt cover Machine Plan, Roast Log, and
// the actuator schedule respectively; every other numeric field (%, g, days) is untouched.
const TEMP_UNIT_KEY = "roastPlannerTempUnit.v1";
const TEMP_FIELD_RE = /\.(tempC|actualBt|expectedBt)$/;
let tempUnit = localStorage.getItem(TEMP_UNIT_KEY) === "C" ? "C" : "F";
const cToF = (c) => (c * 9) / 5 + 32;
const fToC = (f) => ((f - 32) * 5) / 9;
function isTempField(name) {
return TEMP_FIELD_RE.test(name);
}
// Canonical °C string -> what the field should display right now.
function tempToDisplay(canonical) {
const n = Number.parseFloat(canonical);
if (!Number.isFinite(n)) return canonical ?? "";
return String(Math.round(tempUnit === "F" ? cToF(n) : n));
}
// What the user typed (in the current display unit) -> canonical °C string.
function tempToCanonical(display) {
const n = Number.parseFloat(display);
if (!Number.isFinite(n)) return display;
return String(Math.round((tempUnit === "F" ? fToC(n) : n) * 10) / 10);
}
export const state = { plan: blankPlan() };
const form = document.getElementById("plan-form");
// The print worksheet (#print-sheet) sits outside #plan-form on purpose — see index.html —
// so its radios don't fight the screen form's identically-named radios for exclusivity.
// Every sync pass therefore has to reach both containers explicitly.
function allNamedInputs() {
return document.querySelectorAll(
"#plan-form input, #plan-form select, #plan-form textarea, " +
"#print-sheet input, #print-sheet select, #print-sheet textarea",
);
}
// ---- nested path get/set for names like "temps.charge.tempC" or "blendComponents.0.cultivar"
function getPath(obj, path) {
return path.split(".").reduce((o, k) => (o == null ? undefined : o[k]), obj);
}
function setPath(obj, path, value) {
const parts = path.split(".");
let node = obj;
for (let i = 0; i < parts.length - 1; i++) {
if (node[parts[i]] === undefined || node[parts[i]] === null)
node[parts[i]] = {};
node = node[parts[i]];
}
node[parts[parts.length - 1]] = value;
}
function valueForName(name) {
if (FIELD_ID_SET.has(name)) return state.plan.fields[name] ?? "";
const v = getPath(state.plan, name);
return v ?? "";
}
function setValueForName(name, value) {
if (FIELD_ID_SET.has(name)) {
state.plan.fields[name] = value;
} else {
setPath(state.plan, name, value);
}
}
// ---- dynamic rows: blend components + actuator schedule
// Each renders into TWO places that share `name` attributes: the interactive screen
// component, and the hidden print-only worksheet table (#blend-rows / #actuator-rows),
// which is kept in sync by renderFormFromPlan() and only needs to look right on paper.
function renderBlendPrintRows() {
const tbody = document.getElementById("blend-rows");
tbody.replaceChildren();
state.plan.blendComponents.forEach((_, i) => {
const tr = document.createElement("tr");
tr.append(
document.createRange().createContextualFragment(`
`),
);
tbody.appendChild(tr);
});
}
function renderBlendCards() {
const wrap = document.getElementById("blend-cards");
wrap.replaceChildren();
state.plan.blendComponents.forEach((_, i) => {
const card = document.createElement("div");
card.className = "blend-card";
card.append(
document.createRange().createContextualFragment(`
`),
);
wrap.appendChild(card);
});
for (const btn of wrap.querySelectorAll("[data-remove-blend]")) {
btn.addEventListener("click", () => {
const i = Number(btn.dataset.removeBlend);
if (state.plan.blendComponents.length <= 1) return;
state.plan.blendComponents.splice(i, 1);
renderBlend();
renderFormFromPlan();
recompute();
});
}
}
function updateBlendTotal() {
const total = state.plan.blendComponents.reduce(
(sum, c) => sum + (Number.parseFloat(c.sharePct) || 0),
0,
);
const fill = document.getElementById("blend-total-fill");
const label = document.getElementById("blend-total-label");
if (!fill || !label) return;
fill.style.width = `${Math.min(100, total)}%`;
fill.classList.toggle("over", total > 100);
fill.classList.toggle("under", total > 0 && total < 100);
label.textContent = `${Math.round(total * 10) / 10}% of 100%`;
}
function updateBlendVisibility(mode) {
document
.getElementById("blend-body")
.classList.toggle("collapsed", mode !== "blend");
}
function renderBlend() {
renderBlendPrintRows();
renderBlendCards();
updateBlendTotal();
}
function renderActuators() {} // Older plans retain their saved schedule for report export.
// ---- populate every control in the form from state.plan
export function renderFormFromPlan() {
for (const el of allNamedInputs()) {
const name = el.name;
if (!name) continue;
const value = valueForName(name);
if (el.type === "radio") {
el.checked = el.value === value;
} else if (el.type === "checkbox") {
el.checked = Boolean(value);
} else {
el.value = isTempField(name) ? tempToDisplay(value) : (value ?? "");
}
}
updateBlendVisibility(state.plan.fields["2.1"]);
document.getElementById("header-coffee-name").textContent =
currentPlanName || state.plan.fields["0.1"] || "New plan";
}
function fmtOut(id, text) {
for (const el of document.querySelectorAll(`[data-out="${id}"]`))
el.textContent = text ?? "—";
}
function renderLedger() {
const ledger = computeLedger(state.plan, machineProfile);
const d = (s) => (s === null || s === undefined ? "—" : formatDuration(s));
const ds = (s) => (s === null || s === undefined ? "—" : formatSigned(s));
fmtOut("l1", d(ledger.lines.l1));
fmtOut("l2", ds(ledger.lines.l2));
fmtOut("l3", ds(ledger.lines.l3));
fmtOut("subtotalA", d(ledger.subtotalA));
fmtOut("pace-factor", `×${ledger.pace.toFixed(2)}`);
fmtOut("A", d(ledger.A));
fmtOut("yellow", d(ledger.yellow));
fmtOut("maillard", d(ledger.maillard));
fmtOut("l7", d(ledger.lines.l7));
fmtOut("l8", ds(ledger.lines.l8));
fmtOut("l9", ds(ledger.lines.l9));
fmtOut("C", d(ledger.C));
fmtOut("D", d(ledger.D));
const pct = (v) => (v === null || v === undefined ? "—" : `${v.toFixed(1)}%`);
fmtOut("check-drying", pct(ledger.checks.drying.pct));
fmtOut("check-maillard", pct(ledger.checks.maillard.pct));
fmtOut("check-dtr", pct(ledger.checks.dtr.pct));
fmtOut("check-ceiling", d(ledger.checks.ceiling.valueS));
ledger.warnings.push(...curveWarnings(buildPlanCurve(state.plan, ledger)));
const warnings = document.getElementById("ledger-warnings");
warnings.replaceChildren(
...ledger.warnings.map((warning) => {
const item = document.createElement("p");
item.textContent = warning;
return item;
}),
);
warnings.classList.toggle("hidden", ledger.warnings.length === 0);
for (const [key, check] of Object.entries(ledger.checks)) {
for (const cell of document.querySelectorAll(`[data-pass="${key}"]`)) {
cell.classList.remove("pass", "fail", "unknown", "informational");
// Drying/Maillard are mathematically derived from DTR (yellow = 0.56 × A), so they can
// never fail independently of it — shown as plain figures, not a pass/fail verdict.
if (check.informational) cell.classList.add("informational");
else
cell.classList.add(
check.pass === null ? "unknown" : check.pass ? "pass" : "fail",
);
}
}
// Box 8 / box 11 derived time columns
fmtOut("t-charge", "0:00");
fmtOut("t-yellow", d(ledger.yellow));
fmtOut("t-fc", d(ledger.A));
fmtOut("t-drop", d(ledger.D));
fmtOut("pa-charge", "0:00");
fmtOut("pa-tp", state.plan.temps.tp.time || "—");
fmtOut("pa-yellow", d(ledger.yellow));
fmtOut("pa-fc", d(ledger.A));
fmtOut("pa-drop", d(ledger.D));
document.getElementById("back-coffee-name").textContent =
currentPlanName || state.plan.fields["0.1"] || "";
document.getElementById("header-coffee-name").textContent =
currentPlanName || state.plan.fields["0.1"] || "New plan";
return ledger;
}
function domainPct(v) {
const [lo, hi] = BAND_DOMAIN;
return Math.max(0, Math.min(100, ((v - lo) / (hi - lo)) * 100));
}
function renderBandRanges() {
for (const row of document.querySelectorAll(".band-row")) {
const lo = Number(row.dataset.bandLo);
const hi = Number(row.dataset.bandHi);
const range = row.querySelector(".band-range");
if (!range) continue;
const loPct = domainPct(lo);
range.style.left = `${loPct}%`;
range.style.width = `${domainPct(hi) - loPct}%`;
}
}
function renderBandMarkers() {
for (const row of document.querySelectorAll(".band-row")) {
const input = row.querySelector('input[name$=".tempC"]');
const marker = row.querySelector(".band-marker");
if (!input || !marker) continue;
const raw = Number.parseFloat(input.value);
const v = Number.isFinite(raw) ? (tempUnit === "F" ? fToC(raw) : raw) : NaN;
if (!Number.isFinite(v)) {
marker.style.display = "none";
continue;
}
const lo = Number(row.dataset.bandLo);
const hi = Number(row.dataset.bandHi);
marker.style.display = "block";
marker.style.left = `${domainPct(v)}%`;
marker.classList.toggle("out-of-band", v < lo || v > hi);
}
}
function paintCurveInto(planGroup, refGroup, planPoints, ref) {
planGroup.replaceChildren();
if (planPoints.length > 0) {
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", pointsToPathD(planPoints));
path.setAttribute("fill", "none");
path.setAttribute("stroke", "currentColor");
path.setAttribute("stroke-width", "1.6");
path.style.color = "var(--ink, #1a1512)";
planGroup.appendChild(path);
for (const p of planPoints) {
const c = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle",
);
c.setAttribute("cx", tToX(p.timeS).toFixed(1));
c.setAttribute("cy", tempToY(p.tempC).toFixed(1));
c.setAttribute("r", "3.2");
c.setAttribute("fill", "currentColor");
c.style.color = "var(--ink, #1a1512)";
planGroup.appendChild(c);
}
}
refGroup.replaceChildren();
if (ref?.curve?.length) {
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
const d = ref.curve
.map(
(p, i) =>
`${i === 0 ? "M" : "L"}${tToX(p.t).toFixed(1)},${tempToY(p.bt).toFixed(1)}`,
)
.join(" ");
path.setAttribute("d", d);
path.setAttribute("fill", "none");
path.setAttribute("stroke", "currentColor");
path.setAttribute("stroke-width", "1.3");
path.setAttribute("stroke-dasharray", "4 3");
path.setAttribute("opacity", ".65");
path.style.color = "var(--ink-2, #5a5048)";
refGroup.appendChild(path);
for (const m of ref.milestones ?? []) {
if (m.tempC === null) continue;
const c = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle",
);
c.setAttribute("cx", tToX(m.timeS).toFixed(1));
c.setAttribute("cy", tempToY(m.tempC).toFixed(1));
c.setAttribute("r", "2.6");
c.setAttribute("fill", "none");
c.setAttribute("stroke", "currentColor");
c.setAttribute("stroke-width", "1.2");
c.style.color = "var(--ink-2, #5a5048)";
refGroup.appendChild(c);
}
}
}
function renderCurve(ledger) {
const planPoints = buildPlanCurve(state.plan, ledger);
const ref = state.plan.reference;
paintCurveInto(
document.getElementById("plan-curve"),
document.getElementById("ref-curve"),
planPoints,
ref,
);
const live = document.getElementById("curve-svg-live");
live.setAttribute("viewBox", "0 0 900 440");
live.innerHTML = renderReportCurve(planPoints, ledger, ref).replace(/^