import React, { useCallback, useContext } from 'react';
import { isNil } from 'ramda';
import classnames from 'classnames';
import { getLabel, isReference } from '@reltio/mdm-sdk';
import { getAttributeDescription } from '../helpers/common';
import { EntityUriLink } from '../../../EntityUriLink';
import { useMdmEntityUri } from '../../../contexts/MdmModuleContext';
import { AttributesChangesContext } from '../AttributesChanges/AttributesChangesContext';
import styles from './RelationEntityLink.module.css';
export var RelationEntityLink = function (_a) {
    var _b, _c;
    var _d;
    var attributeOrRelationType = _a.attributeOrRelationType, attributeValue = _a.attributeValue, className = _a.className;
    var objectsInfo = useContext(AttributesChangesContext);
    var entityUri = useMdmEntityUri();
    var getLinkDescription = useCallback(function () {
        var _a, _b, _c;
        if (isReference(attributeOrRelationType)) {
            var value = (_b = (_a = attributeValue.value) === null || _a === void 0 ? void 0 : _a.newValue) !== null && _b !== void 0 ? _b : (_c = attributeValue.value) === null || _c === void 0 ? void 0 : _c.oldValue;
            return getAttributeDescription(attributeOrRelationType, value || attributeValue, entityUri, objectsInfo);
        }
        else if ('endObject' in attributeOrRelationType) {
            var _d = attributeValue, endObjectUri = _d.endObjectUri, endObjectLabel = _d.endObjectLabel;
            return {
                objectUri: endObjectUri,
                objectLabel: endObjectLabel
            };
        }
    }, [attributeOrRelationType, attributeValue, entityUri, objectsInfo]);
    var _e = getLinkDescription() || {}, objectUri = _e.objectUri, objectLabel = _e.objectLabel;
    if (isNil(objectUri) || isNil(objectLabel)) {
        return null;
    }
    var isNotExist = ((_d = objectsInfo[objectUri]) === null || _d === void 0 ? void 0 : _d.isExist) === false;
    return (React.createElement(EntityUriLink, { value: objectUri, className: classnames(styles.linkContainer, (_b = {}, _b[styles.disabledLink] = isNotExist, _b), className) },
        React.createElement("span", { className: classnames(styles.linkLabel, (_c = {}, _c[styles.label] = isNotExist, _c)) }, getLabel(objectLabel))));
};
