"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/inserter/hooks/use-patterns-paging.js
var use_patterns_paging_exports = {};
__export(use_patterns_paging_exports, {
  default: () => usePatternsPaging
});
module.exports = __toCommonJS(use_patterns_paging_exports);
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_dom = require("@wordpress/dom");
var PAGE_SIZE = 20;
function usePatternsPaging(currentCategoryPatterns, currentCategory, scrollContainerRef, currentFilter = "") {
  const [currentPage, setCurrentPage] = (0, import_element.useState)(1);
  const previousCategory = (0, import_compose.usePrevious)(currentCategory);
  const previousFilter = (0, import_compose.usePrevious)(currentFilter);
  if ((previousCategory !== currentCategory || previousFilter !== currentFilter) && currentPage !== 1) {
    setCurrentPage(1);
  }
  const totalItems = currentCategoryPatterns.length;
  const pageIndex = currentPage - 1;
  const categoryPatterns = (0, import_element.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 = (0, import_dom.getScrollContainer)(
      scrollContainerRef?.current
    );
    scrollContainer?.scrollTo(0, 0);
    setCurrentPage(page);
  };
  (0, import_element.useEffect)(
    function scrollToTopOnCategoryChange() {
      const scrollContainer = (0, import_dom.getScrollContainer)(
        scrollContainerRef?.current
      );
      scrollContainer?.scrollTo(0, 0);
    },
    [currentCategory, scrollContainerRef]
  );
  return {
    totalItems,
    categoryPatterns,
    numPages,
    changePage,
    currentPage
  };
}
//# sourceMappingURL=use-patterns-paging.js.map
