"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.ActionItemButton = 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 SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const PropsHelper_1 = require("../utils/PropsHelper");
const useEscapeSetFocusToHome_1 = require("../hooks/useEscapeSetFocusToHome");
const UiFramework_1 = require("../UiFramework");
/** Helper method to set state from props */
const getItemStateFromProps = (props) => {
    // Parent Component can only modify the isEnable state if the actionItem.isEnabled value is set to true.
    return {
        isEnabled: undefined !== props.isEnabled ? props.isEnabled && props.actionItem.isEnabled : props.actionItem.isEnabled,
        isVisible: props.actionItem.isVisible,
        isActive: undefined !== props.actionItem.isActive ? props.actionItem.isActive : /* istanbul ignore next */ false,
    };
};
/** A Toolbar button React Component that executes an action defined by a CommandItemDef or a ToolItemDef.
 * @deprecated in 3.5. Use [ActionButton]($appui-abstract) instead.
 * @public
 */
class ActionItemButton extends React.Component {
    constructor(props) {
        super(props);
        this._componentUnmounting = false;
        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.actionItem.id === UiFramework_1.UiFramework.frontstages.activeToolId;
                refreshState = true;
            }
            if (!refreshState && this.props.actionItem.stateSyncIds && this.props.actionItem.stateSyncIds.length > 0) // eslint-disable-line deprecation/deprecation
                refreshState = this.props.actionItem.stateSyncIds.some((value) => args.eventIds.has(value.toLowerCase())); // eslint-disable-line deprecation/deprecation
            if (refreshState) {
                if (this.props.actionItem.stateFunc) // eslint-disable-line deprecation/deprecation
                    newState = this.props.actionItem.stateFunc(newState); // eslint-disable-line deprecation/deprecation
                if ((this.state.isActive !== newState.isActive) || (this.state.isEnabled !== newState.isEnabled) || (this.state.isVisible !== newState.isVisible)) {
                    // update actionItem as it hold the 'truth' for all state
                    /* istanbul ignore else */
                    if (undefined !== newState.isVisible) // eslint-disable-line deprecation/deprecation
                        this.props.actionItem.isVisible = newState.isVisible; // eslint-disable-line deprecation/deprecation
                    /* istanbul ignore else */
                    if (undefined !== newState.isActive)
                        this.props.actionItem.isActive = newState.isActive;
                    this.setState({
                        isActive: newState.isActive,
                        isEnabled: newState.isEnabled,
                        isVisible: newState.isVisible,
                    });
                }
            }
        };
        this._execute = () => {
            // istanbul ignore else
            if (this.props.actionItem.execute) {
                this.props.actionItem.execute();
            }
        };
        this.state = getItemStateFromProps(props);
    }
    /** @internal */
    static getDerivedStateFromProps(props, state) {
        const updatedState = getItemStateFromProps(props);
        // istanbul ignore else
        if (!PropsHelper_1.PropsHelper.isShallowEqual(updatedState, state))
            return updatedState;
        return null;
    }
    /** @internal */
    componentDidMount() {
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    /** @internal */
    componentWillUnmount() {
        this._componentUnmounting = true;
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    /** @internal */
    render() {
        if (!this.state.isVisible)
            return null;
        const { actionItem, ...props } = this.props;
        const icon = React.createElement(core_react_1.Icon, { iconSpec: actionItem.iconSpec });
        const badge = core_react_1.BadgeUtilities.getComponentForBadgeType(actionItem.badgeType);
        return (React.createElement(appui_layout_react_1.Item // eslint-disable-line deprecation/deprecation
        , { ...props, isActive: this.state.isActive, isDisabled: !this.state.isEnabled, title: actionItem.label, key: actionItem.getKey(), onClick: this._execute, onKeyDown: useEscapeSetFocusToHome_1.onEscapeSetFocusToHome, icon: icon, onSizeKnown: this.props.onSizeKnown, badge: badge }));
    }
}
exports.ActionItemButton = ActionItemButton;
//# sourceMappingURL=ActionItemButton.js.map