/** @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_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;
	// if (!event_presenter_id) {
	//     console.log(`ae events_pres_mgmt presenter [presenter_id] +page.ts: The event_presenter_id was not found in the params.presenter_id!!!`);
	//     return false;
	// }

	ae_acct.slct.event_presenter_id = event_presenter_id;

	if (browser) {
		// Load event presenter object
		const load_event_presenter_obj = 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
		});

		ae_acct.slct.event_presenter_obj = await load_event_presenter_obj;

		// Load event files for the presenter
		const load_event_file_obj_li = await 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: 50,
				try_cache: true
			})
			.then((event_file_obj_li) => {
				if (log_lvl) {
					console.log(`event_file_obj_li = `, event_file_obj_li);
				}
				return event_file_obj_li;
			});
		if (log_lvl) {
			console.log(`load_event_file_obj_li = `, load_event_file_obj_li);
		}
		ae_acct.slct.event_file_obj_li = load_event_file_obj_li;
	}

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

	return data;
}
