{"version":3,"file":"useSorting.mjs","names":[],"sources":["../../../../package/common/virtualList/useSorting.ts"],"sourcesContent":["import { useMemo, useState } from 'react';\n\nimport { nextSort, sortItems } from './virtualList.utils';\nimport type { VirtualListColumn, VirtualListSort } from './virtualList.types';\n\ninterface UseSortingArgs<T> {\n  items: T[];\n  columns?: VirtualListColumn<T>[];\n  /** Controlled sort. When provided, internal state is ignored. */\n  sort?: VirtualListSort | null;\n  defaultSort?: VirtualListSort | null;\n  onSortChange?: (sort: VirtualListSort | null) => void;\n  /** For comparing strings. */\n  locale?: string;\n}\n\ninterface UseSortingResult<T> {\n  /** `items`, reordered when the sorted column carries a comparator. */\n  rows: T[];\n  effectiveSort: VirtualListSort | null;\n  /** True only when this hook reordered the rows, not when it merely reported. */\n  isSorted: boolean;\n  handleHeaderSort: (key: string) => void;\n}\n\n/**\n * Header sorting, controlled or uncontrolled. Whether the list reorders the data\n * or only reports the click is the column's decision, not a prop: a column with\n * `sortValue`/`compare` is sorted here, a bare `sortable` one is left alone so a\n * server-side sort can own it. Sorting a paginated list here would silently\n * order only the rows loaded so far.\n */\nexport function useSorting<T>({\n  items,\n  columns,\n  sort,\n  defaultSort = null,\n  onSortChange,\n  locale,\n}: UseSortingArgs<T>): UseSortingResult<T> {\n  const isControlled = sort !== undefined;\n  const [internalSort, setInternalSort] = useState<VirtualListSort | null>(defaultSort);\n  const effectiveSort = isControlled ? sort! : internalSort;\n\n  // Returns `items` itself when nothing applies, which is what `isSorted` reads.\n  const rows = useMemo(() => sortItems(items, effectiveSort, columns, locale), [items, effectiveSort, columns, locale]);\n\n  function handleHeaderSort(key: string) {\n    const next = nextSort(effectiveSort, key);\n    if (!isControlled) setInternalSort(next);\n    onSortChange?.(next);\n  }\n\n  return { rows, effectiveSort, isSorted: rows !== items, handleHeaderSort };\n}\n"],"mappings":"6HAgCA,SAAgB,EAAc,CAC5B,QACA,UACA,OACA,cAAc,KACd,eACA,UACyC,CACzC,IAAM,EAAe,IAAS,IAAA,GACxB,CAAC,EAAc,GAAmB,EAAiC,CAAW,EAC9E,EAAgB,EAAe,EAAQ,EAGvC,EAAO,MAAc,EAAU,EAAO,EAAe,EAAS,CAAM,EAAG,CAAC,EAAO,EAAe,EAAS,CAAM,CAAC,EAEpH,SAAS,EAAiB,EAAa,CACrC,IAAM,EAAO,EAAS,EAAe,CAAG,EACnC,GAAc,EAAgB,CAAI,EACvC,IAAe,CAAI,CACrB,CAEA,MAAO,CAAE,OAAM,gBAAe,SAAU,IAAS,EAAO,kBAAiB,CAC3E"}