import { writable } from 'svelte/store';

/**
 * A store that tracks whether a form or modal has unsaved changes.
 * Use this with the unsavedChangesGuard composable.
 */
function createDirtyState() {
  const { subscribe, set, update } = writable(false);

  return {
    subscribe,
    markDirty: () => set(true),
    markClean: () => set(false),
    toggle: () => update(v => !v),
  };
}

export const dirtyState = createDirtyState();
