var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React, { useEffect, useState } from 'react';
import i18n from 'ui-i18n';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import Typography from '@mui/material/Typography';
import Divider from '@mui/material/Divider';
import { updateAssigneeForTask } from '@reltio/mdm-sdk';
import { AssigneeSelector } from '../../../AssigneeSelector';
import { useWorkflowAssignee } from '../../../hooks/useWorkflowAssignee';
import { useMdmTenant, useMdmWorkflowEnvironmentUrl, useMdmWorkflowPath } from '../../../../../contexts/MdmModuleContext';
import { LinearLoadIndicator } from '../../../../../LinearLoadIndicator';
import { showWorkflowError } from '../../../helpers/errors';
import { useStyles } from './styles';
export var ReassignDialog = function (_a) {
    var isOpen = _a.isOpen, onClose = _a.onClose, task = _a.task, onChangeTask = _a.onChangeTask;
    var styles = useStyles();
    var workflowPath = useMdmWorkflowPath();
    var environment = useMdmWorkflowEnvironmentUrl();
    var tenant = useMdmTenant();
    var _b = useState(true), loading = _b[0], setLoading = _b[1];
    var _c = task || {}, currentAssignee = _c.assignee, taskId = _c.taskId;
    var _d = useState(null), assignee = _d[0], setAssignee = _d[1];
    var handleClose = function () {
        setAssignee(null);
        onClose();
    };
    var readOnly = useWorkflowAssignee({
        taskId: taskId,
        assignee: '',
        isOpen: task === null || task === void 0 ? void 0 : task.isOpen
    }).readOnly;
    var handleReassign = function () {
        if (!readOnly) {
            setLoading(true);
            updateAssigneeForTask({
                taskId: taskId,
                workflowPath: workflowPath,
                environment: environment,
                tenant: tenant,
                assignee: assignee
            })
                .then(function () {
                onChangeTask(__assign(__assign({}, task), { assignee: assignee }));
                handleClose();
            })
                .catch(showWorkflowError)
                .finally(function () {
                setLoading(false);
            });
        }
    };
    useEffect(function () {
        if (task) {
            setLoading(false);
        }
    }, [task]);
    return (React.createElement(Dialog, { open: isOpen, onClose: handleClose },
        React.createElement(DialogTitle, null, i18n.text('Reassign task')),
        React.createElement(DialogContent, { className: styles.dialogContent },
            loading && React.createElement(LinearLoadIndicator, null),
            React.createElement("div", { className: styles.dialogText },
                React.createElement(Typography, { className: styles.caption }, i18n.text('Assigned to')),
                React.createElement(Typography, { className: styles.currentAssignee }, currentAssignee)),
            !readOnly && (React.createElement(AssigneeSelector, { taskId: taskId, isTaskOpen: isOpen, label: i18n.text('Reassign to'), assignee: currentAssignee, showCurrentAssignee: false, onChange: setAssignee }))),
        React.createElement(Divider, null),
        React.createElement(DialogActions, null,
            React.createElement(Button, { onClick: handleClose }, i18n.text('Cancel')),
            React.createElement(Button, { color: "primary", onClick: handleReassign, disabled: readOnly || !assignee || loading }, i18n.text('Reassign')))));
};
