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

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

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

  try {
    return {
      mixes: hasModuleAccess(session, 'mix_master') || session?.role === 'internal' ? await api.mixes(fetch) : []
    };
  } catch {
    return {
      mixes: []
    };
  }
}
