<script module lang="ts">
	import { tv, type VariantProps } from "tailwind-variants";

	export const tag = tv({
		base: "inline-flex items-center gap-1 rounded-sm leading-none font-medium select-none",
		variants: {
			color: {
				default: "border border-border-default bg-background-default text-foreground-default",
				gray: "border border-transparent bg-background-inset text-foreground-default",
				success:
					"border border-transparent bg-background-success-subtle text-foreground-success-strong",
				danger:
					"border border-transparent bg-background-danger-subtle text-foreground-danger-strong",
				warning: "border border-transparent bg-background-warning-subtle text-foreground-warning",
				info: "border border-transparent bg-background-info-subtle text-foreground-info-strong"
			},
			size: {
				sm: "px-1 typography-paragraph-mini",
				md: "px-1.5 py-0.5 typography-paragraph-small",
				lg: "px-2 py-1 typography-paragraph-regular font-normal"
			}
		},
		defaultVariants: {
			color: "default",
			size: "md"
		}
	});

	export type TagVariants = VariantProps<typeof tag>;
</script>

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

	interface Props extends Omit<HTMLAttributes<HTMLSpanElement>, "class" | "children"> {
		color?: TagVariants["color"];
		size?: TagVariants["size"];
		/** Optional leading icon component (e.g. `import Hash from "$icon/hash"`). */
		icon?: Component<SvelteHTMLElements["svg"]>;
		class?: string;
		children?: Snippet;
	}

	let {
		color = "default",
		size = "md",
		icon,
		class: className,
		children,
		...rest
	}: Props = $props();

	const classes = $derived(tag({ color, size, class: className }));
	const Icon = $derived(icon);
</script>

<span class={classes} {...rest}>
	{#if Icon}
		<Icon class="size-3 mt-px" aria-hidden="true" />
	{/if}
	{#if children}
		{@render children()}
	{/if}
</span>
