"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWorkflowAssignee = void 0;
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useSafePromise_1 = require("../../../hooks/useSafePromise");
var WorkflowTasksContext_1 = require("../../../contexts/WorkflowTasksContext");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var errors_1 = require("../helpers/errors");
var useWorkflowCheckPermission_1 = require("./useWorkflowCheckPermission");
var common_1 = require("../helpers/common");
var assigneeToValue = function (assignee) { return ({
    label: assignee,
    value: assignee
}); };
var filterAssignees = function (assignees, text) {
    return assignees.filter(function (item) { return item.value.toLowerCase().includes(text === null || text === void 0 ? void 0 : text.toLowerCase()); });
};
var useWorkflowAssignee = function (_a) {
    var assignee = _a.assignee, _b = _a.showCurrentAssignee, showCurrentAssignee = _b === void 0 ? true : _b, taskId = _a.taskId, isOpen = _a.isOpen, onChange = _a.onChange;
    var updateTaskState = ((0, react_1.useContext)(WorkflowTasksContext_1.WorkflowTasksContext) || {}).updateTaskState;
    var workflowPath = (0, MdmModuleContext_1.useMdmWorkflowPath)();
    var environment = (0, MdmModuleContext_1.useMdmWorkflowEnvironmentUrl)();
    var tenant = (0, MdmModuleContext_1.useMdmTenant)();
    var updateSafePromise = (0, useSafePromise_1.useSafePromise)();
    var _c = (0, react_1.useState)(null), currentAssignee = _c[0], setCurrentAssignee = _c[1];
    var _d = (0, react_1.useState)(false), updateRequestIsInProgress = _d[0], setUpdateRequestIsInProgress = _d[1];
    var checkWorkflowPermission = (0, useWorkflowCheckPermission_1.useWorkflowCheckPermission)();
    var canUpdateTask = checkWorkflowPermission('UPDATE_TASK') && (0, common_1.isOpenTask)(isOpen);
    var assigneesRequestRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(function () {
        if (showCurrentAssignee)
            setCurrentAssignee(assigneeToValue(assignee));
    }, 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [assignee]);
    var getAssignees = (0, react_1.useCallback)(function (text) {
        if (workflowPath && canUpdateTask && taskId) {
            if (!assigneesRequestRef.current) {
                assigneesRequestRef.current = (0, mdm_sdk_1.getAssigneeForTask)({
                    taskId: taskId,
                    workflowPath: workflowPath,
                    environment: environment,
                    tenant: tenant
                });
            }
            return assigneesRequestRef.current
                .then(function (result) {
                var data = result.data;
                var fetchedAssignees = data
                    .filter(function (a) { return (showCurrentAssignee ? true : a !== assignee); })
                    .sort(function (a, b) { return mdm_sdk_1.utils.strings.sort('asc', a, b); })
                    .map(assigneeToValue);
                return filterAssignees(fetchedAssignees, text);
            })
                .catch(function (e) {
                (0, errors_1.showWorkflowError)(e);
                assigneesRequestRef.current = null;
            });
        }
    }, [workflowPath, taskId, environment, tenant, canUpdateTask, assignee, showCurrentAssignee]);
    var updateAssignee = (0, react_1.useCallback)(function (newValue) {
        var assignee = newValue.value;
        if (onChange) {
            setCurrentAssignee(assigneeToValue(assignee));
            onChange(assignee);
        }
        if (!onChange && workflowPath && canUpdateTask && taskId) {
            setUpdateRequestIsInProgress(true);
            updateSafePromise((0, mdm_sdk_1.updateAssigneeForTask)({
                taskId: taskId,
                workflowPath: workflowPath,
                environment: environment,
                tenant: tenant,
                assignee: assignee
            }))
                .then(function () {
                setCurrentAssignee(assigneeToValue(assignee));
                updateTaskState === null || updateTaskState === void 0 ? void 0 : updateTaskState(taskId, { assignee: assignee });
            })
                .catch(errors_1.showWorkflowError)
                .finally(function () {
                setUpdateRequestIsInProgress(false);
            });
        }
    }, 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [workflowPath, taskId, environment, tenant, canUpdateTask, onChange]);
    return {
        assignee: currentAssignee,
        getAssignees: getAssignees,
        updateAssignee: updateAssignee,
        updateRequestIsInProgress: updateRequestIsInProgress,
        readOnly: !canUpdateTask
    };
};
exports.useWorkflowAssignee = useWorkflowAssignee;
