import type {LayoutServerLoad} from './$types';
import {defaultLocale, locales} from "$lib/translations";

export const load: LayoutServerLoad = async ({ request }) => {
  const supportedLocales = locales.get().map((l) => l.toLowerCase());

  let locale = `${`${request.headers.get('accept-language')}`.match(/[a-zA-Z]+?(?=[-_,;])/)}`.toLowerCase();

  if (!supportedLocales.includes(locale)) {
    locale = defaultLocale;
  }

  return {
    i18n: { locale },
  };
}
