/** @type {import('./$types').PageLoad} */
import { error } from '@sveltejs/kit';
console.log(`ae_p_event_session [session_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 session [session_id] +page.ts data:`, data);

	const account_id = data.account_id;
	const ae_acct = data[account_id];
	// console.log(`ae_acct = `, ae_acct);

	// if (!account_id) {
	//     console.log(`ae events_pres_mgmt session [session_id] +page.ts: The account_id was not found in the data!!!`);
	//     return false;
	// }

	data.ae_events_pres_mgmt_event_session_id_page_ts = true;

	const event_session_id = params.session_id;
	if (!event_session_id) {
		console.log(
			`ae events_pres_mgmt session [session_id] +page.ts: The event_session_id was not found in the params!!!`
		);
		error(404, {
			message: 'Session not found'
		});
	}
	ae_acct.slct.event_session_id = event_session_id;

	if (browser) {
		// Load event session object
		const 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_presentation_li: true,
			// inc_presenter_li: true,
			try_cache: true
		});

		ae_acct.slct.event_session_obj = load_event_session_obj;

		// Load event presentations for the session
		const load_event_presentation_obj_li = events_func
			.load_ae_obj_li__event_presentation({
				api_cfg: ae_acct.api,
				for_obj_type: 'event_session',
				for_obj_id: event_session_id,
				inc_presenter_li: true,
				enabled: 'all',
				hidden: 'all',
				limit: 19,
				// params: {},
				try_cache: true,
				log_lvl: 2
			})
			.then((event_presentation_obj_li) => {
				if (log_lvl) {
					console.log(`event_presentation_obj_li = `, event_presentation_obj_li);
				}
				for (let index = 0; index < event_presentation_obj_li?.length; index++) {
					const event_presentation_obj = event_presentation_obj_li[index];
					const event_presentation_id = event_presentation_obj.event_presentation_id_random;

					const load_event_presenter_obj_li = events_func.load_ae_obj_li__event_presenter({
						api_cfg: ae_acct.api,
						for_obj_type: 'event_presentation',
						for_obj_id: event_presentation_id,
						enabled: 'all',
						hidden: 'all',
						limit: 19,
						params: {},
						try_cache: true,
						log_lvl: 2
					});
					if (log_lvl) {
						console.log(`load_event_presenter_obj_li = `, load_event_presenter_obj_li);
					}
					event_presentation_obj_li[index].event_presenter_li = load_event_presenter_obj_li;
				}

				return event_presentation_obj_li;
			});
		if (log_lvl) {
			console.log(`load_event_presentation_obj_li = `, load_event_presentation_obj_li);
		}
		ae_acct.slct.event_presentation_obj_li = load_event_presentation_obj_li;

		// Load event files for the session
		const load_event_file_obj_li = await events_func
			.load_ae_obj_li__event_file({
				api_cfg: ae_acct.api,
				for_obj_type: 'event_session',
				for_obj_id: event_session_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;
}
