"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_window_1 = require("react-window");
const react_select_1 = require("react-select");
const utils_1 = require("../../../styles/utils");
const styles_1 = require("../../../styles");
function isFocused(children) {
    if (!children) {
        return false;
    }
    const { isFocused } = children.props;
    return isFocused === true;
}
exports.isFocused = isFocused;
function getCurrentIndex(children) {
    return Math.max(children.findIndex(isFocused), 0);
}
exports.getCurrentIndex = getCurrentIndex;
const MenuListComponent = props => {
    const { options, children: childrenProp, maxHeight, getValue, getStyles, RSTheme, } = props;
    const { theme } = props, otherProps = __rest(props, ["theme"]);
    let initialOffset = 0;
    const inputsStyles = utils_1.inputs({ theme });
    const height = inputsStyles.height;
    const value = getValue();
    const listRef = react_1.useRef(null);
    const children = react_1.default.Children.toArray(childrenProp);
    const { paddingTop, paddingBottom } = getStyles('menuList', Object.assign({}, props, { theme: RSTheme }));
    react_1.useEffect(() => {
        const currentIndex = getCurrentIndex(children);
        /**
         * enables scrolling on key down arrow
         *
         * note: prevents scrolling on index 0 and 1 to avoid
         * returning to top of menu when it remains open after selecting
         */
        if (currentIndex > 1) {
            listRef && listRef.current.scrollToItem(currentIndex);
        }
    }, [children, childrenProp]);
    if (value && !Array.isArray(value)) {
        initialOffset = options.indexOf(value) * height;
    }
    return (react_1.default.createElement(react_select_1.components.MenuList, Object.assign({}, otherProps, { theme: RSTheme }),
        react_1.default.createElement(react_window_1.FixedSizeList, { overscanCount: 10, ref: listRef, height: maxHeight - paddingTop - paddingBottom, width: "100%", itemCount: children.length, itemSize: height, initialScrollOffset: initialOffset }, ({ index, style }) => {
            return (react_1.default.createElement("div", { style: style }, react_1.default.Children.toArray(children)[index]));
        })));
};
const MenuListWithTheme = styles_1.withTheme(MenuListComponent);
exports.MenuList = (_a) => {
    var { theme: RSTheme } = _a, otherProps = __rest(_a, ["theme"]);
    return react_1.default.createElement(MenuListWithTheme, Object.assign({ RSTheme: RSTheme }, otherProps));
};
//# sourceMappingURL=index.js.map