/**
 * ae_events_stores__leads.svelte.ts
 *
 * Svelte 5 PersistedState store for the Exhibitor Leads module local config.
 * Replaces the `events_loc.leads` sub-object from the Svelte 4 persisted store.
 *
 * localStorage key: 'ae_leads_loc'
 * Version gate:     AE_LEADS_LOC_VERSION in store_versions.ts
 *
 * Session state (non-persisted) stays in `events_sess.leads` — same pattern as
 * pres_mgmt and badges.
 *
 * Usage:
 *   import { leads_loc } from '$lib/stores/ae_events_stores__leads.svelte';
 *   leads_loc.current.auth_exhibit_kv   // read
 *   leads_loc.current.tab[exhibit_id] = 'list'  // write
 */
import { PersistedState } from 'runed';
import { leads_loc_defaults } from './ae_events_stores__leads_defaults';
import { AE_LEADS_LOC_VERSION } from './store_versions';

export const leads_loc = new PersistedState('ae_leads_loc', leads_loc_defaults, {
    serializer: {
        // Stamp __version on every write so store_versions.ts's _check_and_wipe() can
        // detect a breaking schema change and clear stale browsers on next load. Found
        // 2026-06-16: this was previously bare JSON.stringify with no __version field,
        // which made _check_and_wipe('ae_leads_loc', ...) see undefined !== expected
        // every time and wipe this store on EVERY page load. This import also guarantees
        // store_versions.ts's wipe side-effect runs before this PersistedState reads
        // from localStorage (ES module execution order).
        serialize: (value) =>
            JSON.stringify({ ...value, __version: AE_LEADS_LOC_VERSION }),
        deserialize: (raw: string) => {
            const { __version, ...stored } = JSON.parse(raw);
            return { ...leads_loc_defaults, ...stored };
        }
    }
});
