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 __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { useContext, useMemo, memo } from 'react';
import classnames from 'classnames';
import { getAttributeTypeSubAttributes, getHistoryAppearanceByUri, getHistoryAttributeClassName, getLabel, getReferencedEntityUriFromAttrValue, isEmptyValue, isOv } from '@reltio/mdm-sdk';
import { HistoryDiffContext } from '../contexts/HistoryAppearanceContext';
import { UrlGeneratorsContext } from '../contexts/UrlGeneratorsContext';
import { useMdmMetadata, useMdmUiPath } from '../contexts/MdmModuleContext';
import { SearchHighlighter } from '../SearchHighlighter';
import { ReadOnlyComplexAttribute } from '../ReadOnlyComplexAttribute';
import { InternalLink } from '../InternalLink';
import { useStyles } from './styles';
export var ReferenceAttribute = memo(function (_a) {
    var attributeValue = _a.attributeValue, attributeType = _a.attributeType, otherProps = __rest(_a, ["attributeValue", "attributeType"]);
    var styles = useStyles();
    var metadata = useMdmMetadata();
    var uiPath = useMdmUiPath();
    var refEntityURI = getReferencedEntityUriFromAttrValue(attributeValue);
    var attributeTypeList = useMemo(function () { return getAttributeTypeSubAttributes(metadata, attributeType); }, [attributeType, metadata]);
    var appearance = useContext(HistoryDiffContext).appearance;
    var generateEntityUrl = useContext(UrlGeneratorsContext).generateEntityUrl;
    var entityLabel = useMemo(function () {
        var _a;
        var attributeAppearance = getHistoryAppearanceByUri(attributeValue.uri, appearance);
        var historyAttributesClassName = getHistoryAttributeClassName(attributeAppearance);
        var screen = (refEntityURI === null || refEntityURI === void 0 ? void 0 : refEntityURI.startsWith('changeRequests')) ? 'dcrReview' : 'profile';
        return (React.createElement(InternalLink, { href: isEmptyValue(appearance) ? generateEntityUrl({ uiPath: uiPath, uri: refEntityURI, screen: screen }) : undefined, className: classnames(styles[historyAttributesClassName], (_a = {},
                _a[styles.ovFalse] = !isOv(attributeValue),
                _a)) },
            React.createElement(SearchHighlighter, { text: getLabel(attributeValue.label) }),
            attributeValue.relationshipLabel && (React.createElement("span", { className: styles.relationshipLabel },
                React.createElement(SearchHighlighter, { text: attributeValue.relationshipLabel })))));
    }, [attributeValue, appearance, uiPath, refEntityURI, styles, generateEntityUrl]);
    return (React.createElement(ReadOnlyComplexAttribute, __assign({ attributeTypesList: attributeTypeList, attributeValue: attributeValue, label: entityLabel, attributeType: attributeType }, otherProps)));
});
ReferenceAttribute.displayName = 'ReferenceAttribute';
