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 { Dropdown } from "semantic-ui-react";
import * as React from 'react';
import { useState, useEffect } from "react";
import { uniq } from "lodash";
export function createDropdownTextMulti(config) {
    return function (props) {
        var onChange = props.onChange, value = props.value, onSetError = props.onSetError, ctlProps = __rest(props, ["onChange", "value", "onSetError"]);
        var _a = useState(false), loading = _a[0], setLoading = _a[1];
        var _b = useState([]), results = _b[0], setResults = _b[1];
        useEffect(function () {
            setLoading(true);
            config.getData('')
                .then(function (res) {
                var allRes = uniq((value || []).concat(res));
                setResults(allRes.map(function (r, id) { return ({
                    value: id,
                    key: r,
                    text: r
                }); }));
                setLoading(false);
            })
                .catch(function (err) {
                setResults([]);
                setLoading(false);
            });
        }, [value]);
        var getValues = function () {
            return value && results.map(function (res, ix) {
                return value.findIndex(function (item) { return item === res.key; });
            }).filter(function (i) { return i > -1; }) || [];
        };
        var handleResultSelect = function (event, d) {
            var ix = d.value;
            var selData = ix.map(function (i) { return typeof (i) === 'number' ? results[i].key : i; });
            onChange(selData);
        };
        return <Dropdown {...ctlProps} loading={loading} onChange={handleResultSelect} allowAdditions={config.allowNew} selection multiple search value={getValues()} options={results}/>;
    };
}
