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, { useRef, useState } from 'react';
import classnames from 'classnames';
import { defaultTo, identity, isEmpty, path, prop } from 'ramda';
import { isEmptyValue } from '@reltio/mdm-sdk';
import { ReactSelectDropdownIndicator } from '../ReactSelectDropdownIndicator';
import { AsyncReactSelect } from '../AsyncReactSelect';
import { ReactSelect } from '../ReactSelect';
import { EmptyStub } from '../EmptyStub';
import { Menu } from './components/Menu';
import { SingleValue } from './components/SingleValue';
import { useStyles, customStyles, overloadMenuListStyle, withoutLabelInputStyle } from './styles';
export var DropDownSelector = function (_a) {
    var _b;
    var value = _a.value, label = _a.label, options = _a.options, getOptions = _a.getOptions, createLabel = _a.createLabel, _c = _a.height, height = _c === void 0 ? 46 : _c, _d = _a.onChange, onChange = _d === void 0 ? identity : _d, onCreate = _a.onCreate, _e = _a.canCreateOption, canCreateOption = _e === void 0 ? function () { return Boolean(onCreate); } : _e, _f = _a.onClear, onClear = _f === void 0 ? identity : _f, _g = _a.components, components = _g === void 0 ? {} : _g, textFieldInputRef = _a.textFieldInputRef, TextFieldProps = _a.TextFieldProps, _h = _a.classes, classes = _h === void 0 ? {} : _h, otherProps = __rest(_a, ["value", "label", "options", "getOptions", "createLabel", "height", "onChange", "onCreate", "canCreateOption", "onClear", "components", "textFieldInputRef", "TextFieldProps", "classes"]);
    var styles = useStyles({ height: height });
    var _j = useState(false), open = _j[0], setOpen = _j[1];
    var inputRef = useRef(null);
    var SelectComponent = getOptions ? AsyncReactSelect : ReactSelect;
    var showIndicatorSeparator = prop('ClearIndicator', components)
        ? {}
        : { IndicatorSeparator: EmptyStub, ClearIndicator: EmptyStub };
    var handleCreate = onCreate
        ? function (value, group) {
            onCreate(value, group);
            setOpen(false);
        }
        : undefined;
    var overloadStylesIfGroup = prop('Group', components) ? overloadMenuListStyle : {};
    var overloadStylesIfNoLabel = isEmpty(label) ? withoutLabelInputStyle : {};
    var mergedStyles = __assign(__assign(__assign({}, customStyles), overloadStylesIfGroup), overloadStylesIfNoLabel);
    var mergedClasses = __assign(__assign({}, styles), classes);
    return (React.createElement(SelectComponent, __assign({ placeholder: "", defaultOptions: true }, otherProps, { value: defaultTo(null, value), loadOptions: getOptions, options: options, cacheOptions: true, onChange: onChange, onCreate: handleCreate, onClear: onClear, createLabel: createLabel, canCreateOption: canCreateOption, classes: mergedClasses, styles: mergedStyles, components: __assign(__assign({ DropdownIndicator: ReactSelectDropdownIndicator, LoadingIndicator: EmptyStub, SingleValue: SingleValue, Menu: Menu }, showIndicatorSeparator), components), menuPlacement: "auto", TextFieldProps: __assign(__assign({}, TextFieldProps), { label: label, variant: 'filled', size: 'small', classes: __assign(__assign({}, prop('classes', TextFieldProps)), { root: classnames(styles.formControl, path(['classes', 'root'], TextFieldProps)) }), inputProps: __assign({}, prop('inputProps', TextFieldProps)), InputProps: __assign(__assign({}, prop('InputProps', TextFieldProps)), { classes: __assign(__assign({}, path(['InputProps', 'classes'], TextFieldProps)), { root: classnames(styles.filledInputRoot, path(['InputProps', 'classes', 'root'], TextFieldProps)), underline: classnames((_b = {}, _b[styles.filledInputUnderline] = isEmptyValue(value), _b), path(['InputProps', 'classes', 'underline'], TextFieldProps)), focused: classnames('focused', path(['InputProps', 'classes', 'focused'], TextFieldProps)) }) }), InputLabelProps: __assign(__assign({}, prop('InputLabelProps', TextFieldProps)), { classes: __assign(__assign({}, path(['InputLabelProps', 'classes'], TextFieldProps)), { root: classnames(styles.inputLabel, path(['InputLabelProps', 'classes', 'root'], TextFieldProps)), shrink: classnames('shrink', path(['InputLabelProps', 'classes', 'shrink'], TextFieldProps)) }), shrink: !isEmptyValue(value) ? true : undefined }), ref: textFieldInputRef || inputRef }), inputRef: textFieldInputRef || inputRef, menuIsOpen: open, onMenuOpen: function () { return setOpen(true); }, onMenuClose: function () { return setOpen(false); } })));
};
