"use strict";
/*---------------------------------------------------------------------------------------------
* 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FrontstageLaunchBackstageItem = 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);
/** Backstage item that activates a Frontstage
 * @public
 * @deprecated in 3.0. Use [BackstageStageLauncher]($appui-abstract) instead.
 */
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_1.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_1.PropsHelper.isShallowEqual(newState, this.state))
                        this.setState((_prevState) => newState);
                }
            }
        };
        this.execute = async () => {
            Backstage_1.Backstage.hide(); // eslint-disable-line deprecation/deprecation
            const frontstageDef = await UiFramework_1.UiFramework.frontstages.getFrontstageDef(this.props.frontstageId);
            if (frontstageDef)
                await UiFramework_1.UiFramework.frontstages.setActiveFrontstageDef(frontstageDef);
            else
                core_bentley_1.Logger.logError(UiFramework_1.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_1.BackstageItemUtilities.getBackstageItemStateFromProps(props);
        /* istanbul ignore else */
        if (this.props.isActive === undefined)
            state.isActive = UiFramework_1.UiFramework.frontstages.activeFrontstageId === this.props.frontstageId;
        this.state = state;
    }
    componentDidMount() {
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            this._removeSyncUiEventListener = SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
        this._removeFrontstageActivatedEventListener = UiFramework_1.UiFramework.frontstages.onFrontstageActivatedEvent.addListener(this._handleFrontstageActivatedEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        this._removeSyncUiEventListener && this._removeSyncUiEventListener();
        this._removeFrontstageActivatedEventListener && this._removeFrontstageActivatedEventListener();
    }
    componentDidUpdate(_prevProps) {
        const updatedState = BackstageItemUtilities_1.BackstageItemUtilities.getBackstageItemStateFromProps(this.props);
        updatedState.isActive = UiFramework_1.UiFramework.frontstages.activeFrontstageId === this.props.frontstageId;
        if (!PropsHelper_1.PropsHelper.isShallowEqual(updatedState, this.state))
            this.setState((_prevState) => updatedState);
    }
    // TODO: add tooltip, 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.frontstageId, onClick: this.execute, subtitle: this.state.subtitle }, this.state.label));
    }
}
exports.FrontstageLaunchBackstageItem = FrontstageLaunchBackstageItem;
//# sourceMappingURL=FrontstageLaunch.js.map