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, useRef, useState, memo } from 'react';
import i18n from 'ui-i18n';
import { useDraggable } from '@dnd-kit/core';
import { eqCrosswalks, getLastUriPart, getSourceSystemLabel, isCrosswalkDisabled } from '@reltio/mdm-sdk';
import Typography from '@mui/material/Typography';
import DeleteIcon from '@mui/icons-material/Delete';
import VisibilityIcon from '@mui/icons-material/Visibility';
import { useCrosswalkColor, useCrosswalkFocus, useCrosswalkHighlight, useHighlightedCrosswalks } from '../../../contexts/CrosswalksDisplayContext';
import { useMdmMetadata } from '../../../contexts/MdmModuleContext';
import { Spacer } from '../../../Spacer';
import { ArrowExpandButton } from '../../../ArrowExpandButton';
import { ExpandedValueTooltip } from '../../../ExpandedValueTooltip';
import { ConfirmDeleteDialog } from '../../../ConfirmDeleteDialog';
import { SmallIconButtonWithTooltip } from '../../../SmallIconButton';
import { SourceIcon } from '../../../SourceIcon';
import DependentIcon from '../../../icons/NestedAttribute';
import { ColoredBlock } from '../ColoredBlock';
import { CrosswalkAttributes } from '../CrosswalkAttributes';
import { useStyles } from './styles';
export var CrosswalkRow = memo(function (_a) {
    var crosswalk = _a.crosswalk, isDependent = _a.isDependent, _b = _a.isEmpty, isEmpty = _b === void 0 ? false : _b, _c = _a.isColored, isColored = _c === void 0 ? true : _c, _d = _a.focusable, focusable = _d === void 0 ? true : _d, disableEdit = _a.disableEdit, disableDelete = _a.disableDelete, onDelete = _a.onDelete, onEdit = _a.onEdit;
    var ref = useRef(null);
    var _e = useState(false), expanded = _e[0], setExpanded = _e[1];
    var _f = useState(false), isDeleteDialogOpen = _f[0], setDeleteDialogOpen = _f[1];
    var metadata = useMdmMetadata();
    var crosswalkColor = useCrosswalkColor(crosswalk);
    var _g = useCrosswalkFocus(crosswalk), toggleCrosswalkFocus = _g.toggleCrosswalkFocus, isFocused = _g.isFocused;
    var _h = useCrosswalkHighlight(crosswalk), isDimmed = _h.isDimmed, highlightCrosswalk = _h.highlightCrosswalk, removeHighlight = _h.removeHighlight;
    var scrollToCrosswalk = useHighlightedCrosswalks()[0];
    var coloredCrosswalk = isDimmed ? 'rgba(0, 0, 0, 0.12)' : crosswalkColor;
    var color = isEmpty ? '#FFF' : isColored ? coloredCrosswalk : '#CACCD1';
    var sourceSystemLabel = getSourceSystemLabel(metadata, crosswalk.type);
    var crosswalkId = getLastUriPart(crosswalk.uri);
    var crosswalkDisabled = isCrosswalkDisabled(crosswalk);
    var styles = useStyles({ color: color, isOpaque: !isDimmed, isFocused: isFocused });
    var _j = useDraggable({
        id: crosswalk.uri,
        data: {
            type: 'Crosswalk',
            crosswalk: crosswalk
        },
        disabled: crosswalkDisabled
    }), setNodeRef = _j.setNodeRef, attributes = _j.attributes, listeners = _j.listeners;
    var canDelete = !crosswalkDisabled && crosswalk.ownerType !== 'relation';
    useEffect(function () {
        var _a, _b;
        if (focusable &&
            scrollToCrosswalk &&
            eqCrosswalks(scrollToCrosswalk, crosswalk) &&
            scrollToCrosswalk !== crosswalk) {
            (_b = (_a = ref === null || ref === void 0 ? void 0 : ref.current) === null || _a === void 0 ? void 0 : _a.scrollIntoView) === null || _b === void 0 ? void 0 : _b.call(_a, { behavior: 'smooth' });
        }
    }, [scrollToCrosswalk]);
    var handleDeleteCrosswalk = function () {
        if (isFocused)
            toggleCrosswalkFocus();
        onDelete(crosswalkId);
    };
    return (React.createElement("div", { className: styles.root, ref: ref },
        React.createElement(SmallIconButtonWithTooltip, { size: "XXS", className: styles.focusedIcon, icon: focusable && VisibilityIcon, onClick: function () { return toggleCrosswalkFocus(); }, disabled: !focusable, tooltipTitle: focusable && i18n.text('Focus') }),
        React.createElement("div", { className: styles.crosswalkWrapper },
            React.createElement(ColoredBlock, { className: styles.header, color: color, variant: crosswalkDisabled ? 'disabled' : 'standart', onMouseEnter: highlightCrosswalk, onMouseLeave: removeHighlight },
                isDependent && React.createElement(DependentIcon, { className: styles.dependentIcon }),
                React.createElement(ArrowExpandButton, { onClick: function () { return setExpanded(function (value) { return !value; }); }, expanded: expanded }),
                React.createElement("div", __assign({ className: styles.dragWrapper, ref: setNodeRef }, listeners, attributes, { tabIndex: -1 }),
                    React.createElement(SourceIcon, { className: styles.sourceIcon, sourceType: crosswalk.type }),
                    React.createElement(ExpandedValueTooltip, { value: sourceSystemLabel },
                        React.createElement(Typography, { variant: 'body2', noWrap: true }, sourceSystemLabel)),
                    React.createElement(Spacer, null),
                    React.createElement(ExpandedValueTooltip, { value: crosswalk.value },
                        React.createElement(Typography, { variant: 'body2', className: styles.labelValue, noWrap: true }, crosswalk.value)))),
            expanded && (React.createElement("div", { className: styles.attributesWrapper },
                canDelete && (React.createElement("div", { className: styles.deleteCrosswalkButton },
                    React.createElement(SmallIconButtonWithTooltip, { icon: DeleteIcon, disabled: disableDelete, onClick: function () { return setDeleteDialogOpen(true); }, size: "XS", tooltipTitle: disableDelete
                            ? i18n.text("You don't have permissions to delete")
                            : i18n.text('Delete crosswalk'), showForDisabled: true }),
                    React.createElement(ConfirmDeleteDialog, { open: isDeleteDialogOpen, onConfirm: handleDeleteCrosswalk, onClose: function () { return setDeleteDialogOpen(false); } }))),
                React.createElement(CrosswalkAttributes, { crosswalk: crosswalk, canEditAttribute: !disableEdit, onEditAttribute: function (attributeName, value) {
                        onEdit({
                            crosswalkId: crosswalkId,
                            attributeName: attributeName,
                            value: value
                        });
                    }, onDeleteAttribute: function (attributeName) {
                        onEdit({
                            crosswalkId: crosswalkId,
                            attributeName: attributeName
                        });
                    } }))))));
});
CrosswalkRow.displayName = 'CrosswalkRow';
