import React, { useCallback, useContext } from 'react';
import classnames from 'classnames';
import { getAttrDataTypeDefinition, getAttributeValue, isComplexAttribute, getLabel } from '@reltio/mdm-sdk';
import Chip from '@mui/material/Chip';
import { DataTypeValue } from '../DataTypeValue';
import { withTooltip } from '../HOCs/withTooltip';
import { EntityContext } from '../contexts/EntityContext';
import { useMdmAction, useMdmIsSourcesScreenEnabled } from '../contexts/MdmModuleContext';
import { getLinkedEntity } from './helpers';
import { useStyles } from './styles';
export var ChipWithTooltip = withTooltip(Chip);
export var OvIcon = function (_a) {
    var className = _a.className, attributeType = _a.attributeType, _b = _a.nonOvValues, nonOvValues = _b === void 0 ? [] : _b, nonOvTotal = _a.nonOvTotal;
    var styles = useStyles();
    var openEntity = useMdmAction('openEntity');
    var entity = useContext(EntityContext);
    var linkedEntity = getLinkedEntity(entity, nonOvValues, attributeType);
    var isSourcesScreenEnabled = useMdmIsSourcesScreenEnabled(linkedEntity === null || linkedEntity === void 0 ? void 0 : linkedEntity.type);
    var onClick = useCallback(function () {
        openEntity({ uri: linkedEntity.uri, screen: 'sources' });
    }, [openEntity, linkedEntity]);
    if (!nonOvTotal) {
        return null;
    }
    var tooltipTitle = nonOvValues.length
        ? nonOvValues.map(function (value, i) { return (React.createElement("div", { "data-reltio-id": "non-ov-value", key: i }, isComplexAttribute(attributeType) ? (getLabel(value.label)) : (React.createElement(DataTypeValue, { value: getAttributeValue(value), dataTypeDefinition: getAttrDataTypeDefinition(attributeType), rich: false })))); })
        : null;
    return (React.createElement(ChipWithTooltip, { "data-reltio-id": "ov-icon", tooltipTitle: tooltipTitle, tooltipPlacement: "bottom", label: "+ ".concat(nonOvTotal), variant: "outlined", onClick: (linkedEntity === null || linkedEntity === void 0 ? void 0 : linkedEntity.uri) && isSourcesScreenEnabled ? onClick : undefined, classes: {
            root: classnames(styles.container, className),
            label: styles.label
        } }));
};
