import type { ReactNode } from "react"; // The column contract every table-shaped view shares — `DataTable`, the playlist manager's hybrid // table, the channel card grid — plus the filter predicate that reads it. // // It lives in `lib/`, not beside the header component in `components/ui/tableHeader`, because `lib/` // helpers need it too (`sortOptions`, `useFilterState`, `channelsUrlState`) and the dependency // direction is one-way: `lib/` may not import from `components/` (lint-enforced, see // eslint.config.js). The contract is data + closures; nothing in it renders. type ColumnFilter = | { kind: "text"; get: (row: T) => string } | { kind: "select"; options: { value: string; label: string }[]; test: (row: T, value: string) => boolean; } | { kind: "multi"; options: { value: string; label: string }[]; test: (row: T, values: string[]) => boolean; }; export interface Column { key: string; header: string; render: (row: T) => ReactNode; align?: "left" | "right" | "center"; width?: string; sortable?: boolean; sortValue?: (row: T) => string | number; filter?: ColumnFilter; // Keep the cell on one line so the column auto-sizes to its content (table-layout: auto). nowrap?: boolean; // Card fallback (below md): `cardPrimary` renders as the card heading (no label); `hideInCard` // omits the column; `cardLabel:false` shows the value without its header label. cardPrimary?: boolean; hideInCard?: boolean; cardLabel?: boolean; } export type FilterMap = Record; export function isActive(value: string | string[] | undefined): boolean { return Array.isArray(value) ? value.length > 0 : !!value; } /** Apply every column's own filter predicate. A column with no filter, or an empty value, passes. */ export function filterRows(rows: T[], columns: Column[], filters: FilterMap): T[] { return rows.filter((row) => columns.every((col) => { const f = col.filter; if (!f) return true; const val = filters[col.key]; if (!isActive(val)) return true; if (f.kind === "text") return f.get(row).toLowerCase().includes(String(val).toLowerCase()); if (f.kind === "select") return f.test(row, String(val)); return f.test(row, val as string[]); }), ); }