import type { FieldsSource } from '@wix/bex-core'; import type { SchemaState } from './SchemaState'; import { schemaFieldManagement } from './schemaFieldManagement'; /** * Exposes SchemaState as a source-neutral FieldsSource. Reads through to the * loaded schema so MobX reactivity flows. A flat schema has no groups or * archiving, so those members are omitted. */ export const schemaFieldsSource = (s: SchemaState): FieldsSource => { const { sourceState } = s; const source: FieldsSource = { load: () => sourceState.load(), get fields() { return sourceState.fields; }, get filterableFields() { return s.filterableFields; }, get manageableFields() { return sourceState.fields.filter((f) => !f.isSystem); }, columnIdForField: (field) => field.id, queryFieldId: (field) => field.id, getCellValue: (row, field) => (row as Record)[field.id], setCellValue: (row, field, value) => ({ ...(row as Record), [field.id]: value, }), fieldManagement: schemaFieldManagement(sourceState), get fieldActions() { return sourceState.fieldActions; }, // Merge a returned fields map into the loaded schema (e.g. after an import // creates fields). `mergeFields` is a SourceState action, so the columns // rebuild reactively with no refetch. mergeFields: (fields) => sourceState.mergeFields(fields), }; return source; };