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

// Single access guard for the whole Order Management family. Each child route
// (orders, products, customers, pricing, settings, integrations) loads its own
// data; this layout just keeps non-managers out of all of them in one place.
export async function load() {
  if (!hasStoredClientSession()) {
    return {};
  }

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

  return {};
}
