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