/*---------------------------------------------------------------------------------------------
* 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 { Icon } from "@itwin/core-react";
import { AppButton, Direction, Tools as NZ_ToolsWidget } from "@itwin/appui-layout-react";
import { ToolbarWidgetDefBase } from "./ToolbarWidgetBase";
import { WidgetType } from "./WidgetDef";
import { UiFramework } from "../UiFramework";
/** Definition of a Tool Widget normally displayed in the top left zone in the 9-Zone Layout system.
 * @public
 * @deprecated in 3.6. Use [[ToolWidgetComposer]] instead
 */
export class ToolWidgetDef extends ToolbarWidgetDefBase {
    constructor(props) {
        super(props);
        this._backstageLabel = UiFramework.translate("buttons.openBackstageMenu");
        this._appButton = props.appButton;
        this.widgetType = WidgetType.Tool;
        this.verticalDirection = (props.verticalDirection !== undefined) ? props.verticalDirection : Direction.Right; // eslint-disable-line deprecation/deprecation
        const activeStageName = UiFramework.frontstages.activeFrontstageDef ? UiFramework.frontstages.activeFrontstageDef.id : /* istanbul ignore next */ "";
        this.widgetBaseName = `[${activeStageName}]ToolWidget`;
    }
    get reactNode() {
        // istanbul ignore else
        if (!this._reactNode)
            this._reactNode = React.createElement(ToolWidgetWithDef, { toolWidgetDef: this });
        return this._reactNode;
    }
    renderCornerItem() {
        if (this._appButton) {
            return (React.createElement(AppButton // eslint-disable-line deprecation/deprecation
            , { onClick: this._appButton.execute, icon: React.createElement(Icon, { iconSpec: this._appButton.iconSpec }), title: this._appButton.tooltip || this._backstageLabel }));
        }
        return undefined;
    }
}
/** ToolWidget React component.
 * @public
 * @deprecated in 3.6. Use [[ToolWidgetComposer]] instead
 */
export class ToolWidget extends React.Component {
    constructor(props) {
        super(props);
        this.state = { toolWidgetDef: new ToolWidgetDef(props) }; // eslint-disable-line deprecation/deprecation
    }
    componentDidUpdate(prevProps, _prevState) {
        if (this.props !== prevProps) {
            this.setState((_, props) => ({ toolWidgetDef: new ToolWidgetDef(props) })); // eslint-disable-line deprecation/deprecation
        }
    }
    render() {
        return (React.createElement(ToolWidgetWithDef, { className: this.props.className, style: this.props.style, toolWidgetDef: this.state.toolWidgetDef, button: this.props.button, horizontalToolbar: this.props.horizontalToolbar, verticalToolbar: this.props.verticalToolbar }));
    }
}
/** Tool Widget React component.
 */
class ToolWidgetWithDef extends React.Component {
    constructor(props) {
        super(props);
        const horizontalToolbar = (this.props.horizontalToolbar) ? this.props.horizontalToolbar : this.props.toolWidgetDef.renderHorizontalToolbar();
        const verticalToolbar = (this.props.verticalToolbar) ? this.props.verticalToolbar : this.props.toolWidgetDef.renderVerticalToolbar();
        const cornerItem = (this.props.button !== undefined) ? this.props.button : this.props.toolWidgetDef.renderCornerItem();
        this.state = { horizontalToolbar, verticalToolbar, cornerItem };
    }
    reloadToolbars() {
        const horizontalToolbar = (this.props.horizontalToolbar) ? this.props.horizontalToolbar : /* istanbul ignore next */ this.props.toolWidgetDef.renderHorizontalToolbar();
        const verticalToolbar = (this.props.verticalToolbar) ? /* istanbul ignore next */ this.props.verticalToolbar : this.props.toolWidgetDef.renderVerticalToolbar();
        this.setState({ horizontalToolbar, verticalToolbar });
    }
    componentDidUpdate(prevProps) {
        if (this.props !== prevProps)
            this.reloadToolbars();
    }
    render() {
        return (React.createElement(NZ_ToolsWidget, { 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=ToolWidget.js.map