"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.FloatingViewportContent = void 0;
require("./FloatingViewportContent.css");
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const presentation_components_1 = require("@itwin/presentation-components");
const imodel_components_react_1 = require("@itwin/imodel-components-react");
const ViewportContentControl_1 = require("./ViewportContentControl");
const ContentLayout_1 = require("./ContentLayout");
const core_react_1 = require("@itwin/core-react");
const UiFramework_1 = require("../UiFramework");
// eslint-disable-next-line @typescript-eslint/naming-convention
const FloatingViewport = (0, presentation_components_1.viewWithUnifiedSelection)(imodel_components_react_1.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
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 = (0, core_react_1.useRefs)(ref, ...(viewportRef ? [viewportRef] : []));
    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]);
    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_1.UiFramework.content.dropFloatingContentControl(contentControl.current);
                contentControl.current = undefined;
            }
        };
        return core_frontend_1.IModelApp.viewManager.onViewClose.addListener(onViewClose);
    }, []);
    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: viewPortControl, style: { height: "100%", position: "relative" } })));
}
exports.FloatingViewportContent = FloatingViewportContent;
//# sourceMappingURL=FloatingViewportContent.js.map