<script lang="ts">
	import { trapFocus } from '$lib/Actions';
	import { getReaderContext } from '$lib/Reader.svelte';
	import { fadeDefault, slideXDefault } from '$lib/Transitions';
	import type { Snippet } from 'svelte';
	import { fade, slide } from 'svelte/transition';
	import CloseReaderButton from './components/CloseReaderButton.svelte';
	import ReaderMenuButton from './components/ReaderMenuButton.svelte';
	import ToggleFullscreenButton from './components/ToggleFullscreenButton.svelte';

	let { sidebar, children }: { sidebar?: Snippet; children?: Snippet } = $props();

	const reader = getReaderContext();

	let dialog: HTMLDivElement | undefined = $state();
</script>

{#if reader.visible}
	<div
		role="dialog"
		class="fixed top-0 right-0 bottom-0 left-0 z-10 flex h-full w-full bg-black"
		transition:fade={fadeDefault}
		use:trapFocus
		bind:this={dialog}
	>
		{#if sidebar && reader.sidebar}
			<aside
				class="z-10 w-[36rem] shrink-0 bg-slate-800 shadow-md shadow-slate-800"
				transition:slide={slideXDefault}
			>
				<div class="flex h-full min-w-[36rem] flex-col gap-4 overflow-auto p-4">
					{@render sidebar?.()}
				</div>
			</aside>
		{/if}
		<main class="relative flex grow">
			<div class="absolute flex w-full p-1 text-lg">
				<div class="flex flex-col gap-1">
					{#if sidebar}
						<ReaderMenuButton />
					{/if}
				</div>
				<div class="ml-auto flex flex-col gap-1">
					<CloseReaderButton />
					<ToggleFullscreenButton {dialog} />
				</div>
			</div>

			{@render children?.()}
		</main>
	</div>
{/if}
