/**
 * User font persistence wiring (docs/architecture/01 §5; scope doc §4 → fonts).
 *
 * A startup subscriber that write-throughs the `fonts` array to the IndexedDB
 * adapter via the shared `wireEntityWriteThrough` diff loop. The store stays
 * browser-free; failures surface as a toast rather than silent loss. Fonts change
 * on explicit add/delete, so there is no debounce.
 */

import { deleteFont, saveFont } from '../infrastructure/font-store';
import { entityStorageErrorNotification } from '../services/storage-errors';
import { notify } from '../stores/NotificationStore';
import { useFontStore } from '../stores/FontStore';
import { wireEntityWriteThrough } from './entity-persistence';

type Unsubscribe = () => void;

/** Persist font upserts and deletions whenever the array changes. */
export function wireFontPersistence(): Unsubscribe {
  return wireEntityWriteThrough(useFontStore, (s) => s.fonts, {
    save: saveFont,
    remove: deleteFont,
    onError: (op, err) => notify(entityStorageErrorNotification('font', op, err)),
  });
}
