"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useEditorContext = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var useEditorContext = function (fieldName, dataTypeDefinition) {
    var lookupsLoaded = (0, MdmModuleContext_1.useMdmAction)('lookupsLoaded');
    var lookupsForTypeResolved = (0, MdmModuleContext_1.useMdmAction)('lookupsForTypeResolved');
    var tenant = (0, MdmModuleContext_1.useMdmTenant)();
    var lookups = (0, MdmModuleContext_1.useMdmLookups)();
    var attributesPresentation = (0, MdmModuleContext_1.useMdmAttributesPresentation)();
    var globalSearchRequestOptions = (0, MdmModuleContext_1.useMdmGlobalSearchRequestOptions)();
    var typeaheadGetSuggestion = (0, react_1.useCallback)(function (searchValue, max, pageNo) {
        return (0, mdm_sdk_1.getFacetedAttributeData)({
            fieldName: fieldName,
            searchValue: searchValue,
            options: __assign(__assign({}, globalSearchRequestOptions), { max: max, pageNo: pageNo })
        }).then((0, ramda_1.pipe)((0, ramda_1.prop)(fieldName), ramda_1.keys));
    }, [fieldName, globalSearchRequestOptions]);
    var type = dataTypeDefinition.type, values = dataTypeDefinition.values, lookupCode = dataTypeDefinition.lookupCode, dependentLookupCode = dataTypeDefinition.dependentLookupCode, options = dataTypeDefinition.options;
    switch (type) {
        case mdm_sdk_1.DataTypes.TYPE_ENUM:
            return { entries: values.map(function (value) { return ({ value: value }); }) };
        case mdm_sdk_1.DataTypes.TYPE_LOOKUP:
            return {
                lookups: lookups,
                lookupCode: lookupCode,
                getLookups: function () {
                    return (0, mdm_sdk_1.getLookups)().then(function (lookups) {
                        lookupsLoaded(lookups);
                    });
                }
            };
        case mdm_sdk_1.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 (0, mdm_sdk_1.getDependentLookups)({ type: type, parents: parents, displayNamePrefix: displayNamePrefix, max: max, offset: offset }).then(function (_a) {
                        var codeValues = _a.codeValues;
                        return (0, ramda_1.propOr)({}, type, codeValues);
                    });
                },
                resolveLookups: function (type, codeValues) {
                    var lookupsList = codeValues.map(function (codeValue) { return ({ type: type, codeValue: codeValue }); });
                    return (0, mdm_sdk_1.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 mdm_sdk_1.DataTypes.TYPE_TYPEAHEAD:
            return {
                getSuggestions: typeaheadGetSuggestion
            };
        case mdm_sdk_1.DataTypes.TYPE_NUMBER:
        case mdm_sdk_1.DataTypes.TYPE_FLOAT:
        case mdm_sdk_1.DataTypes.TYPE_DOUBLE:
        case mdm_sdk_1.DataTypes.TYPE_LONG:
        case mdm_sdk_1.DataTypes.TYPE_INT:
            return {
                format: (0, mdm_sdk_1.getNumberFormat)(dataTypeDefinition, attributesPresentation, mdm_sdk_1.intl.getLocale())
            };
        case mdm_sdk_1.DataTypes.TYPE_FILE:
            return { tenant: tenant };
        case mdm_sdk_1.DataTypes.TYPE_SELECT:
            return { entries: options };
        default:
            return {};
    }
};
exports.useEditorContext = useEditorContext;
