<script lang="ts" module>
	import type { FormPathLeaves as _FormPathLeaves } from "sveltekit-superforms"

	type T = Record<string, unknown>
	type U = _FormPathLeaves<T>
</script>

<script lang="ts" generics="T extends Record<string, unknown>, U extends _FormPathLeaves<T>">
	import { cn } from "@kksh/ui/utils"
	import type { WithElementRef } from "bits-ui"
	import * as FormPrimitive from "formsnap"
	import type { HTMLAttributes } from "svelte/elements"

	let {
		ref = $bindable(null),
		class: className,
		form,
		name,
		children: childrenProp,
		...restProps
	}: WithElementRef<HTMLAttributes<HTMLDivElement>> &
		FormPrimitive.ElementFieldProps<T, U> = $props()
</script>

<FormPrimitive.ElementField {form} {name}>
	{#snippet children({ constraints, errors, tainted, value })}
		<div bind:this={ref} class={cn("space-y-2", className)} {...restProps}>
			{@render childrenProp?.({ constraints, errors, tainted, value: value as T[U] })}
		</div>
	{/snippet}
</FormPrimitive.ElementField>
