{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-table",
  "type": "registry:lib",
  "title": "core/table",
  "description": "Framework-free behaviour shared by the ui-* components (table); no DOM, no lit. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [],
  "registryDependencies": [
    "@kanok-ui/core-field"
  ],
  "files": [
    {
      "path": "src/core/table.ts",
      "type": "registry:lib",
      "target": "components/ui/core/table.ts",
      "content": "/**\n * Data table behaviour, framework-free: Carbon's sorting and filtering, client pagination, and\n * the selection / expansion rules. Rows are plain objects with a stable `id`; every function\n * returns new arrays and leaves its inputs alone.\n */\n\nimport { valueText } from './field.ts';\n\n/** A row: a stable `id` (keeps selection across sorting), optional `disabled`, and the cells by key. */\nexport interface TableRow {\n    id: string;\n    disabled?: boolean;\n    [key: string]: unknown;\n}\n\n/** A column: the row property it shows. */\nexport interface TableColumn {\n    key: string;\n}\n\nexport type SortDirection = 'none' | 'ascending' | 'descending';\n\n/** What a custom comparison gets (Carbon `sortRow`): the column, direction, locale and the default compare to wrap. */\nexport interface SortContext {\n    key: string;\n    direction: Exclude<SortDirection, 'none'>;\n    locale: string;\n    compare: (a: unknown, b: unknown, locale: string) => number;\n}\n\n/** A cell's text: null and undefined show as empty. */\nexport const cellText = (value: unknown) => valueText(value);\n\nconst collators = new Map<string, Intl.Collator>();\n\n/** Same order as `localeCompare(…, locale, { numeric })`, without building a collator per comparison (40× faster sorts). */\nfunction collator(locale: string, numeric: boolean) {\n    const key = `${locale}|${numeric}`;\n    let found = collators.get(key);\n    if (!found) {\n        found = new Intl.Collator(locale, { numeric });\n        collators.set(key, found);\n    }\n    return found;\n}\n\n/**\n * Carbon `compare` (DataTable/tools/sorting.ts): numbers by value, strings with `localeCompare`\n * (numeric collation when both parse as numbers), anything else by its string form.\n */\nexport function compareCells(a: unknown, b: unknown, locale = 'en'): number {\n    const x = a ?? '';\n    const y = b ?? '';\n    if (typeof x === 'number' && typeof y === 'number') return x - y;\n    const sx = cellText(x);\n    const sy = cellText(y);\n    const numeric = !Number.isNaN(Number.parseFloat(sx)) && !Number.isNaN(Number.parseFloat(sy));\n    return collator(locale, numeric).compare(sx, sy);\n}\n\nconst NEXT_DIRECTION: Record<SortDirection, SortDirection> = {\n    none: 'ascending',\n    ascending: 'descending',\n    descending: 'none',\n};\n\n/** Carbon's sort cycle on the same header: none → ascending → descending → none. */\nexport const nextSortDirection = (current: SortDirection): SortDirection => NEXT_DIRECTION[current];\n\n/**\n * Rows ordered by `key` (stable; `none` keeps them as given). `sortRow` replaces the comparison\n * and is called for ascending order of its own choosing, as Carbon's `sortRow`.\n */\nexport function sortRows<R extends TableRow>(rows: readonly R[], key: string, direction: SortDirection, options: { locale?: string; sortRow?: (a: unknown, b: unknown, context: SortContext) => number } = {}): R[] {\n    if (!key || direction === 'none') return [...rows];\n    const locale = options.locale ?? 'en';\n    const context: SortContext = { key, direction, locale, compare: compareCells };\n    const sign = direction === 'ascending' ? 1 : -1;\n    const custom = options.sortRow;\n    return rows.toSorted((a, b) => (custom ? custom(a[key], b[key], context) : sign * compareCells(a[key], b[key], locale)));\n}\n\n/** Carbon `filterRows`: the trimmed text is a case-insensitive substring of a cell; boolean cells never match. */\nexport function rowMatchesText(row: TableRow, columns: readonly TableColumn[], text: string): boolean {\n    const q = text.trim().toLowerCase();\n    if (!q) return true;\n    return columns.some((c) => {\n        const value = row[c.key];\n        return typeof value !== 'boolean' && cellText(value).toLowerCase().includes(q);\n    });\n}\n\n/** Rows the search text keeps; `filterRow(row, text, columns)` replaces the test (empty text keeps every row). */\nexport function filterRows<R extends TableRow, C extends TableColumn>(rows: readonly R[], columns: readonly C[], text: string, filterRow?: (row: R, text: string, columns: C[]) => boolean): R[] {\n    if (!text.trim()) return [...rows];\n    return rows.filter((row) => (filterRow ? filterRow(row, text, [...columns]) : rowMatchesText(row, columns, text)));\n}\n\n// ---- pagination ----\n\n/** Pages needed for `total` rows of `pageSize` (at least 1; 1 when not paged). */\nexport const pageCount = (total: number, pageSize: number) => (pageSize > 0 ? Math.max(1, Math.ceil(total / pageSize)) : 1);\n\n/** The rows of `page` (clamped to 1…pageCount); every row when `pageSize` is 0. */\nexport function pageRows<R>(rows: readonly R[], page: number, pageSize: number): R[] {\n    if (pageSize <= 0) return [...rows];\n    const p = Math.min(Math.max(1, page), pageCount(rows.length, pageSize));\n    return rows.slice((p - 1) * pageSize, p * pageSize);\n}\n\n// ---- selection and expansion (ids) ----\n\n/** `ids` still present in `rows` (rows that left take their selection / expansion with them, as Carbon). */\nexport function keepPresent(ids: readonly string[], rows: readonly TableRow[]): string[] {\n    const present = new Set(rows.map((r) => r.id));\n    return ids.filter((id) => present.has(id));\n}\n\n/**\n * Selection after a row's checkbox (or radio) changes to `checked`; undefined when nothing changes\n * (a disabled row, or the radio row that is already chosen).\n */\nexport function toggleRowSelection(selected: readonly string[], row: TableRow, checked: boolean, radio = false): string[] | undefined {\n    if (row.disabled) return undefined;\n    if (radio) return selected.length === 1 && selected[0] === row.id ? undefined : [row.id];\n    const others = selected.filter((id) => id !== row.id);\n    return checked ? [...others, row.id] : others;\n}\n\n/**\n * Carbon select-all: when any enabled row of `scope` is selected it unselects the enabled rows of\n * `shown`, else it selects them; rows outside `shown` (filtered out, other pages) keep their state.\n */\nexport function toggleAllSelection(selected: readonly string[], scope: readonly TableRow[], shown: readonly TableRow[]): string[] {\n    const chosen = new Set(selected);\n    const anySelected = scope.some((r) => !r.disabled && chosen.has(r.id));\n    const ids = shown.filter((r) => !r.disabled).map((r) => r.id);\n    if (anySelected) {\n        const unselect = new Set(ids);\n        return selected.filter((id) => !unselect.has(id));\n    }\n    return [...selected, ...ids.filter((id) => !chosen.has(id))];\n}\n\n/** `selected` plus every enabled row of `rows` (\"Select all N\" across pages). */\nexport function selectAllRows(selected: readonly string[], rows: readonly TableRow[]): string[] {\n    const chosen = new Set(selected);\n    return [...selected, ...rows.filter((r) => !r.disabled && !chosen.has(r.id)).map((r) => r.id)];\n}\n\n/** Expanded rows after a row's expander is used. */\nexport function toggleExpanded(expanded: readonly string[], id: string): { ids: string[]; expanded: boolean } {\n    const open = expanded.includes(id);\n    return { ids: open ? expanded.filter((x) => x !== id) : [...expanded, id], expanded: !open };\n}\n\n/** Carbon expand-all: opens every shown row unless all of them are open, then closes them all. */\nexport function toggleAllExpanded(expanded: readonly string[], shownIds: readonly string[]): { ids: string[]; expanded: boolean } {\n    const open = new Set(expanded);\n    const allOpen = shownIds.length > 0 && shownIds.every((id) => open.has(id));\n    return { ids: allOpen ? [] : [...shownIds], expanded: !allOpen };\n}\n\n/** A column the user can show / hide (`hideable: false` keeps it on screen, `hidden: true` starts it hidden). */\nexport interface HideableColumn {\n    key: string;\n    hideable?: boolean;\n    hidden?: boolean;\n}\n\n/** The columns hidden until the user shows them (`hidden: true`): where the columns start, and what Reset brings back. */\nexport function defaultHiddenColumns(columns: readonly HideableColumn[]): string[] {\n    return columns.filter((c) => c.hidden && c.hideable !== false).map((c) => c.key);\n}\n\n/** Whether two lists of hidden columns hide the same ones. */\nexport function sameColumns(a: readonly string[], b: readonly string[]): boolean {\n    return a.length === b.length && a.every((key) => b.includes(key));\n}\n\n/** The columns on screen: those not in `hidden`; one that can't be hidden always stays, and if all would go the first stays. */\nexport function visibleColumns<C extends HideableColumn>(columns: readonly C[], hidden: readonly string[]): C[] {\n    if (hidden.length === 0) return [...columns];\n    const shown = columns.filter((c) => c.hideable === false || !hidden.includes(c.key));\n    return shown.length > 0 ? shown : columns.slice(0, 1);\n}\n\n/** Whether a column's checkbox is locked on: it can't be hidden, or it's the last one shown. */\nexport function columnLocked(columns: readonly HideableColumn[], hidden: readonly string[], key: string): boolean {\n    const column = columns.find((c) => c.key === key);\n    if (!column || column.hideable === false) return true;\n    const shown = visibleColumns(columns, hidden);\n    return shown.length === 1 && shown[0].key === key;\n}\n\n/** `hidden` after the user shows (`show`) or hides a column; keys of columns that no longer exist drop out. */\nexport function toggleColumn(columns: readonly HideableColumn[], hidden: readonly string[], key: string, show: boolean): string[] {\n    const known = hidden.filter((k) => columns.some((c) => c.key === k));\n    if (show) return known.filter((k) => k !== key);\n    if (known.includes(key) || columnLocked(columns, known, key)) return known;\n    return [...known, key];\n}\n"
    }
  ]
}
