import { jsx as _jsx } 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 React from "react";
import { DEFAULT_CAMERA_STATE } from "@foxglove/studio-base/panels/ThreeDeeRender/camera.js";
import { Panel } from "../components/Panel.js";
import { createRenderDelaySampler } from "../components/measure-render-delay.js";
import { constructKeyForTopicRenderMetrics, globalRenderMetricsManager, } from "../utils/metrics-manager.js";
const ThreeDeeRenderComponent = React.lazy(async () => await import("@foxglove/studio-base/panels/ThreeDeeRender"));
const PointCloudPanelBody = ({ topic, frameRenderedEvent, handleOnClick, handleCursorPositionUpdate, registerRendered, unregisterRendered, customOverlay, }) => {
    const neuralCameraIntrinsicsData = localStorage.getItem("neuralCameraIntrinsics");
    const getFovy = React.useCallback(() => {
        try {
            const neuralCameraIntrinsics = JSON.parse(neuralCameraIntrinsicsData ?? "");
            if (neuralCameraIntrinsics) {
                const fovy = 2 *
                    Math.atan(neuralCameraIntrinsics.right.principalPoint.y /
                        neuralCameraIntrinsics.right.focalLenght.y) *
                    (180 / Math.PI);
                return fovy;
            }
            return 45;
        }
        catch (error) {
            return 45;
        }
    }, [neuralCameraIntrinsicsData]);
    const defaultConfig = React.useMemo(() => ({
        topics: {
            [topic]: {
                maxValue: 5000,
                minValue: 0,
                pointSize: 2,
                pointShape: "square",
                visible: true,
            },
        },
        cameraState: {
            distance: DEFAULT_CAMERA_STATE.distance,
            perspective: localStorage.getItem("perspective") ? localStorage.getItem("perspective") === "true" : DEFAULT_CAMERA_STATE.perspective,
            phi: 89,
            target: DEFAULT_CAMERA_STATE.target,
            targetOffset: DEFAULT_CAMERA_STATE.targetOffset,
            targetOrientation: DEFAULT_CAMERA_STATE.targetOrientation,
            thetaOffset: DEFAULT_CAMERA_STATE.thetaOffset,
            near: DEFAULT_CAMERA_STATE.near,
            far: DEFAULT_CAMERA_STATE.far,
            fovy: getFovy(),
        },
        imageMode: {
            colorMode: "rgba-fields",
        },
    }), [getFovy, topic]);
    return (_jsx(ThreeDeeRenderComponent, { overrideConfig: defaultConfig, frameRenderedEvent: frameRenderedEvent, handleOnClick: handleOnClick, handleCursorPositionUpdate: handleCursorPositionUpdate, registerRendered: registerRendered, unregisterRendered: unregisterRendered, customOverlay: customOverlay }));
};
export const PointCloudPanel = React.memo(function PointCloudPanel({ topic, toggleTopic, toggleFullscreen, targetFps, hideToolbar = false, canMoveUp, moveUp, canMoveDown, moveDown, handleOnClick, handleCursorPositionUpdate, registerRendered, unregisterRendered, customOverlay, panelToolbarActions, }) {
    const { averageFrameDelay, addFrameTime } = createRenderDelaySampler();
    const renderMetricsKey = React.useMemo(() => constructKeyForTopicRenderMetrics("renderPointCloudFrame", topic), [topic]);
    React.useEffect(() => {
        globalRenderMetricsManager.ensureTopic(renderMetricsKey);
    }, [renderMetricsKey]);
    const handleFrameRendered = (frameTime) => {
        globalRenderMetricsManager.registerEvent(renderMetricsKey);
        addFrameTime(frameTime);
    };
    return (_jsx(Panel, { kind: "pointCloud", topic: topic, toggleTopic: toggleTopic, toggleFullscreen: toggleFullscreen, renderDelay: averageFrameDelay, disableAnnotations: true, targetFps: targetFps, hideToolbar: hideToolbar, canMoveUp: canMoveUp, moveUp: moveUp, canMoveDown: canMoveDown, moveDown: moveDown, panelToolbarActions: panelToolbarActions, children: _jsx(PointCloudPanelBody, { topic: topic, frameRenderedEvent: handleFrameRendered, handleOnClick: handleOnClick, handleCursorPositionUpdate: handleCursorPositionUpdate, registerRendered: registerRendered, unregisterRendered: unregisterRendered, customOverlay: customOverlay }) }));
});
//# sourceMappingURL=PointCloudPanel.js.map