"use strict";
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 __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;
};
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiSelect = void 0;
var react_1 = __importStar(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var CreatableReactSelect_1 = require("../CreatableReactSelect");
var ReactSelect_1 = require("../ReactSelect");
var AsyncReactSelect_1 = require("../AsyncReactSelect");
var classes_1 = require("../helpers/classes");
var useKeepInputVisible_1 = require("./useKeepInputVisible");
var helpers_1 = require("./helpers");
var styles_1 = require("./styles");
var getControlRef = function (innerRef) {
    var controlRef = {};
    if (innerRef.current) {
        controlRef.current = (0, ramda_1.prop)('controlRef', innerRef.current);
    }
    return controlRef;
};
var EMPTY_OBJECT = {};
var MultiSelect = function (props) {
    var label = props.label, _a = props.creatable, creatable = _a === void 0 ? false : _a, getOptions = props.getOptions, _b = props.loadingIndicator, loadingIndicator = _b === void 0 ? false : _b, _c = props.components, componentsProp = _c === void 0 ? EMPTY_OBJECT : _c, _d = props.classes, classesProp = _d === void 0 ? EMPTY_OBJECT : _d, stylesProp = props.styles, _e = props.onChange, onChangeProp = _e === void 0 ? mdm_sdk_1.noop : _e, _f = props.onInputChange, onInputChangeProp = _f === void 0 ? mdm_sdk_1.noop : _f, _g = props.menuPlacement, menuPlacement = _g === void 0 ? 'auto' : _g, _h = props.cacheOptions, cacheOptions = _h === void 0 ? true : _h, _j = props.placeholder, placeholder = _j === void 0 ? '' : _j, reactSelectProps = __rest(props, ["label", "creatable", "getOptions", "loadingIndicator", "components", "classes", "styles", "onChange", "onInputChange", "menuPlacement", "cacheOptions", "placeholder"]);
    var SelectComponent = creatable ? CreatableReactSelect_1.CreatableReactSelect : getOptions ? AsyncReactSelect_1.AsyncReactSelect : ReactSelect_1.ReactSelect;
    var _k = (0, react_1.useState)(''), inputValue = _k[0], setInputValue = _k[1];
    var multiSelectClasses = (0, styles_1.useStyles)((0, ramda_1.omit)(['classes'], props));
    var inputId = (0, react_1.useId)();
    var innerRef = (0, react_1.useRef)(null);
    (0, useKeepInputVisible_1.useKeepInputVisible)(props.value, getControlRef(innerRef));
    var components = __assign(__assign({}, helpers_1.customComponents), componentsProp);
    var classes = (0, classes_1.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_1.default.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, cacheOptions: cacheOptions, placeholder: placeholder, loadOptions: getOptions, innerRef: innerRef }, labelProps, reactSelectProps)));
};
exports.MultiSelect = MultiSelect;
exports.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: prop_types_1.default.bool,
    /** True for enabling cache. Recommended. */
    cacheOptions: prop_types_1.default.bool,
    defaultOptions: prop_types_1.default.bool,
    /** Array of selected options. */
    value: prop_types_1.default.arrayOf(prop_types_1.default.shape({
        label: prop_types_1.default.string.isRequired,
        value: prop_types_1.default.any
    })),
    options: prop_types_1.default.arrayOf(prop_types_1.default.shape({
        label: prop_types_1.default.string.isRequired,
        value: prop_types_1.default.any
    })),
    /** Way to receive updated values */
    onChange: prop_types_1.default.func,
    /** Way to receive updated inputValue */
    onInputChange: prop_types_1.default.func,
    /** Input placeholder. Will not be shown if the label prop is specified */
    placeholder: prop_types_1.default.string,
    /** Floating label from @mui/material/TextField component */
    label: prop_types_1.default.string,
    /** Enable to show default react-select's indicator */
    loadingIndicator: prop_types_1.default.bool,
    /** You could redefine any of these components */
    components: prop_types_1.default.shape({
        ClearIndicator: prop_types_1.default.func,
        Control: prop_types_1.default.func,
        DropdownIndicator: prop_types_1.default.func,
        DownChevron: prop_types_1.default.func,
        CrossIcon: prop_types_1.default.func,
        Group: prop_types_1.default.func,
        GroupHeading: prop_types_1.default.func,
        IndicatorsContainer: prop_types_1.default.func,
        IndicatorSeparator: prop_types_1.default.func,
        Input: prop_types_1.default.func,
        LoadingIndicator: prop_types_1.default.func,
        Menu: prop_types_1.default.func,
        MenuList: prop_types_1.default.func,
        MenuPortal: prop_types_1.default.func,
        LoadingMessage: prop_types_1.default.func,
        NoOptionsMessage: prop_types_1.default.func,
        MultiValue: prop_types_1.default.func,
        MultiValueContainer: prop_types_1.default.func,
        MultiValueLabel: prop_types_1.default.func,
        MultiValueRemove: prop_types_1.default.func,
        Option: prop_types_1.default.func,
        Placeholder: prop_types_1.default.func,
        SelectContainer: prop_types_1.default.func,
        SingleValue: prop_types_1.default.func,
        ValueContainer: prop_types_1.default.func
    }),
    /** Classes are passed to mui skin components. Provided classes will be merged with existing ones. */
    classes: prop_types_1.default.shape({
        control: prop_types_1.default.string,
        valueContainer: prop_types_1.default.string,
        multiValue: prop_types_1.default.string,
        noOptionsMessage: prop_types_1.default.string,
        singleValue: prop_types_1.default.string,
        placeholder: prop_types_1.default.string,
        menu: prop_types_1.default.string,
        clearIndicator: prop_types_1.default.string,
        dropdownIndicator: prop_types_1.default.string,
        option: prop_types_1.default.string,
        'option--selected': prop_types_1.default.string,
        label: prop_types_1.default.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: prop_types_1.default.shape({
        clearIndicator: prop_types_1.default.func,
        container: prop_types_1.default.func,
        control: prop_types_1.default.func,
        dropdownIndicator: prop_types_1.default.func,
        group: prop_types_1.default.func,
        groupHeading: prop_types_1.default.func,
        indicatorsContainer: prop_types_1.default.func,
        indicatorSeparator: prop_types_1.default.func,
        input: prop_types_1.default.func,
        loadingIndicator: prop_types_1.default.func,
        loadingMessage: prop_types_1.default.func,
        menu: prop_types_1.default.func,
        menuList: prop_types_1.default.func,
        menuPortal: prop_types_1.default.func,
        multiValue: prop_types_1.default.func,
        multiValueLabel: prop_types_1.default.func,
        multiValueRemove: prop_types_1.default.func,
        noOptionsMessage: prop_types_1.default.func,
        option: prop_types_1.default.func,
        placeholder: prop_types_1.default.func,
        singleValue: prop_types_1.default.func,
        valueContainer: prop_types_1.default.func
    }),
    /** Props from https://material-ui.com/api/text-field/ */
    TextFieldProps: prop_types_1.default.object,
    getOptions: prop_types_1.default.func,
    menuPlacement: prop_types_1.default.string,
    menuPortalTarget: prop_types_1.default.object,
    menuShouldBlockScroll: prop_types_1.default.bool,
    captureMenuScroll: prop_types_1.default.bool,
    getOptionValue: prop_types_1.default.func,
    isSearchable: prop_types_1.default.bool,
    inputRef: prop_types_1.default.shape({ current: prop_types_1.default.object })
};
