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, { useState, useRef } from 'react';
import PropTypes from 'prop-types';
import { omit, prop } from 'ramda';
import { CreatableReactSelect } from '../CreatableReactSelect';
import { ReactSelect } from '../ReactSelect';
import { AsyncReactSelect } from '../AsyncReactSelect';
import { mergeClasses } from '../helpers/classes';
import { useKeepInputVisible } from './useKeepInputVisible';
import { customComponents } from './helpers';
import { useStyles } from './styles';
var getControlRef = function (innerRef) {
    var controlRef = {};
    if (innerRef.current) {
        controlRef.current = prop('controlRef', innerRef.current);
    }
    return controlRef;
};
var id = 0;
var getInputId = function () { return "custom-select-".concat(id++); };
export var MultiSelect = function (props) {
    var label = props.label, creatable = props.creatable, getOptions = props.getOptions, loadingIndicator = props.loadingIndicator, componentsProp = props.components, classesProp = props.classes, stylesProp = props.styles, onChangeProp = props.onChange, onInputChangeProp = props.onInputChange, _a = props.menuPlacement, menuPlacement = _a === void 0 ? 'auto' : _a, reactSelectProps = __rest(props, ["label", "creatable", "getOptions", "loadingIndicator", "components", "classes", "styles", "onChange", "onInputChange", "menuPlacement"]);
    var SelectComponent = creatable ? CreatableReactSelect : getOptions ? AsyncReactSelect : ReactSelect;
    var _b = useState(''), inputValue = _b[0], setInputValue = _b[1];
    var multiSelectClasses = useStyles(omit(['classes'], props));
    var inputId = getInputId();
    var innerRef = useRef();
    useKeepInputVisible(props.value, getControlRef(innerRef));
    var components = __assign(__assign({}, customComponents), componentsProp);
    var classes = mergeClasses(multiSelectClasses, classesProp);
    function handleInputChange(value, _a) {
        var action = _a.action;
        onInputChangeProp(value, { action: action });
        if (action !== 'set-value') {
            setInputValue(value);
        }
    }
    function handleChange(value, actionMeta) {
        onChangeProp(value, actionMeta);
        if (actionMeta.action === 'create-option') {
            setInputValue('');
            onInputChangeProp('', { action: 'input-change' });
        }
    }
    var styles = __assign({ indicatorSeparator: function (base) { return (__assign(__assign({}, base), { margin: '10px 0' })); }, loadingIndicator: function (base) { return (__assign(__assign({}, base), { display: loadingIndicator ? base.display : 'none' })); }, loadingMessage: function (base) { return (__assign(__assign({}, base), { fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif' })); } }, stylesProp);
    var labelProps = label
        ? {
            inputId: inputId,
            TextFieldProps: __assign({ label: label, InputLabelProps: {
                    htmlFor: inputId,
                    shrink: props.value && props.value.length ? true : undefined,
                    className: classes.label
                } }, props.TextFieldProps)
        }
        : {};
    return (React.createElement(SelectComponent, __assign({ isMulti: true, inputId: inputId, inputValue: inputValue, onChange: handleChange, onInputChange: handleInputChange, closeMenuOnSelect: false, hideSelectedOptions: false, classes: classes, components: components, styles: styles, menuPlacement: menuPlacement, loadOptions: getOptions, innerRef: innerRef }, labelProps, reactSelectProps)));
};
MultiSelect.defaultProps = {
    creatable: false,
    cacheOptions: true,
    components: {},
    classes: {},
    placeholder: '',
    loadingIndicator: false,
    onInputChange: function () { },
    onChange: function () { }
};
MultiSelect.propTypes = {
    /**
     * Async creatable multi select with optional placeholder, shrink-label, non-closing menu and options with checkboxes.
     * Based on react-select/creatable and react-select/async.
     * See react-select docs: https://react-select.com
     *
     * Below are listed only props used in the MultiSelect component. Check https://react-select.com/props first.
     * */
    /** Ability to create values. React-select/async-creatable is used internally */
    creatable: PropTypes.bool,
    /** True for enabling cache. Recommended. */
    cacheOptions: PropTypes.bool,
    defaultOptions: PropTypes.bool,
    /** Array of selected options. */
    value: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string.isRequired,
        value: PropTypes.any
    })),
    options: PropTypes.arrayOf(PropTypes.shape({
        label: PropTypes.string.isRequired,
        value: PropTypes.any
    })),
    /** Way to receive updated values */
    onChange: PropTypes.func,
    /** Way to receive updated inputValue */
    onInputChange: PropTypes.func,
    /** Input placeholder. Will not be shown if the label prop is specified */
    placeholder: PropTypes.string,
    /** Floating label from @mui/material/TextField component */
    label: PropTypes.string,
    /** Enable to show default react-select's indicator */
    loadingIndicator: PropTypes.bool,
    /** You could redefine any of these components */
    components: PropTypes.shape({
        ClearIndicator: PropTypes.func,
        Control: PropTypes.func,
        DropdownIndicator: PropTypes.func,
        DownChevron: PropTypes.func,
        CrossIcon: PropTypes.func,
        Group: PropTypes.func,
        GroupHeading: PropTypes.func,
        IndicatorsContainer: PropTypes.func,
        IndicatorSeparator: PropTypes.func,
        Input: PropTypes.func,
        LoadingIndicator: PropTypes.func,
        Menu: PropTypes.func,
        MenuList: PropTypes.func,
        MenuPortal: PropTypes.func,
        LoadingMessage: PropTypes.func,
        NoOptionsMessage: PropTypes.func,
        MultiValue: PropTypes.func,
        MultiValueContainer: PropTypes.func,
        MultiValueLabel: PropTypes.func,
        MultiValueRemove: PropTypes.func,
        Option: PropTypes.func,
        Placeholder: PropTypes.func,
        SelectContainer: PropTypes.func,
        SingleValue: PropTypes.func,
        ValueContainer: PropTypes.func
    }),
    /** Classes are passed to mui skin components. Provided classes will be merged with existing ones. */
    classes: PropTypes.shape({
        control: PropTypes.string,
        valueContainer: PropTypes.string,
        multiValue: PropTypes.string,
        noOptionsMessage: PropTypes.string,
        singleValue: PropTypes.string,
        placeholder: PropTypes.string,
        menu: PropTypes.string,
        clearIndicator: PropTypes.string,
        dropdownIndicator: PropTypes.string,
        option: PropTypes.string,
        'option--selected': PropTypes.string,
        label: PropTypes.string
    }),
    /**
     * React-select's way to customise style. Could be used to style default react-select components.
     * In other cases better to use classes and components props
     * */
    styles: PropTypes.shape({
        clearIndicator: PropTypes.func,
        container: PropTypes.func,
        control: PropTypes.func,
        dropdownIndicator: PropTypes.func,
        group: PropTypes.func,
        groupHeading: PropTypes.func,
        indicatorsContainer: PropTypes.func,
        indicatorSeparator: PropTypes.func,
        input: PropTypes.func,
        loadingIndicator: PropTypes.func,
        loadingMessage: PropTypes.func,
        menu: PropTypes.func,
        menuList: PropTypes.func,
        menuPortal: PropTypes.func,
        multiValue: PropTypes.func,
        multiValueLabel: PropTypes.func,
        multiValueRemove: PropTypes.func,
        noOptionsMessage: PropTypes.func,
        option: PropTypes.func,
        placeholder: PropTypes.func,
        singleValue: PropTypes.func,
        valueContainer: PropTypes.func
    }),
    /** Props from https://material-ui.com/api/text-field/ */
    TextFieldProps: PropTypes.object,
    getOptions: PropTypes.func,
    menuPlacement: PropTypes.string,
    menuPortalTarget: PropTypes.object,
    menuShouldBlockScroll: PropTypes.bool,
    captureMenuScroll: PropTypes.bool,
    getOptionValue: PropTypes.func,
    isSearchable: PropTypes.bool,
    inputRef: PropTypes.shape({ current: PropTypes.object })
};
