{"version":3,"sources":["../../src/use-pagination/index.ts"],"sourcesContent":["import { useMemo } from 'react'\nimport { useClamp } from '../use-clamp'\nimport { useCreation } from '../use-creation'\nimport { useLatest } from '../use-latest'\nimport { usePrevious } from '../use-previous'\nimport { useStableFn } from '../use-stable-fn'\nimport { useUpdateEffect } from '../use-update-effect'\n\nexport interface UsePaginationOptions<T> {\n  /**\n   * Total number of items.\n   *\n   * @defaultValue Number.POSITIVE_INFINITY\n   */\n  total?: number\n  /**\n   * The number of items to display per page.\n   *\n   * @defaultValue 10\n   */\n  pageSize?: number\n  /**\n   * The current page number.\n   *\n   * @defaultValue 1\n   */\n  page?: number\n  /**\n   * The original list to be paginated.\n   */\n  list?: T[]\n  /**\n   * Callback when the `page` change.\n   *\n   * @param {PaginationInfo} pagination - `PaginationInfo`, the pagination info\n   * @param {number} lastPage - `number`, the last page\n   * @returns {void} `void`\n   */\n  onPageChange?: (pagination: UsePaginationReturnsState, lastPage: number) => void\n  /**\n   * Callback when the `pageSize` change.\n   *\n   * @param {PaginationInfo} pagination - `PaginationInfo`, the pagination info\n   * @param {number} lastPageSize - `number`, the last page size\n   * @returns {void} `void`\n   */\n  onPageSizeChange?: (pagination: UsePaginationReturnsState, lastPageSize: number) => void\n  /**\n   * Callback when the `pageCount` change.\n   *\n   * @param {PaginationInfo} pagination - `PaginationInfo`, the pagination info\n   * @param {number} lastPageCount - `number`, the last page count\n   * @returns {void} `void`\n   */\n  onPageCountChange?: (pagination: UsePaginationReturnsState, lastPageCount: number) => void\n}\n\nexport interface UsePaginationReturnsState<T = any> {\n  /**\n   * Total number of items.\n   */\n  total: number\n  /**\n   * The current page number.\n   *\n   * @deprecated Use `page` instead.\n   */\n  currentPage: number\n  /**\n   * The number of items to display per page.\n   *\n   * @deprecated Use `pageSize` instead.\n   */\n  currentPageSize: number\n  /**\n   * The current page number.\n   *\n   * @since 1.7.0\n   */\n  page: number\n  /**\n   * The number of items to display per page.\n   *\n   * @since 1.7.0\n   */\n  pageSize: number\n  /**\n   * The total number of pages.\n   */\n  pageCount: number\n  /**\n   * The start index of the displayed list (helpful to slice the list).\n   */\n  indexStart: number\n  /**\n   * The end index of the displayed list (helpful to slice the list).\n   */\n  indexEnd: number\n  /**\n   * The start count of the displayed list.\n   *\n   * @since 1.7.0\n   */\n  countStart: number\n  /**\n   * The end count of the displayed list.\n   *\n   * @since 1.7.0\n   */\n  countEnd: number\n  /**\n   * Whether the current page is the first page.\n   */\n  isFirstPage: boolean\n  /**\n   * Whether the current page is the last page.\n   */\n  isLastPage: boolean\n  /**\n   * The sliced list to display.\n   */\n  list: T[]\n}\n\nexport interface UsePaginationReturnsActions {\n  /**\n   * Go to the previous page.\n   *\n   * @returns {void} `void`\n   */\n  prev(): void\n  /**\n   * Go to the next page.\n   *\n   * @returns {void} `void`\n   */\n  next(): void\n  /**\n   * Go to the specified page.\n   *\n   * @param {number} page - `number`, the page number\n   * @returns {void} `void`\n   */\n  go: (page: number) => void\n  /**\n   * Set the number of items to display per page.\n   *\n   * @param {number} size - `number`, the number of items to display per page\n   * @returns {void} `void`\n   */\n  setPageSize: (size: number) => void\n}\n\nexport type UsePaginationReturns<T> = readonly [UsePaginationReturnsState<T>, UsePaginationReturnsActions]\n\n/**\n * A React Hook that manage pagination state.\n */\nexport function usePagination<T = any>(options: UsePaginationOptions<T> = {}): UsePaginationReturns<T> {\n  const total = options.list ? options.list.length : (options.total ?? Number.POSITIVE_INFINITY)\n  const { list = [] as T[], page = 1, pageSize = 10, onPageChange, onPageSizeChange, onPageCountChange } = options\n\n  const isInfinity = total === Number.POSITIVE_INFINITY\n  const [currentPageSize, pageSizeActions] = useClamp(pageSize, 1, Number.POSITIVE_INFINITY)\n  const pageCount = isInfinity ? 0 : Math.max(1, Math.ceil(total / currentPageSize))\n  const [currentPage, pageActions] = useClamp(page, 1, isInfinity ? Number.POSITIVE_INFINITY : pageCount)\n\n  const go = useStableFn((page: number) => pageActions.set(page))\n  const prev = useStableFn(() => pageActions.dec())\n  const next = useStableFn(() => pageActions.inc())\n  const setPageSize = useStableFn((size: number) => pageSizeActions.set(size))\n\n  const [indexStart, indexEnd] = [(currentPage - 1) * currentPageSize, Math.min(currentPage * currentPageSize, total)]\n  const [countStart, countEnd] = [indexStart + 1, indexEnd]\n\n  const slicedList = useMemo(() => list.slice(indexStart, indexEnd), [list, indexStart, indexEnd])\n  const isFirstPage = currentPage === 1\n  const isLastPage = isInfinity ? false : currentPage === pageCount\n\n  const pagination = {\n    total,\n    currentPage,\n    page: currentPage,\n    currentPageSize,\n    pageSize: currentPageSize,\n    pageCount,\n    indexStart,\n    indexEnd,\n    countStart,\n    countEnd,\n    isFirstPage,\n    isLastPage,\n    list: slicedList,\n  }\n\n  const lastPage = usePrevious(currentPage) || currentPage\n  const lastPageSize = usePrevious(currentPageSize) || currentPageSize\n  const lastPageCount = usePrevious(pageCount) || pageCount\n\n  const latest = useLatest({\n    pagination,\n    lastPage,\n    lastPageSize,\n    lastPageCount,\n    onPageChange,\n    onPageCountChange,\n    onPageSizeChange,\n  })\n\n  // biome-ignore lint/correctness/useExhaustiveDependencies: effect need to re-run when currentPage changes\n  useUpdateEffect(() => {\n    latest.current.onPageChange?.(latest.current.pagination, latest.current.lastPage)\n  }, [currentPage])\n\n  // biome-ignore lint/correctness/useExhaustiveDependencies: effect need to re-run when pageCount changes\n  useUpdateEffect(() => {\n    latest.current.onPageCountChange?.(latest.current.pagination, latest.current.lastPageCount)\n  }, [pageCount])\n\n  // biome-ignore lint/correctness/useExhaustiveDependencies: effect need to re-run when currentPageSize changes\n  useUpdateEffect(() => {\n    latest.current.onPageSizeChange?.(latest.current.pagination, latest.current.lastPageSize)\n  }, [currentPageSize])\n\n  const actions = useCreation(() => ({ go, prev, next, setPageSize }))\n\n  return [pagination, actions] as const\n}\n"],"names":["usePagination","options","total","list","length","Number","POSITIVE_INFINITY","page","pageSize","onPageChange","onPageSizeChange","onPageCountChange","isInfinity","useClamp","currentPageSize","pageSizeActions","pageCount","Math","max","ceil","currentPage","pageActions","go","useStableFn","set","prev","dec","next","inc","setPageSize","size","min","indexStart","indexEnd","countStart","countEnd","slicedList","useMemo","slice","isFirstPage","isLastPage","pagination","lastPage","usePrevious","lastPageSize","lastPageCount","latest","useLatest","useUpdateEffect","current","actions","useCreation"],"mappings":";;;;+BA8JgBA;;;eAAAA;;;qBA9JQ;wBACC;2BACG;yBACF;2BACE;2BACA;+BACI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwJzB,SAASA;QAAuBC,UAAAA,iEAAmC,CAAC;QACrBA;IAApD,IAAMC,QAAQD,QAAQE,IAAI,GAAGF,QAAQE,IAAI,CAACC,MAAM,GAAIH,CAAAA,iBAAAA,QAAQC,KAAK,cAAbD,4BAAAA,iBAAiBI,OAAOC,iBAAiB;IAC7F,oBAAyGL,QAAjGE,MAAAA,kCAAO,EAAE,kCAAwFF,QAA/EM,MAAAA,kCAAO,uCAAwEN,QAArEO,UAAAA,0CAAW,wBAAIC,eAAsDR,QAAtDQ,cAAcC,mBAAwCT,QAAxCS,kBAAkBC,oBAAsBV,QAAtBU;IAEnF,IAAMC,aAAaV,UAAUG,OAAOC,iBAAiB;IACrD,IAA2CO,6BAAAA,IAAAA,kBAAQ,EAACL,UAAU,GAAGH,OAAOC,iBAAiB,OAAlFQ,kBAAoCD,cAAnBE,kBAAmBF;IAC3C,IAAMG,YAAYJ,aAAa,IAAIK,KAAKC,GAAG,CAAC,GAAGD,KAAKE,IAAI,CAACjB,QAAQY;IACjE,IAAmCD,8BAAAA,IAAAA,kBAAQ,EAACN,MAAM,GAAGK,aAAaP,OAAOC,iBAAiB,GAAGU,gBAAtFI,cAA4BP,eAAfQ,cAAeR;IAEnC,IAAMS,KAAKC,IAAAA,wBAAW,EAAC,SAAChB;eAAiBc,YAAYG,GAAG,CAACjB;;IACzD,IAAMkB,OAAOF,IAAAA,wBAAW,EAAC;eAAMF,YAAYK,GAAG;;IAC9C,IAAMC,OAAOJ,IAAAA,wBAAW,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