/** @type {import('./$types').PageLoad} */

import { error } from '@sveltejs/kit';
console.log(`ae_events_pres_mgmt_event [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 }) {
	// route
	const log_lvl: number = 0;

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

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

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

	if (browser) {
		// Load event location object
		const load_event_location_obj = events_func.load_ae_obj_id__event_location({
			api_cfg: ae_acct.api,
			event_location_id: event_location_id,
			try_cache: true
		});

		ae_acct.slct.event_location_obj = load_event_location_obj;

		// Load event sessions for the location
		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,
				enabled: 'all',
				limit: 50,
				log_lvl: log_lvl
			})
			.then((event_session_obj_li) => {
				if (log_lvl) {
					console.log(`event_session_obj_li = `, event_session_obj_li);
				}
				for (let index = 0; index < event_session_obj_li.length; index++) {
					const event_session_obj = event_session_obj_li[index];
					const event_session_id = event_session_obj.event_session_id_random;

					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,
						enabled: 'all',
						limit: 25,
						log_lvl: log_lvl
					});
					if (log_lvl) {
						console.log(`load_event_presentation_obj_li = `, load_event_presentation_obj_li);
					}
					event_session_obj_li[index].event_presentation_li = load_event_presentation_obj_li;
				}

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

		const load_event_file_obj_li = await events_func
			.load_ae_obj_li__event_file({
				api_cfg: ae_acct.api,
				for_obj_type: 'event_location',
				for_obj_id: event_location_id,
				enabled: 'all',
				hidden: 'all',
				limit: 50,
				log_lvl: log_lvl
			})
			.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;

		const load_event_device_obj_li = events_func.load_ae_obj_li__event_device({
			api_cfg: ae_acct.api,
			for_obj_type: 'event_location',
			for_obj_id: event_location_id,
			log_lvl: log_lvl
		});
		ae_acct.slct.event_device_obj_li = load_event_device_obj_li;
	}

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

	return data;
}
