/*---------------------------------------------------------------------------------------------
* 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 "./FloatingViewportContent.css";
import * as React from "react";
import { IModelApp } from "@itwin/core-frontend";
import { viewWithUnifiedSelection } from "@itwin/presentation-components";
import { ViewportComponent } from "@itwin/imodel-components-react";
import { FloatingViewportContentControl } from "./ViewportContentControl";
import { ContentWrapper } from "./ContentLayout";
import { useRefs } from "@itwin/core-react";
import { UiFramework } from "../UiFramework";
// eslint-disable-next-line @typescript-eslint/naming-convention
const FloatingViewport = viewWithUnifiedSelection(ViewportComponent);
/**
 * FloatingViewportContent component that creates its own [FloatingViewportContentControl].
 * This allows it to be recognized as an "active" content control so that tools operate on this
 * content.
 * @beta
 */
// istanbul ignore next
export function FloatingViewportContent(props) {
    const { contentId, initialViewState, viewportRef } = props;
    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 onViewportRef = useRefs(ref, ...(viewportRef ? [viewportRef] : []));
    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]);
    const viewPortControl = React.useMemo(() => {
        const node = React.createElement(FloatingViewport, { viewportRef: onViewportRef, imodel: viewState.iModel, viewState: viewState, onContextMenu: props.onContextMenu, controlId: contentId });
        let control = node;
        if (!node.key) {
            const additionalProps = { key: contentId };
            control = React.cloneElement(node, additionalProps);
        }
        return control;
    }, [onViewportRef, props.onContextMenu, viewState, contentId]);
    React.useEffect(() => {
        if (viewport && contentControl.current) {
            contentControl.current.viewport = viewport;
            if (null === contentControl.current.reactNode) {
                contentControl.current.reactNode = viewPortControl;
            }
            // eslint-disable-next-line @typescript-eslint/no-floating-promises
            contentControl.current.viewport.changeView(viewState);
        }
    }, [viewState, viewPortControl, viewport]);
    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 (React.createElement("div", { onMouseMove: UiFramework.visibility.handleContentMouseMove, className: "uifw-dialog-imodel-content", style: { height: "100%", position: "relative" } },
        React.createElement(ContentWrapper, { content: viewPortControl, style: { height: "100%", position: "relative" } })));
}
//# sourceMappingURL=FloatingViewportContent.js.map