"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.ReadOnlyComplexAttribute = void 0;
var react_1 = __importStar(require("react"));
var ramda_1 = require("ramda");
var classnames_1 = __importDefault(require("classnames"));
var react_context_selector_1 = require("@fluentui/react-context-selector");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Typography_1 = __importDefault(require("@mui/material/Typography"));
var ReadOnlyAttributesList_1 = require("../ReadOnlyAttributesList");
var ArrowExpandButton_1 = require("../ArrowExpandButton");
var CommentsContainer_1 = require("../CommentsContainer");
var HighlightedValuesContext_1 = require("../contexts/HighlightedValuesContext");
var AttributesVerificationContext_1 = require("../contexts/AttributesVerificationContext");
var classnames_2 = require("../constants/classnames");
var AttributeVerificationStatus_1 = require("../AttributeVerificationStatus/AttributeVerificationStatus");
var ReadOnlyComplexAttribute_module_css_1 = __importDefault(require("./ReadOnlyComplexAttribute.module.css"));
var ReadOnlyComplexAttribute = function (_a) {
    var _b;
    var attributeTypesList = _a.attributeTypesList, attributeValue = _a.attributeValue, label = _a.label, _c = _a.expanded, expandedProp = _c === void 0 ? false : _c, children = _a.children, showNonOv = _a.showNonOv, attributeType = _a.attributeType, classes = _a.classes, LabelRenderer = _a.LabelRenderer, RightSlot = _a.RightSlot;
    var uri = attributeValue.uri;
    var _d = (0, react_1.useState)(false), expanded = _d[0], setExpanded = _d[1];
    var _e = (0, react_1.useContext)(HighlightedValuesContext_1.HighlightedValuesContext), _f = _e.highlightedValuesUris, highlightedValuesUris = _f === void 0 ? [] : _f, highlightedClassName = _e.highlightedClassName;
    (0, react_1.useEffect)(function () {
        setExpanded(expandedProp);
    }, [expandedProp]);
    var hasAnalyticValue = (attributeTypesList || []).some(function (attributeType) { return (0, mdm_sdk_1.isAnalyticAttribute)(attributeType); });
    var attributeListEntity = (0, react_1.useMemo)(function () { return (__assign({ attributes: hasAnalyticValue ? null : attributeValue.value, analyticsAttributes: hasAnalyticValue ? attributeValue.value : null }, ((0, mdm_sdk_1.isSpecialAttribute)(attributeType) ? attributeValue.value : {}))); }, [attributeValue, hasAnalyticValue, attributeType]);
    var isVerificationAttribute = (0, react_context_selector_1.useContextSelector)(AttributesVerificationContext_1.AttributesVerificationContext, function (context) { return (context === null || context === void 0 ? void 0 : context.isVerificationAttribute) || ramda_1.F; });
    var objectType = (0, mdm_sdk_1.getObjectTypeByAttributeUri)(attributeValue.uri);
    var showVerificationStatus = isVerificationAttribute(attributeType);
    return (react_1.default.createElement("div", { className: ReadOnlyComplexAttribute_module_css_1.default.complexContainer },
        react_1.default.createElement("div", { className: (0, classnames_1.default)(ReadOnlyComplexAttribute_module_css_1.default.labelContainer, classnames_2.COMMENTS_CONTAINER_VISIBILITY_AREA) },
            react_1.default.createElement(ArrowExpandButton_1.ArrowExpandButton, { onClick: function () { return setExpanded(function (value) { return !value; }); }, expanded: expanded }),
            LabelRenderer ? (react_1.default.createElement(LabelRenderer, { attributeType: attributeType, attributeValue: attributeValue })) : (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(Typography_1.default, { component: "span", variant: "body2", classes: { body2: ReadOnlyComplexAttribute_module_css_1.default.label }, className: (0, classnames_1.default)((_b = {},
                        _b[highlightedClassName] = highlightedValuesUris.includes(attributeValue.uri),
                        _b[ReadOnlyComplexAttribute_module_css_1.default.ovFalse] = !(0, mdm_sdk_1.isOv)(attributeValue),
                        _b)), "data-reltio-id": "reltio-attribute-complex-label" },
                    label,
                    showVerificationStatus && (react_1.default.createElement(AttributeVerificationStatus_1.AttributeVerificationStatus, { attributeType: attributeType, attributeValue: attributeValue }))),
                react_1.default.createElement("div", { className: ReadOnlyComplexAttribute_module_css_1.default.spacer }),
                RightSlot && react_1.default.createElement(RightSlot, { attributeType: attributeType, attributeValue: attributeValue }),
                react_1.default.createElement(CommentsContainer_1.CommentsContainer, { uri: attributeValue.uri, relatedObjectUris: (0, mdm_sdk_1.createRelatedObjectUris)(objectType, {
                        uri: attributeValue.uri
                    }), objectType: objectType })))),
        expanded && (react_1.default.createElement(ReadOnlyAttributesList_1.ReadOnlyAttributesList, { attrTypes: attributeTypesList, entity: attributeListEntity, drawLines: true, parentUri: uri, showNonOv: showNonOv, classes: classes }, children))));
};
exports.ReadOnlyComplexAttribute = ReadOnlyComplexAttribute;
