"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWorkflowComments = 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 errors_1 = require("../helpers/errors");
var common_1 = require("../helpers/common");
var useWorkflowCheckPermission_1 = require("./useWorkflowCheckPermission");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var useWorkflowComments = function (_a) {
    var processInstanceComments = _a.processInstanceComments, taskId = _a.taskId, isOpen = _a.isOpen, onCommentWrite = _a.onCommentWrite;
    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 safePromise = (0, useSafePromise_1.useSafePromise)();
    var _b = (0, react_1.useState)([]), comments = _b[0], setComments = _b[1];
    var _c = (0, react_1.useState)(false), requestIsInProgress = _c[0], setRequestIsInProgress = _c[1];
    var checkWorkflowPermission = (0, useWorkflowCheckPermission_1.useWorkflowCheckPermission)();
    var canUpdateTask = checkWorkflowPermission('UPDATE_TASK') && (0, common_1.isOpenTask)(isOpen);
    (0, react_1.useEffect)(function () {
        setComments(processInstanceComments || []);
    }, [processInstanceComments]);
    var writeComment = (0, react_1.useCallback)(function (comment) {
        if (canUpdateTask) {
            setRequestIsInProgress(true);
            safePromise((0, mdm_sdk_1.addCommentForTask)({
                workflowPath: workflowPath,
                environment: environment,
                tenant: tenant,
                taskId: taskId,
                comment: comment
            }).then(function () {
                return (0, mdm_sdk_1.getTaskById)({
                    workflowPath: workflowPath,
                    environment: environment,
                    tenant: tenant,
                    taskId: taskId
                });
            }))
                .then(function (_a) {
                var processInstanceComments = _a.processInstanceComments;
                setComments(processInstanceComments || []);
                updateTaskState === null || updateTaskState === void 0 ? void 0 : updateTaskState(taskId, { processInstanceComments: processInstanceComments });
                onCommentWrite === null || onCommentWrite === void 0 ? void 0 : onCommentWrite({ processInstanceComments: processInstanceComments });
            })
                .catch(errors_1.showWorkflowError)
                .finally(function () { return setRequestIsInProgress(false); });
        }
    }, [environment, taskId, tenant, workflowPath, safePromise, updateTaskState, canUpdateTask, onCommentWrite]);
    return { comments: comments, requestIsInProgress: requestIsInProgress, writeComment: writeComment, readOnly: !canUpdateTask };
};
exports.useWorkflowComments = useWorkflowComments;
