<script lang="ts">
	import { boxWith, mergeProps } from "svelte-toolbelt";
	import { IsMounted } from "runed";
	import type { ScrollAreaThumbProps } from "../types.js";
	import { ScrollAreaThumbImplState } from "../scroll-area.svelte.js";

	let {
		ref = $bindable(null),
		id,
		child,
		children,
		present,
		...restProps
	}: Omit<ScrollAreaThumbProps, "forceMount" | "id"> & {
		id: string;
		present: boolean;
	} = $props();

	const isMounted = new IsMounted();

	const thumbState = ScrollAreaThumbImplState.create({
		id: boxWith(() => id),
		ref: boxWith(
			() => ref,
			(v) => (ref = v)
		),
		mounted: boxWith(() => isMounted.current),
	});

	const mergedProps = $derived(
		mergeProps(restProps, thumbState.props, {
			style: {
				hidden: !present,
			},
		})
	);
</script>

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