/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { IModelApp } from "@itwin/core-frontend";
import { ViewportComponent } from "@itwin/imodel-components-react";
import { FloatingViewportContentControl } from "./ViewportContentControl";
import { ContentWrapper } from "./ContentLayout";
import { UiFramework } from "../UiFramework";
import { useRefs } from "@itwin/core-react";
/** @beta */
export function FloatingViewportContent(props) {
    const { viewportControl } = useFloatingViewport(props);
    return (React.createElement(FloatingViewportContentWrapper, null, viewportControl));
}
/** @alpha */
export function FloatingViewportContentWrapper({ children, }) {
    return (React.createElement("div", { onMouseMove: UiFramework.visibility.handleContentMouseMove, className: "uifw-dialog-imodel-content", style: { height: "100%", position: "relative" } },
        React.createElement(ContentWrapper, { content: children, style: { height: "100%", position: "relative" } })));
}
/** @alpha */
export 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 = useRefs(ref, viewportRef);
    const viewportControl = React.useMemo(() => {
        const node = (React.createElement(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 FloatingViewportContentControl(contentId, contentId, null);
            UiFramework.content.addFloatingContentControl(contentControl.current);
        }
        return () => {
            if (contentControl.current) {
                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;
            }
        }
    }, [viewport, viewportControl]);
    React.useEffect(() => {
        const onViewClose = (vp) => {
            var _a;
            if (((_a = contentControl.current) === null || _a === void 0 ? void 0 : _a.viewport) === vp) {
                UiFramework.content.dropFloatingContentControl(contentControl.current);
                contentControl.current = undefined;
            }
        };
        return IModelApp.viewManager.onViewClose.addListener(onViewClose);
    }, []);
    return {
        viewportControl,
    };
}
//# sourceMappingURL=FloatingViewportContent.js.map