// packages/block-editor/src/components/inserter/hooks/use-patterns-paging.js
import { useMemo, useState, useEffect } from "@wordpress/element";
import { usePrevious } from "@wordpress/compose";
import { getScrollContainer } from "@wordpress/dom";
var PAGE_SIZE = 20;
function usePatternsPaging(currentCategoryPatterns, currentCategory, scrollContainerRef, currentFilter = "") {
  const [currentPage, setCurrentPage] = useState(1);
  const previousCategory = usePrevious(currentCategory);
  const previousFilter = usePrevious(currentFilter);
  if ((previousCategory !== currentCategory || previousFilter !== currentFilter) && currentPage !== 1) {
    setCurrentPage(1);
  }
  const totalItems = currentCategoryPatterns.length;
  const pageIndex = currentPage - 1;
  const categoryPatterns = useMemo(() => {
    return currentCategoryPatterns.slice(
      pageIndex * PAGE_SIZE,
      pageIndex * PAGE_SIZE + PAGE_SIZE
    );
  }, [pageIndex, currentCategoryPatterns]);
  const numPages = Math.ceil(currentCategoryPatterns.length / PAGE_SIZE);
  const changePage = (page) => {
    const scrollContainer = getScrollContainer(
      scrollContainerRef?.current
    );
    scrollContainer?.scrollTo(0, 0);
    setCurrentPage(page);
  };
  useEffect(
    function scrollToTopOnCategoryChange() {
      const scrollContainer = getScrollContainer(
        scrollContainerRef?.current
      );
      scrollContainer?.scrollTo(0, 0);
    },
    [currentCategory, scrollContainerRef]
  );
  return {
    totalItems,
    categoryPatterns,
    numPages,
    changePage,
    currentPage
  };
}
export {
  usePatternsPaging as default
};
//# sourceMappingURL=use-patterns-paging.js.map
