"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 WorkflowTask
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowManager = exports.TaskActivatedEvent = exports.WorkflowActivatedEvent = exports.Workflow = void 0;
/* eslint-disable deprecation/deprecation */
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const ItemDefBase_1 = require("../shared/ItemDefBase");
const UiFramework_1 = require("../UiFramework");
const Task_1 = require("./Task");
/** Workflow class.
 * A Workflow is a defined sequence of tasks used to accomplish a goal.
 * @internal
 * @deprecated in 3.0. Workflow is obsolete.
 */
class Workflow extends ItemDefBase_1.ItemDefBase {
    constructor(workflowDef) {
        super(workflowDef);
        /** Active Task Id */
        this.activeTaskId = null;
        this._tasks = new Map();
        this.workflowId = workflowDef.id;
        this.defaultTaskId = workflowDef.defaultTaskId;
        this._taskIds = workflowDef.tasks;
        this.isDefault = (workflowDef.isDefault !== undefined) ? workflowDef.isDefault : true;
        this._taskIds.map((taskId, _index) => {
            const task = Task_1.TaskManager.findTask(taskId);
            // istanbul ignore else
            if (task)
                this._tasks.set(taskId, task);
        });
    }
    /** Gets the Id of the Workflow. */
    get id() {
        return this.workflowId;
    }
    /** Gets the active Task. */
    get activeTask() {
        if (!this.activeTaskId)
            return undefined;
        return this._tasks.get(this.activeTaskId);
    }
    /** Gets a Task with a given Id.
     * @param taskId Id of the Task to get
     */
    getTask(taskId) {
        return this._tasks.get(taskId);
    }
    /** Gets the last active Task. If no Task is active, it returns the default Task.
     */
    get lastActiveTask() {
        if (!this.activeTaskId) {
            return this.getTask(this.defaultTaskId);
        }
        return this.getTask(this.activeTaskId);
    }
    /** Determines if the Workflow is active.
     */
    get isActive() {
        let isActive = false;
        if (WorkflowManager.activeWorkflow === this)
            isActive = true;
        return isActive;
    }
    set isActive(_) {
        // do nothing - needed because subclassing from ItemDefBase
    }
    /** Sets a Task as active.
     * @param task  The Task to set as active
     */
    async setActiveTask(task) {
        this.activeTaskId = task.taskId;
        await task.onActivated();
        WorkflowManager.onTaskActivatedEvent.emit({ task, taskId: task.id, workflow: this, workflowId: this.id });
    }
    /** Gets an array of sorted Tasks in the Workflow. */
    getSortedTasks() {
        const sortedTasks = new Array();
        for (const key of this._tasks.keys()) {
            const task = this._tasks.get(key);
            // istanbul ignore else
            if (task && task.isVisible) // eslint-disable-line deprecation/deprecation
                sortedTasks.push(task);
        }
        sortedTasks.sort((a, b) => {
            return a.label.localeCompare(b.label);
        });
        return sortedTasks;
    }
}
exports.Workflow = Workflow;
/** Workflow Activated Event class.
 * @internal
 * @deprecated in 3.0. Workflow is obsolete.
 */
class WorkflowActivatedEvent extends core_react_1.UiEvent {
}
exports.WorkflowActivatedEvent = WorkflowActivatedEvent;
/** Task Activated Event class.
 * @internal
 * @deprecated in 3.0. Task is obsolete.
 */
class TaskActivatedEvent extends core_react_1.UiEvent {
}
exports.TaskActivatedEvent = TaskActivatedEvent;
/** Workflow Manager class.
 * @internal
 * @deprecated in 3.0. WorkflowManager is obsolete.
 */
