var _a;
import { getAttributeTypeUriByValueUri, getCrosswalkAttributes, isComplexAttribute } from '@reltio/mdm-sdk';
import { curry, evolve, propEq, sort } from 'ramda';
import { CrosswalkType } from '../types';
var isEmptyCrosswalk = function (entityTypeUri, selectedAttributeTypes, crosswalk) {
    var visibleAttributes = getCrosswalkAttributes(crosswalk).filter(function (attributeUri) {
        var attributeTypeUri = getAttributeTypeUriByValueUri(attributeUri, entityTypeUri);
        return selectedAttributeTypes.some(propEq('uri', attributeTypeUri));
    });
    return visibleAttributes.length === 0;
};
var isCrosswalkWithoutSimple = function (entityTypeUri, selectedAttributeTypes, crosswalk) {
    return !getCrosswalkAttributes(crosswalk).some(function (attributeUri) {
        var attributeTypeUri = getAttributeTypeUriByValueUri(attributeUri, entityTypeUri);
        var attributeType = selectedAttributeTypes.find(propEq('uri', attributeTypeUri));
        return attributeType && !isComplexAttribute(attributeType);
    });
};
var getCrosswalksWeight = function (crosswalks) {
    if (crosswalks[CrosswalkType.Simple].length > 0) {
        return 1;
    }
    if (crosswalks[CrosswalkType.Dependent].length > 0) {
        return crosswalks[CrosswalkType.Empty].length > 0 ? 2 : 3;
    }
    return 4;
};
var sortDependentCrosswalksBySourceTable = evolve((_a = {},
    _a[CrosswalkType.Dependent] = 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));
export var groupCrosswalksByTypesAndSources = curry(function (entityTypeUri, selectedAttributeTypes, crosswalks) {
    var crosswalksBySourcesMap = (crosswalks || []).reduce(function (acc, crosswalk) {
        var _a;
        if (!acc[crosswalk.type]) {
            acc[crosswalk.type] = (_a = {},
                _a[CrosswalkType.Simple] = [],
                _a[CrosswalkType.Empty] = [],
                _a[CrosswalkType.Dependent] = [],
                _a);
        }
        if (crosswalk.ownerType === 'relation') {
            acc[crosswalk.type][CrosswalkType.Dependent].push(crosswalk);
        }
        else if (isEmptyCrosswalk(entityTypeUri, selectedAttributeTypes, crosswalk)) {
            acc[crosswalk.type][CrosswalkType.Empty].push(crosswalk);
        }
        else if (isCrosswalkWithoutSimple(entityTypeUri, selectedAttributeTypes, crosswalk)) {
            acc[crosswalk.type][CrosswalkType.Dependent].push(crosswalk);
        }
        else {
            acc[crosswalk.type][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])
    }); });
});
