"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 Item
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolButton = void 0;
const React = require("react");
const core_frontend_1 = require("@itwin/core-frontend");
const core_react_1 = require("@itwin/core-react");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../UiFramework");
const PropsHelper_1 = require("../utils/PropsHelper");
const useEscapeSetFocusToHome_1 = require("../hooks/useEscapeSetFocusToHome");
/** Tool Button React Component.
 * @deprecated in 3.5. Use [CommonToolbarItem]($appui-abstract) instead.
 * @public
 */
class ToolButton extends React.Component {
    constructor(props) {
        super(props);
        this._componentUnmounting = false;
        this._label = "";
        this._handleSyncUiEvent = (args) => {
            // istanbul ignore if
            if (this._componentUnmounting)
                return;
            let refreshState = false;
            let newState = { ...this.state };
            // since this is a tool button automatically monitor the activation of tools so the active state of the button is updated.
            if (args.eventIds.has(SyncUiEventDispatcher_1.SyncUiEventId.ToolActivated)) {
                newState.isActive = this.props.toolId === UiFramework_1.UiFramework.frontstages.activeToolId;
                refreshState = true;
            }
            if (!refreshState && this.props.stateSyncIds && this.props.stateSyncIds.length > 0) // eslint-disable-line deprecation/deprecation
                refreshState = this.props.stateSyncIds.some((value) => args.eventIds.has(value.toLowerCase())); // eslint-disable-line deprecation/deprecation
            if (refreshState) {
                if (this.props.stateFunc) // eslint-disable-line deprecation/deprecation
                    newState = this.props.stateFunc(newState); // eslint-disable-line deprecation/deprecation
                // istanbul ignore next
                if ((this.state.isActive !== newState.isActive) ||
                    ( /* istanbul ignore next */this.state.isEnabled !== newState.isEnabled) ||
                    ( /* istanbul ignore next */this.state.isVisible !== newState.isVisible)) {
                    this.setState({
                        isActive: newState.isActive,
                        isEnabled: newState.isEnabled,
                        isVisible: newState.isVisible,
                    });
                }
            }
        };
        this._execute = async () => {
            if (this.props.execute) {
                this.props.execute();
            }
            else {
                const thisTool = core_frontend_1.IModelApp.tools.find(this.props.toolId);
                // istanbul ignore next
                if (thisTool)
                    await (new thisTool()).run();
            }
        };
        if (props.label)
            this._label = props.label;
        else if (props.labelKey)
            this._label = UiFramework_1.UiFramework.localization.getLocalizedString(props.labelKey);
        this.state = {
            isVisible: undefined !== props.isVisible ? props.isVisible : true,
            isEnabled: undefined !== props.isEnabled ? props.isEnabled : true,
            isActive: undefined !== props.isActive ? props.isActive : false,
            isPressed: undefined !== props.isPressed ? props.isPressed : false,
        };
    }
    componentDidMount() {
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    get label() {
        return PropsHelper_1.PropsHelper.getStringFromSpec(this._label);
    }
    render() {
        if (!this.state.isVisible)
            return null;
        const icon = React.createElement(core_react_1.Icon, { iconSpec: this.props.iconSpec });
        const toolbarItemProps = (0, appui_layout_react_1.getToolbarItemProps)(this.props);
        const badge = core_react_1.BadgeUtilities.getComponentForBadgeType(this.props.badgeType);
        return (React.createElement(appui_layout_react_1.Item // eslint-disable-line deprecation/deprecation
        , { ...toolbarItemProps, className: this.props.className, style: this.props.style, isActive: this.state.isActive, isDisabled: !this.state.isEnabled, title: this.label, key: this.props.toolId, onClick: this._execute, onKeyDown: useEscapeSetFocusToHome_1.onEscapeSetFocusToHome, icon: icon, badge: badge }));
    }
}
exports.ToolButton = ToolButton;
//# sourceMappingURL=ToolButton.js.map