"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ContentView
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFloatingViewport = exports.FloatingViewportContentWrapper = exports.FloatingViewportContent = void 0;
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const imodel_components_react_1 = require("@itwin/imodel-components-react");
const ViewportContentControl_1 = require("./ViewportContentControl");
const ContentLayout_1 = require("./ContentLayout");
const UiFramework_1 = require("../UiFramework");
const core_react_1 = require("@itwin/core-react");
/** @beta */
function FloatingViewportContent(props) {
    const { viewportControl } = useFloatingViewport(props);
    return (React.createElement(FloatingViewportContentWrapper, null, viewportControl));
}
exports.FloatingViewportContent = FloatingViewportContent;
/** @alpha */
function FloatingViewportContentWrapper({ children, }) {
    return (React.createElement("div", { onMouseMove: UiFramework_1.UiFramework.visibility.handleContentMouseMove, className: "uifw-dialog-imodel-content", style: { height: "100%", position: "relative" } },
        React.createElement(ContentLayout_1.ContentWrapper, { content: children, style: { height: "100%", position: "relative" } })));
}
exports.FloatingViewportContentWrapper = FloatingViewportContentWrapper;
/** @alpha */
function useFloatingViewport(args) {
    const { contentId, initialViewState, onContextMenu, viewportRef } = args;
    const [viewport, setViewport] = React.useState();
    const contentControl = React.useRef();
    const viewState = React.useMemo(() => typeof initialViewState === "function"
        ? initialViewState()
        : initialViewState, [initialViewState]);
    const ref = React.useCallback((v) => {
        setViewport(v);
    }, []);
    const refs = (0, core_react_1.useRefs)(ref, viewportRef);
    const viewportControl = React.useMemo(() => {
        const node = (React.createElement(imodel_components_react_1.ViewportComponent, { key: contentId, imodel: viewState.iModel, viewState: viewState, controlId: contentId, onContextMenu: onContextMenu, viewportRef: refs }));
        return node;
    }, [refs, onContextMenu, viewState, contentId]);
    React.useEffect(() => {
        if (!contentControl.current) {
            contentControl.current = new ViewportContentControl_1.FloatingViewportContentControl(contentId, contentId, null);
            UiFramework_1.UiFramework.content.addFloatingContentControl(contentControl.current);
        }
        return () => {
            if (contentControl.current) {
                UiFramework_1.UiFramework.content.dropFloatingContentControl(contentControl.current);
                contentControl.current = undefined;
            }
        };
    }, [contentId]);
    React.useEffect(() => {
        if (viewport && contentControl.current) {
            contentControl.current.viewport = viewport;
            if (null === contentControl.current.reactNode) {
                contentControl.current.reactNode = viewportControl;
            }
            void contentControl.current.viewport.changeView(viewState);
        }
    }, [viewState, viewport, viewportControl]);
    React.useEffect(() => {
        const onViewClose = (vp) => {
            if (contentControl.current?.viewport === vp) {
                UiFramework_1.UiFramework.content.dropFloatingContentControl(contentControl.current);
                contentControl.current = undefined;
            }
        };
        return core_frontend_1.IModelApp.viewManager.onViewClose.addListener(onViewClose);
    }, []);
    return {
        viewportControl,
    };
}
exports.useFloatingViewport = useFloatingViewport;
//# sourceMappingURL=FloatingViewportContent.js.map