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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
import React, { useState } from 'react';
import i18n from 'ui-i18n';
import { identity, pipe, propEq, remove, uniq, when, trim, isEmpty } from 'ramda';
import classnames from 'classnames';
import { wrapInArrayIfNeeded } from '@reltio/mdm-sdk';
import TextField from '@mui/material/TextField';
import Tooltip from '@mui/material/Tooltip';
import { getValue } from '../helpers/events';
import { ValueChip } from '../ValueChip';
import { AutosizeInputWrapper } from './components/AutosizeInputWrapper';
import { useStyles } from './styles';
var EMPTY_VALUES_EDIT_HINT = i18n.text('Press Enter to add. You may enter multiple values');
var isEmptyValue = pipe(trim, isEmpty);
export var MultipleInput = function (_a) {
    var _b;
    var _c = _a.values, valuesProp = _c === void 0 ? [] : _c, _d = _a.InputProps, InputProps = _d === void 0 ? {} : _d, _e = _a.inputProps, inputProps = _e === void 0 ? {} : _e, _f = _a.classes, classes = _f === void 0 ? {} : _f, isCrossedOut = _a.isCrossedOut, _g = _a.onChange, onChange = _g === void 0 ? identity : _g, _h = _a.getValueLabel, getValueLabel = _h === void 0 ? identity : _h, otherProps = __rest(_a, ["values", "InputProps", "inputProps", "classes", "isCrossedOut", "onChange", "getValueLabel"]);
    var styles = useStyles();
    var _j = InputProps.classes || {}, root = _j.root, InputClasses = __rest(_j, ["root"]);
    var _k = useState(''), inputValue = _k[0], setInputValue = _k[1];
    var _l = useState(false), isFocused = _l[0], setIsFocused = _l[1];
    var values = wrapInArrayIfNeeded(valuesProp);
    var hasValues = values.length > 0;
    var clearInputValue = function () { return setInputValue(''); };
    var setFocused = function () { return setIsFocused(true); };
    var setUnfocused = function () { return setIsFocused(false); };
    var removeValueAt = function (index) { return onChange(remove(index, 1, values)); };
    var appendValue = function (value) { return onChange(uniq(__spreadArray(__spreadArray([], values, true), [value], false))); };
    var appendInputValueIfNotEmpty = function () {
        if (!isEmptyValue(inputValue)) {
            appendValue(inputValue);
            clearInputValue();
        }
    };
    var popLastValueIfInputIsEmpty = function () {
        if (inputValue === '' && hasValues) {
            removeValueAt(-1);
        }
    };
    var hintText = !hasValues && isFocused ? EMPTY_VALUES_EDIT_HINT : '';
    return (React.createElement(Tooltip, { title: hintText },
        React.createElement(TextField, __assign({ variant: "standard" }, otherProps, { classes: {
                root: classes.root
            }, value: inputValue, onChange: pipe(getValue, setInputValue), InputProps: __assign(__assign({}, InputProps), { startAdornment: values.map(function (value, index) { return (React.createElement(ValueChip, { key: value, label: getValueLabel(value), onDelete: function () { return removeValueAt(index); } })); }), inputComponent: AutosizeInputWrapper, classes: __assign({ root: classnames(styles.inputRoot, root, (_b = {}, _b[styles.isCrossedOut] = isCrossedOut, _b)), input: classnames(styles.autosizeInput, InputClasses.input) }, InputClasses) }), inputProps: __assign(__assign({}, inputProps), { CustomInputComponent: InputProps.inputComponent }), onKeyPress: when(propEq('key', 'Enter'), appendInputValueIfNotEmpty), onKeyDown: when(propEq('key', 'Backspace'), popLastValueIfInputIsEmpty), onFocus: setFocused, onBlur: pipe(appendInputValueIfNotEmpty, setUnfocused) }))));
};
