/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { ViewportComponentEvents } from "@itwin/imodel-components-react";
import { useProximityToMouse, useWidgetOpacityContext, WidgetElementSet, WidgetOpacityContext, } from "@itwin/core-react";
import { NavigationArea } from "@itwin/appui-layout-react";
import { UiFramework } from "../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.getIModelConnection();
    const navigationAidControl = 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
 */
export function NavigationAidHost(props) {
    const [activeContentControl, setActiveContentControl] = React.useState(() => UiFramework.content.getActiveContentControl());
    const [activeContentViewport, setActiveContentViewport] = React.useState(() => /* istanbul ignore next */ 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.frontstages.onContentControlActivatedEvent.addListener(handleContentControlActivated);
        return () => {
            UiFramework.frontstages.onContentControlActivatedEvent.removeListener(handleContentControlActivated);
        };
    }, []);
    const [activeViewClass, setActiveViewClass] = React.useState(() => {
        const content = UiFramework.content.getActiveContentControl();
        if (content && content.viewport)
            return content.viewport.view.classFullName;
        return "";
    });
    React.useEffect(() => {
        // istanbul ignore next
        const handleViewClassFullNameChange = (args) => {
            setActiveViewClass(args.newName);
        };
        ViewportComponentEvents.onViewClassFullNameChangedEvent.addListener(handleViewClassFullNameChange);
        return () => {
            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 } = 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 (UiFramework.visibility.useProximityOpacity && !UiFramework.isMobile()) {
        const navigationAidOpacity = 0.3 * proximityScale + 0.7;
        divStyle.opacity = `${navigationAidOpacity}`;
    }
    return (React.createElement("div", { style: divStyle, ref: ref }, navigationAidControl && navigationAidControl.reactNode));
}
/**
 * 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
 */
export function NavigationWidgetComposer(props) {
    const { navigationAidHost, horizontalToolbar, verticalToolbar, hideNavigationAid, ...otherProps } = props;
    const [elementSet] = React.useState(new WidgetElementSet());
    const handleChildRef = React.useCallback((elementRef) => {
        elementSet.add(elementRef);
    }, [elementSet]);
    const proximityScale = useProximityToMouse(elementSet, UiFramework.visibility.snapWidgetOpacity);
    /* istanbul ignore next */
    const navigationAid = hideNavigationAid
        ? undefined
        : navigationAidHost ?? React.createElement(NavigationAidHost, null);
    return (React.createElement(WidgetOpacityContext.Provider, { value: {
            onElementRef: handleChildRef,
            proximityScale,
        } },
        React.createElement(NavigationArea, { navigationAid: navigationAid, horizontalToolbar: horizontalToolbar, verticalToolbar: verticalToolbar, ...otherProps, onMouseEnter: UiFramework.visibility.handleWidgetMouseEnter })));
}
//# sourceMappingURL=NavigationWidgetComposer.js.map