/** @type {import('./$types').PageLoad} */

// import { error } from '@sveltejs/kit';
import { browser } from '$app/environment';
import { journals_func } from '$lib/ae_journals/ae_journals_functions';

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

    const parent_data = await parent();

    const account_id = parent_data.account_id;

    let ae_acct = parent_data[account_id];

    if (!ae_acct) {
        console.warn(
            `ae Journals +page.ts: Account ${account_id} not found in parent data. Initializing ghost acct.`
        );
        ae_acct = {
            api: parent_data.ae_api || {},
            loc: {},
            slct: {
                account_id: account_id
            }
        };
    }

    if (browser) {
        if (log_lvl) console.log(`ae_journals +page.ts (Non-Blocking)`);

        const person_id = ae_acct.loc.person_id;

        // OPTIMIZATION: Fire the journal list load in the background.
        // Components using LiveQuery (db_journals) will display cached data
        // instantly while this refresh runs.
        journals_func.load_ae_obj_li__journal({
            api_cfg: ae_acct.api,
            for_obj_type: 'account',
            for_obj_id: account_id,
            qry_person_id: person_id,
            inc_entry_li: true,
            hidden: 'all', // 'not_hidden'
            enabled: 'enabled',
            limit: 25,
            try_cache: true,
            log_lvl: 0
        });
    }

    parent_data[account_id] = ae_acct;

    return parent_data;
}
