(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "../../styles", "../DropdownItem", "../../helpers"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var styles_1 = require("../../styles");
    var DropdownItem_1 = (0, tslib_1.__importDefault)(require("../DropdownItem"));
    var helpers_1 = require("../../helpers");
    var DropdownList = (0, react_1.forwardRef)(function (props, ref) {
        var _a = (0, helpers_1.withDefaultGetters)(props), items = _a.items, _b = _a.direction, direction = _b === void 0 ? "bottom" : _b, getItemKey = _a.getItemKey, getItemOnClick = _a.getItemOnClick, getItemLeadContent = _a.getItemLeadContent, getItemGroupId = _a.getItemGroupId, getItemDisabled = _a.getItemDisabled, getItemAttributes = _a.getItemAttributes, getItemLabel = _a.getItemLabel, className = _a.className, onItemClick = _a.onItemClick, sortGroup = _a.sortGroup, width = _a.width, otherProps = (0, tslib_1.__rest)(_a, ["items", "direction", "getItemKey", "getItemOnClick", "getItemLeadContent", "getItemGroupId", "getItemDisabled", "getItemAttributes", "getItemLabel", "className", "onItemClick", "sortGroup", "width"]);
        var groups = (0, react_1.useMemo)(function () {
            var _groups = items.reduce(function (result, x) {
                if (getItemGroupId(x)) {
                    var index = result.length &&
                        result[0].length &&
                        result.findIndex(function (r) {
                            return getItemGroupId(r[0]) ===
                                getItemGroupId(x);
                        });
                    if (index !== -1) {
                        result[index].push(x);
                    }
                    else {
                        result.push([]);
                        result[result.length - 1].push(x);
                    }
                }
                else {
                    result[0].push(x);
                }
                return result;
            }, [[]]);
            return sortGroup
                ? _groups.sort(function (items1, items2) {
                    return sortGroup(getItemGroupId(items1[0]) || 1, getItemGroupId(items2[0]) || -1);
                })
                : _groups;
        }, [items]);
        return (react_1.default.createElement(styles_1.DropdownListWrapper, (0, tslib_1.__assign)({ ref: ref, direction: direction, className: className, width: width }, otherProps), groups.map(function (gItems, index) { return (react_1.default.createElement(react_1.default.Fragment, { key: "dropdown-group-" + getItemGroupId(gItems[0]) },
            react_1.default.createElement(styles_1.DropdownItemsWrapper, null, gItems.map(function (item) { return (react_1.default.createElement(DropdownItem_1.default, { key: getItemKey(item), item: item, getItemAttributes: getItemAttributes, getItemLabel: getItemLabel, getItemDisabled: getItemDisabled, getItemLeadContent: getItemLeadContent, getItemOnClick: getItemOnClick, onItemClick: onItemClick })); })),
            index !== groups.length - 1 && react_1.default.createElement(styles_1.DividerStyled, null))); })));
    });
    DropdownList.displayName = "DropdownList";
    exports.default = DropdownList;
});
