/** @type {import('./$types').PageLoad} */
console.log(`ae_p_event_presenter [presenter_id] +page.ts: start`);

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 presenter [presenter_id] +page.ts data:`, data);
    data.log_lvl = log_lvl;

    const account_id = data.account_id;
    const ae_acct = data[account_id];
    // console.log(`ae_acct = `, ae_acct);

    const event_presenter_id = params.presenter_id;
    ae_acct.slct.event_presenter_id = event_presenter_id;

    if (browser) {
        if (log_lvl)
            console.log(
                `ae_events [presenter_id] +page.ts (Non-Blocking Refresh)`
            );

        // OPTIMIZATION: Fire these in the background without 'await'.
        // The Presenter View components use LiveQuery/Dexie and will update
        // automatically once these background tasks complete.

        // 1. Refresh the presenter object
        events_func.load_ae_obj_id__event_presenter({
            api_cfg: ae_acct.api,
            event_presenter_id: event_presenter_id,
            try_cache: true,
            log_lvl: log_lvl
        });

        // 2. Refresh files for the presenter
        events_func.load_ae_obj_li__event_file({
            api_cfg: ae_acct.api,
            for_obj_type: 'event_presenter',
            for_obj_id: event_presenter_id,
            enabled: 'all',
            hidden: 'all',
            limit: 150,
            try_cache: true,
            log_lvl: 0
        });
    }

    // WARNING: Precaution against shared data between sites and presentations.
    data[account_id] = ae_acct;

    return data;
}
