"use strict";
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InlineComplexAttribute = void 0;
var react_1 = __importStar(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var Spacer_1 = require("../Spacer");
var CrosswalkIcon_1 = require("./components/CrosswalkIcon");
var ArrowExpandButton_1 = require("../ArrowExpandButton");
var InlineAttributesList_1 = require("../InlineAttributesList");
var PinButton_1 = require("../PinButton");
var IgnoreButton_1 = require("../IgnoreButton");
var DeleteButton_1 = require("../DeleteButton");
var useDidUpdateEffect_1 = require("../hooks/useDidUpdateEffect");
var useAttributeState_1 = require("../hooks/useAttributeState");
var AutoExpandedAttributesContext_1 = require("../contexts/AutoExpandedAttributesContext");
var styles_1 = require("./styles");
var InlineComplexAttribute = function (_a) {
    var attributeType = _a.attributeType, attributeValue = _a.attributeValue, attributeTypesList = _a.attributeTypesList, label = _a.label, crosswalksMap = _a.crosswalksMap, disableEdit = _a.disableEdit, disableDelete = _a.disableDelete, onPin = _a.onPin, onIgnore = _a.onIgnore, onDelete = _a.onDelete, onEdit = _a.onEdit, onAdd = _a.onAdd, emptyTempAttributeUris = _a.emptyTempAttributeUris;
    var styles = (0, styles_1.useStyles)();
    var _b = (0, useAttributeState_1.useAttributeState)(), isReadingMode = _b.isReadingMode, onMouseEnter = _b.onMouseEnter, onMouseLeave = _b.onMouseLeave;
    var canEdit = !disableEdit && (0, mdm_sdk_1.checkMetadataForUpdate)(mdm_sdk_1.Mode.Editing, attributeType);
    var canDelete = !disableDelete && (0, mdm_sdk_1.checkMetadataForDelete)(mdm_sdk_1.Mode.Editing, attributeType);
    var isTemporary = (0, mdm_sdk_1.isTempUri)(attributeValue.uri);
    var showPin = !isTemporary && ((!isReadingMode && canEdit) || attributeValue.pin);
    var showIgnore = !isTemporary && ((!isReadingMode && canEdit) || attributeValue.ignored);
    var showDelete = !isReadingMode && canDelete;
    var expandedFromContext = (0, AutoExpandedAttributesContext_1.useAttributeAutoExpanded)(attributeValue.uri);
    var _c = (0, react_1.useState)(expandedFromContext || isTemporary), expanded = _c[0], setExpanded = _c[1];
    (0, useDidUpdateEffect_1.useDidUpdateEffect)(function () {
        if (expandedFromContext)
            setExpanded(expandedFromContext);
    }, [expandedFromContext]);
    var crosswalks = (0, ramda_1.pipe)((0, ramda_1.pickBy)(function (val, attrUri) { return (0, mdm_sdk_1.areOneHierarchyUris)(attrUri, attributeValue === null || attributeValue === void 0 ? void 0 : attributeValue.uri); }), ramda_1.values, ramda_1.flatten, (0, ramda_1.uniqBy)((0, ramda_1.prop)('uri')))(crosswalksMap);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { "data-reltio-id": "inline-complex-attribute", className: styles.root, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave },
            react_1.default.createElement(ArrowExpandButton_1.ArrowExpandButton, { onClick: function () { return setExpanded(function (value) { return !value; }); }, expanded: expanded }),
            react_1.default.createElement("span", null,
                label,
                crosswalks.map(function (crosswalk) { return (react_1.default.createElement(CrosswalkIcon_1.CrosswalkIcon, { key: (0, mdm_sdk_1.getCrosswalkId)(crosswalk), crosswalk: crosswalk, className: styles.crosswalkIcon })); })),
            react_1.default.createElement(Spacer_1.Spacer, null),
            react_1.default.createElement("div", { className: styles.actions },
                showPin && (react_1.default.createElement(PinButton_1.PinButton, { disabled: !canEdit, isPinned: attributeValue.pin, onClick: function () { return onPin({ attributeValue: attributeValue }); } })),
                showIgnore && (react_1.default.createElement(IgnoreButton_1.IgnoreButton, { disabled: !canEdit, isIgnored: attributeValue.ignored, onClick: function () { return onIgnore({ attributeValue: attributeValue }); } })),
                showDelete && (react_1.default.createElement(DeleteButton_1.DeleteButton, { onClick: function () {
                        return onDelete({
                            attributeType: attributeType,
                            attributeValue: attributeValue,
                            crosswalks: crosswalksMap[attributeValue.uri]
                        });
                    } })))),
        expanded && (react_1.default.createElement(InlineAttributesList_1.InlineAttributesList, { attributeTypes: attributeTypesList, attributes: attributeValue.value, parentUri: attributeValue.uri, crosswalksMap: crosswalksMap, disableEdit: !canEdit, disableDelete: !canDelete, onPin: onPin, onIgnore: onIgnore, onDelete: onDelete, onEdit: onEdit, onAdd: onAdd, emptyTempAttributeUris: emptyTempAttributeUris }))));
};
exports.InlineComplexAttribute = InlineComplexAttribute;
