import { TableLegacySchema } from "../legacy/table" import type { TableLegacy } from "../legacy/table" import { RichTextContentType } from "../richText" import { TableContentSchema, TableContentType } from "../table" import type { TableContent } from "../table" import type { LegacyContentCtx, LegacyCodec } from "./legacyContentCtx" import { blockLegacyToValue, blockValueToLegacy } from "./richText" export const TableLegacyCodec = ( ctx: LegacyContentCtx, ): LegacyCodec => ({ name: "TableLegacy", is(input): input is TableContent { return TableContentSchema.safeParse(input).success }, toContent(input) { const parsed = TableLegacySchema.safeParse(input) if (!parsed.success) { return parsed } return { success: true, data: { __TYPE__: TableContentType, content: parsed.data.content.map((row) => { return { ...row, content: row.content.map((cell) => { return { ...cell, content: { __TYPE__: RichTextContentType, value: cell.content.map((block) => blockLegacyToValue(block)), }, } }), } }), }, } }, fromContent(input) { return { content: { content: input.content.map((row) => { return { ...row, content: row.content.map((cell) => { return { ...cell, content: cell.content.value.map((block) => blockValueToLegacy(block)), } }), } }), }, types: { [ctx.keyOfType]: "Table" }, keys: {}, } }, })