/* * Centralized application state. Every module imports the same `state` object so * all shared data lives in one place. The `elements` object caches DOM references * established during initialization. */ export const state = { db: null, reports: [], templatesCatalog: [], templateDefinitions: new Map(), lookups: new Map(), imageRules: null, appConfig: new Map(), currentReportId: null, currentAttachments: [], selectedTemplateCode: null, saveState: 'idle', saveTimer: null, autosaveIntervalId: null, lastSyncAt: null, /* Dirty flag: true when the current report has unsaved field changes (P6). */ dirty: false, /* Search/filter state for the report list (F4). */ reportSearchQuery: '', reportFilterStatus: '' }; /* Cached DOM element references, populated once during init. */ export const elements = {}; /* ── State accessors ────────────────────────────────────────────────────── */ export function getCurrentReport() { return state.reports.find((item) => item.id === state.currentReportId) || null; } export function getTemplateRecord(code, version) { if (!code || version == null) { return null; } const { makeTemplateKey } = stateHelpers; return state.templateDefinitions.get(makeTemplateKey(code, version)) || null; } /* * Externalizing makeTemplateKey as a helper avoids a circular import — utils.js * cannot import state.js. Instead state.js imports nothing from utils; callers * that need both can reference stateHelpers.makeTemplateKey. */ const stateHelpers = { makeTemplateKey(code, version) { return `${code}::${version}`; } }; export { stateHelpers };