// Global application state with simple pub/sub const _listeners = {}; export const state = { project: null, // { id, name, createdAt } auth: { adobe: false, docusign: false, adobeLabel: 'Adobe Sign', docusignLabel: 'DocuSign', }, templates: [], // [{ adobe_id, name, status, blockers, warnings, ... }] selectedIds: new Set(), lastMigrationResults: null, // final batch job results issueCount: 0, // blocked template count (drives nav badge) }; // Subscribe to state key changes: fn is called with (newValue, oldValue) export function subscribe(key, fn) { if (!_listeners[key]) _listeners[key] = []; _listeners[key].push(fn); } // Publish a state change export function publish(key, newValue) { const old = state[key]; state[key] = newValue; (_listeners[key] || []).forEach(fn => { try { fn(newValue, old); } catch (e) { console.error('state listener error', e); } }); } // Convenience setter that publishes export function setState(key, value) { publish(key, value); } // Recompute derived values after template list updates export function updateDerivedState() { const blocked = state.templates.filter(t => t.blockers && t.blockers.length > 0).length; setState('issueCount', blocked); }