/** @type {import('./$types').PageLoad} */
import { error } from '@sveltejs/kit';
console.log(`ae events launcher [event_location_id] +page.ts: start`);

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 = 1;

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

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

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

	if (browser) {
		if (log_lvl) {
			console.log(
				`ae_events launcher [event_location_id] +page.ts: event_location_id = `,
				event_location_id
			);
		}
		// Load event_session object
		// let load_event_session_obj_li = await events_func.load_ae_obj_li__event_session({
		const load_event_session_obj_li = events_func
			.load_ae_obj_li__event_session({
				api_cfg: ae_acct.api,
				for_obj_type: 'event_location',
				for_obj_id: event_location_id,
				inc_file_li: true, // Only include files directly under the session?
				inc_all_file_li: false, // Also include files under presentations and presenters as well?
				inc_presentation_li: true,
				inc_presenter_li: true,
				enabled: 'enabled',
				hidden: 'all',
				limit: 150,
				log_lvl: log_lvl
			})
			.finally(() => {
				// console.log(`load_event_session_obj_li = `, load_event_session_obj_li);
				// ae_acct.trig.event_session_obj_li = false;
			});
		ae_acct.slct.event_session_obj_li = load_event_session_obj_li;
		// console.log(`ae_acct.slct.event_session_obj_li = `, ae_acct.slct.event_session_obj_li);

		// let id_li__event_session = [];

		// let tmp_li = []; // This is to prevent the array from constantly updating and triggering the liveQuery.

		// for (let i = 0; i < load_event_session_obj_li.length; i++) {
		//     let event_session_obj = load_event_session_obj_li[i];
		//     let event_session_id_random = event_session_obj.event_session_id_random;
		//     tmp_li.push(event_session_id_random);
		// }
		// id_li__event_session = tmp_li;
		// console.log(`id_li__event_session:`, id_li__event_session);
		// ae_acct.slct.id_li__event_session = id_li__event_session;

		// This should only be needed if the session ID passed is not part of the location sessions.
		// let event_session_id = url.searchParams.get('session_id');
		// if (event_session_id) {
		//     let load_event_session_obj = await 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,
		//         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;
		// }
	} else {
		console.log(`ae pres_mgmt launcher [slug] +page.ts: browser = false`);
	}

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

	return data;
}
