<script module lang="ts">
	import type { Component } from "svelte";
	import type { SvelteHTMLElements } from "svelte/elements";

	export interface TabDef {
		id: string;
		label: string;
		href: string;
		icon?: Component<SvelteHTMLElements["svg"]>;
		count?: number;
	}
</script>

<script lang="ts">
	import { resolve } from "$app/paths";

	interface Props {
		tabs: TabDef[];
		active: string;
		label?: string;
	}

	let { tabs, active, label }: Props = $props();
</script>

<nav class="flex w-full overflow-x-auto overflow-y-hidden pl-4" aria-label={label}>
	{#each tabs as tab (tab.id)}
		{@const isActive = active === tab.id}
		{@const Glyph = tab.icon}
		<a
			href={tab.href.startsWith("/") ? resolve(tab.href as "/") : tab.href}
			aria-current={isActive ? "page" : undefined}
			class={`relative mr-1 flex items-center rounded-t px-4 py-1 whitespace-nowrap text-foreground-default no-underline hover:no-underline ${
				isActive
					? "-mb-px bg-background-default [-webkit-text-stroke:0.3px_currentColor]"
					: "hover:bg-background-inset"
			}`}
		>
			{#if Glyph}
				<Glyph class="mr-2 h-4 w-4" aria-hidden="true" />
			{/if}
			<span class="flex flex-col">
				{tab.label}
				<span aria-hidden="true" class="invisible h-0 select-none overflow-hidden font-medium"
					>{tab.label}</span
				>
			</span>
			{#if tab.count}
				<span class="ml-1 rounded bg-background-inset px-1 text-sm">{tab.count}</span>
			{/if}
		</a>
	{/each}
</nav>
