<script lang="ts">
	import type { Snippet } from "svelte";
	import type { HTMLInputAttributes } from "svelte/elements";

	interface Props extends Omit<HTMLInputAttributes, "type" | "class" | "value" | "children"> {
		value: string;
		group?: string;
		disabled?: boolean;
		class?: string;
		children?: Snippet;
	}

	let {
		value,
		group = $bindable(undefined),
		disabled = false,
		class: className,
		children,
		...rest
	}: Props = $props();

	const labelClasses = $derived(
		`inline-flex items-center gap-2 cursor-pointer ${disabled ? "cursor-not-allowed text-foreground-disabled" : ""} ${className ?? ""}`
	);
</script>

<label class={labelClasses}>
	<span class="relative inline-flex size-4 shrink-0">
		<input
			type="radio"
			{value}
			bind:group
			{disabled}
			class="peer size-4 shrink-0 appearance-none rounded-full border border-border-strong bg-background-default transition-colors duration-150 hover:bg-background-subtle checked:border-foreground-default focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-foreground-default disabled:cursor-not-allowed disabled:pointer-events-none disabled:opacity-50"
			{...rest}
		/>
		<span
			class="pointer-events-none absolute inset-0 m-auto hidden size-2 rounded-full bg-foreground-default peer-checked:block peer-disabled:opacity-50"
			aria-hidden="true"
		></span>
	</span>
	{#if children}
		<span>{@render children()}</span>
	{/if}
</label>
