"use strict";
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);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrosswalkRow = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var core_1 = require("@dnd-kit/core");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var Delete_1 = __importDefault(require("@mui/icons-material/Delete"));
var Visibility_1 = __importDefault(require("@mui/icons-material/Visibility"));
var CrosswalksDisplayContext_1 = require("../../../contexts/CrosswalksDisplayContext");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var Spacer_1 = require("../../../Spacer");
var ArrowExpandButton_1 = require("../../../ArrowExpandButton");
var ExpandedValueTooltip_1 = require("../../../ExpandedValueTooltip");
var ConfirmDeleteDialog_1 = require("../../../ConfirmDeleteDialog");
var SmallIconButton_1 = require("../../../SmallIconButton");
var SourceIcon_1 = require("../../../SourceIcon");
var NestedAttribute_1 = __importDefault(require("../../../icons/NestedAttribute"));
var ColoredBlock_1 = require("../ColoredBlock");
var CrosswalkAttributes_1 = require("../CrosswalkAttributes");
var styles_1 = require("./styles");
exports.CrosswalkRow = (0, react_1.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 = (0, react_1.useRef)(null);
    var _e = (0, react_1.useState)(false), expanded = _e[0], setExpanded = _e[1];
    var _f = (0, react_1.useState)(false), isDeleteDialogOpen = _f[0], setDeleteDialogOpen = _f[1];
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var crosswalkColor = (0, CrosswalksDisplayContext_1.useCrosswalkColor)(crosswalk);
    var _g = (0, CrosswalksDisplayContext_1.useCrosswalkFocus)(crosswalk), toggleCrosswalkFocus = _g.toggleCrosswalkFocus, isFocused = _g.isFocused;
    var _h = (0, CrosswalksDisplayContext_1.useCrosswalkHighlight)(crosswalk), isDimmed = _h.isDimmed, highlightCrosswalk = _h.highlightCrosswalk, removeHighlight = _h.removeHighlight;
    var scrollToCrosswalk = (0, CrosswalksDisplayContext_1.useHighlightedCrosswalks)()[0];
    var coloredCrosswalk = isDimmed ? 'rgba(0, 0, 0, 0.12)' : crosswalkColor;
    var color = isEmpty ? '#FFF' : isColored ? coloredCrosswalk : '#CACCD1';
    var sourceSystemLabel = (0, mdm_sdk_1.getSourceSystemLabel)(metadata, crosswalk.type);
    var crosswalkId = (0, mdm_sdk_1.getLastUriPart)(crosswalk.uri);
    var crosswalkDisabled = (0, mdm_sdk_1.isCrosswalkDisabled)(crosswalk);
    var styles = (0, styles_1.useStyles)({ color: color, isOpaque: !isDimmed, isFocused: isFocused });
    var _j = (0, core_1.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';
    (0, react_1.useEffect)(function () {
        var _a, _b;
        if (focusable &&
            scrollToCrosswalk &&
            (0, mdm_sdk_1.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_1.default.createElement("div", { className: styles.root, ref: ref },
        react_1.default.createElement(SmallIconButton_1.SmallIconButtonWithTooltip, { size: "XXS", className: styles.focusedIcon, icon: focusable && Visibility_1.default, onClick: function () { return toggleCrosswalkFocus(); }, disabled: !focusable, tooltipTitle: focusable && ui_i18n_1.default.text('Focus') }),
        react_1.default.createElement("div", { className: styles.crosswalkWrapper },
            react_1.default.createElement(ColoredBlock_1.ColoredBlock, { className: styles.header, color: color, variant: crosswalkDisabled ? 'disabled' : 'standart', onMouseEnter: highlightCrosswalk, onMouseLeave: removeHighlight },
                isDependent && react_1.default.createElement(NestedAttribute_1.default, { className: styles.dependentIcon }),
                react_1.default.createElement(ArrowExpandButton_1.ArrowExpandButton, { onClick: function () { return setExpanded(function (value) { return !value; }); }, expanded: expanded }),
                react_1.default.createElement("div", __assign({ className: styles.dragWrapper, ref: setNodeRef }, listeners, attributes, { tabIndex: -1 }),
                    react_1.default.createElement(SourceIcon_1.SourceIcon, { className: styles.sourceIcon, sourceType: crosswalk.type }),
                    react_1.default.createElement(ExpandedValueTooltip_1.ExpandedValueTooltip, { value: sourceSystemLabel },
                        react_1.default.createElement(Typography_1.default, { variant: 'body2', noWrap: true }, sourceSystemLabel)),
                    react_1.default.createElement(Spacer_1.Spacer, null),
                    react_1.default.createElement(ExpandedValueTooltip_1.ExpandedValueTooltip, { value: crosswalk.value },
                        react_1.default.createElement(Typography_1.default, { variant: 'body2', className: styles.labelValue, noWrap: true }, crosswalk.value)))),
            expanded && (react_1.default.createElement("div", { className: styles.attributesWrapper },
                canDelete && (react_1.default.createElement("div", { className: styles.deleteCrosswalkButton },
                    react_1.default.createElement(SmallIconButton_1.SmallIconButtonWithTooltip, { icon: Delete_1.default, disabled: disableDelete, onClick: function () { return setDeleteDialogOpen(true); }, size: "XS", tooltipTitle: disableDelete
                            ? ui_i18n_1.default.text("You don't have permissions to delete")
                            : ui_i18n_1.default.text('Delete crosswalk'), showForDisabled: true }),
                    react_1.default.createElement(ConfirmDeleteDialog_1.ConfirmDeleteDialog, { open: isDeleteDialogOpen, onConfirm: handleDeleteCrosswalk, onClose: function () { return setDeleteDialogOpen(false); } }))),
                react_1.default.createElement(CrosswalkAttributes_1.CrosswalkAttributes, { crosswalk: crosswalk, canEditAttribute: !disableEdit, onEditAttribute: function (attributeName, value) {
                        onEdit({
                            crosswalkId: crosswalkId,
                            attributeName: attributeName,
                            value: value
                        });
                    }, onDeleteAttribute: function (attributeName) {
                        onEdit({
                            crosswalkId: crosswalkId,
                            attributeName: attributeName
                        });
                    } }))))));
});
exports.CrosswalkRow.displayName = 'CrosswalkRow';
