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);
};
import { curry } from 'ramda';
import { getMaxItemWidth } from '@reltio/mdm-sdk';
import { checkAttributeIncludes, isEqualAttributes } from '../helpers/attributesView';
var filterItem = function (item, filterText) {
    return item.title.toLowerCase().includes(filterText.toLowerCase());
};
export var filterItemsTree = function (item, filterText) {
    return filterItem(item, filterText) || (item.children || []).some(function (item) { return filterItemsTree(item, filterText); });
};
export var filterSelectedAttributes = function (_a) {
    var attribute = _a.attribute, selectedAttributes = _a.selectedAttributes, checked = _a.checked;
    return checked
        ? selectedAttributes.concat(attribute)
        : selectedAttributes.filter(function (selectedAttribute) { return !isEqualAttributes(attribute, selectedAttribute); });
};
export var getAttributeIndex = function (selectedAttributes, attribute) {
    return selectedAttributes.findIndex(isEqualAttributes(attribute));
};
export var getContainerWidth = getMaxItemWidth('title');
export var getOnEnterHandler = function (_a) {
    var onListItemClick = _a.onListItemClick, selectedAttributes = _a.selectedAttributes;
    return function (_a) {
        var item = _a.item, groupData = _a.groupData;
        var checked = checkAttributeIncludes(item, selectedAttributes);
        onListItemClick(__assign(__assign({}, item), { groupName: groupData.groupName }), !checked);
    };
};
var getAttributeGroupData = function (item, groupId, groupName, filterText, level, noFilter) {
    if (level === void 0) { level = 0; }
    if (noFilter === void 0) { noFilter = false; }
    var isItemFiltered = filterItem(item, filterText);
    return [
        {
            item: filterText ? __assign(__assign({}, item), { filterText: filterText }) : item,
            level: level,
            groupId: groupId,
            groupName: groupName
        }
    ]
        .concat(item.children
        ? item.children
            .filter(function (subItem) { return noFilter || isItemFiltered || filterItemsTree(subItem, filterText); })
            .flatMap(function (subItem) {
            return getAttributeGroupData(subItem, groupId, groupName, filterText, level + 1, noFilter || isItemFiltered);
        })
        : [])
        .flat();
};
export var prepareGroupsForSelectAttributeList = curry(function (filterText, groups) {
    return groups
        .map(function (group) { return ({
        item: group,
        items: group.attributes
            .filter(function (attribute) { return filterItemsTree(attribute, filterText); })
            .map(function (item) { return getAttributeGroupData(item, group.id, group.title, filterText); })
    }); })
        .filter(function (group) { return group.items.length; });
});
