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, { useEffect } from 'react';
import { always, defaultTo, map, path, pipe, pluck, prop, propEq } from 'ramda';
import { GroupedDropDownEditor } from './components/GroupedDropDownEditor';
import { buildLookupEntries } from './helpers';
export var LookupEditor = function (_a) {
    var multiple = _a.multiple, value = _a.value, lookupTypeCode = _a.lookupCode, lookups = _a.lookups, onChange = _a.onChange, getLookups = _a.getLookups, otherProps = __rest(_a, ["multiple", "value", "lookupCode", "lookups", "onChange", "getLookups"]);
    var codeToLookup = function (lookupCode) { return ({
        lookupCode: lookupCode,
        value: path([lookupTypeCode, lookupCode, 'displayName'], lookups)
    }); };
    useEffect(function () {
        var _a;
        if (getLookups) {
            (_a = getLookups()) === null || _a === void 0 ? void 0 : _a.catch(function () { });
        }
    }, [getLookups]);
    var entries = buildLookupEntries(lookupTypeCode, lookups); // TODO: this code could be not valid if lookups are not loaded
    var commonProps = __assign(__assign({}, otherProps), { multiple: multiple, entries: entries });
    value = defaultTo(multiple ? [] : {}, value);
    return multiple ? (React.createElement(GroupedDropDownEditor, __assign({}, commonProps, { value: pluck('lookupCode', value), getValuePlaceholder: function (lookupCode) { return value.find(propEq('lookupCode', lookupCode)).value; }, onChange: pipe(map(codeToLookup), onChange) }))) : (React.createElement(GroupedDropDownEditor, __assign({}, commonProps, { value: prop('lookupCode', value), getValuePlaceholder: always(prop('value', value)), onChange: pipe(codeToLookup, onChange) })));
};
