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);
};
import { useCallback } from 'react';
import { propOr, prop, pipe, keys } from 'ramda';
import { DataTypes, intl, getDependentLookups, getFacetedAttributeData, getLookups, resolveLookupsList, getNumberFormat } from '@reltio/mdm-sdk';
import { useMdmAction, useMdmAttributesPresentation, useMdmGlobalSearchRequestOptions, useMdmLookups, useMdmTenant } from '../contexts/MdmModuleContext';
export var useEditorContext = function (fieldName, dataTypeDefinition) {
    var lookupsLoaded = useMdmAction('lookupsLoaded');
    var lookupsForTypeResolved = useMdmAction('lookupsForTypeResolved');
    var tenant = useMdmTenant();
    var lookups = useMdmLookups();
    var attributesPresentation = useMdmAttributesPresentation();
    var globalSearchRequestOptions = useMdmGlobalSearchRequestOptions();
    var typeaheadGetSuggestion = useCallback(function (searchValue, max, pageNo) {
        return getFacetedAttributeData({
            fieldName: fieldName,
            searchValue: searchValue,
            options: __assign(__assign({}, globalSearchRequestOptions), { max: max, pageNo: pageNo })
        }).then(pipe(prop(fieldName), keys));
    }, [fieldName, globalSearchRequestOptions]);
    var type = dataTypeDefinition.type, values = dataTypeDefinition.values, lookupCode = dataTypeDefinition.lookupCode, dependentLookupCode = dataTypeDefinition.dependentLookupCode, options = dataTypeDefinition.options;
    switch (type) {
        case DataTypes.TYPE_ENUM:
            return { entries: values.map(function (value) { return ({ value: value }); }) };
        case DataTypes.TYPE_LOOKUP:
            return {
                lookups: lookups,
                lookupCode: lookupCode,
                getLookups: function () {
                    return getLookups().then(function (lookups) {
                        lookupsLoaded(lookups);
                    });
                }
            };
        case DataTypes.TYPE_DEPENDENT_LOOKUP:
            return {
                lookups: lookups,
                lookupCode: dependentLookupCode,
                getLookups: function (_a) {
                    var type = _a.type, parents = _a.parents, displayNamePrefix = _a.displayNamePrefix, max = _a.max, offset = _a.offset;
                    return getDependentLookups({ type: type, parents: parents, displayNamePrefix: displayNamePrefix, max: max, offset: offset }).then(function (_a) {
                        var codeValues = _a.codeValues;
                        return propOr({}, type, codeValues);
                    });
                },
                resolveLookups: function (type, codeValues) {
                    var lookupsList = codeValues.map(function (codeValue) { return ({ type: type, codeValue: codeValue }); });
                    return resolveLookupsList(lookupsList).then(function (response) {
                        if (Array.isArray(response)) {
                            var lookups_1 = response.reduce(function (acc, lookupData) { return (__assign(__assign({}, acc), lookupData[type])); }, {});
                            lookupsForTypeResolved({ type: type, values: lookups_1 });
                        }
                    });
                }
            };
        case DataTypes.TYPE_TYPEAHEAD:
            return {
                getSuggestions: typeaheadGetSuggestion
            };
        case DataTypes.TYPE_NUMBER:
        case DataTypes.TYPE_FLOAT:
        case DataTypes.TYPE_DOUBLE:
        case DataTypes.TYPE_LONG:
        case DataTypes.TYPE_INT:
            return {
                format: getNumberFormat(dataTypeDefinition, attributesPresentation, intl.getLocale())
            };
        case DataTypes.TYPE_FILE:
            return { tenant: tenant };
        case DataTypes.TYPE_SELECT:
            return { entries: options };
        default:
            return {};
    }
};
