/*---------------------------------------------------------------------------------------------
* 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
 */
import * as React from "react";
import { Logger } from "@itwin/core-bentley";
import { BackstageItem as NZ_BackstageItem } from "@itwin/appui-layout-react";
import { withSafeArea } from "../safearea/SafeAreaContext";
import { SyncUiEventDispatcher } from "../syncui/SyncUiEventDispatcher";
import { UiFramework } from "../UiFramework";
import { PropsHelper } from "../utils/PropsHelper";
import { Backstage } from "./Backstage";
import { BackstageItemUtilities } from "./BackstageItemUtilities";
// cspell:ignore safearea
// eslint-disable-next-line @typescript-eslint/naming-convention
const BackstageItem = withSafeArea(NZ_BackstageItem); // eslint-disable-line deprecation/deprecation
/** Backstage item that launches a Command
 * @public
 * @deprecated in 3.0. Use [BackstageActionItem]($appui-abstract) instead.
 */
export 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.hasEventOfInterest(args.eventIds, this._stateSyncIds)) {
                /* istanbul ignore else */
                if (this.props.stateFunc) {
                    const newState = this.props.stateFunc(this.state);
                    /* istanbul ignore next */
                    if (!PropsHelper.isShallowEqual(newState, this.state))
                        this.setState((_prevState) => newState);
                }
            }
        };
        this.execute = () => {
            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
                Logger.logError(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.getBackstageItemStateFromProps(props);
    }
    componentDidMount() {
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        /* istanbul ignore else */
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    // deprecated class
    // istanbul ignore next
    componentDidUpdate(_prevProps) {
        // eslint-disable-next-line deprecation/deprecation
        const updatedState = BackstageItemUtilities.getBackstageItemStateFromProps(this.props);
        if (!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.getIcon(this.state.iconSpec), isActive: this.state.isActive, isDisabled: !this.state.isEnabled, key: this.props.commandId, onClick: this.execute }, this.state.label));
    }
}
//# sourceMappingURL=CommandLaunch.js.map