/*---------------------------------------------------------------------------------------------
* 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 { BadgeUtilities, Icon } from "@itwin/core-react";
import { Item } from "@itwin/appui-layout-react";
import { SyncUiEventDispatcher, SyncUiEventId } from "../syncui/SyncUiEventDispatcher";
import { PropsHelper } from "../utils/PropsHelper";
import { onEscapeSetFocusToHome } from "../hooks/useEscapeSetFocusToHome";
import { UiFramework } from "../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
 */
export 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(SyncUiEventId.ToolActivated)) {
                newState.isActive = this.props.actionItem.id === 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.isShallowEqual(updatedState, state))
            return updatedState;
        return null;
    }
    /** @internal */
    componentDidMount() {
        SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    /** @internal */
    componentWillUnmount() {
        this._componentUnmounting = true;
        SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    /** @internal */
    render() {
        if (!this.state.isVisible)
            return null;
        const { actionItem, ...props } = this.props;
        const icon = React.createElement(Icon, { iconSpec: actionItem.iconSpec });
        const badge = BadgeUtilities.getComponentForBadgeType(actionItem.badgeType);
        return (React.createElement(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: onEscapeSetFocusToHome, icon: icon, onSizeKnown: this.props.onSizeKnown, badge: badge }));
    }
}
//# sourceMappingURL=ActionItemButton.js.map