/** @type {import('./$types').LayoutLoad} */
console.log(`Events - [event_id] launcher +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, url }) {
	// route
	const log_lvl: number = 0;

	const data = await parent();
	// console.log(`ae events_pres_mgmt event [event_id] +page.ts data:`, data);
	data.log_lvl = log_lvl;

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

	const event_id = params.event_id;
	if (!event_id) {
		console.log(
			`ae Events - [event_id] +page.ts: The event_id was not found in the params.event_id!!!`
		);
		error(404, {
			message: 'Events Pres Mgmt - Event ID not found'
		});
	}

	ae_acct.slct.event_id = event_id;

	if (browser) {
		// console.log(`TEST URL Params`, params);
		// console.log(`TEST URL`, url);

		const load_event_obj = await events_func.load_ae_obj_id__event({
			api_cfg: ae_acct.api,
			event_id: event_id,
			// inc_file_li: true,
			// inc_device_li: true,
			inc_location_li: true,
			inc_session_li: true,
			// inc_badge_li: true,
			inc_template_li: true,
			log_lvl: log_lvl
			// })
			// .then((results) => {
			//     if (!results) {
			//         error(404, {
			//             message: 'Events - Event not found'
			//         });
			//     } else {
			//         // ae_acct.slct.event_obj = results;
			//     }
		});
		if (!load_event_obj) {
			error(404, {
				message: 'Events - Event not found'
			});
		}
		console.log(`load_event_obj = `, load_event_obj);
		ae_acct.slct.event_obj = load_event_obj;
		// ae_acct.slct.event_device_obj_li = load_event_obj.event_device_obj_li;
		ae_acct.slct.event_location_obj_li = load_event_obj.event_location_obj_li;
		ae_acct.slct.event_session_obj_li = load_event_obj.event_session_obj_li;
	}

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

	return data;
}
