/**
 * Custom theme persistence wiring (docs/architecture/01 §5; scope doc §4.4).
 *
 * A startup subscriber that write-throughs the `themes` 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. Themes
 * change on explicit save/delete, so there is no debounce.
 */

import { deleteCustomTheme, saveCustomTheme } from '../infrastructure/theme-store';
import { entityStorageErrorNotification } from '../services/storage-errors';
import { notify } from '../stores/NotificationStore';
import { useCustomThemeStore } from '../stores/CustomThemeStore';
import { wireEntityWriteThrough } from './entity-persistence';

type Unsubscribe = () => void;

/** Persist theme upserts and deletions whenever the array changes. */
export function wireThemePersistence(): Unsubscribe {
  return wireEntityWriteThrough(useCustomThemeStore, (s) => s.themes, {
    save: saveCustomTheme,
    remove: deleteCustomTheme,
    onError: (op, err) => notify(entityStorageErrorNotification('theme', op, err)),
  });
}
