<script lang="ts">
import { untrack } from 'svelte';
let log_lvl: number = $state(0);

// *** Import Svelte specific
import { page } from '$app/state';

// *** Import Aether specific variables and functions
import {
    ae_snip,
    ae_loc,
    ae_sess,
    ae_api,
    ae_trig,
    slct,
    slct_trigger
} from '$lib/stores/ae_stores';
import {
    idaa_loc,
    idaa_sess,
    idaa_slct,
    idaa_trig,
    idaa_prom
} from '$lib/stores/ae_idaa_stores';
import { posts_func } from '$lib/ae_posts/ae_posts_functions';

interface Props {
    /** @type {import('./$types').LayoutData} */
    data: any;
    children?: import('svelte').Snippet;
}

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

// 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 > 1) {
        console.log(`ae_idaa_bulletin_board +layout.svelte`, data);
    }

    // *** Quickly pull out data from parent(s)
    $slct.account_id = data.account_id;
});

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

    if (ae_acct) {
        untrack(() => {
            $idaa_slct.post_obj_li = ae_acct.slct.post_obj_li;
        });
    }
});
// $idaa_slct.post_id = ae_acct.slct.post_id; // Not set here yet.

// *** Set initial variables

$effect(() => {
    if ($idaa_trig.post_id) {
        // log_lvl = 1;
        $idaa_trig.post_id = false;

        if (log_lvl) {
            console.log(`Triggered: $idaa_trig.post_id`);
        }

        $idaa_prom.load__post_obj = posts_func
            .load_ae_obj_id__post({
                api_cfg: $ae_api,
                post_id: $idaa_slct.post_id,
                enabled: $idaa_loc.bb.qry__enabled,
                hidden: $idaa_loc.bb.qry__hidden,
                limit: $idaa_loc.bb.qry__limit,
                inc_comment_li: true,
                // try_cache: true,
                log_lvl: log_lvl
            })
            .then((post_obj) => {
                if (log_lvl) {
                    console.log(`Post object loaded: `, post_obj);
                }

                $idaa_slct.post_obj = post_obj;
                $idaa_slct.post_comment_id = null;
                $idaa_slct.post_comment_obj = null;

                // $idaa_sess.bb.show__modal_view__post_id = $idaa_slct.post_id;
                // $idaa_sess.bb.show__modal_edit__post_id = false;
            });

        // $idaa_slct.post_obj = $idaa_prom.load__post_obj;

        if (!page.url.searchParams.get('post_id')) {
            const url = new URL(location.href);
            // url.searchParams.set('post_id', $idaa_slct.post_id);
            url.searchParams.delete('post_id');
            history.pushState({}, '', url);

            let message = { post_id: $idaa_slct.post_id };
            window.parent.postMessage(message, '*');
        }

        // $idaa_sess.bb.show__modal_view__post_id = $idaa_slct.post_id;
        // $idaa_sess.bb.show__modal_edit__post_id = false;
    }
});
</script>

{@render children?.()}
