"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupCrosswalksByTypesAndSources = void 0;
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var types_1 = require("../types");
var isEmptyCrosswalk = function (entityTypeUri, selectedAttributeTypes, crosswalk) {
    var visibleAttributes = (0, mdm_sdk_1.getCrosswalkAttributes)(crosswalk).filter(function (attributeUri) {
        var attributeTypeUri = (0, mdm_sdk_1.getAttributeTypeUriByValueUri)(attributeUri, entityTypeUri);
        return selectedAttributeTypes.some((0, ramda_1.propEq)('uri', attributeTypeUri));
    });
    return visibleAttributes.length === 0;
};
var isCrosswalkWithoutSimple = function (entityTypeUri, selectedAttributeTypes, crosswalk) {
    return !(0, mdm_sdk_1.getCrosswalkAttributes)(crosswalk).some(function (attributeUri) {
        var attributeTypeUri = (0, mdm_sdk_1.getAttributeTypeUriByValueUri)(attributeUri, entityTypeUri);
        var attributeType = selectedAttributeTypes.find((0, ramda_1.propEq)('uri', attributeTypeUri));
        return attributeType && !(0, mdm_sdk_1.isComplexAttribute)(attributeType);
    });
};
var getCrosswalksWeight = function (crosswalks) {
    if (crosswalks[types_1.CrosswalkType.Simple].length > 0) {
        return 1;
    }
    if (crosswalks[types_1.CrosswalkType.Dependent].length > 0) {
        return crosswalks[types_1.CrosswalkType.Empty].length > 0 ? 2 : 3;
    }
    return 4;
};
var sortDependentCrosswalksBySourceTable = (0, ramda_1.evolve)((_a = {},
    _a[types_1.CrosswalkType.Dependent] = (0, ramda_1.sort)(function (cwA, cwB) {
        var sourceTableA = cwA.sourceTable || '';
        var sourceTableB = cwB.sourceTable || '';
        if (sourceTableA === sourceTableB)
            return 0;
        if (!sourceTableA)
            return 1;
        if (!sourceTableB)
            return -1;
        return sourceTableA < sourceTableB ? -1 : 1;
    }),
    _a));
exports.groupCrosswalksByTypesAndSources = (0, ramda_1.curry)(function (entityTypeUri, selectedAttributeTypes, crosswalks) {
    var crosswalksBySourcesMap = (crosswalks || []).reduce(function (acc, crosswalk) {
        var _a;
        if (!acc[crosswalk.type]) {
            acc[crosswalk.type] = (_a = {},
                _a[types_1.CrosswalkType.Simple] = [],
                _a[types_1.CrosswalkType.Empty] = [],
                _a[types_1.CrosswalkType.Dependent] = [],
                _a);
        }
        if (crosswalk.ownerType === 'relation') {
            acc[crosswalk.type][types_1.CrosswalkType.Dependent].push(crosswalk);
        }
        else if (isEmptyCrosswalk(entityTypeUri, selectedAttributeTypes, crosswalk)) {
            acc[crosswalk.type][types_1.CrosswalkType.Empty].push(crosswalk);
        }
        else if (isCrosswalkWithoutSimple(entityTypeUri, selectedAttributeTypes, crosswalk)) {
            acc[crosswalk.type][types_1.CrosswalkType.Dependent].push(crosswalk);
        }
        else {
            acc[crosswalk.type][types_1.CrosswalkType.Simple].push(crosswalk);
        }
        return acc;
    }, {});
    return Object.keys(crosswalksBySourcesMap)
        .sort(function (sourceA, sourceB) {
        var weightA = getCrosswalksWeight(crosswalksBySourcesMap[sourceA]);
        var weightB = getCrosswalksWeight(crosswalksBySourcesMap[sourceB]);
        var weightDiff = weightA - weightB;
        return weightDiff || (sourceA < sourceB ? -1 : 1);
    })
        .map(function (source) { return ({
        source: source,
        crosswalksByTypes: sortDependentCrosswalksBySourceTable(crosswalksBySourcesMap[source])
    }); });
});