class WorkflowManager {
    /** Loads one or more Workflows.
     * @param workflowPropsList  the list of Workflows to load
     */
    static loadWorkflows(workflowPropsList) {
        this.setDefaultWorkflowId(workflowPropsList.defaultWorkflowId);
        WorkflowManager.loadWorkflowDefs(workflowPropsList.workflows);
    }
    static loadWorkflowDefs(workflowDefs) {
        workflowDefs.map((workflowProps) => {
            WorkflowManager.loadWorkflow(workflowProps);
        });
    }
    /** Loads a Workflow.
     * @param workflowProps  Properties of the Workflow to load
     */
    static loadWorkflow(workflowProps) {
        const workflow = new Workflow(workflowProps);
        WorkflowManager.addWorkflow(workflowProps.id, workflow);
    }
    /** Adds a Workflow.
     * @param workflowId  Id of the Workflow to add
     * @param workflow    The Workflow to add
     */
    static addWorkflow(workflowId, workflow) {
        this._workflows.set(workflowId, workflow);
    }
    /** Finds a Workflow with a given Id.
     * @param workflowId  Id of the Workflow to find
     * @returns The Workflow if found, or undefined if not found
     */
    static findWorkflow(workflowId) {
        return this._workflows.get(workflowId);
    }
    /** Sets the active Workflow
     * @param workflow  The Workflow to set as active
     */
    static setActiveWorkflow(workflow) {
        this._activeWorkflow = workflow;
        WorkflowManager.onWorkflowActivatedEvent.emit({ workflow, workflowId: workflow ? workflow.id : undefined });
    }
    /** Sets the active Workflow and Task
     * @param workflow  The Workflow to set as active
     * @param task      The Task to set as active
     */
    static async setActiveWorkflowAndTask(workflow, task) {
        // istanbul ignore else
        if (!workflow.isActive)
            this.setActiveWorkflow(workflow);
        // istanbul ignore else
        if (!task.isActive)
            await workflow.setActiveTask(task);
    }
    /** Gets the active Workflow */
    static get activeWorkflow() {
        return this._activeWorkflow;
    }
    /** Gets the active Workflow id */
    static get activeWorkflowId() {
        if (this._activeWorkflow !== undefined)
            return this._activeWorkflow.id;
        return "";
    }
    /** Gets the active Task */
    static get activeTask() {
        if (this._activeWorkflow !== undefined)
            return this._activeWorkflow.activeTask;
        return undefined;
    }
    /** Gets the active Task id */
    static get activeTaskId() {
        if (this._activeWorkflow !== undefined) {
            // istanbul ignore else
            if (this._activeWorkflow.activeTask)
                return this._activeWorkflow.activeTask.id;
        }
        return "";
    }
    /** Gets the Id of the default Workflow */
    static get defaultWorkflowId() {
        return this._defaultWorkflowId;
    }
    /** Sets the Id of the default Workflow */
    static setDefaultWorkflowId(id) {
        this._defaultWorkflowId = id;
    }
    /** Removes a Workflow
     * @param workflow  The Workflow to remove
     */
    static removeWorkflow(workflow) {
        const id = workflow.id;
        let result = false;
        if (this._workflows.get(id)) {
            this._workflows.delete(id);
            result = true;
        }
        else {
            core_bentley_1.Logger.logError(UiFramework_1.UiFramework.loggerCategory(this), `removeWorkflow: Workflow with id '${id}' not found`);
            result = false;
        }
        return result;
    }
    static getSortedWorkflows() {
        const sortedWorkflows = new Array();
        for (const key of this._workflows.keys()) {
            const workflow = this._workflows.get(key);
            // istanbul ignore else
            if (workflow)
                sortedWorkflows.push(workflow);
        }
        sortedWorkflows.sort((a, b) => {
            return a.label.localeCompare(b.label);
        });
        return sortedWorkflows;
    }
}
exports.WorkflowManager = WorkflowManager;
WorkflowManager._workflows = new Map();
/** Get Workflow Activated event. */
WorkflowManager.onWorkflowActivatedEvent = new WorkflowActivatedEvent();
/** Get Task Activated event. */
WorkflowManager.onTaskActivatedEvent = new TaskActivatedEvent();
//# sourceMappingURL=Workflow.js.map