import { redirect } from '@sveltejs/kit';
import { api } from '$lib/api';
import { getStoredClientSession, hasStoredClientSession } from '$lib/session';
import { canEditThroughput, getWorkspaceHomeHref } from '$lib/workspace-access';

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

  const session = getStoredClientSession();
  if (!canEditThroughput(session)) {
    throw redirect(307, getWorkspaceHomeHref(session));
  }

  try {
    const products = await api.throughputProducts(fetch);
    return { products };
  } catch {
    return { products: [] };
  }
}
