var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React, { memo, useCallback, useRef, useMemo } from 'react';
import { pipe, pluck } from 'ramda';
import { isEmptyValue } from '@reltio/mdm-sdk';
import { ReactSelectDropdownIndicatorWithIconButton } from '../ReactSelectDropdownIndicatorWithIconButton';
import { ReactSelectOptionWithCheckIcon } from '../ReactSelectOptionWithCheckIcon';
import { ReactSelectMenuWithPopper } from '../ReactSelectMenuWithPopper';
import { MultiValueChip } from '../MultiValueChip';
import { EmptyStub } from '../EmptyStub';
import { MultiSelect } from '../MultiSelect';
import { useStyles, customStyles } from './styles';
var EMPTY_ARRAY = [];
var EMPTY_TEXT_FIELD_PROPS = {};
var MultiValueSelector = function (_a) {
    var _b = _a.values, values = _b === void 0 ? EMPTY_ARRAY : _b, onChange = _a.onChange, options = _a.options, fullWidth = _a.fullWidth, _c = _a.TextFieldProps, TextFieldProps = _c === void 0 ? EMPTY_TEXT_FIELD_PROPS : _c;
    var styles = useStyles();
    var inputRef = useRef(null);
    var valueToOption = useCallback(function (value) { return options.find(function (option) { return option.value === value; }) || { value: value, label: value }; }, [options]);
    var preparedValues = useMemo(function () { return values.map(valueToOption); }, [valueToOption, values]);
    var _d = TextFieldProps.InputProps, InputProps = _d === void 0 ? {} : _d, textFieldClassName = TextFieldProps.className, otherTextFieldProps = __rest(TextFieldProps, ["InputProps", "className"]);
    return (React.createElement(MultiSelect, { value: preparedValues, onChange: pipe(pluck('value'), onChange), options: options, TextFieldProps: __assign({ InputProps: __assign({ disableUnderline: isEmptyValue(values) }, InputProps), variant: 'filled', className: textFieldClassName, ref: inputRef }, otherTextFieldProps), components: {
            DropdownIndicator: ReactSelectDropdownIndicatorWithIconButton,
            Option: ReactSelectOptionWithCheckIcon,
            Menu: ReactSelectMenuWithPopper,
            MultiValue: MultiValueChip,
            IndicatorSeparator: EmptyStub
        }, inputRef: inputRef, classes: styles, styles: __assign(__assign({}, customStyles), { container: function (base) { return (__assign(__assign({}, base), (fullWidth ? { width: '100%' } : {}))); } }), menuPortalTarget: document.body }));
};
MultiValueSelector.displayName = 'MultiValueSelector';
export default memo(MultiValueSelector);
