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, useEffect, useState } from 'react';
import { concat, difference, isEmpty, map, pipe, prop, reduce, reject, uniq } from 'ramda';
import { FilterOptions, getFilteredEntities, isEntityUri, getEntity, ActivityFilter } from '@reltio/mdm-sdk';
import { useSafePromise } from '../useSafePromise';
import { useMdmEntityUriWithDataTenant } from '../../contexts/MdmModuleContext';
var SELECT_OPTION = 'uri,label,type';
export var useCommentsEntitiesMap = function (comments) {
    var safePromise = useSafePromise();
    var currentEntityUri = useMdmEntityUriWithDataTenant();
    var _a = useState({}), entitiesMap = _a[0], setEntitiesMap = _a[1];
    var getEntities = useCallback(function (entitiesUris) {
        var requestFilters = [
            {
                filter: FilterOptions.EQUALS,
                fieldName: 'uri',
                values: entitiesUris
            }
        ];
        var requestOptions = {
            max: entitiesUris.length,
            activeness: ActivityFilter.ALL,
            select: 'uri,label,type'
        };
        return safePromise(getFilteredEntities({ filters: requestFilters, options: requestOptions }));
    }, [safePromise]);
    var getNotFoundEntity = function (entityUri) {
        return getEntity(entityUri, { options: SELECT_OPTION })
            .then(function (entity) { return (__assign(__assign({}, entity), { uri: entityUri })); })
            .catch(function () { return ({}); });
    };
    var addNotFoundEntities = useCallback(function (entitiesUris, entities) {
        return safePromise(Promise.all((entitiesUris || []).map(getNotFoundEntity))).then(function (notFoundEntities) { return pipe(reject(isEmpty), concat(entities))(notFoundEntities); });
    }, [safePromise]);
    useEffect(function () {
        var uniqEntitiesUris = pipe(reduce(function (entitiesUris, comment) {
            var type = comment.objectType, relatedObjectUris = comment.relatedObjectUris;
            if (type === 'relation' || type === 'potentialMatch') {
                (relatedObjectUris || []).forEach(function (uri) {
                    if (uri !== currentEntityUri && isEntityUri(uri) && !entitiesMap[uri]) {
                        entitiesUris.push(uri);
                    }
                });
            }
            return entitiesUris;
        }, []), uniq)(comments || []);
        if (uniqEntitiesUris.length) {
            getEntities(uniqEntitiesUris)
                .then(function (results) {
                var getNotFoundUris = pipe(map(prop('uri')), 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;
};
