"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.useChangeRequest = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var useSafePromise_1 = require("../../../hooks/useSafePromise");
var dcr_1 = require("../helpers/dcr");
var errors_1 = require("../../../helpers/errors");
var useChangeRequest = function (dcrUri) {
    var _a = (0, react_1.useState)([]), groupedObjects = _a[0], setGroupedObjects = _a[1];
    var _b = (0, react_1.useState)(null), dcr = _b[0], setDcr = _b[1];
    var safePromise = (0, useSafePromise_1.useSafePromise)();
    var requestEntities = (0, react_1.useCallback)(function (entityUris) {
        if ((0, ramda_1.isEmpty)(entityUris)) {
            return Promise.resolve([]);
        }
        return safePromise((0, mdm_sdk_1.getEntitiesByUris)(entityUris, { searchOptions: 'ovOnly', defaultMaxValues: 1 }));
    }, []); // eslint-disable-line
    var loadDcr = (0, react_1.useCallback)(function () {
        safePromise((0, mdm_sdk_1.getDataChangeRequest)(dcrUri))
            .then(function (dcr) {
            var changes = dcr.changes, objectsInfo = dcr.objectsInfo;
            var objectsWithChanges = (0, dcr_1.extractObjectsFromChangeRequest)(changes, objectsInfo);
            var entityObjectsWithRelations = (0, ramda_1.pipe)(dcr_1.getEntitiesObjects, (0, ramda_1.map)((0, dcr_1.createEntityRelationsObjectByEntity)(objectsWithChanges)))(objectsWithChanges);
            var relationsObjects = (0, ramda_1.pipe)(dcr_1.getRelationsObjects, (0, ramda_1.map)((0, dcr_1.createEntityRelationsObjectByRelation)(objectsInfo)))(objectsWithChanges);
            var _a = (0, ramda_1.partition)((0, ramda_1.has)('entity'), relationsObjects), relationsObjectsWithEntity = _a[0], relationsObjectsWithoutEntity = _a[1];
            var entityUris = relationsObjectsWithoutEntity.map((0, ramda_1.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 }) });
                });
                (0, ramda_1.pipe)(ramda_1.flatten, (0, ramda_1.reduce)(dcr_1.concatEntityRelationsObjects, {}), ramda_1.values, setGroupedObjects)([
                    entityObjectsWithRelations,
                    relationsObjectsWithEntity,
                    relationsObjectsWithRequestedEntities
                ]);
            })
                .catch(errors_1.showErrorMessage);
            setDcr(dcr);
        })
            .catch(errors_1.showErrorMessage);
    }, [dcrUri, requestEntities, safePromise]);
    (0, react_1.useEffect)(function () {
        loadDcr();
    }, [loadDcr]);
    return { dcr: dcr, groupedObjects: groupedObjects, reloadDcr: loadDcr };
};
exports.useChangeRequest = useChangeRequest;
