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

console.log(`ae_idaa_bulletin_board [post_id] +page.ts start`);

import { error } from '@sveltejs/kit';
import { browser } from '$app/environment';
import { posts_func } from '$lib/ae_posts/ae_posts_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 post_id = params.post_id;

	ae_acct.slct.post_id = post_id;

	if (browser) {
		if (log_lvl) {
			console.log(`ae_idaa_posts posts [post_id] +page.ts: post_id = `, post_id);
		}
		// Load post object
		const load_post_obj = await posts_func
			.load_ae_obj_id__post({
				api_cfg: ae_acct.api,
				post_id: post_id,
				inc_comment_li: true,
				log_lvl: log_lvl
			})
			.then((results) => {
				if (!results) {
					error(404, {
						message: 'IDAA BB - Post not found'
					});
				} else {
					// ae_acct.slct.post_obj = results;
				}
			});

		if (log_lvl) {
			console.log(`load_post_obj = `, load_post_obj);
		}
		ae_acct.slct.post_obj = load_post_obj;
	}

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

	return data;
}) satisfies PageLoad;
