"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var BasicAttributeSelector_1 = require("../../../BasicAttributeSelector");
var AttributeSelector = function (_a) {
    var rowData = _a.rowData, rowContext = _a.rowContext, attributeSelectorProps = _a.attributeSelectorProps;
    var _b = rowData || {}, attribute = _b.attribute, id = _b.id, filter = _b.filter, operator = _b.operator;
    var changeFilter = rowContext.changeFilter, addNewFilter = rowContext.addNewFilter, attributeSelectorItemsGroups = rowContext.attributeSelectorItemsGroups, onFilterAttributeChange = rowContext.onFilterAttributeChange;
    var getSelectedAttributeLabel = (0, react_1.useCallback)(function (data) { return (0, mdm_sdk_1.getSearchGroupAttributeLabel)({ data: data }); }, []);
    var changeAttribute = (0, react_1.useCallback)(function (selectInfo) {
        if (rowData) {
            var newFilter = {
                fieldName: selectInfo.attribute.fieldName,
                filter: filter,
                operator: operator,
                values: []
            };
            changeFilter(newFilter, id);
        }
        else {
            addNewFilter({
                fieldName: selectInfo.attribute.fieldName,
                filter: null,
                operator: mdm_sdk_1.SearchOperator.and,
                values: []
            });
        }
        onFilterAttributeChange === null || onFilterAttributeChange === void 0 ? void 0 : onFilterAttributeChange(selectInfo);
    }, [rowData, onFilterAttributeChange, filter, operator, changeFilter, id, addNewFilter]);
    return (react_1.default.createElement(BasicAttributeSelector_1.BasicAttributeSelector, { itemsGroups: attributeSelectorItemsGroups, ungroup: attributeSelectorItemsGroups.length < 2, single: true, value: attribute, getLabel: getSelectedAttributeLabel, onSelect: changeAttribute, onOpenPopup: attributeSelectorProps.onOpen }));
};
exports.default = (0, react_1.memo)(AttributeSelector);
