import React, { useCallback, useEffect, memo } from 'react';
import i18n from 'ui-i18n';
import { pipe, map } from 'ramda';
import { capitalize } from '@mui/material/utils';
import { getFilterOptionLabel, getFilterOptionsForQueryBuilder } from '@reltio/mdm-sdk';
import { SimpleDropDownSelector } from '../SimpleDropDownSelector';
import { findValue } from '../AttributesFiltersBuilder/helpers';
import { useStyles } from './styles';
var AttributesFilterSelector = function (_a) {
    var _b;
    var rowData = _a.rowData, rowContext = _a.rowContext, textFieldInputRef = _a.textFieldInputRef, optionsProp = _a.options;
    var styles = useStyles();
    var _c = rowData || {}, _d = _c.attribute, _e = _d === void 0 ? {} : _d, _f = _e.attrType, attrType = _f === void 0 ? {} : _f, _g = _e.fieldName, fieldName = _g === void 0 ? null : _g, filter = _c.filter, id = _c.id, operator = _c.operator;
    var changeFilter = rowContext.changeFilter;
    var options = optionsProp ||
        (attrType &&
            fieldName &&
            pipe(getFilterOptionsForQueryBuilder, map(function (value) { return ({ value: value, label: capitalize(getFilterOptionLabel(value)) }); }))(attrType, fieldName));
    var onChangeFilter = useCallback(function (filterOption) {
        var newFilter = {
            fieldName: fieldName,
            filter: filterOption.value,
            operator: operator,
            values: []
        };
        changeFilter(newFilter, id);
    }, [fieldName, operator, changeFilter, id]);
    useEffect(function () {
        if ((fieldName && !filter) || (fieldName && !findValue(options, filter))) {
            onChangeFilter(options[0]);
        }
    }, [fieldName, filter, onChangeFilter, options]);
    return (React.createElement(SimpleDropDownSelector, { label: "", height: 40, isDisabled: !options, placeholder: i18n.text('Equals'), value: findValue(options, filter), options: options, onChange: onChangeFilter, className: styles.dropDownSelector, textFieldInputRef: textFieldInputRef, TextFieldProps: { inputProps: (_b = {}, _b['data-reltio-id'] = 'filter-selector', _b) } }));
};
export default memo(AttributesFilterSelector);
