/** @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;

	const ae_acct = parent_data[account_id];

	const journal_id = ae_acct.slct.journal_id; // From root +layout.ts
	if (!journal_id) {
		if (log_lvl) {
			console.log(`INFO: journals +layout.ts: The journal_id was not found in the parent_data.`);
		}
		// return false;
	}

	if (browser) {
		if (journal_id) {
			// let ae_params = {};

			const load_journal_obj_li = journals_func.load_ae_obj_id__journal({
				api_cfg: ae_acct.api,
				journal_id: journal_id,
				hidden: 'all', // 'not_hidden' to load only visible entries
				// params: ae_params,
				try_cache: true,
				log_lvl: log_lvl
			});
			ae_acct.slct.journal_obj_li = load_journal_obj_li;
		}

		// console.log(`ae_acct = `, ae_acct);
		// WARNING: This does not currently work because the person_id has not been set yet.
		const person_id = ae_acct.loc.person_id;
		// console.log(`person_id = `, person_id);

		const load_journal_obj_li = await 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',
			// order_by_li: {'priority': 'DESC', 'sort': 'DESC', 'name': 'ASC', 'updated_on': 'DESC', 'created_on': 'DESC'},
			limit: 25,
			// params: ae_params,
			try_cache: true,
			log_lvl: log_lvl
		});
		ae_acct.slct.journal_obj_li = load_journal_obj_li;
	}

	parent_data[account_id] = ae_acct;

	return parent_data;
}
