"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.useCommentsEntitiesMap = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useSafePromise_1 = require("../useSafePromise");
var MdmModuleContext_1 = require("../../contexts/MdmModuleContext");
var SELECT_OPTION = 'uri,label,type';
var useCommentsEntitiesMap = function (comments) {
    var safePromise = (0, useSafePromise_1.useSafePromise)();
    var currentEntityUri = (0, MdmModuleContext_1.useMdmEntityUriWithDataTenant)();
    var _a = (0, react_1.useState)({}), entitiesMap = _a[0], setEntitiesMap = _a[1];
    var getEntities = (0, react_1.useCallback)(function (entitiesUris) {
        var requestFilters = [
            {
                filter: mdm_sdk_1.FilterOptions.EQUALS,
                fieldName: 'uri',
                values: entitiesUris
            }
        ];
        var requestOptions = {
            max: entitiesUris.length,
            activeness: mdm_sdk_1.ActivityFilter.ALL,
            select: 'uri,label,type'
        };
        return safePromise((0, mdm_sdk_1.getFilteredEntities)({ filters: requestFilters, options: requestOptions }));
    }, [safePromise]);
    var getNotFoundEntity = function (entityUri) {
        return (0, mdm_sdk_1.getEntity)(entityUri, { options: SELECT_OPTION })
            .then(function (entity) { return (__assign(__assign({}, entity), { uri: entityUri })); })
            .catch(function () { return ({}); });
    };
    var addNotFoundEntities = (0, react_1.useCallback)(function (entitiesUris, entities) {
        return safePromise(Promise.all((entitiesUris || []).map(getNotFoundEntity))).then(function (notFoundEntities) { return (0, ramda_1.pipe)((0, ramda_1.reject)(ramda_1.isEmpty), (0, ramda_1.concat)(entities))(notFoundEntities); });
    }, [safePromise]);
    (0, react_1.useEffect)(function () {
        var uniqEntitiesUris = (0, ramda_1.pipe)((0, ramda_1.reduce)(function (entitiesUris, comment) {
            var type = comment.objectType, relatedObjectUris = comment.relatedObjectUris;
            if (type === 'relation' || type === 'potentialMatch') {
                (relatedObjectUris || []).forEach(function (uri) {
                    if (uri !== currentEntityUri && (0, mdm_sdk_1.isEntityUri)(uri) && !entitiesMap[uri]) {
                        entitiesUris.push(uri);
                    }
                });
            }
            return entitiesUris;
        }, []), ramda_1.uniq)(comments || []);
        if (uniqEntitiesUris.length) {
            getEntities(uniqEntitiesUris)
                .then(function (results) {
                var getNotFoundUris = (0, ramda_1.pipe)((0, ramda_1.map)((0, ramda_1.prop)('uri')), (0, ramda_1.difference)(uniqEntitiesUris));
                var notFoundEntities = getNotFoundUris(results);
                return addNotFoundEntities(notFoundEntities, results);
            })
                .then(function (results) {
                setEntitiesMap(function (prevEntitiesMap) {
                    return results.reduce(function (entities, entity) {
                        entities[entity.uri] = entity;
                        return entities;
                    }, __assign({}, prevEntitiesMap));
                });
            })
                .catch(function (error) {
                console.warn("Can't load entities for comments map", error);
            });
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [getEntities, addNotFoundEntities, comments, currentEntityUri]);
    return entitiesMap;
};
exports.useCommentsEntitiesMap = useCommentsEntitiesMap;
