"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupColumnsDataByMatchAttributes = exports.getMatchAttrColumns = exports.getColumnsData = void 0;
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var getColumnsDataFromEntityAttributes = (0, ramda_1.pipe)((0, ramda_1.map)(function (attr) { return ({
    id: (0, mdm_sdk_1.attributeUriToSearchUri)(attr.uri),
    label: (0, mdm_sdk_1.getLabel)(attr.label),
    dataTypeDefinition: (0, mdm_sdk_1.getAttrDataTypeDefinition)(attr)
}); }));
exports.getColumnsData = (0, ramda_1.pipe)(mdm_sdk_1.getEntityAttributesFromMetadata, getColumnsDataFromEntityAttributes);
var isReferenceAttrColumn = function (column) { return (0, mdm_sdk_1.isReference)(column.dataTypeDefinition); };
var isMatchAttrColumn = (0, ramda_1.curry)(function (matchAttributes, column) {
    return matchAttributes
        .map(function (attrUri) { return (0, mdm_sdk_1.attributeUriToSearchUri)(attrUri); })
        .some(function (id) { return column.id === id || (isReferenceAttrColumn(column) && isSubAttrIdForColumn(column, id)); });
});
var isSubAttrIdForColumn = (0, ramda_1.curry)(function (parentColumn, columnId) {
    return columnId.startsWith("".concat(parentColumn.id, "."));
});
var isNestedForOneOfColumns = (0, ramda_1.curry)(function (columns, column) {
    return isNestedAttrColumn(column) && columns.some(function (_a) {
        var id = _a.id;
        return isSubAttrIdForColumn(column, id);
    });
});
var isNestedAttrColumn = function (column) { return (0, mdm_sdk_1.isNested)(column.dataTypeDefinition); };
var isRelatedToColumns = function (columns) { return (0, ramda_1.either)((0, ramda_1.includes)(ramda_1.__, columns), isNestedForOneOfColumns(columns)); };
var getMatchAttrColumns = function (matchAttributes, columnsData) {
    return (0, ramda_1.pipe)((0, ramda_1.reject)(isNestedAttrColumn), (0, ramda_1.filter)(isMatchAttrColumn(matchAttributes)))(columnsData);
};
exports.getMatchAttrColumns = getMatchAttrColumns;
var groupColumnsDataByMatchAttributes = function (matchAttributes, columnsData) {
    var _a = (0, ramda_1.pipe)((0, ramda_1.reject)(isNestedAttrColumn), (0, ramda_1.partition)(isMatchAttrColumn(matchAttributes)))(columnsData), matchAttrColumns = _a[0], otherColumns = _a[1];
    if (matchAttrColumns.length) {
        return [
            {
                id: 'match',
                label: ui_i18n_1.default.text('Match attributes'),
                columns: columnsData.filter(isRelatedToColumns(matchAttrColumns))
            },
            {
                id: 'all',
                label: ui_i18n_1.default.text('All attributes'),
                columns: columnsData.filter(isRelatedToColumns(otherColumns))
            }
        ];
    }
    return columnsData;
};
exports.groupColumnsDataByMatchAttributes = groupColumnsDataByMatchAttributes;
