---
import { lowerHumanize } from "@lib/textConverter";

const { className }: { className?: string } = Astro.props;

const paths = Astro.url.pathname.split("/").filter((x) => x);
let parts = [
  {
    label: "Home",
    href: "/",
    "aria-label": Astro.url.pathname === "/" ? "page" : undefined,
  },
];

paths.forEach((label: string, i: number) => {
  const href = `/${paths.slice(0, i + 1).join("/")}`;
  label !== "page" &&
    parts.push({
      label: lowerHumanize(label.replace(/[-_]/g, " ")) || "",
      href,
      "aria-label": Astro.url.pathname === href ? "page" : undefined,
    });
});
---

<nav aria-label="Breadcrumb" class={className}>
  <ol class="inline-flex" role="list">
    {
      parts.map(({ label, ...attrs }, index) => (
        <li class="mx-1 capitalize" role="listitem">
          {index > 0 && <span class="inlin-block mr-1">/</span>}
          {index !== parts.length - 1 ? (
            <a class="text-txt-p dark:text-darkmode-txt-p" {...attrs}>
              {label}
            </a>
          ) : (
            <span class="text-txt-light dark:text-darkmode-txt-light">{label}</span>
          )}
        </li>
      ))
    }
  </ol>
</nav>
