import { getStoredClientSession, hasModuleAccess, hasStoredClientSession } from '$lib/session';
import { api } from '$lib/api';

export async function load({ fetch }) {
  if (!hasStoredClientSession()) {
    return {
      products: [],
      productCosts: []
    };
  }

  const session = getStoredClientSession();

  try {
    const [products, productCosts] = await Promise.all([
      hasModuleAccess(session, 'products') ? api.products(fetch) : Promise.resolve([]),
      hasModuleAccess(session, 'products') ? api.productCosts(fetch) : Promise.resolve([])
    ]);
    return {
      products,
      productCosts
    };
  } catch {
    return {
      products: [],
      productCosts: []
    };
  }
}
