import { useMemo } from 'react';
import i18n from 'ui-i18n';
import { SortingStrategy } from '@reltio/mdm-sdk';
import { flattenGroupedItemsData } from '../VirtualGroupedList';
import { useMdmAttributesSortingStrategy } from '../contexts/MdmModuleContext';
import { preparedGroupedItems } from './helpers';
var GROUP_NAMES;
(function (GROUP_NAMES) {
    GROUP_NAMES["parent"] = "parent";
    GROUP_NAMES["attributes"] = "attributes";
})(GROUP_NAMES || (GROUP_NAMES = {}));
export var useMoreAttributesItems = function (_a) {
    var data = _a.data, parent = _a.parent, filter = _a.filter;
    var strategy = useMdmAttributesSortingStrategy();
    var hasGroups = Boolean(parent);
    var sortingFn = useMemo(function () {
        switch (strategy) {
            case SortingStrategy.ASC_BY_NAME:
                return function (item1, item2) { var _a; return (_a = item1 === null || item1 === void 0 ? void 0 : item1.label) === null || _a === void 0 ? void 0 : _a.localeCompare(item2 === null || item2 === void 0 ? void 0 : item2.label); };
            default:
                return function (_item1, _item2) { return 0; };
        }
    }, [strategy]);
    var preparedParentGroupItem = useMemo(function () { return (hasGroups ? preparedGroupedItems({ attrTypes: [parent], filter: filter, sortingFn: sortingFn }) : []); }, [hasGroups, parent, filter, sortingFn]);
    var parentGroup = useMemo(function () {
        return hasGroups && (preparedParentGroupItem === null || preparedParentGroupItem === void 0 ? void 0 : preparedParentGroupItem.length)
            ? [
                {
                    item: {
                        id: GROUP_NAMES.parent,
                        label: i18n.text('Parent')
                    },
                    items: preparedParentGroupItem
                }
            ]
            : [];
    }, [hasGroups, preparedParentGroupItem]);
    var preparedAttributesGroupItems = useMemo(function () {
        return preparedGroupedItems({
            attrTypes: data,
            filter: filter,
            sortingFn: sortingFn
        });
    }, [data, filter, sortingFn]);
    var attributesGroup = useMemo(function () {
        return hasGroups && (preparedAttributesGroupItems === null || preparedAttributesGroupItems === void 0 ? void 0 : preparedAttributesGroupItems.length)
            ? [
                {
                    item: {
                        id: GROUP_NAMES.attributes,
                        label: i18n.text('Attributes')
                    },
                    items: preparedAttributesGroupItems
                }
            ]
            : preparedAttributesGroupItems;
    }, [hasGroups, preparedAttributesGroupItems]);
    var items = useMemo(function () { return flattenGroupedItemsData((parentGroup || []).concat(attributesGroup)); }, [parentGroup, attributesGroup]);
    var parentGroupLength = parentGroup.length;
    var attributesGroupLength = attributesGroup.length;
    return { items: items, hasGroups: hasGroups, parentGroupLength: parentGroupLength, attributesGroupLength: attributesGroupLength };
};
