"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.ToolWidget = exports.ToolWidgetDef = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const ToolbarWidgetBase_1 = require("./ToolbarWidgetBase");
const WidgetDef_1 = require("./WidgetDef");
const UiFramework_1 = require("../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
 */
class ToolWidgetDef extends ToolbarWidgetBase_1.ToolbarWidgetDefBase {
    constructor(props) {
        super(props);
        this._backstageLabel = UiFramework_1.UiFramework.translate("buttons.openBackstageMenu");
        this._appButton = props.appButton;
        this.widgetType = WidgetDef_1.WidgetType.Tool;
        this.verticalDirection = (props.verticalDirection !== undefined) ? props.verticalDirection : appui_layout_react_1.Direction.Right; // eslint-disable-line deprecation/deprecation
        const activeStageName = UiFramework_1.UiFramework.frontstages.activeFrontstageDef ? UiFramework_1.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(appui_layout_react_1.AppButton // eslint-disable-line deprecation/deprecation
            , { onClick: this._appButton.execute, icon: React.createElement(core_react_1.Icon, { iconSpec: this._appButton.iconSpec }), title: this._appButton.tooltip || this._backstageLabel }));
        }
        return undefined;
    }
}
exports.ToolWidgetDef = ToolWidgetDef;
/** ToolWidget React component.
 * @public
 * @deprecated in 3.6. Use [[ToolWidgetComposer]] instead
 */
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 }));
    }
}
exports.ToolWidget = ToolWidget;
/** 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(appui_layout_react_1.Tools, { className: this.props.className, style: this.props.style, button: this.state.cornerItem, horizontalToolbar: this.state.horizontalToolbar, verticalToolbar: this.state.verticalToolbar, preserveSpace: true, onMouseEnter: UiFramework_1.UiFramework.visibility.handleWidgetMouseEnter }));
    }
}
//# sourceMappingURL=ToolWidget.js.map