import { jsx as _jsx } from "react/jsx-runtime";
import { JSONCode, XMLViewer, MarkdownRenderer } from '@vertesia/ui/widgets';
import { useEffect, useLayoutEffect, useState } from "react";
import { usePdfPagesInfo } from "./PdfPageProvider";
const darkTheme = {
    attributeKeyColor: '#FFD700',
    attributeValueColor: '#FF4500',
    tagColor: '#87CEFA',
    textColor: '#00FF00',
    separatorColor: '#FFD700',
    commentColor: "#BEBEBE",
    cdataColor: "#33CC66",
};
export function TextPageView({ viewType, pageNumber }) {
    switch (viewType) {
        case "json":
            return _jsx(JsonPageLayoutView, { pageNumber: pageNumber });
        case "markdown":
            return _jsx(MarkdownPageView, { pageNumber: pageNumber });
        default:
            return _jsx(XmlPageView, { pageNumber: pageNumber });
    }
}
function XmlPageView({ pageNumber }) {
    const [theme, setTheme] = useState();
    const { xmlPages: pages } = usePdfPagesInfo();
    useLayoutEffect(() => {
        const media = window.matchMedia('(prefers-color-scheme: dark)');
        const onMediaChange = (event) => {
            setTheme(event.matches ? darkTheme : undefined);
        };
        media.addEventListener('change', onMediaChange);
        media.matches && setTheme(darkTheme);
        return () => {
            media.removeEventListener('change', onMediaChange);
        };
    }, []);
    return (_jsx("div", { className: "px-4 py-2", children: _jsx(XMLViewer, { xml: pages[pageNumber - 1], collapsible: true, theme: theme }) }));
}
function JsonPageLayoutView({ pageNumber }) {
    const [content, setContent] = useState();
    const { layoutProvider } = usePdfPagesInfo();
    useEffect(() => {
        layoutProvider.getPageLayout(pageNumber).then(content => setContent(content ? JSON.parse(content) : undefined)).catch(err => {
            console.error(err);
            setContent(undefined);
        });
    }, [pageNumber]);
    return (content && _jsx(JSONCode, { className: "w-full", data: content }));
}
function MarkdownPageView({ pageNumber }) {
    const [content, setContent] = useState();
    const { markdownProvider } = usePdfPagesInfo();
    useEffect(() => {
        markdownProvider.getPageMarkdown(pageNumber).then(setContent).catch((err) => {
            console.error(err);
            setContent(undefined);
        });
    }, [pageNumber]);
    return (_jsx("div", { className: "px-4 py-2 prose prose-sm max-w-none dark:prose-invert", children: content ? _jsx(MarkdownRenderer, { children: content }) : _jsx("div", { children: "No markdown content available" }) }));
}
//# sourceMappingURL=TextPageView.js.map