"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useWorkflowPriority = void 0;
var react_1 = require("react");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
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 useWorkflowPriority = function (_a) {
    var priority = _a.priority, taskId = _a.taskId, isOpen = _a.isOpen;
    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)(false), updateRequestIsInProgress = _b[0], setUpdateRequestIsInProgress = _b[1];
    var checkWorkflowPermission = (0, useWorkflowCheckPermission_1.useWorkflowCheckPermission)();
    var canUpdateTask = checkWorkflowPermission('UPDATE_TASK') && (0, common_1.isOpenTask)(isOpen);
    var updatePriority = (0, react_1.useCallback)(function (newValue) {
        var newPriority = newValue.value;
        if (workflowPath && canUpdateTask && taskId && priority !== newPriority) {
            setUpdateRequestIsInProgress(true);
            safePromise((0, mdm_sdk_1.updatePriorityForTask)({
                taskId: taskId,
                workflowPath: workflowPath,
                environment: environment,
                tenant: tenant,
                priority: newPriority
            }))
                .then(function () {
                updateTaskState === null || updateTaskState === void 0 ? void 0 : updateTaskState(taskId, { priority: newPriority });
            })
                .catch(errors_1.showWorkflowError)
                .finally(function () {
                setUpdateRequestIsInProgress(false);
            });
        }
    }, [workflowPath, taskId, environment, tenant, updateTaskState, safePromise, priority, canUpdateTask]);
    var priorities = (0, react_1.useMemo)(function () { return [
        {
            label: ui_i18n_1.default.text('Low'),
            value: mdm_sdk_1.WorkflowPriority.low
        },
        {
            label: ui_i18n_1.default.text('Medium'),
            value: mdm_sdk_1.WorkflowPriority.medium
        },
        {
            label: ui_i18n_1.default.text('High'),
            value: mdm_sdk_1.WorkflowPriority.high
        },
        {
            label: ui_i18n_1.default.text('Urgent'),
            value: mdm_sdk_1.WorkflowPriority.urgent
        }
    ]; }, []);
    return { priorities: priorities, updatePriority: updatePriority, updateRequestIsInProgress: updateRequestIsInProgress, readOnly: !canUpdateTask };
};
exports.useWorkflowPriority = useWorkflowPriority;
