/*---------------------------------------------------------------------------------------------
* 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
 */
import * as React from "react";
import { IModelApp } from "@itwin/core-frontend";
import { BadgeUtilities, Icon } from "@itwin/core-react";
import { getToolbarItemProps, Item } from "@itwin/appui-layout-react";
import { SyncUiEventDispatcher, SyncUiEventId } from "../syncui/SyncUiEventDispatcher";
import { UiFramework } from "../UiFramework";
import { PropsHelper } from "../utils/PropsHelper";
import { onEscapeSetFocusToHome } from "../hooks/useEscapeSetFocusToHome";
/** Tool Button React Component.
 * @deprecated in 3.5. Use [CommonToolbarItem]($appui-abstract) instead.
 * @public
 */
export 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(SyncUiEventId.ToolActivated)) {
                newState.isActive = this.props.toolId === 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 = 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.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.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    get label() {
        return PropsHelper.getStringFromSpec(this._label);
    }
    render() {
        if (!this.state.isVisible)
            return null;
        const icon = React.createElement(Icon, { iconSpec: this.props.iconSpec });
        const toolbarItemProps = getToolbarItemProps(this.props);
        const badge = BadgeUtilities.getComponentForBadgeType(this.props.badgeType);
        return (React.createElement(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: onEscapeSetFocusToHome, icon: icon, badge: badge }));
    }
}
//# sourceMappingURL=ToolButton.js.map