import React, { useCallback, memo } from 'react';
import { SearchOperator, getSearchGroupAttributeLabel } from '@reltio/mdm-sdk';
import { BasicAttributeSelector } from '../../../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 = useCallback(function (data) { return getSearchGroupAttributeLabel({ data: data }); }, []);
    var changeAttribute = 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: SearchOperator.and,
                values: []
            });
        }
        onFilterAttributeChange === null || onFilterAttributeChange === void 0 ? void 0 : onFilterAttributeChange(selectInfo);
    }, [rowData, onFilterAttributeChange, filter, operator, changeFilter, id, addNewFilter]);
    return (React.createElement(BasicAttributeSelector, { itemsGroups: attributeSelectorItemsGroups, ungroup: attributeSelectorItemsGroups.length < 2, single: true, value: attribute, getLabel: getSelectedAttributeLabel, onSelect: changeAttribute, onOpenPopup: attributeSelectorProps.onOpen }));
};
export default memo(AttributeSelector);
