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

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

  const session = getStoredClientSession();

  try {
    const [rawMaterials, mixes, products, productCosts] = await Promise.all([
      hasModuleAccess(session, 'raw_materials') ? api.rawMaterials(fetch) : Promise.resolve([]),
      hasModuleAccess(session, 'mix_master') ? api.mixes(fetch) : Promise.resolve([]),
      hasModuleAccess(session, 'products') ? api.products(fetch) : Promise.resolve([]),
      hasModuleAccess(session, 'products') ? api.productCosts(fetch) : Promise.resolve([])
    ]);

    return {
      rawMaterials,
      mixes,
      products,
      productCosts
    };
  } catch {
    return {
      rawMaterials: [],
      mixes: [],
      products: [],
      productCosts: []
    };
  }
}
