{
  "version": 3,
  "sources": ["../../src/lib/recordDiff.ts"],
  "sourcesContent": ["import { RecordType, UnknownRecord } from '@tldraw/store'\nimport { ObjectDiff, applyObjectDiff, diffRecord } from './diff'\nimport { TLSyncError, TLSyncErrorCloseEventReason } from './TLSyncClient'\n\n/**\n * Validate a record and compute the diff between two states.\n * Returns null if the states are identical.\n *\n * @param prevState - The previous record state\n * @param newState - The new record state\n * @param recordType - The record type definition for validation\n * @param legacyAppendMode - If true, string append operations will be converted to Put operations\n * @returns Result containing the diff and new state, or null if no changes, or validation error\n *\n * @internal\n */\nexport function diffAndValidateRecord<R extends UnknownRecord>(\n\tprevState: R,\n\tnewState: R,\n\trecordType: RecordType<R, any>,\n\tlegacyAppendMode = false\n) {\n\tconst diff = diffRecord(prevState, newState, legacyAppendMode)\n\tif (!diff) return\n\ttry {\n\t\trecordType.validate(newState)\n\t} catch (error: any) {\n\t\tthrow new TLSyncError(error.message, TLSyncErrorCloseEventReason.INVALID_RECORD)\n\t}\n\treturn diff\n}\n\n/**\n * Apply a diff to a record state, validate the result, and compute the final diff.\n * Returns null if the diff produces no changes.\n *\n * @param prevState - The previous record state\n * @param diff - The object diff to apply\n * @param recordType - The record type definition for validation\n * @param legacyAppendMode - If true, string append operations will be converted to Put operations\n * @returns Result containing the final diff and new state, or null if no changes, or validation error\n *\n * @internal\n */\nexport function applyAndDiffRecord<R extends UnknownRecord>(\n\tprevState: R,\n\tdiff: ObjectDiff,\n\trecordType: RecordType<R, any>,\n\tlegacyAppendMode = false\n): [ObjectDiff, R] | undefined {\n\tconst newState = applyObjectDiff(prevState, diff)\n\tif (newState === prevState) return\n\tconst actualDiff = diffAndValidateRecord(prevState, newState, recordType, legacyAppendMode)\n\tif (!actualDiff) return\n\treturn [actualDiff, newState]\n}\n\n/**\n * Validate a record without computing a diff. Used when creating new records.\n *\n * @param state - The record state to validate\n * @param recordType - The record type definition for validation\n * @returns Result indicating success or validation error\n *\n * @internal\n */\nexport function validateRecord<R extends UnknownRecord>(state: R, recordType: RecordType<R, any>) {\n\ttry {\n\t\trecordType.validate(state)\n\t} catch (error: any) {\n\t\tthrow new TLSyncError(error.message, TLSyncErrorCloseEventReason.INVALID_RECORD)\n\t}\n}\n"],
  "mappings": "AACA,SAAqB,iBAAiB,kBAAkB;AACxD,SAAS,aAAa,mCAAmC;AAclD,SAAS,sBACf,WACA,UACA,YACA,mBAAmB,OAClB;AACD,QAAM,OAAO,WAAW,WAAW,UAAU,gBAAgB;AAC7D,MAAI,CAAC,KAAM;AACX,MAAI;AACH,eAAW,SAAS,QAAQ;AAAA,EAC7B,SAAS,OAAY;AACpB,UAAM,IAAI,YAAY,MAAM,SAAS,4BAA4B,cAAc;AAAA,EAChF;AACA,SAAO;AACR;AAcO,SAAS,mBACf,WACA,MACA,YACA,mBAAmB,OACW;AAC9B,QAAM,WAAW,gBAAgB,WAAW,IAAI;AAChD,MAAI,aAAa,UAAW;AAC5B,QAAM,aAAa,sBAAsB,WAAW,UAAU,YAAY,gBAAgB;AAC1F,MAAI,CAAC,WAAY;AACjB,SAAO,CAAC,YAAY,QAAQ;AAC7B;AAWO,SAAS,eAAwC,OAAU,YAAgC;AACjG,MAAI;AACH,eAAW,SAAS,KAAK;AAAA,EAC1B,SAAS,OAAY;AACpB,UAAM,IAAI,YAAY,MAAM,SAAS,4BAA4B,cAAc;AAAA,EAChF;AACD;",
  "names": []
}
