import type { PageLoad } from './$types';

console.log(`ae_p_idaa_archives [archive_id] +page.ts start`);

import { error } from '@sveltejs/kit';
import { browser } from '$app/environment';
import { archives_func } from '$lib/ae_archives/ae_archives_functions';

export const load = (async ({ params, parent }) => {
	// route
	const log_lvl: number = 0;

	const data = await parent();
	data.log_lvl = log_lvl;

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

	const archive_id = params.archive_id;

	ae_acct.slct.archive_id = archive_id;

	if (browser) {
		if (log_lvl) {
			console.log(`ae_idaa_archives archives [archive_id] +page.ts: archive_id = `, archive_id);
		}
		// Load archive object
		const load_archive_obj = await archives_func
			.load_ae_obj_id__archive({
				api_cfg: ae_acct.api,
				archive_id: archive_id,
				inc_content_li: true,
				enabled: 'enabled',
				hidden: 'all', // 'not_hidden' to load only visible entries
				limit: 99,
				log_lvl: log_lvl
			})
			.then((results) => {
				if (!results) {
					error(404, {
						message: 'IDAA Archives - Archive not found'
					});
				} else {
					// ae_acct.slct.post_obj = results;
				}
			});

		if (log_lvl) {
			console.log(`load_archive_obj = `, load_archive_obj);
		}
		ae_acct.slct.archive_obj = load_archive_obj;
	}

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

	return data;
}) satisfies PageLoad;
