"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWorkflowTasks = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useSafePromise_1 = require("../../../hooks/useSafePromise");
var useWorkflowCheckPermission_1 = require("./useWorkflowCheckPermission");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var common_1 = require("../helpers/common");
var useWorkflowTasks = function (config) {
    var _a = config || {}, processTypes = _a.processTypes, assignee = _a.assignee, showClosedTasks = _a.showClosedTasks;
    var _b = (0, react_1.useState)(null), tasks = _b[0], setTasks = _b[1];
    var _c = (0, react_1.useState)(0), total = _c[0], setTotal = _c[1];
    var _d = (0, react_1.useState)(false), loading = _d[0], setLoading = _d[1];
    var safePromise = (0, useSafePromise_1.useSafePromise)();
    var checkWorkflowPermission = (0, useWorkflowCheckPermission_1.useWorkflowCheckPermission)();
    var workflowPath = (0, MdmModuleContext_1.useMdmWorkflowPath)();
    var environment = (0, MdmModuleContext_1.useMdmWorkflowEnvironmentUrl)();
    var isWorkflowEnabled = (0, MdmModuleContext_1.useMdmIsWorkflowEnabled)();
    var tenant = (0, MdmModuleContext_1.useMdmTenant)();
    var entityUri = (0, MdmModuleContext_1.useMdmEntityUri)();
    var isEditableMode = (0, MdmModuleContext_1.useMdmIsEditableMode)();
    var entityLastLoadedTime = (0, MdmModuleContext_1.useMdmProfileLastLoadedTime)();
    var canViewTasks = checkWorkflowPermission('VIEW_TASKS');
    var enabled = !!(workflowPath && environment) && isWorkflowEnabled && canViewTasks;
    var loadTasks = (0, react_1.useCallback)(function () {
        setLoading(true);
        var parsedProcessTypes = !Array.isArray(processTypes) || processTypes.includes('*') ? null : processTypes;
        var requests = (parsedProcessTypes || [null]).map(function (processType) {
            return (0, mdm_sdk_1.getTasksForEntity)({
                entityUri: (0, mdm_sdk_1.isDCRUri)(entityUri) ? (0, mdm_sdk_1.getEntityUriFromDCRUri)(entityUri) : entityUri,
                workflowPath: workflowPath,
                environment: environment,
                tenant: tenant,
                filter: { assignee: assignee, processType: processType },
                includeClosed: !!showClosedTasks
            });
        });
        safePromise(Promise.all(requests))
            .then(function (responses) {
            var tasks = (0, ramda_1.pipe)((0, ramda_1.map)((0, ramda_1.prop)('data')), (0, ramda_1.reject)(ramda_1.isNil), ramda_1.flatten, (0, ramda_1.uniqBy)((0, ramda_1.prop)('taskId')), (0, ramda_1.filter)((0, common_1.isRequestedTask)(parsedProcessTypes, assignee)))(responses);
            setTasks(tasks);
            setTotal(tasks.length);
        })
            .catch(function (error) {
            console.warn('Workflow error', error);
        })
            .finally(function () {
            setLoading(false);
        });
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [
        entityUri,
        environment,
        safePromise,
        tenant,
        workflowPath,
        assignee,
        showClosedTasks,
        processTypes,
        entityLastLoadedTime
    ]);
    (0, react_1.useEffect)(function () {
        if (entityUri && enabled && !(0, mdm_sdk_1.isTempUri)(entityUri)) {
            loadTasks();
        }
        else {
            setTasks(null);
            setTotal(0);
        }
    }, [enabled, entityUri, loadTasks, isEditableMode]);
    var updateTaskState = (0, react_1.useCallback)(function (taskId, payload) {
        setTasks((0, ramda_1.map)((0, ramda_1.when)((0, ramda_1.propEq)('taskId', taskId), (0, ramda_1.mergeLeft)(payload))));
    }, []);
    return {
        tasks: tasks,
        total: total,
        loading: loading,
        updateTaskState: updateTaskState,
        loadTasks: loadTasks
    };
};
exports.useWorkflowTasks = useWorkflowTasks;
