import type { Page } from './types' export const MOCK_DELAY_MS = 50 export const delay = () => new Promise(r => setTimeout(r, MOCK_DELAY_MS)) export function paginate(items: T[], page = 1, pageSize = 10): Page { const start = (page - 1) * pageSize return { items: items.slice(start, start + pageSize), total: items.length, page, pageSize } } export type SortDir = 'asc' | 'desc' /** * Sorts a copy of `items` by `key`. Sorting lives in the service rather than the * table because results are paginated — a client-side sort would only reorder the * rows on the current page, which misrepresents the data. */ export function sortRows( items: T[], key?: keyof T | null, dir: SortDir = 'asc', ): T[] { if (!key) return items const factor = dir === 'desc' ? -1 : 1 return [...items].sort((a, b) => { const left = a[key] const right = b[key] if (typeof left === 'number' && typeof right === 'number') return (left - right) * factor return String(left ?? '').localeCompare(String(right ?? '')) * factor }) }