"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PickerVirtualizationListBox = void 0;
var _styled = _interopRequireDefault(require("@emotion/styled"));
var _react = require("react");
var _reactWindow = require("react-window");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2023-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const ListboxContainer = _styled.default.div({
  width: "100%",
  height: "100%"
});
const Row = ({
  data,
  index,
  setItemSize,
  style
}) => {
  const rowRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (rowRef.current) {
      const firstChild = rowRef.current.firstElementChild;
      const height = firstChild ? firstChild.clientHeight : rowRef.current.clientHeight;
      setItemSize(height);
    }
  }, [index, rowRef, setItemSize]);
  const baseOption = data[index];
  const {
    props
  } = baseOption;
  const styles = (0, _react.useMemo)(() => ({
    ...style,
    height: "auto"
  }), [style]);
  return (0, _jsxRuntime.jsx)("div", {
    ref: rowRef,
    children: (0, _jsxRuntime.jsx)("li", {
      ...props,
      style: styles
    })
  });
};
const OuterListboxContext = (0, _react.createContext)({});
const OuterListboxElementType = (0, _react.forwardRef)((props, ref) => {
  const outerProps = (0, _react.useContext)(OuterListboxContext);
  return (0, _jsxRuntime.jsx)("div", {
    ref: ref,
    ...props,
    ...outerProps
  });
});
const useResetCache = length => {
  const resetCacheRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (resetCacheRef.current) {
      resetCacheRef.current.resetAfterIndex(0, true);
    }
  }, [length]);
  return resetCacheRef;
};
const PickerVirtualizationListBox = (0, _react.forwardRef)((props = {}, ref) => {
  const [listHeight, setListHeight] = (0, _react.useState)(0);
  const {
    children,
    ...other
  } = props;
  const itemData = _react.Children.toArray(children).flatMap(child => typeof child === "number" || typeof child === "string" ? [child] : [child].concat("children" in child ? _react.Children.toArray(children) : []));
  const sizeMapRef = (0, _react.useRef)([]);
  const getListBoxHeight = (0, _react.useCallback)(() => {
    const COMBINED_LISTBOX_PADDING = 16;
    if (itemData.length > OVERSCAN_ROW_COUNT) {
      return 9999;
    } else {
      const itemsHeightCalculated = sizeMapRef.current.slice(0, itemData.length).map((_, index) => sizeMapRef.current[index] || 0).reduce((prevItemHeight, nextItemHeight) => prevItemHeight + nextItemHeight, 0);
      return COMBINED_LISTBOX_PADDING + itemsHeightCalculated;
    }
  }, [itemData, sizeMapRef]);
  (0, _react.useEffect)(() => {
    if (sizeMapRef.current.length && itemData.length) {
      setListHeight(getListBoxHeight());
    }
  }, [getListBoxHeight, itemData, sizeMapRef]);
  const OVERSCAN_ROW_COUNT = 8;
  const gridRef = useResetCache(itemData.length);
  const setItemSize = (0, _react.useCallback)(size => {
    gridRef?.current?.resetAfterIndex(0, true);
    sizeMapRef.current = sizeMapRef.current.concat(size);
  }, [gridRef, sizeMapRef]);
  const getItemSize = (0, _react.useCallback)(index => sizeMapRef.current[index] || 45, [sizeMapRef]);
  return (0, _jsxRuntime.jsx)(ListboxContainer, {
    ref: ref,
    children: (0, _jsxRuntime.jsx)(OuterListboxContext.Provider, {
      value: other,
      children: (0, _jsxRuntime.jsx)(_reactWindow.VariableSizeList, {
        height: listHeight,
        innerElementType: "ul",
        itemCount: itemData.length,
        itemData: itemData,
        itemSize: getItemSize,
        outerElementType: OuterListboxElementType,
        overscanCount: OVERSCAN_ROW_COUNT,
        ref: gridRef,
        width: "100%",
        children: ({
          data,
          index,
          style
        }) => (0, _jsxRuntime.jsx)(Row, {
          data: data,
          index: index,
          setItemSize: setItemSize,
          style: style
        })
      })
    })
  });
});
const MemoizedPickerVirtualizationListBox = exports.PickerVirtualizationListBox = (0, _react.memo)(PickerVirtualizationListBox);
MemoizedPickerVirtualizationListBox.displayName = "PickerVirtualizationListBox";
//# sourceMappingURL=PickerVirtualizationListBox.cjs.map