"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMoreAttributesItems = void 0;
var react_1 = require("react");
var ui_i18n_1 = __importDefault(require("ui-i18n"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var VirtualGroupedList_1 = require("../VirtualGroupedList");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var helpers_1 = require("./helpers");
var GROUP_NAMES;
(function (GROUP_NAMES) {
    GROUP_NAMES["parent"] = "parent";
    GROUP_NAMES["attributes"] = "attributes";
})(GROUP_NAMES || (GROUP_NAMES = {}));
var useMoreAttributesItems = function (_a) {
    var data = _a.data, parent = _a.parent, filter = _a.filter;
    var strategy = (0, MdmModuleContext_1.useMdmAttributesSortingStrategy)();
    var hasGroups = Boolean(parent);
    var sortingFn = (0, react_1.useMemo)(function () {
        switch (strategy) {
            case mdm_sdk_1.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 = (0, react_1.useMemo)(function () { return (hasGroups ? (0, helpers_1.preparedGroupedItems)({ attrTypes: [parent], filter: filter, sortingFn: sortingFn }) : []); }, [hasGroups, parent, filter, sortingFn]);
    var parentGroup = (0, react_1.useMemo)(function () {
        return hasGroups && (preparedParentGroupItem === null || preparedParentGroupItem === void 0 ? void 0 : preparedParentGroupItem.length)
            ? [
                {
                    item: {
                        id: GROUP_NAMES.parent,
                        label: ui_i18n_1.default.text('Parent')
                    },
                    items: preparedParentGroupItem
                }
            ]
            : [];
    }, [hasGroups, preparedParentGroupItem]);
    var preparedAttributesGroupItems = (0, react_1.useMemo)(function () {
        return (0, helpers_1.preparedGroupedItems)({
            attrTypes: data,
            filter: filter,
            sortingFn: sortingFn
        });
    }, [data, filter, sortingFn]);
    var attributesGroup = (0, react_1.useMemo)(function () {
        return hasGroups && (preparedAttributesGroupItems === null || preparedAttributesGroupItems === void 0 ? void 0 : preparedAttributesGroupItems.length)
            ? [
                {
                    item: {
                        id: GROUP_NAMES.attributes,
                        label: ui_i18n_1.default.text('Attributes')
                    },
                    items: preparedAttributesGroupItems
                }
            ]
            : preparedAttributesGroupItems;
    }, [hasGroups, preparedAttributesGroupItems]);
    var items = (0, react_1.useMemo)(function () { return (0, VirtualGroupedList_1.flattenGroupedItemsData)((parentGroup || []).concat(attributesGroup)); }, [parentGroup, attributesGroup]);
    var parentGroupLength = parentGroup.length;
    var attributesGroupLength = attributesGroup.length;
    return { items: items, hasGroups: hasGroups, parentGroupLength: parentGroupLength, attributesGroupLength: attributesGroupLength };
};
exports.useMoreAttributesItems = useMoreAttributesItems;
