import { hasStoredClientSession } from '$lib/session';
import { api } from '$lib/api';
import type { CatalogueProduct, OrderingCustomer } from '$lib/types';

// Pricing needs the customer list (to choose whose pricing to edit) and the
// product catalogue (for the price-rule product picker). Per-customer pricing
// itself is loaded client-side once a customer is selected.
export async function load({ fetch }) {
  if (!hasStoredClientSession()) {
    return { customers: [] as OrderingCustomer[], products: [] as CatalogueProduct[] };
  }

  try {
    const [customers, products] = await Promise.all([
      api.orderingAdmin.customers(fetch),
      api.orderingAdmin.products(fetch)
    ]);
    return { customers, products };
  } catch {
    return { customers: [] as OrderingCustomer[], products: [] as CatalogueProduct[] };
  }
}
