import type { LayoutServerLoad } from './$types';
import { getMemberEntitlements, getMemberLangganan, hasPrivilege } from '$lib/server/langganan';
import { pakaiStokInventori } from '$lib/server/warung';

export const load: LayoutServerLoad = async ({ locals }) => {
	if (!locals.member) {
		return {
			pakaiStok: false,
			pakaiHutangPiutang: false,
			pakaiOpname: false,
			pakaiLaporanPdf: false,
			pakaiWarungPublik: false,
			entitlements: null,
			langganan: null
		};
	}

	const [pakaiStok, entitlements, langganan] = await Promise.all([
		pakaiStokInventori(locals.member.id),
		getMemberEntitlements(locals.member.id),
		getMemberLangganan(locals.member.id)
	]);

	return {
		pakaiStok,
		pakaiHutangPiutang: hasPrivilege(entitlements, 'hutang_piutang'),
		pakaiOpname: hasPrivilege(entitlements, 'stok_opname'),
		pakaiLaporanPdf: hasPrivilege(entitlements, 'laporan_pdf'),
		pakaiWarungPublik: hasPrivilege(entitlements, 'warung_publik'),
		entitlements,
		langganan
	};
};
