"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 Backstage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandLaunchBackstageItem = void 0;
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const SafeAreaContext_1 = require("../safearea/SafeAreaContext");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../UiFramework");
const PropsHelper_1 = require("../utils/PropsHelper");
const Backstage_1 = require("./Backstage");
const BackstageItemUtilities_1 = require("./BackstageItemUtilities");
// cspell:ignore safearea
// eslint-disable-next-line @typescript-eslint/naming-convention
const BackstageItem = (0, SafeAreaContext_1.withSafeArea)(appui_layout_react_1.BackstageItem); // eslint-disable-line deprecation/deprecation
/** Backstage item that launches a Command
 * @public
 * @deprecated in 3.0. Use [BackstageActionItem]($appui-abstract) instead.
 */
class CommandLaunchBackstageItem extends React.PureComponent {
    constructor(props) {
        super(props);
        this._componentUnmounting = false; // used to ensure _handleSyncUiEvent callback is not processed after componentWillUnmount is called
        this._stateSyncIds = []; // local version of syncId that are lower cased
        this._handleSyncUiEvent = (args) => {
            /* istanbul ignore next */
            if (this._componentUnmounting)
                return;
            /* istanbul ignore else */
            if (SyncUiEventDispatcher_1.SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, this._stateSyncIds)) {
                /* istanbul ignore else */
                if (this.props.stateFunc) {
                    const newState = this.props.stateFunc(this.state);
                    /* istanbul ignore next */
                    if (!PropsHelper_1.PropsHelper.isShallowEqual(newState, this.state))
                        this.setState((_prevState) => newState);
                }
            }
        };
        this.execute = () => {
            Backstage_1.Backstage.hide(); // eslint-disable-line deprecation/deprecation
            if (this.props.execute) {
                if (this.props.getCommandArgs)
                    this.props.execute(this.props.getCommandArgs());
                else
                    this.props.execute(this.props.parameters);
            }
            else
                core_bentley_1.Logger.logError(UiFramework_1.UiFramework.loggerCategory(this), `'${this.props.commandId}' has no execute() function`);
        };
        if (props.stateSyncIds)
            this._stateSyncIds = props.stateSyncIds.map((value) => value.toLowerCase());
        // eslint-disable-next-line deprecation/deprecation
        this.state = BackstageItemUtilities_1.BackstageItemUtilities.getBackstageItemStateFromProps(props);
    }
    componentDidMount() {
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        /* istanbul ignore else */
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    // deprecated class
    // istanbul ignore next
    componentDidUpdate(_prevProps) {
        // eslint-disable-next-line deprecation/deprecation
        const updatedState = BackstageItemUtilities_1.BackstageItemUtilities.getBackstageItemStateFromProps(this.props);
        if (!PropsHelper_1.PropsHelper.isShallowEqual(updatedState, this.state))
            this.setState((_prevState) => updatedState);
    }
    // TODO: add tooltip, subtitle, aria-label? to NZ_BackstageItem
    render() {
        return (React.createElement(BackstageItem, { icon: PropsHelper_1.PropsHelper.getIcon(this.state.iconSpec), isActive: this.state.isActive, isDisabled: !this.state.isEnabled, key: this.props.commandId, onClick: this.execute }, this.state.label));
    }
}
exports.CommandLaunchBackstageItem = CommandLaunchBackstageItem;
//# sourceMappingURL=CommandLaunch.js.map