/**
 * PDF side panel for the thread form.
 * Shows alongside the thread content when a file is clicked for viewing.
 *
 * The panel is horizontally resizable via a drag handle on its left edge.
 * Width persists in localStorage between sessions.
 */
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useRef, useState } from 'react';
const MIN_WIDTH = 320;
const STORAGE_KEY = 'hazo_thread_form_pdf_panel_width';
const DEFAULT_WIDTH = 480;
function load_initial_width() {
    if (typeof window === 'undefined')
        return DEFAULT_WIDTH;
    const saved = window.localStorage.getItem(STORAGE_KEY);
    const parsed = saved ? parseInt(saved, 10) : NaN;
    if (Number.isFinite(parsed) && parsed >= MIN_WIDTH)
        return parsed;
    return DEFAULT_WIDTH;
}
export function ThreadPdfSidePanel({ item, file_url, PdfViewer, on_close, }) {
    const file_name = item.file?.file_name || 'Unknown file';
    const [width, set_width] = useState(load_initial_width);
    const dragging_ref = useRef(false);
    const handle_mouse_down = useCallback((e) => {
        e.preventDefault();
        dragging_ref.current = true;
        document.body.style.cursor = 'col-resize';
        document.body.style.userSelect = 'none';
    }, []);
    useEffect(() => {
        function on_move(e) {
            if (!dragging_ref.current)
                return;
            const max = Math.max(MIN_WIDTH, window.innerWidth - 320);
            const next = Math.min(max, Math.max(MIN_WIDTH, window.innerWidth - e.clientX));
            set_width(next);
        }
        function on_up() {
            if (!dragging_ref.current)
                return;
            dragging_ref.current = false;
            document.body.style.cursor = '';
            document.body.style.userSelect = '';
            try {
                window.localStorage.setItem(STORAGE_KEY, String(Math.round(width)));
            }
            catch { /* ignore */ }
        }
        window.addEventListener('mousemove', on_move);
        window.addEventListener('mouseup', on_up);
        return () => {
            window.removeEventListener('mousemove', on_move);
            window.removeEventListener('mouseup', on_up);
        };
    }, [width]);
    return (_jsxs("div", { className: "relative h-full flex-shrink-0 border-l border-[#E2E8F0] bg-white flex flex-col overflow-hidden", style: { width: `${width}px` }, children: [_jsx("div", { onMouseDown: handle_mouse_down, onDoubleClick: () => set_width(DEFAULT_WIDTH), title: "Drag to resize \u00B7 double-click to reset", className: "absolute left-0 top-0 bottom-0 w-1.5 cursor-col-resize z-10 hover:bg-[#3B82F6]/30 active:bg-[#3B82F6]/50 transition-colors" }), _jsxs("div", { className: "flex items-center justify-between px-3 py-2 border-b border-[#E2E8F0] bg-[#F8FAFC] shrink-0", children: [_jsx("span", { className: "text-[13px] font-medium text-[#0F172A] truncate flex-1 mr-2", title: file_name, children: file_name }), _jsx("button", { type: "button", onClick: on_close, className: "h-6 w-6 inline-flex items-center justify-center rounded text-[#94A3B8] hover:text-[#0F172A] hover:bg-[#F1F5F9] transition-colors cursor-pointer", title: "Close panel", children: "\u00D7" })] }), _jsx("div", { className: "flex-1 min-h-0", children: !file_url ? (_jsx("div", { className: "flex items-center justify-center h-full text-[13px] text-[#94A3B8] p-4", children: "Unable to resolve file URL. The file may need to be re-uploaded." })) : PdfViewer ? (_jsx(PdfViewer, { url: file_url, display_filename: file_name, fit_to_width: true, enable_popout: true, className: "h-full" })) : (
                /* Native browser preview via iframe — works for PDFs, images, and most file types */
                _jsx("iframe", { src: file_url, title: file_name, className: "w-full h-full border-0" })) })] }));
}
//# sourceMappingURL=pdf_side_panel.js.map