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 { flatten, has, isEmpty, map, partition, path, pipe, reduce, values } from 'ramda';
import { getDataChangeRequest, getEntitiesByUris } from '@reltio/mdm-sdk';
import { useSafePromise } from '../../../hooks/useSafePromise';
import { concatEntityRelationsObjects, createEntityRelationsObjectByEntity, createEntityRelationsObjectByRelation, extractObjectsFromChangeRequest, getEntitiesObjects, getRelationsObjects } from '../helpers/dcr';
import { showErrorMessage } from '../../../helpers/errors';
export var useChangeRequest = function (dcrUri) {
    var _a = useState([]), groupedObjects = _a[0], setGroupedObjects = _a[1];
    var _b = useState(null), dcr = _b[0], setDcr = _b[1];
    var safePromise = useSafePromise();
    var requestEntities = useCallback(function (entityUris) {
        if (isEmpty(entityUris)) {
            return Promise.resolve([]);
        }
        return safePromise(getEntitiesByUris(entityUris, { searchOptions: 'ovOnly', defaultMaxValues: 1 }));
    }, []); // eslint-disable-line
    var loadDcr = useCallback(function () {
        safePromise(getDataChangeRequest(dcrUri))
            .then(function (dcr) {
            var changes = dcr.changes, objectsInfo = dcr.objectsInfo;
            var objectsWithChanges = extractObjectsFromChangeRequest(changes, objectsInfo);
            var entityObjectsWithRelations = pipe(getEntitiesObjects, map(createEntityRelationsObjectByEntity(objectsWithChanges)))(objectsWithChanges);
            var relationsObjects = pipe(getRelationsObjects, map(createEntityRelationsObjectByRelation(objectsInfo)))(objectsWithChanges);
            var _a = partition(has('entity'), relationsObjects), relationsObjectsWithEntity = _a[0], relationsObjectsWithoutEntity = _a[1];
            var entityUris = relationsObjectsWithoutEntity.map(path(['relations', 0, 'startObjectUri']));
            requestEntities(entityUris)
                .then(function (entities) {
                var relationsObjectsWithRequestedEntities = relationsObjectsWithoutEntity.map(function (relationsObject, index) {
                    return __assign(__assign({}, relationsObject), { entity: __assign(__assign({}, entities[index]), { isExist: true }) });
                });
                pipe(flatten, reduce(concatEntityRelationsObjects, {}), values, setGroupedObjects)([
                    entityObjectsWithRelations,
                    relationsObjectsWithEntity,
                    relationsObjectsWithRequestedEntities
                ]);
            })
                .catch(showErrorMessage);
            setDcr(dcr);
        })
            .catch(showErrorMessage);
    }, [dcrUri, requestEntities, safePromise]);
    useEffect(function () {
        loadDcr();
    }, [loadDcr]);
    return { dcr: dcr, groupedObjects: groupedObjects, reloadDcr: loadDcr };
};
