import React, { useCallback, useContext, useMemo, useState } from 'react';
import { isNil } from 'ramda';
import { isDCRUri } from '@reltio/mdm-sdk';
import DcrTaskIcon from '../../../../../icons/DcrTaskIcon';
import { useMdmEntity, useMdmReviewDCREnabled, useMdmUsername } from '../../../../../contexts/MdmModuleContext';
import { GenericWorkflowTaskCard } from '../GenericWorkflowTaskCard';
import { AttributesChanges } from '../../../AttributesChanges';
import { LineDecorator } from '../../../LineDecorator';
import { useChangeRequest } from '../../../hooks/useChangeRequest';
import { getDCRUri } from '../../../helpers/common';
import { ReviewDCRDialog } from '../../../ReviewDCRDialog';
import { ReviewDCRButton } from '../../../ReviewDCRButton';
import { ChangeRequestEditorProvider } from '../../../contexts/ChangeRequestEditorContext';
import { WorkflowTasksContext } from '../../../../../contexts/WorkflowTasksContext';
import { ScrollToElementProvider } from '../../../../../contexts/ScrollToElementContext';
export var DataChangeRequestTaskCard = function (_a) {
    var task = _a.task;
    var reviewDCREnabledByEntityType = (useContext(WorkflowTasksContext) || {}).reviewDCR;
    var reviewDCREnabled = useMdmReviewDCREnabled();
    var entity = useMdmEntity();
    var isDCREntity = isDCRUri(entity === null || entity === void 0 ? void 0 : entity.uri);
    var isReviewDCREnabled = isNil(reviewDCREnabledByEntityType) ? reviewDCREnabled : reviewDCREnabledByEntityType;
    var username = useMdmUsername();
    var _b = useState(false), isReviewDCRDialogOpen = _b[0], setIsReviewDCRDialogOpen = _b[1];
    var objectURIs = task.objectURIs, assignee = task.assignee;
    var dcrUri = getDCRUri(objectURIs);
    var _c = useChangeRequest(dcrUri), dcr = _c.dcr, groupedObjects = _c.groupedObjects;
    var handleReviewDCR = useCallback(function () {
        setIsReviewDCRDialogOpen(true);
    }, []);
    var handleCloseReviewDCRDialog = useCallback(function () {
        setIsReviewDCRDialogOpen(false);
    }, []);
    var showReviewDCRButton = !isDCREntity && isReviewDCREnabled && assignee == username;
    var showReviewDialog = !isDCREntity && isReviewDCRDialogOpen && (showReviewDCRButton || isReviewDCREnabled);
    var customActionsSlot = useMemo(function () { return showReviewDCRButton && React.createElement(ReviewDCRButton, { onClick: handleReviewDCR }); }, [showReviewDCRButton, handleReviewDCR]);
    return (React.createElement(React.Fragment, null,
        React.createElement(GenericWorkflowTaskCard, { task: task, Icon: DcrTaskIcon, customActionsSlot: customActionsSlot }, dcr &&
            groupedObjects.map(function (object, index) { return (React.createElement(LineDecorator, { key: index, last: index === groupedObjects.length - 1, divider: true },
                React.createElement(AttributesChanges, { entityInfo: object.entity, relationsInfo: object.relations, changes: dcr.changes, objectsInfo: dcr.objectsInfo, dcrUri: dcrUri }))); })),
        showReviewDialog && (React.createElement(ChangeRequestEditorProvider, null,
            React.createElement(ScrollToElementProvider, null,
                React.createElement(ReviewDCRDialog, { open: isReviewDCRDialogOpen, onClose: handleCloseReviewDCRDialog, dcr: dcr, groupedObjects: groupedObjects, task: task }))))));
};
