import { applyReferenceCalibration, removeReferenceCalibration, calibrationIdentity, calibrationStatus } from './reference-calibration.mjs'; let state, manifest, lightingOffTrial; let request = 1100000; const emit = (name, detail) => window.dispatchEvent(new CustomEvent(name, { detail })); const box = document.createElement('section'); box.id = 'referenceFinish'; box.innerHTML = `
Loading reference evidence…
`; document.querySelector('[data-inspector-body="finish"]').append(box); const get = id => document.getElementById(id); function sync() { if (!state || !manifest) return; let matching = false; try { matching = calibrationIdentity(state.project) === manifest.identity; } catch {} const active = state.project.referenceCalibration; get('applyReferenceFinish').disabled = !matching || state.raster; get('applyReferenceUnlit').disabled = !matching || state.raster || !lightingOffTrial; get('removeReferenceFinish').disabled = !active || state.raster; const usingTrial = active?.id === lightingOffTrial?.id && Boolean(lightingOffTrial); const status = calibrationStatus(state.project, usingTrial ? lightingOffTrial : manifest); const prefix = status === 'current' ? 'Measured preset applied. ' : status === 'not-applied' ? '' : 'Design/settings changed; measured error no longer applies. '; const result = usingTrial ? 'Lighting off reduced mean RGB error from 16.34 to 12.04 on the registered P250810477 JPEG crop. ' : 'Both settings were evaluated only on P250810477 original colours. '; get('referenceFinishInfo').textContent = prefix + result + 'Native finishing remains unverified.'; } function edit(change) { try { const project = change(state.project); emit('cad:edit', { id: ++request, type: 'replace-project', revision: state.revision, project }); } catch (error) { get('referenceFinishInfo').textContent = error.message; } } get('applyReferenceFinish').onclick = () => edit(project => applyReferenceCalibration(project, manifest)); get('applyReferenceUnlit').onclick = () => edit(project => applyReferenceCalibration(project, lightingOffTrial)); get('removeReferenceFinish').onclick = () => edit(removeReferenceCalibration); window.addEventListener('cad:document', event => { state = event.detail; sync(); }); window.addEventListener('cad:edit-result', event => { if (event.detail.id === request && !event.detail.ok) get('referenceFinishInfo').textContent = event.detail.error; }); fetch('./reference-calibrations/P250810477.json').then(response => { if (!response.ok) throw Error('Reference evidence unavailable'); return response.json(); }).then(data => { const { lightingOffTrial: trial, ...base } = data; manifest = base; if (trial) lightingOffTrial = { ...base, ...trial }; sync(); }).catch(error => { get('referenceFinishInfo').textContent = error.message; }); emit('cad:request-state');