import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import { Suspense, useRef } from "react";
import EmptyState from "@foxglove/studio-base/components/EmptyState.js";
import ErrorBoundary from "@foxglove/studio-base/components/ErrorBoundary.js";
import RemountOnValueChange from "@foxglove/studio-base/components/RemountOnValueChange.js";
import { PanelToolbar } from "./PanelToolbar.js";
import "../fonts/fonts.css";
const DetectionContext = React.createContext({ detections: [] });
export const useDetections = () => {
    return React.useContext(DetectionContext);
};
const rawToAnnotations = (rawAnnotations, kind, current) => rawAnnotations.map((annotationTopic) => ({
    name: annotationTopic,
    extra: kind === "extra",
    enabled: current.find(({ name }) => name === annotationTopic)?.enabled ?? kind === "main",
}));
export const Panel = ({ children, kind, topic, disableAnnotations = false, annotationTopics = [], extraAnnotationTopics = [], targetFps, renderDelay, toggleTopic, triggerToast, hideToolbar = false, toggleFullscreen, canMoveUp, moveUp, canMoveDown, moveDown, panelOverlay, fakedAs, panelToolbarActions, }) => {
    const [detections, setDetections] = React.useState([]);
    const sectionRef = useRef(null);
    React.useEffect(() => {
        if (!disableAnnotations) {
            setDetections((current) => [
                ...rawToAnnotations(annotationTopics, "main", current),
                ...rawToAnnotations(extraAnnotationTopics, "extra", current),
            ]);
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [JSON.stringify(annotationTopics), JSON.stringify(extraAnnotationTopics)]);
    const toggleFullscreenForThisPanel = () => {
        if (!toggleFullscreen || !sectionRef.current) {
            return;
        }
        toggleFullscreen(sectionRef);
    };
    return (_jsx(RemountOnValueChange, { value: "internal-player", children: _jsx(ErrorBoundary, { children: _jsx(DetectionContext.Provider, { value: { detections }, children: _jsxs("section", { className: "flex flex-col grow border border-solid border-[#d3d3d3d9] overflow-hidden rounded-lg", ref: sectionRef, children: [panelOverlay, !hideToolbar && (_jsx(PanelToolbar, { fakedAs: fakedAs, kind: kind, topic: topic, setDetections: setDetections, disableAnnotations: disableAnnotations, targetFps: targetFps, renderDelay: renderDelay, toggleTopic: toggleTopic, toggleFullscreen: toggleFullscreenForThisPanel, triggerToast: triggerToast, canMoveUp: canMoveUp, moveUp: moveUp, canMoveDown: canMoveDown, moveDown: moveDown, touchesBreakpoint: sectionRef.current?.clientWidth ? sectionRef.current.clientWidth <= 550 : false, isPointcloud: kind === "pointCloud", actions: panelToolbarActions })), _jsx(Suspense, { fallback: _jsx(EmptyState, { children: _jsx("div", { children: "Loading..." }) }), children: _jsx(RemountOnValueChange, { value: detections, children: children }) })] }) }) }) }));
};
//# sourceMappingURL=Panel.js.map