<script lang="ts">
    import type { FileRecord } from '$lib/types';
    import { fade } from 'svelte/transition';
    import VideoPreview from './VideoPreview.svelte';

    let { file, onclose }: { file: FileRecord; onclose: () => void } = $props();
    const isVideo = $derived(file.fileType.includes('video'));
</script>

<svelte:window
    onkeydown={(e) => {
        if (e.key === 'Escape') onclose();
    }}
/>

<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
    class="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4"
    onclick={onclose}
    transition:fade={{ duration: 150 }}
>
    <div
        class="relative aspect-video h-[85vh] max-w-[95vw] overflow-hidden rounded-xl border border-sky-200/20 bg-[#0f1117]"
        onclick={(e) => e.stopPropagation()}
    >
        {#if isVideo}
            <VideoPreview src={`/api/files/${file.id}/stream_part`} />
            <div
                class="pointer-events-none absolute inset-x-0 top-0 z-10 flex items-center justify-between bg-linear-to-b from-black/70 to-transparent px-4 py-3"
            >
                <span class="pointer-events-auto truncate pr-3 text-sm font-medium text-white"
                    >{file.name}</span
                >
                <button
                    class="pointer-events-auto cursor-pointer text-white/70 transition-colors hover:text-white"
                    onclick={onclose}
                    aria-label="Close"
                >
                    <svg
                        xmlns="http://www.w3.org/2000/svg"
                        class="h-4 w-4"
                        viewBox="0 0 24 24"
                        fill="none"
                        stroke="currentColor"
                        stroke-width="2"
                        stroke-linecap="round"
                        stroke-linejoin="round"
                    >
                        <line x1="18" y1="6" x2="6" y2="18" />
                        <line x1="6" y1="6" x2="18" y2="18" />
                    </svg>
                </button>
            </div>
        {:else}
            <div class="flex h-full w-full items-center justify-center text-sm text-white/40">
                No preview available
            </div>
        {/if}
    </div>
</div>
