/*---------------------------------------------------------------------------------------------
* 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
 */
/* eslint-disable deprecation/deprecation */
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 { WorkflowManager } from "../workflow/Workflow";
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 Task
 * @internal
 * @deprecated in 3.0. UI 1.0 will be removed in AppUi 4.0.
 */
export class TaskLaunchBackstageItem 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 = () => {
            Backstage.hide(); // eslint-disable-line deprecation/deprecation
            const workflow = WorkflowManager.findWorkflow(this.props.workflowId);
            if (workflow) {
                const task = workflow.getTask(this.props.taskId);
                if (task)
                    WorkflowManager.setActiveWorkflowAndTask(workflow, task); // eslint-disable-line @typescript-eslint/no-floating-promises
                else
                    Logger.logError(UiFramework.loggerCategory(this), `Task with id '${this.props.taskId}' not found`);
            }
            else
                Logger.logError(UiFramework.loggerCategory(this), `Workflow with id '${this.props.workflowId}' not found`);
        };
        this._handleTaskActivatedEvent = (args) => {
            const isActive = args.taskId === this.props.taskId && args.workflowId === this.props.workflowId;
            /* 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 = WorkflowManager.activeTaskId === this.props.taskId && WorkflowManager.activeWorkflowId === this.props.workflowId;
        this.state = state;
    }
    componentDidMount() {
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
        WorkflowManager.onTaskActivatedEvent.addListener(this._handleTaskActivatedEvent);
    }
    componentWillUnmount() {
        this._componentUnmounting = true;
        if (this.props.stateFunc && this._stateSyncIds.length > 0)
            SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
        WorkflowManager.onTaskActivatedEvent.removeListener(this._handleTaskActivatedEvent);
    }
    get id() {
        return `${this.props.workflowId}:${this.props.taskId}`;
    }
    componentDidUpdate(_prevProps) {
        const updatedState = BackstageItemUtilities.getBackstageItemStateFromProps(this.props);
        updatedState.isActive = WorkflowManager.activeTaskId === this.props.taskId && WorkflowManager.activeWorkflowId === this.props.workflowId;
        if (!PropsHelper.isShallowEqual(updatedState, this.state))
            this.setState((_prevState) => updatedState);
    }
    render() {
        return (React.createElement(BackstageItem, { icon: PropsHelper.getIcon(this.state.iconSpec), isActive: this.state.isActive, isDisabled: !this.state.isEnabled, key: this.id, onClick: this.execute }, this.state.label));
    }
}
//# sourceMappingURL=TaskLaunch.js.map