"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavigationWidgetComposer = exports.NavigationAidHost = void 0;
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Widget
 */
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const core_frontend_1 = require("@itwin/core-frontend");
const core_react_1 = require("@itwin/core-react");
const imodel_components_react_1 = require("@itwin/imodel-components-react");
const NavigationArea_1 = require("../layout/widget/NavigationArea");
const UiFramework_1 = require("../UiFramework");
const useActiveViewport_1 = require("../hooks/useActiveViewport");
const ViewUtilities_1 = require("../utils/ViewUtilities");
const SheetNavigationAid_1 = require("../navigationaids/SheetNavigationAid");
function createNavigationAidControl(
// eslint-disable-next-line deprecation/deprecation
activeContentControl, navigationAidId, activeViewport
// eslint-disable-next-line deprecation/deprecation
) {
    if (!activeContentControl ||
        !navigationAidId ||
        activeViewport !== activeContentControl.viewport)
        return undefined;
    const viewport = activeContentControl.viewport;
    const imodel = viewport ? viewport.iModel : UiFramework_1.UiFramework.getIModelConnection();
    // eslint-disable-next-line deprecation/deprecation
    const navigationAidControl = UiFramework_1.UiFramework.controls.create(navigationAidId, navigationAidId, { imodel, viewport }
    // eslint-disable-next-line deprecation/deprecation
    );
    navigationAidControl.initialize();
    return navigationAidControl;
}
/** Component that is used as a default navigation aid.
 * Uses registered navigation aids and the active content control to determine if or which navigation aid to display.
 * If an active content is defined without a content control (`classId` is set to an empty string) renders a default navigation aid based on an active viewport view state.
 * @public
 */
function NavigationAidHost(props) {
    const [activeContentControl, setActiveContentControl] = React.useState(() => 
    // eslint-disable-next-line deprecation/deprecation
    UiFramework_1.UiFramework.content.getActiveContentControl());
    const [activeContentViewport, setActiveContentViewport] = React.useState(() => activeContentControl?.viewport);
    const [navigationAidId, setNavigationAidId] = React.useState(() => activeContentControl ? activeContentControl.navigationAidControl : "");
    React.useEffect(() => {
        // eslint-disable-next-line deprecation/deprecation
        return UiFramework_1.UiFramework.frontstages.onContentControlActivatedEvent.addListener((args) => {
            setActiveContentControl(args.activeContentControl);
            setActiveContentViewport(args.activeContentControl.viewport);
            setNavigationAidId(args.activeContentControl.navigationAidControl);
        });
    }, []);
    React.useEffect(() => {
        return UiFramework_1.UiFramework.content.onActiveContentChangedEvent.addListener((args) => {
            args.id;
            const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
            if (!frontstageDef)
                return;
            const contentGroup = frontstageDef.contentGroup;
            if (!contentGroup)
                return;
            const content = contentGroup.contentPropsList.find((contentProps) => contentProps.id === args.id);
            if (!content?.content)
                return;
            // Content w/o a control is activated - reset the navigation.
            setActiveContentControl(undefined);
            setActiveContentViewport(undefined);
            setNavigationAidId("");
        });
    }, []);
    const [activeViewClass, setActiveViewClass] = React.useState(() => {
        // eslint-disable-next-line deprecation/deprecation
        const content = UiFramework_1.UiFramework.content.getActiveContentControl();
        if (content && content.viewport)
            return content.viewport.view.classFullName;
        return "";
    });
    React.useEffect(() => {
        return imodel_components_react_1.ViewportComponentEvents.onViewClassFullNameChangedEvent.addListener((args) => {
            setActiveViewClass(args.newName);
        });
    }, [activeViewClass]);
    const navigationAidControl = React.useMemo(() => createNavigationAidControl(activeContentControl, navigationAidId, activeContentViewport), [activeContentControl, navigationAidId, activeContentViewport]);
    const ref = React.useRef(null);
    const isInitialMount = React.useRef(true);
    const { onElementRef, proximityScale } = (0, core_react_1.useWidgetOpacityContext)();
    React.useEffect(() => {
        if (isInitialMount.current) {
            isInitialMount.current = false;
            onElementRef(ref);
        }
    }, [onElementRef]);
    const divStyle = {
        minWidth: props.minWidth ? props.minWidth : "64px",
        minHeight: props.minHeight ? props.minHeight : "64px",
    };
    if ((UiFramework_1.UiFramework.visibility.useProximityOpacity || // eslint-disable-line deprecation/deprecation
        UiFramework_1.UiFramework.visibility.snapWidgetOpacity) &&
        !core_bentley_1.ProcessDetector.isMobileBrowser) {
        const navigationAidOpacity = 0.3 * proximityScale + 0.7;
        divStyle.opacity = `${navigationAidOpacity}`;
    }
    const navigationAid = navigationAidControl?.reactNode ?? (React.createElement(DefaultNavigationAid, null));
    return (React.createElement("div", { style: divStyle, ref: ref }, navigationAid));
}
exports.NavigationAidHost = NavigationAidHost;
function DefaultNavigationAid() {
    const viewport = (0, useActiveViewport_1.useActiveViewport)();
    if (!viewport)
        return null;
    const className = ViewUtilities_1.ViewUtilities.getBisBaseClass(viewport.view.classFullName);
    switch (className) {
        case core_frontend_1.SheetViewState.className:
            return React.createElement(SheetNavigationAid_1.SheetNavigationAid, { iModelConnection: viewport.iModel });
        case core_frontend_1.DrawingViewState.className:
            return (React.createElement(imodel_components_react_1.DrawingNavigationAid, { iModelConnection: viewport.iModel, viewport: viewport }));
        case core_frontend_1.SpatialViewState.className:
        case core_frontend_1.OrthographicViewState.className:
            return (React.createElement(imodel_components_react_1.CubeNavigationAid, { iModelConnection: viewport.iModel, viewport: viewport }));
    }
    return null;
}
/**
 * Component that Composes a NavigationWidget typically using toolbars generated via [[ToolbarComposer]] class. The Navigation widget is shown in the top right of the content area
 * and typically holds tools to visually navigate, orient, and zoom to specific content.
 * @public
 */
function NavigationWidgetComposer(props) {
    const { navigationAidHost, horizontalToolbar, verticalToolbar, hideNavigationAid, ...otherProps } = props;
    const [elementSet] = React.useState(new core_react_1.WidgetElementSet());
    const handleChildRef = React.useCallback((elementRef) => {
        elementSet.add(elementRef);
    }, [elementSet]);
    const proximityScale = (0, core_react_1.useProximityToMouse)(elementSet, UiFramework_1.UiFramework.visibility.snapWidgetOpacity);
    const navigationAid = hideNavigationAid
        ? undefined
        : // eslint-disable-next-line deprecation/deprecation
            navigationAidHost ?? React.createElement(NavigationAidHost, null);
    return (React.createElement(core_react_1.WidgetOpacityContext.Provider, { value: {
            onElementRef: handleChildRef,
            proximityScale,
        } },
        React.createElement(NavigationArea_1.NavigationArea, { navigationAid: navigationAid, horizontalToolbar: horizontalToolbar, verticalToolbar: verticalToolbar, ...otherProps, onMouseEnter: UiFramework_1.UiFramework.visibility.handleWidgetMouseEnter })));
}
exports.NavigationWidgetComposer = NavigationWidgetComposer;
//# sourceMappingURL=NavigationWidgetComposer.js.map