/** @type {import('./$types').LayoutLoad} */
// console.log(`IDAA Recovery Meetings - [account_id] +layout.ts start`);

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

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

    const data = await parent();
    data.log_lvl = log_lvl;

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

    if (!ae_acct) {
        console.warn(
            `ae IDAA Recovery Meetings +layout.ts: Account ${account_id} not found in data. Initializing ghost acct.`
        );
        ae_acct = {
            api: data.ae_api || {},
            slct: {
                account_id: account_id
            }
        };
    }

    if (browser) {
        const load_event_obj_li = events_func.load_ae_obj_li__event({
            api_cfg: ae_acct.api,
            for_obj_id: account_id,
            qry_conference: false, // IDAA Recovery Meetings are not standard conferences
            enabled: 'enabled',
            hidden: 'not_hidden',
            limit: 499,
            order_by_li: {
                priority: 'DESC',
                sort: 'DESC',
                updated_on: 'DESC',
                created_on: 'DESC',
                name: 'ASC'
            },
            try_cache: true,
            log_lvl: log_lvl
        });
        if (log_lvl) {
            console.log(`load_event_obj_li = `, load_event_obj_li);
        }
        ae_acct.slct.event_obj_li = load_event_obj_li;
    }

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

    return data;
}
