import type { KbxGridColumn } from '@shared/contracts/kbx-types'
import type { UiGridColumn } from './adapter/contracts'

/** Converts registry-owned KBX columns into the provider-neutral grid contract. */
export function toUiGridColumns(columns: readonly KbxGridColumn[]): UiGridColumn[] {
  return columns.map(column => {
    if (typeof column.field !== 'string') {
      throw new Error(`Grid column field must be a string: ${String(column.field)}`)
    }

    return {
      field: column.field,
      header: column.header,
      width: typeof column.width === 'number' ? column.width : undefined,
      sortable: column.sortable,
      filterable: column.filterable,
      formatter: column.formatter,
    }
  })
}
