"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWorkflowProcessInstance = void 0;
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var WorkflowTasksContext_1 = require("../../../contexts/WorkflowTasksContext");
var errors_1 = require("../helpers/errors");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var useWorkflowProcessInstance = function () {
    var _a = (0, react_1.useState)(false), loading = _a[0], setLoading = _a[1];
    var loadTasks = (0, react_1.useContext)(WorkflowTasksContext_1.WorkflowTasksContext).loadTasks;
    var workflowPath = (0, MdmModuleContext_1.useMdmWorkflowPath)();
    var environment = (0, MdmModuleContext_1.useMdmWorkflowEnvironmentUrl)();
    var tenant = (0, MdmModuleContext_1.useMdmTenant)();
    var entity = (0, MdmModuleContext_1.useMdmEntity)();
    var entityUri = entity.uri;
    var processInstance = (0, react_1.useCallback)(function (comment, taskType) {
        setLoading(true);
        return (0, mdm_sdk_1.startProcessInstance)({
            workflowPath: workflowPath,
            tenant: tenant,
            environment: environment,
            processType: taskType,
            comment: comment,
            objectURIs: [entityUri]
        })
            .then(function () {
            loadTasks();
        })
            .catch(function (error) {
            (0, errors_1.showWorkflowError)(error);
            throw error;
        })
            .finally(function () {
            setLoading(false);
        });
    }, [entityUri, environment, loadTasks, tenant, workflowPath]);
    return { processInstance: processInstance, loading: loading };
};
exports.useWorkflowProcessInstance = useWorkflowProcessInstance;
