/*---------------------------------------------------------------------------------------------
* 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 { UiError } from "@itwin/appui-abstract";
import { ViewportComponentEvents } from "@itwin/imodel-components-react";
import { Direction, Tools as NZ_ToolsWidget, ToolbarPanelAlignment } from "@itwin/appui-layout-react";
import { ConfigurableUiControlType } from "../configurableui/ConfigurableUiControl";
import { UiFramework } from "../UiFramework";
import { ToolbarWidgetDefBase } from "./ToolbarWidgetBase";
import { WidgetType } from "./WidgetDef";
/** Definition of a Navigation Widget normally displayed in the top right zone in the 9-Zone Layout system.
 * @public
 * @deprecated in 2.x. Use NavigationWidgetComposer instead
 */
export class NavigationWidgetDef extends ToolbarWidgetDefBase {
    constructor(props) {
        super(props);
        this.widgetType = WidgetType.Navigation;
        // eslint-disable-next-line deprecation/deprecation
        this.verticalDirection = (props.verticalDirection !== undefined) ? props.verticalDirection : Direction.Left;
        this.horizontalPanelAlignment = ToolbarPanelAlignment.End; // eslint-disable-line deprecation/deprecation
        this._navigationAidId = (props.navigationAidId !== undefined) ? props.navigationAidId : "";
        const activeStageName = UiFramework.frontstages.activeFrontstageDef ? UiFramework.frontstages.activeFrontstageDef.id : /* istanbul ignore next */ "";
        this.widgetBaseName = `[${activeStageName}]NavigationWidget`;
    }
    get reactNode() {
        // istanbul ignore else
        if (!this._reactNode)
            this._reactNode = React.createElement(NavigationWidgetWithDef, { navigationWidgetDef: this });
        return this._reactNode;
    }
    renderCornerItem() {
        // istanbul ignore if
        if (UiFramework.frontstages.isLoading)
            return null;
        // istanbul ignore else
        if (!this._navigationAidControl && this._navigationAidId) {
            const activeContentControl = UiFramework.content.getActiveContentControl();
            const viewport = activeContentControl ? activeContentControl.viewport : /* istanbul ignore next */ undefined;
            this._navigationAidControl = UiFramework.controls.create(this._navigationAidId, this._navigationAidId, { imodel: this._imodel, viewport });
            if (this._navigationAidControl.getType() !== ConfigurableUiControlType.NavigationAid) {
                throw new UiError(UiFramework.loggerCategory(this), `renderCornerItem: navigationAidId '${this._navigationAidId}' is registered to a control that is NOT a NavigationAid`);
            }
            this._navigationAidControl.initialize();
        }
        // istanbul ignore else
        if (this._navigationAidControl) {
            const size = this._navigationAidControl.getSize() || "64px";
            const divStyle = {
                width: size,
                height: size,
            };
            return (React.createElement("div", { style: divStyle }, this._navigationAidControl.reactNode));
        }
        // istanbul ignore next
        return null;
    }
    updateNavigationAid(navigationAidId, imodel) {
        this._navigationAidId = navigationAidId;
        this._imodel = imodel;
        this._navigationAidControl = undefined;
    }
}
/** Navigation Widget React component.
 * @public
 * @deprecated in 2.x. Use NavigationWidgetComposer instead
 */
export class NavigationWidget extends React.Component {
    constructor(props) {
        super(props);
        this._handleContentControlActivated = (args) => {
            const navigationAidId = args.activeContentControl.navigationAidControl;
            setTimeout(() => {
                UiFramework.frontstages.setActiveNavigationAid(navigationAidId, this.props.iModelConnection);
            });
        };
        this._handleViewClassFullNameChange = (args) => {
            setTimeout(() => {
                const activeContentControl = UiFramework.content.getActiveContentControl();
                // istanbul ignore else
                if (activeContentControl && args.viewport === activeContentControl.viewport) {
                    const navigationAidId = activeContentControl.navigationAidControl;
                    UiFramework.frontstages.setActiveNavigationAid(navigationAidId, this.props.iModelConnection);
                }
            });
        };
        this.state = { navigationWidgetDef: new NavigationWidgetDef(props) }; // eslint-disable-line deprecation/deprecation
    }
    /** Adds listeners */
    componentDidMount() {
        UiFramework.frontstages.onContentControlActivatedEvent.addListener(this._handleContentControlActivated);
        ViewportComponentEvents.onViewClassFullNameChangedEvent.addListener(this._handleViewClassFullNameChange);
    }
    /** Removes listeners */
    componentWillUnmount() {
        UiFramework.frontstages.onContentControlActivatedEvent.removeListener(this._handleContentControlActivated);
        ViewportComponentEvents.onViewClassFullNameChangedEvent.removeListener(this._handleViewClassFullNameChange);
    }
    componentDidUpdate(prevProps, _prevState) {
        if (this.props !== prevProps) {
            this.setState((_, props) => ({ navigationWidgetDef: new NavigationWidgetDef(props) })); // eslint-disable-line deprecation/deprecation
        }
    }
    render() {
        return (React.createElement(NavigationWidgetWithDef, { className: this.props.className, style: this.props.style, navigationWidgetDef: this.state.navigationWidgetDef, horizontalToolbar: this.props.horizontalToolbar, verticalToolbar: this.props.verticalToolbar }));
    }
}
/** Navigation Widget React component that's passed a NavigationWidgetDef.
 */
class NavigationWidgetWithDef extends React.Component {
    constructor(props) {
        super(props);
        this._handleNavigationAidActivatedEvent = (args) => {
            this.props.navigationWidgetDef.updateNavigationAid(args.navigationAidId, args.iModelConnection);
            const navigationAid = this.props.navigationWidgetDef.renderCornerItem();
            this.setState({ cornerItem: navigationAid });
        };
        const horizontalToolbar = (this.props.horizontalToolbar) ? this.props.horizontalToolbar : this.props.navigationWidgetDef.renderHorizontalToolbar();
        const verticalToolbar = (this.props.verticalToolbar) ? this.props.verticalToolbar : this.props.navigationWidgetDef.renderVerticalToolbar();
        this.state = { horizontalToolbar, verticalToolbar, cornerItem: null };
    }
    reloadToolbars() {
        const horizontalToolbar = (this.props.horizontalToolbar) ? this.props.horizontalToolbar : /* istanbul ignore next */ this.props.navigationWidgetDef.renderHorizontalToolbar();
        const verticalToolbar = (this.props.verticalToolbar) ? /* istanbul ignore next */ this.props.verticalToolbar : this.props.navigationWidgetDef.renderVerticalToolbar();
        this.setState({ horizontalToolbar, verticalToolbar });
    }
    componentDidMount() {
        UiFramework.frontstages.onNavigationAidActivatedEvent.addListener(this._handleNavigationAidActivatedEvent);
    }
    componentWillUnmount() {
        UiFramework.frontstages.onNavigationAidActivatedEvent.removeListener(this._handleNavigationAidActivatedEvent);
    }
    componentDidUpdate(prevProps) {
        if (this.props !== prevProps)
            this.reloadToolbars();
    }
    render() {
        return (React.createElement(NZ_ToolsWidget, { isNavigation: true, className: this.props.className, style: this.props.style, button: this.state.cornerItem, horizontalToolbar: this.state.horizontalToolbar, verticalToolbar: this.state.verticalToolbar, preserveSpace: true, onMouseEnter: UiFramework.visibility.handleWidgetMouseEnter }));
    }
}
//# sourceMappingURL=NavigationWidget.js.map