/** @type {import('./$types').LayoutLoad} */
console.log(`Events - [event_id] launcher +layout.ts start`);

// import { error } from '@sveltejs/kit';
import { browser } from '$app/environment';
import { events_func } from '$lib/ae_events/ae_events_functions';

export async function load({ params, parent }) {
    // route
    const log_lvl: number = 0;

    const data = await parent();
    // console.log(`ae events_pres_mgmt event [event_id] +page.ts data:`, data);
    data.log_lvl = log_lvl;

    const account_id = data.account_id;
    let ae_acct = data[account_id];

    if (!ae_acct) {
        console.warn(
            `ae Events - [event_id] launcher +layout.ts: Account ${account_id} not found in data. Initializing ghost acct.`
        );
        ae_acct = {
            api: data.ae_api || {},
            slct: {
                account_id: account_id
            }
        };
    }
    // console.log(`ae_acct = `, ae_acct);

    const event_id = params.event_id;

    if (browser) {
        if (log_lvl)
            console.log(
                `ae_events Launcher - [event_id] launcher +layout.ts (Non-Blocking)`
            );

        // OPTIMIZATION: Fire these in the background without 'await'.
        // The UI components (menu, session_view) already use LiveQuery/Dexie
        // and will automatically update when these background tasks save to IDB.
        events_func.load_ae_obj_li__event_device({
            api_cfg: ae_acct.api,
            for_obj_type: 'event',
            for_obj_id: event_id,
            log_lvl: 0
        });

        events_func.load_ae_obj_li__event_file({
            api_cfg: ae_acct.api,
            for_obj_type: 'event',
            for_obj_id: event_id,
            log_lvl: 0
        });

        events_func.load_ae_obj_li__event_location({
            api_cfg: ae_acct.api,
            for_obj_type: 'event',
            for_obj_id: event_id,
            enabled: 'enabled',
            hidden: 'all',
            log_lvl: 0
        });
    }

    return data;
}
