import { optima } from "$lib";
import { handleApiError } from "$lib/optima-api/errorHandler";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async ({ locals }) => {
  const accessToken = locals.session?.accessToken;
  if (!accessToken) {
    return { companyCount: null };
  }

  try {
    const companyCount = await optima.company.count(accessToken);

    return {
      companyCount: companyCount ?? null,
    };
  } catch (err) {
    handleApiError(err);
  }
};
