<script lang="ts">
	import { boxWith, mergeProps } from "svelte-toolbelt";
	import type { AlertDialogActionProps } from "../types.js";
	import { DialogActionState } from "../../dialog/dialog.svelte.js";
	import { createId } from "../../../internal/create-id.js";

	const uid = $props.id();

	let {
		children,
		child,
		id = createId(uid),
		ref = $bindable(null),
		...restProps
	}: AlertDialogActionProps = $props();

	const actionState = DialogActionState.create({
		id: boxWith(() => id),
		ref: boxWith(
			() => ref,
			(v) => (ref = v)
		),
	});

	const mergedProps = $derived(mergeProps(restProps, actionState.props));
</script>

{#if child}
	{@render child({ props: mergedProps })}
{:else}
	<button {...mergedProps}>
		{@render children?.()}
	</button>
{/if}
