import type { GridRowId } from '../types' import type { ApiSection } from './types' export type GridContentApi = { isEditable: (location: { columnId: string; rowId: GridRowId }) => boolean getValue: (location: { columnId: string; rowId: GridRowId }) => any getLabel: (location: { columnId: string; rowId: GridRowId }) => string getColumnLabel: (columnId: string) => string } export const createContentApi: ApiSection = (store) => { const api: GridContentApi = { isEditable: ({ columnId, rowId }) => { const state = store.getState() const row = store.selectors.selectRow(state, rowId) const rowMeta = store.selectors.selectRowMeta(state, rowId) if (!row) { return false } const columnDetails = store.selectors.selectColumn(state, columnId) const column = columnDetails?.data const editable = typeof column.cell?.editable === 'function' ? column.cell.editable({ columnId, row, rowMeta, }) : !!column.cell?.editable return editable }, getValue: ({ columnId, rowId }) => { const state = store.getState() const row = store.selectors.selectRow(state, rowId) const rowMeta = store.selectors.selectRowMeta(state, rowId) if (!row) { return undefined } const columnDetails = store.selectors.selectColumn(state, columnId) const column = columnDetails?.data const value = column?.cell?.value ? column?.cell.value({ columnId, row, rowMeta, }) : row?.[columnId] return value }, getLabel: ({ columnId, rowId }) => { const state = store.getState() const row = store.selectors.selectRow(state, rowId) const rowMeta = store.selectors.selectRowMeta(state, rowId) if (!row) { return '' } const columnDetails = store.selectors.selectColumn(state, columnId) const column = columnDetails?.data const value = column?.cell?.value ? column?.cell.value({ columnId, row, rowMeta, }) : row?.[columnId] const label = column?.cell?.label ? column?.cell?.label({ columnId, row, rowMeta, value, }) : value == null ? '' : value.toString() return label ?? '' }, getColumnLabel: (columnId: string) => { const state = store.getState() const { data: column } = store.selectors.selectColumn( state, columnId ) ?? { data: undefined } return (column?.label || column?.accessibleLabel) ?? '' }, } return api }