/** @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];
	// console.log(`ae_acct = `, ae_acct);

	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: 'Event ID not found'
	// 	});
	// }

	// ae_acct.slct.event_id = event_id;

	// let load_event_obj = events_func.handle_load_ae_obj_id__event({
	//     api_cfg: ae_acct.api, event_id:  event_id, try_cache: true
	// });

	// ae_acct.slct.event_obj = await load_event_obj;

	if (browser) {
		console.log(`ae_events Launcher - [event_id] launcher +layout.ts start`);

		// Load event object again, but with the event file list
		// let load_event_obj = events_func.load_ae_obj_id__event({
		//     api_cfg: ae_acct.api,
		//     event_id:  event_id,
		//     inc_file_li: true,
		//     log_lvl: log_lvl
		// })
		// .then((results) => {
		//     if (!results) {
		//         error(404, {
		//             message: 'Events Launcher - Event not found'
		//         });
		//     } else {
		//         // ae_acct.slct.event_obj = results;
		//     }
		// });

		const load_event_device_obj_li = events_func.load_ae_obj_li__event_device({
			api_cfg: ae_acct.api,
			for_obj_type: 'event',
			for_obj_id: event_id,
			log_lvl: log_lvl
		});

		const load_event_file_obj_li = events_func.load_ae_obj_li__event_file({
			api_cfg: ae_acct.api,
			for_obj_type: 'event',
			for_obj_id: event_id,
			log_lvl: log_lvl
		});

		// let load_event_obj = events_func.load_ae_obj_id__event({
		//     api_cfg: ae_acct.api, event_id:  event_id, try_cache: true, log_lvl: 1
		// });
		// console.log(`load_event_obj = `, load_event_obj);
		// ae_acct.slct.event_obj = load_event_obj;

		// let load_event_session_obj_li = events_func.load_ae_obj_li__event_session({
		//     api_cfg: ae_acct.api,
		//     for_obj_type: 'event',
		//     for_obj_id: event_id,
		//     params: {qry__enabled: 'enabled', qry__hidden: 'all', qry__limit: 200},
		//     try_cache: true,
		//     log_lvl: 1
		// });
		// console.log(`load_event_session_obj_li = `, load_event_session_obj_li);
		// ae_acct.slct.event_session_obj_li = load_event_session_obj_li;

		// let load_event_location_obj_li = events_func.load_ae_obj_li__event_location({
		//     api_cfg: ae_acct.api,
		//     for_obj_type: 'event',
		//     for_obj_id: event_id,
		//     params: {qry__enabled: 'enabled', qry__hidden: 'all', qry__limit: 200},
		//     try_cache: true,
		//     log_lvl: log_lvl
		// });
		// console.log(`load_event_location_obj_li = `, load_event_location_obj_li);
		// ae_acct.slct.event_location_obj_li = load_event_location_obj_li;

		// let event_session_id = url.searchParams.get('session_id');
		// if (event_session_id) {
		//     let load_event_session_obj = events_func.load_ae_obj_id__event_session({
		//         api_cfg: ae_acct.api,
		//         event_session_id: event_session_id,
		//         inc_file_li: true,
		//         inc_presentation_li: true,
		//         inc_presenter_li: true,
		//         try_cache: true,
		//         log_lvl: log_lvl
		//     });
		//     ae_acct.slct.event_session_id = event_session_id;
		//     console.log(`load_event_session_obj = `, load_event_session_obj);
		//     ae_acct.slct.event_session_obj = load_event_session_obj;
		// }
	}

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

	return data;
}
