import type { NotebookViewSettings, NotebookSchemaPayload, SchemaProperty } from './types' import { parsePropertyOptions, parseStoredPropertyValue } from './property-utils' import { isValidPropertyType } from './property-utils' type RawSchema = { id: string notebookId: string viewSettings: string | null properties: Array<{ id: string name: string type: string options: string | null position: number }> } export function parseViewSettings(raw: string | null | undefined): NotebookViewSettings { if (!raw) return {} try { const parsed = JSON.parse(raw) as NotebookViewSettings return { kanbanGroupPropertyId: parsed.kanbanGroupPropertyId ?? null, } } catch { return {} } } export function serializeSchema(raw: RawSchema | null): NotebookSchemaPayload | null { if (!raw) return null const properties: SchemaProperty[] = raw.properties .filter((p) => isValidPropertyType(p.type)) .sort((a, b) => a.position - b.position) .map((p) => ({ id: p.id, name: p.name, type: p.type as SchemaProperty['type'], options: parsePropertyOptions(p.options), position: p.position, })) return { id: raw.id, notebookId: raw.notebookId, viewSettings: parseViewSettings(raw.viewSettings), properties, } } export function buildNoteValuesMap( noteIds: string[], rows: Array<{ noteId: string; propertyId: string; value: string | null; property: { type: string } }>, ): Record> { const map: Record> = {} for (const id of noteIds) { map[id] = {} } for (const row of rows) { if (!map[row.noteId]) map[row.noteId] = {} if (!isValidPropertyType(row.property.type)) continue map[row.noteId][row.propertyId] = parseStoredPropertyValue( row.property.type, row.value, ) } return map }