/**
 * ae_events_stores__pres_mgmt.svelte.ts
 *
 * Svelte 5 PersistedState store for Presentation Management local config.
 * Replaces the `events_loc.pres_mgmt` sub-object from the Svelte 4 persisted store.
 *
 * localStorage key: 'ae_pres_mgmt_loc'
 * Version gate:     AE_PRES_MGMT_LOC_VERSION in store_versions.ts
 *
 * Usage:
 *   import { pres_mgmt_loc } from '$lib/stores/ae_events_stores__pres_mgmt.svelte';
 *   pres_mgmt_loc.current.hide__session_code   // read
 *   pres_mgmt_loc.current.hide__session_code = true  // write
 */
import { PersistedState } from 'runed';
import { pres_mgmt_loc_defaults } from './ae_events_stores__pres_mgmt_defaults';
import { AE_PRES_MGMT_LOC_VERSION } from './store_versions';

export const pres_mgmt_loc = new PersistedState('ae_pres_mgmt_loc', pres_mgmt_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. 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_PRES_MGMT_LOC_VERSION }),
        deserialize: (raw: string) => {
            const { __version, ...stored } = JSON.parse(raw);
            return { ...pres_mgmt_loc_defaults, ...stored };
        }
    }
});
