/*!
 * 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.
 */

import styled from "@emotion/styled";
import { Children, createContext, forwardRef, memo, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
import { VariableSizeList } from "react-window";
import { jsx as _jsx } from "react/jsx-runtime";
const ListboxContainer = styled.div({
  width: "100%",
  height: "100%"
});
const Row = ({
  data,
  index,
  setItemSize,
  style
}) => {
  const rowRef = useRef(null);
  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 = useMemo(() => ({
    ...style,
    height: "auto"
  }), [style]);
  return _jsx("div", {
    ref: rowRef,
    children: _jsx("li", {
      ...props,
      style: styles
    })
  });
};
const OuterListboxContext = createContext({});
const OuterListboxElementType = forwardRef((props, ref) => {
  const outerProps = useContext(OuterListboxContext);
  return _jsx("div", {
    ref: ref,
    ...props,
    ...outerProps
  });
});
const useResetCache = length => {
  const resetCacheRef = useRef(null);
  useEffect(() => {
    if (resetCacheRef.current) {
      resetCacheRef.current.resetAfterIndex(0, true);
    }
  }, [length]);
  return resetCacheRef;
};
const PickerVirtualizationListBox = forwardRef((props = {}, ref) => {
  const [listHeight, setListHeight] = useState(0);
  const {
    children,
    ...other
  } = props;
  const itemData = Children.toArray(children).flatMap(child => typeof child === "number" || typeof child === "string" ? [child] : [child].concat("children" in child ? Children.toArray(children) : []));
  const sizeMapRef = useRef([]);
  const getListBoxHeight = 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]);
  useEffect(() => {
    if (sizeMapRef.current.length && itemData.length) {
      setListHeight(getListBoxHeight());
    }
  }, [getListBoxHeight, itemData, sizeMapRef]);
  const OVERSCAN_ROW_COUNT = 8;
  const gridRef = useResetCache(itemData.length);
  const setItemSize = useCallback(size => {
    gridRef?.current?.resetAfterIndex(0, true);
    sizeMapRef.current = sizeMapRef.current.concat(size);
  }, [gridRef, sizeMapRef]);
  const getItemSize = useCallback(index => sizeMapRef.current[index] || 45, [sizeMapRef]);
  return _jsx(ListboxContainer, {
    ref: ref,
    children: _jsx(OuterListboxContext.Provider, {
      value: other,
      children: _jsx(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
        }) => _jsx(Row, {
          data: data,
          index: index,
          setItemSize: setItemSize,
          style: style
        })
      })
    })
  });
});
const MemoizedPickerVirtualizationListBox = memo(PickerVirtualizationListBox);
MemoizedPickerVirtualizationListBox.displayName = "PickerVirtualizationListBox";
export { MemoizedPickerVirtualizationListBox as PickerVirtualizationListBox };
//# sourceMappingURL=PickerVirtualizationListBox.js.map