import { getVisibleLeafColumns, orderByColumnPinning } from '../featureStubs' import { buildHeaderGroups } from './buildHeaderGroups' import type { Table_Internal } from '../../types/Table' import type { Header } from '../../types/Header' import type { RowData } from '../../types/type-utils' import type { TableFeatures } from '../../types/TableFeatures' import type { Header_Header } from './coreHeadersFeature.types' function collectLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, TValue, >( header: Header_Header, leafHeaders: Array>, ): void { for (let i = 0; i < header.subHeaders.length; i++) { collectLeafHeaders(header.subHeaders[i]!, leafHeaders) } leafHeaders.push(header as Header) } /** * Walks a header tree and collects all descendant leaf headers. * * The header itself is included after its descendants, matching the recursive * shape used by nested header groups. * * @example * ```ts * const leafHeaders = header_getLeafHeaders(header) * ``` */ export function header_getLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, TValue, >(header: Header) { const leafHeaders: Array> = [] collectLeafHeaders(header, leafHeaders) return leafHeaders } /** * Builds the render context passed to a column's `header` or `footer` template. * * The context contains the header, its column, and the owning table instance. * * @example * ```ts * const context = header_getContext(header) * ``` */ export function header_getContext< TFeatures extends TableFeatures, TData extends RowData, TValue, >(header: Header) { return { column: header.column, header, table: header.column.table, } } /** * Builds visible header groups for the current column tree. * * Column visibility and pinning are applied before groups are built. When no * columns are pinned, the fast path skips pin partitioning. * * @example * ```ts * const headerGroups = table_getHeaderGroups(table) * ``` */ export function table_getHeaderGroups< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { // Pinning and visibility are optional features; without them nothing is // pinned and every leaf column is visible return buildHeaderGroups( table.getAllColumns(), orderByColumnPinning( table, getVisibleLeafColumns(table), (column) => column.id, ), table, ) } /** * Builds footer groups by reversing the current header groups. * * Footer rendering uses the same header objects and grouping structure, but * renders them from leaf level back toward the root. * * @example * ```ts * const footerGroups = table_getFooterGroups(table) * ``` */ export function table_getFooterGroups< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { const headerGroups = table.getHeaderGroups() return [...headerGroups].reverse() } /** * Flattens every header from every header group into one array. * * The result includes parent headers and placeholder headers, in header-group * order from top to bottom. * * @example * ```ts * const flatHeaders = table_getFlatHeaders(table) * ``` */ export function table_getFlatHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { const headerGroups = table.getHeaderGroups() const result: Array> = [] for (let i = 0; i < headerGroups.length; i++) { const headers = headerGroups[i]!.headers for (let j = 0; j < headers.length; j++) { result.push(headers[j]!) } } return result } /** * Collects only the leaf headers from the current header tree. * * Parent/group headers are skipped, making the result suitable for rendering * one header per visible leaf column. * * @example * ```ts * const leafHeaders = table_getLeafHeaders(table) * ``` */ export function table_getLeafHeaders< TFeatures extends TableFeatures, TData extends RowData, >(table: Table_Internal) { const topHeaders = table.getHeaderGroups()[0]?.headers ?? [] const result: Array> = [] for (let i = 0; i < topHeaders.length; i++) { const leafHeaders = topHeaders[i]!.getLeafHeaders() for (let j = 0; j < leafHeaders.length; j++) { result.push(leafHeaders[j]!) } } return result }