/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable deprecation/deprecation */
/** @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);
/** Backstage item that activates a Frontstage
 * @public
 * @deprecated in 3.0. Use [BackstageStageLauncher]($appui-abstract) instead.
 */
export class FrontstageLaunchBackstageItem 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 else */
                    if (!PropsHelper.isShallowEqual(newState, this.state))
                        this.setState((_prevState) => newState);
                }
            }
        };
        this.execute = async () => {
            Backstage.hide(); // eslint-disable-line deprecation/deprecation
            const frontstageDef = await UiFramework.frontstages.getFrontstageDef(this.props.frontstageId);
            if (frontstageDef)
                await UiFramework.frontstages.setActiveFrontstageDef(frontstageDef);
            else
                Logger.logError(UiFramework.loggerCategory(this), `Frontstage with id '${this.props.frontstageId}' not found`);
        };
        this._handleFrontstageActivatedEvent = (args) => {
            const isActive = args.activatedFrontstageDef.id === this.props.frontstageId;
            /* istanbul ignore else */
            if (isActive !== this.state.isActive)
                this.setState({ isActive });
        };
        if (props.stateSyncIds)
            this._stateSyncIds = props.stateSyncIds.map((value) => value.toLowerCase());
        const state = BackstageItemUtilities.getBackstageItemStateFromProps(props);
        /* istanbul ignore else */
        if (this.props.isActive === undefined)
            state.isActive = UiFramework.frontstages.activeFrontstageId === this.props.frontstageId;
        this.state = state;
    }
    componentDidMount() {
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            this._removeSyncUiEventListener = SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
        this._removeFrontstageActivatedEventListener = UiFramework.frontstages.onFrontstageActivatedEvent.addListener(this._handleFrontstageActivatedEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        this._removeSyncUiEventListener && this._removeSyncUiEventListener();
        this._removeFrontstageActivatedEventListener && this._removeFrontstageActivatedEventListener();
    }
    componentDidUpdate(_prevProps) {
        const updatedState = BackstageItemUtilities.getBackstageItemStateFromProps(this.props);
        updatedState.isActive = UiFramework.frontstages.activeFrontstageId === this.props.frontstageId;
        if (!PropsHelper.isShallowEqual(updatedState, this.state))
            this.setState((_prevState) => updatedState);
    }
    // TODO: add tooltip, 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.frontstageId, onClick: this.execute, subtitle: this.state.subtitle }, this.state.label));
    }
}
//# sourceMappingURL=FrontstageLaunch.js.map