/*---------------------------------------------------------------------------------------------
 * 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.js";
import { ContentWrapper } from "./ContentLayout.js";
import { UiFramework } from "../UiFramework.js";
import { useRefs } from "@itwin/core-react/internal";
/**
 * @beta
 * @deprecated in 4.16.0. Wrap {@link @itwin/imodel-components-react#ViewportComponent} component with a {@link ContentOverlay} instead.
 */
export function FloatingViewportContent(props) {
    const { viewportControl } = useFloatingViewport(props);
    return (React.createElement(FloatingViewportContentWrapper, null, viewportControl));
}
/**
 * @public
 * @deprecated in 4.16.0. Use {@link ContentOverlay} component to display the active content indicator instead.
 */
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" }, contentIndex: undefined })));
}
/**
 * @alpha
 * @deprecated in 4.16.0. Use {@link @itwin/imodel-components-react#ViewportComponent} component instead.
 */
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) {
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            contentControl.current = new FloatingViewportContentControl(contentId, contentId, null);
            // eslint-disable-next-line @typescript-eslint/no-deprecated
            UiFramework.content.addFloatingContentControl(contentControl.current);
        }
        return () => {
            if (contentControl.current) {
                // eslint-disable-next-line @typescript-eslint/no-deprecated
                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(() => {
        return IModelApp.viewManager.onViewClose.addListener((vp) => {
            if (contentControl.current?.viewport === vp) {
                // eslint-disable-next-line @typescript-eslint/no-deprecated
                UiFramework.content.dropFloatingContentControl(contentControl.current);
                contentControl.current = undefined;
            }
        });
    }, []);
    return {
        viewportControl,
    };
}
//# sourceMappingURL=FloatingViewportContent.js.map