"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useExpandInvalidRelations = void 0;
var ramda_1 = require("ramda");
var react_1 = require("react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ActionsHookContext_1 = require("../../contexts/ActionsHookContext");
var MdmModuleContext_1 = require("../../contexts/MdmModuleContext");
var useExpandInvalidRelations = function (configId) {
    var openRelationEditor = (0, MdmModuleContext_1.useMdmAction)('openRelationEditor');
    var mode = (0, MdmModuleContext_1.useMdmMode)();
    var profileErrors = (0, MdmModuleContext_1.useMdmProfileErrors)();
    var shouldOpenInvalidEditors = (0, react_1.useRef)(true);
    var listenToActions = (0, ActionsHookContext_1.useActionsHook)();
    var _a = (0, MdmModuleContext_1.useMdmRelationsDrafts)(configId) || {}, _b = _a.addedConnections, addedConnections = _b === void 0 ? [] : _b, _c = _a.editedConnections, editedConnections = _c === void 0 ? [] : _c;
    var relationsToAddAndEdit = (0, MdmModuleContext_1.useMdmAllRelationsToAddAndEdit)([configId]) || [];
    var preparedConnectionsList = (0, ramda_1.reject)(function (draftConnection) {
        return relationsToAddAndEdit.some((0, ramda_1.eqBy)((0, ramda_1.path)(['relation', 'relationUri']), draftConnection));
    }, __spreadArray(__spreadArray([], addedConnections.map(mdm_sdk_1.markNew), true), editedConnections.map(mdm_sdk_1.markEdited), true));
    var invalidConnections = (0, react_1.useMemo)(function () { return (0, mdm_sdk_1.getInvalidConnections)(profileErrors, preparedConnectionsList); }, [profileErrors, preparedConnectionsList]);
    var expandInvalidRelatonsListener = (0, MdmModuleContext_1.useMdmListener)('expandInvalidRelatonsListener');
    var setShouldOpenInvalidEditors = function (shouldOpen) { return (shouldOpenInvalidEditors.current = shouldOpen); };
    var listenerCallback = expandInvalidRelatonsListener(function () { return setShouldOpenInvalidEditors(true); });
    (0, react_1.useEffect)(function () {
        if ((0, mdm_sdk_1.isEditableMode)(mode) && invalidConnections.length && shouldOpenInvalidEditors.current) {
            invalidConnections.forEach(function (connection) { return openRelationEditor(configId, connection); });
            setShouldOpenInvalidEditors(false);
        }
    }, [mode, invalidConnections]);
    (0, react_1.useEffect)(function () {
        var stopListeningActions = listenToActions(listenerCallback);
        return function () { return stopListeningActions(); };
    }, [listenToActions]);
};
exports.useExpandInvalidRelations = useExpandInvalidRelations;
