import { redirect } from '@sveltejs/kit';
import { api } from '$lib/api';
import { featureFlags } from '$lib/features';
import { getStoredClientSession, hasModuleAccess, hasStoredClientSession } from '$lib/session';

export async function load({ fetch }) {
  if (!featureFlags.mixCalculatorSessionHistory) {
    throw redirect(307, '/mix-calculator/new');
  }

  if (!hasStoredClientSession()) {
    return {
      sessions: []
    };
  }

  const session = getStoredClientSession();

  try {
    return {
      sessions: hasModuleAccess(session, 'mix_calculator') ? await api.mixCalculatorSessions(fetch) : []
    };
  } catch {
    return {
      sessions: []
    };
  }
}
