<script lang="ts">
	import type { Snippet } from "svelte";
	import type { HTMLInputAttributes } from "svelte/elements";
	import Check from "$icon/check";
	import Minus from "$icon/minus";

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

	let {
		checked = $bindable(false),
		indeterminate = $bindable(false),
		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="checkbox"
			bind:checked
			bind:indeterminate
			{disabled}
			class="peer size-4 shrink-0 appearance-none rounded border border-border-strong bg-background-default transition-colors duration-150 hover:bg-background-subtle checked:border-foreground-default checked:bg-foreground-default indeterminate:border-foreground-default indeterminate:bg-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}
		/>
		<Check
			class="pointer-events-none absolute inset-0 m-auto hidden size-3.5 text-background-default peer-checked:block peer-indeterminate:hidden peer-disabled:opacity-50"
			aria-hidden="true"
		/>
		<Minus
			class="pointer-events-none absolute inset-0 m-auto hidden size-3.5 text-background-default peer-indeterminate:block peer-disabled:opacity-50"
			aria-hidden="true"
		/>
	</span>
	{#if children}
		<span>{@render children()}</span>
	{/if}
</label>
