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

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

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

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