---
import { locales, defaultLocale } from "astro-i18n-aut";
import { useTranslations } from "../i18n/utils";

function translatePath(lang: string) {
  const split = Astro.url.pathname.split("/").filter((s) => s.length);

  if (split[0] in locales) {
    split.shift();
  }

  if (lang === defaultLocale) {
    return `/${split.join("/")}`;
  }

  return `/${[lang, ...split].join("/")}`;
}

const t = useTranslations(Astro);
---

<ul>
  {
    Object.entries(locales).map(([lang, label]) => (
      <li>
        <a href={translatePath(lang)} data-astro-prefetch>
          {t(label as "de")}
        </a>
      </li>
    ))
  }
</ul>
