<script lang="ts">
import { untrack } from 'svelte';
interface Props {
    /** @type {import('./$types').LayoutData} */
    data: any;
    children?: import('svelte').Snippet;
}

let { data, children }: Props = $props();

let log_lvl: number = 0;

import {
    ae_snip,
    ae_loc,
    ae_sess,
    ae_api,
    ae_trig,
    slct,
    slct_trigger
} from '$lib/stores/ae_stores';
import {
    events_sess,
    events_slct,
    events_trigger
} from '$lib/stores/ae_events_stores';

// Quickly save the data passed from the parent(s) to the Svelte stores, localStorage, and other.
// $slct.account_id = data.account_id;
// NOTE: Derived from data.account_id (prop) instead of $slct.account_id (store)
// to prevent circular dependency loops during hydration.
let ae_acct = $derived(data[data.account_id]);

$effect(() => {
    if (log_lvl) {
        console.log(`ae_acct = `, ae_acct);
    }

    if (log_lvl > 1) {
        console.log(`[event_id] +layout A: ${data.params.event_id}`);
        if (ae_acct)
            console.log(`[event_id] +layout B: ${ae_acct.slct.event_id}`);
        console.log(`[event_id] +layout C: ${$events_slct.event_id}`);
    }
});

$effect(() => {
    if (ae_acct) {
        untrack(() => {
            if (
                JSON.stringify($events_slct.event_session_obj_li) !==
                JSON.stringify(ae_acct.slct.event_session_obj_li)
            ) {
                $events_slct.event_session_obj_li =
                    ae_acct.slct.event_session_obj_li;
            }
            if (
                JSON.stringify($events_slct.event_location_obj_li) !==
                JSON.stringify(ae_acct.slct.event_location_obj_li)
            ) {
                $events_slct.event_location_obj_li =
                    ae_acct.slct.event_location_obj_li;
            }
        });
    }
});
</script>

{@render children?.()}
