// Module-level store for in-flight transfer state.
//
// This state must OUTLIVE the component tree: useFileControl unmounts when
// the user switches top-level workspace pages (Organization, Time Tracking),
// but the Rust transfer engine keeps running. When this lived in component
// state, a remount reset busy/transfer to idle — the toolbar re-armed and the
// progress strip vanished mid-download, and a second pull started then could
// race the first over the same temp files. The store keeps every mounted
// consumer honest about what the engine is actually doing.
//
// Shape: {
//   transfer: { kind: 'pull'|'upload', path, done, total } | null,
//   pullingPaths: Set<string> | null,   // paths the active pull is landing
// }

let state = { transfer: null, pullingPaths: null };
const listeners = new Set();

export function getTransferState() {
  return state;
}

export function setTransferState(patch) {
  state = { ...state, ...patch };
  listeners.forEach((listener) => listener());
}

export function subscribeTransferState(listener) {
  listeners.add(listener);
  return () => listeners.delete(listener);
}
