"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 Widget
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavigationWidgetComposer = exports.NavigationAidHost = void 0;
const React = require("react");
const imodel_components_react_1 = require("@itwin/imodel-components-react");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const useFrameworkVersion_1 = require("../hooks/useFrameworkVersion");
const UiFramework_1 = require("../UiFramework");
function createNavigationAidControl(activeContentControl, navigationAidId, activeViewport) {
    // istanbul ignore else
    if (!activeContentControl || !navigationAidId || (activeViewport !== activeContentControl.viewport))
        return undefined;
    const viewport = activeContentControl.viewport;
    const imodel = viewport ? viewport.iModel : /* istanbul ignore next */ UiFramework_1.UiFramework.getIModelConnection();
    const navigationAidControl = UiFramework_1.UiFramework.controls.create(navigationAidId, navigationAidId, { imodel, viewport });
    navigationAidControl.initialize();
    return navigationAidControl;
}
/**
 * NavigationAidHost is a component that hosts a NavigationAid that is specific to the active content control.
 * @public
 */
function NavigationAidHost(props) {
    const [activeContentControl, setActiveContentControl] = React.useState(() => UiFramework_1.UiFramework.content.getActiveContentControl());
    const [activeContentViewport, setActiveContentViewport] = React.useState(() => /* istanbul ignore next */ activeContentControl === null || activeContentControl === void 0 ? void 0 : activeContentControl.viewport);
    const [navigationAidId, setNavigationAidId] = React.useState(() => activeContentControl ? activeContentControl.navigationAidControl : /* istanbul ignore next */ "");
    React.useEffect(() => {
        // istanbul ignore next
        const handleContentControlActivated = (args) => {
            setActiveContentControl(args.activeContentControl);
            setActiveContentViewport(args.activeContentControl.viewport);
            setNavigationAidId(args.activeContentControl.navigationAidControl);
        };
        UiFramework_1.UiFramework.frontstages.onContentControlActivatedEvent.addListener(handleContentControlActivated);
        return () => {
            UiFramework_1.UiFramework.frontstages.onContentControlActivatedEvent.removeListener(handleContentControlActivated);
        };
    }, []);
    const [activeViewClass, setActiveViewClass] = React.useState(() => {
        const content = UiFramework_1.UiFramework.content.getActiveContentControl();
        if (content && content.viewport)
            return content.viewport.view.classFullName;
        return "";
    });
    React.useEffect(() => {
        // istanbul ignore next
        const handleViewClassFullNameChange = (args) => {
            setActiveViewClass(args.newName);
        };
        imodel_components_react_1.ViewportComponentEvents.onViewClassFullNameChangedEvent.addListener(handleViewClassFullNameChange);
        return () => {
            imodel_components_react_1.ViewportComponentEvents.onViewClassFullNameChangedEvent.removeListener(handleViewClassFullNameChange);
        };
    }, [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(() => {
        // istanbul ignore else
        if (isInitialMount.current) {
            isInitialMount.current = false;
            onElementRef(ref);
        }
    }, [onElementRef]);
    const divStyle = {
        minWidth: props.minWidth ? /* istanbul ignore next */ props.minWidth : "64px",
        minHeight: props.minHeight ? /* istanbul ignore next */ props.minHeight : "64px",
    };
    // istanbul ignore else
    if ("1" !== (0, useFrameworkVersion_1.useFrameworkVersion)() && UiFramework_1.UiFramework.visibility.useProximityOpacity && !UiFramework_1.UiFramework.isMobile()) { // eslint-disable-line deprecation/deprecation
        const navigationAidOpacity = (0.30 * proximityScale) + 0.70;
        divStyle.opacity = `${navigationAidOpacity}`;
    }
    return (React.createElement("div", { style: divStyle, ref: ref }, navigationAidControl && navigationAidControl.reactNode));
}
exports.NavigationAidHost = NavigationAidHost;
/**
 * 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);
    /* istanbul ignore next */
    const navigationAid = hideNavigationAid ? undefined : navigationAidHost !== null && navigationAidHost !== void 0 ? navigationAidHost : React.createElement(NavigationAidHost, null);
    return (React.createElement(core_react_1.WidgetOpacityContext.Provider, { value: {
            onElementRef: handleChildRef,
            proximityScale,
        } },
        React.createElement(appui_layout_react_1.NavigationArea, { navigationAid: navigationAid, horizontalToolbar: horizontalToolbar, verticalToolbar: verticalToolbar, ...otherProps, onMouseEnter: UiFramework_1.UiFramework.visibility.handleWidgetMouseEnter })));
}
exports.NavigationWidgetComposer = NavigationWidgetComposer;
//# sourceMappingURL=NavigationWidgetComposer.js.map