"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OvIcon = exports.ChipWithTooltip = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var Chip_1 = __importDefault(require("@mui/material/Chip"));
var DataTypeValue_1 = require("../DataTypeValue");
var withTooltip_1 = require("../HOCs/withTooltip");
var EntityContext_1 = require("../contexts/EntityContext");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var helpers_1 = require("./helpers");
var styles_1 = require("./styles");
exports.ChipWithTooltip = (0, withTooltip_1.withTooltip)(Chip_1.default);
var OvIcon = function (_a) {
    var className = _a.className, attributeType = _a.attributeType, _b = _a.nonOvValues, nonOvValues = _b === void 0 ? [] : _b, nonOvTotal = _a.nonOvTotal;
    var styles = (0, styles_1.useStyles)();
    var openEntity = (0, MdmModuleContext_1.useMdmAction)('openEntity');
    var entity = (0, react_1.useContext)(EntityContext_1.EntityContext);
    var linkedEntity = (0, helpers_1.getLinkedEntity)(entity, nonOvValues, attributeType);
    var isSourcesScreenEnabled = (0, MdmModuleContext_1.useMdmIsSourcesScreenEnabled)(linkedEntity === null || linkedEntity === void 0 ? void 0 : linkedEntity.type);
    var onClick = (0, react_1.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_1.default.createElement("div", { "data-reltio-id": "non-ov-value", key: i }, (0, mdm_sdk_1.isComplexAttribute)(attributeType) ? ((0, mdm_sdk_1.getLabel)(value.label)) : (react_1.default.createElement(DataTypeValue_1.DataTypeValue, { value: (0, mdm_sdk_1.getAttributeValue)(value), dataTypeDefinition: (0, mdm_sdk_1.getAttrDataTypeDefinition)(attributeType), rich: false })))); })
        : null;
    return (react_1.default.createElement(exports.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: (0, classnames_1.default)(styles.container, className),
            label: styles.label
        } }));
};
exports.OvIcon = OvIcon;
