"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);
};
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.isColumnSelected = exports.isGroupColumn = exports.getChildColumns = exports.buildColumnItems = void 0;
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var isGroupColumn = function (column) { return column.id === column.groupId; };
exports.isGroupColumn = isGroupColumn;
var isParentToColumn = (0, ramda_1.curry)(function (_a, parent) {
    var id = _a.id, groupId = _a.groupId;
    return parent.groupId === groupId && (id === parent.id || id.startsWith("".concat(parent.id, ".")));
});
var isGroupToColumn = (0, ramda_1.curry)(function (_a, group) {
    var groupId = _a.groupId;
    return groupId === group.id;
});
var isHideableColumn = (0, ramda_1.propOr)(true, 'hideable');
var getColumnRoot = function (columns, column) {
    return columns.find((0, ramda_1.either)(isGroupToColumn(column), isParentToColumn(column)));
};
var addColumn = function (columns, column) {
    var root = getColumnRoot(columns, column);
    if (root) {
        root.children = addColumn(root.children || [], column);
        return columns;
    }
    else {
        return columns.concat(column);
    }
};
var filterColumnsByLabel = (0, ramda_1.curry)(function (filterText, column) {
    return mdm_sdk_1.utils.strings.search(column.label, filterText) ||
        (column.children || []).some(filterColumnsByLabel(filterText));
});
var buildColumnsTrees = (0, ramda_1.reduce)(function (acc, columnData) {
    return addColumn(acc, {
        id: columnData.id,
        groupId: columnData.groupId,
        label: columnData.label,
        attrType: columnData.dataTypeDefinition
    });
}, []);
var buildFilteredColumnItems = function (filterText, columns, level) {
    if (level === void 0) { level = 0; }
    return (0, ramda_1.pipe)((0, ramda_1.filter)(filterColumnsByLabel(filterText)), (0, ramda_1.chain)(function (column) {
        var item = {
            item: __assign(__assign({}, column), { filterText: filterText }),
            level: level
        };
        var childrenItems = column.children
            ? buildFilteredColumnItems(filterText, column.children, level + 1)
            : [];
        return __spreadArray([item], childrenItems, true);
    }))(columns);
};
var isGroupedColumnsData = (0, ramda_1.has)('columns');
var flattenGroupedColumnsData = function (_a) {
    var id = _a.id, label = _a.label, columns = _a.columns;
    return __spreadArray([
        { id: id, groupId: id, label: label }
    ], columns.map((0, ramda_1.assoc)('groupId', id)), true);
};
var buildColumnItems = function (columnsData, filterText) {
    return (0, ramda_1.pipe)((0, ramda_1.chain)((0, ramda_1.when)(isGroupedColumnsData, flattenGroupedColumnsData)), (0, ramda_1.filter)((0, ramda_1.either)(isGroupColumn, isHideableColumn)), buildColumnsTrees, function (columns) { return buildFilteredColumnItems(filterText, columns); })(columnsData);
};
exports.buildColumnItems = buildColumnItems;
var getChildColumns = function (column) {
    return column.children ? (0, ramda_1.chain)(getChildColumns, column.children) : [column];
};
exports.getChildColumns = getChildColumns;
var isColumnSelected = function (selectedColumns, column) {
    var childColumns = getChildColumns(column);
    var isColumnSelected = function (column) { return selectedColumns.includes(column.id); };
    return isGroupColumn(column) ? childColumns.every(isColumnSelected) : childColumns.some(isColumnSelected);
};
exports.isColumnSelected = isColumnSelected;
