"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 __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));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultipleInput = void 0;
var react_1 = __importStar(require("react"));
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var classnames_1 = __importDefault(require("classnames"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var TextField_1 = __importDefault(require("@mui/material/TextField"));
var Tooltip_1 = __importDefault(require("@mui/material/Tooltip"));
var events_1 = require("../helpers/events");
var ValueChip_1 = require("../ValueChip");
var AutosizeInputWrapper_1 = require("./components/AutosizeInputWrapper");
var styles_1 = require("./styles");
var EMPTY_VALUES_EDIT_HINT = ui_i18n_1.default.text('Press Enter to add. You may enter multiple values');
var isEmptyValue = (0, ramda_1.pipe)(ramda_1.trim, ramda_1.isEmpty);
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 ? ramda_1.identity : _g, _h = _a.getValueLabel, getValueLabel = _h === void 0 ? ramda_1.identity : _h, otherProps = __rest(_a, ["values", "InputProps", "inputProps", "classes", "isCrossedOut", "onChange", "getValueLabel"]);
    var styles = (0, styles_1.useStyles)();
    var _j = InputProps.classes || {}, root = _j.root, InputClasses = __rest(_j, ["root"]);
    var _k = (0, react_1.useState)(''), inputValue = _k[0], setInputValue = _k[1];
    var _l = (0, react_1.useState)(false), isFocused = _l[0], setIsFocused = _l[1];
    var values = (0, mdm_sdk_1.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((0, ramda_1.remove)(index, 1, values)); };
    var appendValue = function (value) { return onChange((0, ramda_1.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_1.default.createElement(Tooltip_1.default, { title: hintText },
        react_1.default.createElement(TextField_1.default, __assign({ variant: "standard" }, otherProps, { classes: {
                root: classes.root
            }, value: inputValue, onChange: (0, ramda_1.pipe)(events_1.getValue, setInputValue), InputProps: __assign(__assign({}, InputProps), { startAdornment: values.map(function (value, index) { return (react_1.default.createElement(ValueChip_1.ValueChip, { key: value, label: getValueLabel(value), onDelete: function () { return removeValueAt(index); } })); }), inputComponent: AutosizeInputWrapper_1.AutosizeInputWrapper, classes: __assign({ root: (0, classnames_1.default)(styles.inputRoot, root, (_b = {}, _b[styles.isCrossedOut] = isCrossedOut, _b)), input: (0, classnames_1.default)(styles.autosizeInput, InputClasses.input) }, InputClasses) }), inputProps: __assign(__assign({}, inputProps), { CustomInputComponent: InputProps.inputComponent }), onKeyPress: (0, ramda_1.when)((0, ramda_1.propEq)('key', 'Enter'), appendInputValueIfNotEmpty), onKeyDown: (0, ramda_1.when)((0, ramda_1.propEq)('key', 'Backspace'), popLastValueIfInputIsEmpty), onFocus: setFocused, onBlur: (0, ramda_1.pipe)(appendInputValueIfNotEmpty, setUnfocused) }))));
};
exports.MultipleInput = MultipleInput;
