@kitionstage/sdk 0.0.0-stage → 0.5.2-stage.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/README.md +73 -2
  2. package/dist/LICENSE +177 -0
  3. package/dist/THIRD_PARTY_NOTICES +28 -0
  4. package/dist/lib/assets/kitEngine.worker-3iMvwtlI.js +12732 -0
  5. package/dist/lib/assets/loro_wasm_bg-Gulzks_0.wasm +0 -0
  6. package/dist/lib/chunks/batch-BWpAyHvP.js +789 -0
  7. package/dist/lib/chunks/canonical-BIWfVCL-.js +87 -0
  8. package/dist/lib/chunks/envelope-D6MGY-ZN.js +61 -0
  9. package/dist/lib/chunks/replica-D8w3I4sn.js +2472 -0
  10. package/dist/lib/chunks/runtime-BVJHE1Pg.js +997 -0
  11. package/dist/lib/chunks/session-BqXC9a1-.js +281 -0
  12. package/dist/lib/chunks/toolIds-BCeSGHpV.js +821 -0
  13. package/dist/lib/chunks/workspaceTree-Destqupf.js +23 -0
  14. package/dist/lib/convergence/index.js +1035 -0
  15. package/dist/lib/convergence/node.js +8 -0
  16. package/dist/lib/embed-server/callbacks.js +37 -0
  17. package/dist/lib/embed-server/cli.js +19 -0
  18. package/dist/lib/embed-server/index.js +272 -0
  19. package/dist/lib/extensions/index.js +439 -0
  20. package/dist/lib/extensions/panel.html +208 -0
  21. package/dist/lib/extensions/runtime.js +198 -0
  22. package/dist/lib/extensions/sandbox.html +35 -0
  23. package/dist/lib/kdm/index.js +437 -0
  24. package/dist/lib/kit-dev-host/checkUiIds.mjs +44 -0
  25. package/dist/lib/kit-dev-host/index.js +308 -0
  26. package/dist/lib/kit-dev-host/lineRow.mjs +47 -0
  27. package/dist/lib/kit-dev-host/modeSmoke.mjs +178 -0
  28. package/dist/lib/kit-editor/index.js +6481 -0
  29. package/dist/lib/kit-editor/styles.css +807 -0
  30. package/dist/lib/kit-editor/workspaceTree.js +1 -0
  31. package/dist/lib/kit-engine/index.js +7 -0
  32. package/dist/lib/kit-host/index.js +530 -0
  33. package/dist/lib/kit-ui-protocol/index.js +336 -0
  34. package/dist/lib/node-only/embed-server-callbacks.js +2 -0
  35. package/dist/lib/node-only/embed-server.js +2 -0
  36. package/dist/lib/sync/index.js +1008 -0
  37. package/dist/lib/sync/openToken.js +201 -0
  38. package/dist/lib/sync/replicaVersions.js +51 -0
  39. package/dist/standalone/chunks/batch-CAGYk6m4.js +789 -0
  40. package/dist/standalone/chunks/client-B9xkOcvf.js +772 -0
  41. package/dist/standalone/chunks/collab-CTQZ9h7J.js +1022 -0
  42. package/dist/standalone/chunks/comment-Ds9t8xSE.js +94 -0
  43. package/dist/standalone/chunks/comments-uVDS8Wxp.js +41 -0
  44. package/dist/standalone/chunks/document-B5MMuYyN.js +324 -0
  45. package/dist/standalone/chunks/envelope-GX4Cynkx.js +7612 -0
  46. package/dist/standalone/chunks/index-BEgEEnhU.js +287 -0
  47. package/dist/standalone/chunks/index-EEl8hGbP.js +1967 -0
  48. package/dist/standalone/chunks/kits-Cp51mDlw.js +9 -0
  49. package/dist/standalone/chunks/replica-Di2iCPiD.js +3281 -0
  50. package/dist/standalone/chunks/replicaVersions-C3rJxnk9.js +50 -0
  51. package/dist/standalone/chunks/touched-T9jKJjxn.js +78 -0
  52. package/dist/standalone/index.js +12 -0
  53. package/dist/standalone/script/index.js +339 -0
  54. package/dist/standalone/script/kits/board.js +5 -0
  55. package/dist/standalone/script/kits/cad.js +5 -0
  56. package/dist/standalone/script/kits/design.js +5 -0
  57. package/dist/standalone/script/kits/doc.js +5 -0
  58. package/dist/standalone/script/kits/film.js +5 -0
  59. package/dist/standalone/script/kits/html.js +5 -0
  60. package/dist/standalone/script/kits/index.js +38 -0
  61. package/dist/standalone/script/kits/layout.js +5 -0
  62. package/dist/standalone/script/kits/motion.js +5 -0
  63. package/dist/standalone/script/kits/note.js +5 -0
  64. package/dist/standalone/script/kits/pdf.js +5 -0
  65. package/dist/standalone/script/kits/photo.js +5 -0
  66. package/dist/standalone/script/kits/scene.js +5 -0
  67. package/dist/standalone/script/kits/sheet.js +5 -0
  68. package/dist/standalone/script/kits/slide.js +5 -0
  69. package/dist/standalone/script/kits/table.js +5 -0
  70. package/dist/standalone/script/kits/vector.js +5 -0
  71. package/dist/standalone/script/node.js +401 -0
  72. package/dist/standalone/script/proxy.js +254 -0
  73. package/dist/types/convergence/src/adapter.d.ts +131 -0
  74. package/dist/types/convergence/src/engine.d.ts +66 -0
  75. package/dist/types/convergence/src/index.d.ts +21 -0
  76. package/dist/types/convergence/src/net.d.ts +52 -0
  77. package/dist/types/convergence/src/node.d.ts +6 -0
  78. package/dist/types/convergence/src/report.d.ts +56 -0
  79. package/dist/types/convergence/src/rng.d.ts +12 -0
  80. package/dist/types/convergence/src/scenarios.d.ts +15 -0
  81. package/dist/types/convergence/src/state.d.ts +67 -0
  82. package/dist/types/embed/src/agent.d.ts +88 -0
  83. package/dist/types/embed/src/blobs.d.ts +2 -0
  84. package/dist/types/embed/src/collab.d.ts +250 -0
  85. package/dist/types/embed/src/comments.d.ts +30 -0
  86. package/dist/types/embed/src/errors.d.ts +24 -0
  87. package/dist/types/embed/src/handlers.d.ts +92 -0
  88. package/dist/types/embed/src/index.d.ts +20 -0
  89. package/dist/types/embed/src/mount.d.ts +39 -0
  90. package/dist/types/embed/src/replicaStore.d.ts +3 -0
  91. package/dist/types/embed/src/rooms.d.ts +40 -0
  92. package/dist/types/embed/src/schemas.d.ts +7 -0
  93. package/dist/types/embed/src/syncClients.d.ts +28 -0
  94. package/dist/types/embed/src/theme.d.ts +7 -0
  95. package/dist/types/embed/src/types.d.ts +596 -0
  96. package/dist/types/embed/src/ui.d.ts +33 -0
  97. package/dist/types/embed/src/version.d.ts +2 -0
  98. package/dist/types/embed-server/src/callbacks.d.ts +30 -0
  99. package/dist/types/embed-server/src/cli.d.ts +1 -0
  100. package/dist/types/embed-server/src/index.d.ts +19 -0
  101. package/dist/types/embed-server/src/page.d.ts +15 -0
  102. package/dist/types/embed-server/src/server.d.ts +39 -0
  103. package/dist/types/embed-server/src/session.d.ts +22 -0
  104. package/dist/types/extensions/src/gate.d.ts +17 -0
  105. package/dist/types/extensions/src/generated/runtime.d.ts +8 -0
  106. package/dist/types/extensions/src/host.d.ts +135 -0
  107. package/dist/types/extensions/src/index.d.ts +13 -0
  108. package/dist/types/extensions/src/manifest.d.ts +82 -0
  109. package/dist/types/extensions/src/protocol.d.ts +159 -0
  110. package/dist/types/extensions/src/rpc.d.ts +52 -0
  111. package/dist/types/extensions/src/runtime/document.d.ts +40 -0
  112. package/dist/types/extensions/src/runtime/index.d.ts +10 -0
  113. package/dist/types/extensions/src/runtime/panel.d.ts +19 -0
  114. package/dist/types/extensions/src/runtime/runtime.d.ts +54 -0
  115. package/dist/types/extensions/src/runtime/worker.d.ts +1 -0
  116. package/dist/types/extensions/src/sandbox.d.ts +41 -0
  117. package/dist/types/kdm/src/args.d.ts +12 -0
  118. package/dist/types/kdm/src/batch.d.ts +118 -0
  119. package/dist/types/kdm/src/canonical.d.ts +25 -0
  120. package/dist/types/kdm/src/comment.d.ts +114 -0
  121. package/dist/types/kdm/src/diff.d.ts +125 -0
  122. package/dist/types/kdm/src/envelope.d.ts +27 -0
  123. package/dist/types/kdm/src/errors.d.ts +28 -0
  124. package/dist/types/kdm/src/footprint.d.ts +21 -0
  125. package/dist/types/kdm/src/ids.d.ts +57 -0
  126. package/dist/types/kdm/src/index.d.ts +18 -0
  127. package/dist/types/kdm/src/marks.d.ts +32 -0
  128. package/dist/types/kdm/src/op.d.ts +124 -0
  129. package/dist/types/kdm/src/origin.d.ts +41 -0
  130. package/dist/types/kdm/src/overlay.d.ts +92 -0
  131. package/dist/types/kdm/src/projection.d.ts +29 -0
  132. package/dist/types/kdm/src/read.d.ts +90 -0
  133. package/dist/types/kdm/src/replica.d.ts +403 -0
  134. package/dist/types/kdm/src/schema.d.ts +185 -0
  135. package/dist/types/kdm/src/testing.d.ts +28 -0
  136. package/dist/types/kdm/src/toolIds.d.ts +61 -0
  137. package/dist/types/kdm/src/touched.d.ts +24 -0
  138. package/dist/types/kdm/src/tx.d.ts +215 -0
  139. package/dist/types/kdm/src/value.d.ts +23 -0
  140. package/dist/types/kit-dev-host/src/checkNoPaths.d.ts +19 -0
  141. package/dist/types/kit-dev-host/src/checkUiIds.d.mts +2 -0
  142. package/dist/types/kit-dev-host/src/index.d.ts +179 -0
  143. package/dist/types/kit-dev-host/src/lineRow.d.mts +14 -0
  144. package/dist/types/kit-dev-host/src/memoryFiles.d.ts +15 -0
  145. package/dist/types/kit-dev-host/src/modeSmoke.d.mts +8 -0
  146. package/dist/types/kit-editor/src/CommandDialog.d.ts +22 -0
  147. package/dist/types/kit-editor/src/CommentsPanel.d.ts +45 -0
  148. package/dist/types/kit-editor/src/CustomizeDialog.d.ts +32 -0
  149. package/dist/types/kit-editor/src/HostButtons.d.ts +11 -0
  150. package/dist/types/kit-editor/src/RenderFailure.d.ts +7 -0
  151. package/dist/types/kit-editor/src/TextSink.d.ts +6 -0
  152. package/dist/types/kit-editor/src/brand.d.ts +2 -0
  153. package/dist/types/kit-editor/src/catalogMenus.d.ts +27 -0
  154. package/dist/types/kit-editor/src/chart.d.ts +138 -0
  155. package/dist/types/kit-editor/src/conflicts.d.ts +12 -0
  156. package/dist/types/kit-editor/src/drawing.d.ts +83 -0
  157. package/dist/types/kit-editor/src/editor.d.ts +544 -0
  158. package/dist/types/kit-editor/src/engine/KitCanvas.d.ts +70 -0
  159. package/dist/types/kit-editor/src/engine/backdrop.d.ts +32 -0
  160. package/dist/types/kit-editor/src/engine/createKitFile.d.ts +12 -0
  161. package/dist/types/kit-editor/src/engine/decorations.d.ts +14 -0
  162. package/dist/types/kit-editor/src/engine/engineOverlays.d.ts +197 -0
  163. package/dist/types/kit-editor/src/engine/engineTools.d.ts +116 -0
  164. package/dist/types/kit-editor/src/engine/kitDraft.d.ts +70 -0
  165. package/dist/types/kit-editor/src/engine/kitEngine.d.ts +390 -0
  166. package/dist/types/kit-editor/src/engine/kitEngine.worker.d.ts +30 -0
  167. package/dist/types/kit-editor/src/engine/kitHost.d.ts +41 -0
  168. package/dist/types/kit-editor/src/engine/kitModel.d.ts +32 -0
  169. package/dist/types/kit-editor/src/engine/kitModelRunner.d.ts +99 -0
  170. package/dist/types/kit-editor/src/engine/kitModule.d.ts +185 -0
  171. package/dist/types/kit-editor/src/engine/kitShortcuts.d.ts +32 -0
  172. package/dist/types/kit-editor/src/engine/kitTools.d.ts +79 -0
  173. package/dist/types/kit-editor/src/engine/lazyFonts.d.ts +34 -0
  174. package/dist/types/kit-editor/src/engine/lentFiles.d.ts +29 -0
  175. package/dist/types/kit-editor/src/engine/rasterViewport.d.ts +178 -0
  176. package/dist/types/kit-editor/src/engine/readOnly.d.ts +78 -0
  177. package/dist/types/kit-editor/src/engine/refusal.d.ts +39 -0
  178. package/dist/types/kit-editor/src/engine/replica.d.ts +23 -0
  179. package/dist/types/kit-editor/src/engine/useKitDocument.d.ts +154 -0
  180. package/dist/types/kit-editor/src/engine/workerRelay.d.ts +62 -0
  181. package/dist/types/kit-editor/src/files.d.ts +74 -0
  182. package/dist/types/kit-editor/src/frame/EditorFrame.d.ts +74 -0
  183. package/dist/types/kit-editor/src/frame/FocusBar.d.ts +10 -0
  184. package/dist/types/kit-editor/src/frame/MenuNodes.d.ts +20 -0
  185. package/dist/types/kit-editor/src/frame/MenuRibbon.d.ts +8 -0
  186. package/dist/types/kit-editor/src/frame/ModeButton.d.ts +7 -0
  187. package/dist/types/kit-editor/src/frame/menus.d.ts +64 -0
  188. package/dist/types/kit-editor/src/frame/modes.d.ts +51 -0
  189. package/dist/types/kit-editor/src/frame/useFrameLayout.d.ts +34 -0
  190. package/dist/types/kit-editor/src/host.d.ts +127 -0
  191. package/dist/types/kit-editor/src/index.d.ts +85 -0
  192. package/dist/types/kit-editor/src/ink.d.ts +26 -0
  193. package/dist/types/kit-editor/src/lease.d.ts +53 -0
  194. package/dist/types/kit-editor/src/oneInFlight.d.ts +7 -0
  195. package/dist/types/kit-editor/src/phrases.d.ts +25 -0
  196. package/dist/types/kit-editor/src/presence.d.ts +79 -0
  197. package/dist/types/kit-editor/src/strings.d.ts +6 -0
  198. package/dist/types/kit-editor/src/toolIcons.d.ts +32 -0
  199. package/dist/types/kit-editor/src/uiCatalog.d.ts +59 -0
  200. package/dist/types/kit-editor/src/uiConfig.d.ts +53 -0
  201. package/dist/types/kit-editor/src/uiContext.d.ts +65 -0
  202. package/dist/types/kit-editor/src/uiStore.d.ts +20 -0
  203. package/dist/types/kit-editor/src/useCommentAnchors.d.ts +29 -0
  204. package/dist/types/kit-editor/src/useKitSession.d.ts +115 -0
  205. package/dist/types/kit-editor/src/workspaceTree.d.ts +5 -0
  206. package/dist/types/kit-engine/src/contract.d.ts +154 -0
  207. package/dist/types/kit-engine/src/index.d.ts +2 -0
  208. package/dist/types/kit-engine/src/session.d.ts +133 -0
  209. package/dist/types/kit-host/src/files.d.ts +163 -0
  210. package/dist/types/kit-host/src/index.d.ts +315 -0
  211. package/dist/types/kit-ui-protocol/src/index.d.ts +1145 -0
  212. package/dist/types/loro-crdt.d.ts +9 -0
  213. package/dist/types/script/kits/board.d.ts +534 -0
  214. package/dist/types/script/kits/cad.d.ts +2937 -0
  215. package/dist/types/script/kits/design.d.ts +3213 -0
  216. package/dist/types/script/kits/doc.d.ts +2575 -0
  217. package/dist/types/script/kits/film.d.ts +4067 -0
  218. package/dist/types/script/kits/html.d.ts +38 -0
  219. package/dist/types/script/kits/index.d.ts +20 -0
  220. package/dist/types/script/kits/layout.d.ts +3483 -0
  221. package/dist/types/script/kits/motion.d.ts +4826 -0
  222. package/dist/types/script/kits/note.d.ts +631 -0
  223. package/dist/types/script/kits/pdf.d.ts +829 -0
  224. package/dist/types/script/kits/photo.d.ts +7574 -0
  225. package/dist/types/script/kits/scene.d.ts +2962 -0
  226. package/dist/types/script/kits/sheet.d.ts +2419 -0
  227. package/dist/types/script/kits/slide.d.ts +1894 -0
  228. package/dist/types/script/kits/table.d.ts +654 -0
  229. package/dist/types/script/kits/vector.d.ts +5167 -0
  230. package/dist/types/script/src/agentRelay.d.ts +48 -0
  231. package/dist/types/script/src/capabilities.d.ts +21 -0
  232. package/dist/types/script/src/document.d.ts +87 -0
  233. package/dist/types/script/src/drafts.d.ts +141 -0
  234. package/dist/types/script/src/exchange.d.ts +30 -0
  235. package/dist/types/script/src/headless.d.ts +190 -0
  236. package/dist/types/script/src/index.d.ts +15 -0
  237. package/dist/types/script/src/kitModel.d.ts +17 -0
  238. package/dist/types/script/src/kits.d.ts +8 -0
  239. package/dist/types/script/src/node.d.ts +42 -0
  240. package/dist/types/script/src/port.d.ts +89 -0
  241. package/dist/types/script/src/proxy.d.ts +132 -0
  242. package/dist/types/script/src/room.d.ts +162 -0
  243. package/dist/types/script/src/sources.d.ts +73 -0
  244. package/dist/types/sync/src/backoff.d.ts +9 -0
  245. package/dist/types/sync/src/blobSection.d.ts +23 -0
  246. package/dist/types/sync/src/capabilities.d.ts +20 -0
  247. package/dist/types/sync/src/client.d.ts +253 -0
  248. package/dist/types/sync/src/framing.d.ts +35 -0
  249. package/dist/types/sync/src/hash.d.ts +2 -0
  250. package/dist/types/sync/src/index.d.ts +19 -0
  251. package/dist/types/sync/src/leases.d.ts +82 -0
  252. package/dist/types/sync/src/openToken.d.ts +104 -0
  253. package/dist/types/sync/src/protocol.d.ts +484 -0
  254. package/dist/types/sync/src/relay.d.ts +52 -0
  255. package/dist/types/sync/src/replicaVersions.d.ts +17 -0
  256. package/dist/types/sync/src/saveMachine.d.ts +83 -0
  257. package/dist/types/sync/src/saveRun.d.ts +52 -0
  258. package/dist/types/sync/src/transport.d.ts +19 -0
  259. package/package.json +229 -4
@@ -0,0 +1,789 @@
1
+ import { e as ue, K as he } from "./envelope-GX4Cynkx.js";
2
+ import { d as se, a as F, o as ie, H as M, p as we, K as ye, I as me, x as z, J as be, M as je, t as te, l as re, P as ae, m as ge } from "./replica-Di2iCPiD.js";
3
+ const ve = (e) => Object.values(e.types).some((t) => t.projected), k = (e) => new m("invalid-value", null, e), Z = (e, t, i) => Object.defineProperty(e, t, { value: i, writable: !0, enumerable: !0, configurable: !0 }), x = (e, t) => (t.kind === "list" || t.kind === "tree" || t.kind === "ref" && t.owns) && !!t.of && !!e.types[t.of]?.projected;
4
+ class Oe {
5
+ state;
6
+ objects = /* @__PURE__ */ new Map();
7
+ constructor(t, i) {
8
+ this.state = F.ofReplica(t).showJson(t.stateJson());
9
+ const s = this.state.objects, n = t.schema;
10
+ for (const o of i) {
11
+ if (!o.id || o.id === "root" || o.id.startsWith("$") || Object.hasOwn(s, o.id) || this.objects.has(o.id))
12
+ throw k(`projected id \`${o.id}\` is reserved, duplicated or shadows a stored object`);
13
+ const f = Object.hasOwn(n.types, o.type) ? n.types[o.type] : void 0;
14
+ if (!f?.projected) throw k(`\`${o.type}\` is not a projected type`);
15
+ for (const [l, u] of Object.entries(o.fields)) {
16
+ const b = Object.hasOwn(f.fields, l) ? f.fields[l] : void 0;
17
+ if (!b) throw k(`unknown projected field \`${o.type}.${l}\``);
18
+ let r;
19
+ if (b.kind === "text") r = typeof u == "string" || !!b.marks && Object.keys(b.marks).length > 0 && Array.isArray(u);
20
+ else if (b.kind === "list" || b.kind === "tree") r = Array.isArray(u);
21
+ else if (b.kind === "map") r = typeof u == "object" && u !== null && !Array.isArray(u);
22
+ else
23
+ try {
24
+ se(n, o.type, l, u), r = !0;
25
+ } catch {
26
+ r = !1;
27
+ }
28
+ if (!r) throw k(`invalid projected value \`${o.type}.${l}\``);
29
+ }
30
+ for (const [l, u] of Object.entries(f.fields)) {
31
+ const b = x(n, u);
32
+ if (!u.optional && !b && !Object.hasOwn(o.fields, l)) throw k(`missing projected field \`${o.type}.${l}\``);
33
+ }
34
+ Z(s, o.id, { ...structuredClone(o.fields), $type: o.type }), this.objects.set(o.id, structuredClone(o));
35
+ }
36
+ const d = /* @__PURE__ */ new Map(), h = (o, f) => JSON.stringify([o, f]);
37
+ for (const [o, f] of Object.entries(s))
38
+ for (const [l, u] of Object.entries(n.types[f.$type].fields))
39
+ if (x(n, u)) {
40
+ if (f[l] !== null && f[l] !== void 0 && (!Array.isArray(f[l]) || f[l].length)) throw k(`\`${o}.${l}\`: projected membership must come from object placements`);
41
+ d.set(h(o, l), { parent: o, field: l, kind: u.kind, groups: /* @__PURE__ */ new Map() });
42
+ }
43
+ for (const o of this.objects.values()) {
44
+ if (!o.at) continue;
45
+ const f = o.at, l = d.get(h(f.parent, f.field));
46
+ if (!l) throw k(`\`${o.id}\` has no projected container \`${f.parent}.${f.field}\``);
47
+ if (f.node !== void 0) {
48
+ const a = this.objects.get(f.node)?.at;
49
+ if (l.kind !== "tree" || a?.parent !== f.parent || a.field !== f.field) throw k("projected tree parent belongs to another container");
50
+ }
51
+ const u = s[f.parent];
52
+ if (n.types[u.$type].fields[f.field].of !== o.type) throw k(`projected placement has the wrong type for \`${o.id}\``);
53
+ const b = f.index ?? 0;
54
+ if (!Number.isSafeInteger(b) || b < 0) throw k("projected index is a nonnegative integer");
55
+ const r = l.groups.get(f.node) ?? [];
56
+ r.push([b, o.id]), l.groups.set(f.node, r);
57
+ }
58
+ for (const { parent: o, field: f, kind: l, groups: u } of d.values()) {
59
+ for (const a of u.values())
60
+ if (a.sort(([w, c], [p, j]) => w - p || c.localeCompare(j)), a.some(([w], c) => w !== c)) throw k(`\`${o}.${f}\`: projected indexes must be contiguous and unique`);
61
+ const b = u.get(void 0) ?? [];
62
+ let r;
63
+ if (l === "ref") {
64
+ if (b.length > 1) throw k("a projected owning ref holds at most one object");
65
+ r = b[0]?.[1] ?? null;
66
+ } else if (l === "tree") {
67
+ const a = /* @__PURE__ */ new Set(), w = (c, p) => {
68
+ if (p > 64) throw k("projected tree exceeds depth limit");
69
+ return (u.get(c) ?? []).map(([, j]) => {
70
+ if (a.has(j)) throw k("projected placements contain a cycle");
71
+ return a.add(j), { object: j, children: w(j, p + 1) };
72
+ });
73
+ };
74
+ if (r = w(void 0, 0), a.size !== [...u.values()].reduce((c, p) => c + p.length, 0)) throw k("projected placements contain an unreachable tree cycle");
75
+ } else r = b.map(([, a]) => a);
76
+ Z(s[o], f, r);
77
+ }
78
+ for (const o of this.objects.values()) {
79
+ const f = /* @__PURE__ */ new Set([o.id]);
80
+ let l = o;
81
+ for (; l.at && (l = this.objects.get(l.at.parent)); ) {
82
+ if (f.has(l.id)) throw k("projected placements contain a cycle");
83
+ f.add(l.id);
84
+ }
85
+ }
86
+ }
87
+ load(t, i, s) {
88
+ const n = i === "root" ? t ?? "" : i;
89
+ if (!Object.hasOwn(this.state.objects, n)) throw new m("not-found", null, `no object \`${i}\``);
90
+ const d = this.state.objects[n], h = s ?? Object.keys(d).filter((l) => l !== "$type");
91
+ if (!Array.isArray(h) || h.some((l) => typeof l != "string")) throw k("fields lists names");
92
+ const o = Object.fromEntries(h.map((l) => {
93
+ if (!Object.hasOwn(d, l)) throw k(`unknown field \`${l}\``);
94
+ return [l, structuredClone(d[l])];
95
+ })), f = this.objects.get(n);
96
+ return { id: n, type: d.$type, fields: o, ...f ? { projected: !0, ...f.at ? { at: { ...f.at } } : {} } : {} };
97
+ }
98
+ }
99
+ function $e(e, t, i, s) {
100
+ const n = structuredClone(s);
101
+ let d = n.op;
102
+ const h = n.target === "root" ? i : typeof n.target == "string" ? n.target : "";
103
+ let o = t.objects.get(h)?.type;
104
+ if (d === "create") o = typeof n.type == "string" && e.types[n.type]?.projected ? n.type : void 0;
105
+ else if ((!o || d === "set" || d.startsWith("list.") || d.startsWith("tree.")) && Object.hasOwn(t.state.objects, h) && typeof n.field == "string") {
106
+ const a = t.state.objects[h], w = e.types[a.$type].fields[n.field];
107
+ if (w?.of && e.types[w.of]?.projected) {
108
+ const c = e.types[a.$type];
109
+ if (w.readOnly || c.readOnly || c.readOnlyWhen && c.readOnlyWhen.values.includes(a[c.readOnlyWhen.field])) throw new m("read-only", null, "the projected container is read-only");
110
+ o = w.of;
111
+ const p = d === "list.move" ? n.from : n.index, j = a[n.field], g = n.item ?? n.node ?? (w.kind === "ref" ? n.value : void 0) ?? (Array.isArray(j) && typeof p == "number" ? j[p] : void 0);
112
+ g !== void 0 && (n.in = `${h}.${n.field}`, n.target = g, w.kind === "ref" && d === "set" && (d = "ref.set", n.op = d, delete n.field));
113
+ }
114
+ }
115
+ if (!o) return;
116
+ if (typeof n.in == "string") {
117
+ const a = n.in.lastIndexOf(".");
118
+ if (a < 1) throw k("a projected destination names parent.field");
119
+ const w = n.in.slice(0, a), c = t.state.objects[w === "root" ? i : w], p = c && e.types[c.$type], j = p?.fields[n.in.slice(a + 1)];
120
+ if (!j || !x(e, j) || j.of !== o) throw k("projected destination must own the object's type");
121
+ if (j.readOnly || p.readOnly || p.readOnlyWhen && p.readOnlyWhen.values.includes(c[p.readOnlyWhen.field])) throw new m("read-only", null, "the projected container is read-only");
122
+ }
123
+ const f = e.types[o], l = typeof n.field == "string" ? n.field : void 0, u = t.state.objects[typeof n.target == "string" ? n.target : h];
124
+ if (f.readOnly || f.readOnlyWhen && f.readOnlyWhen.values.includes(u?.[f.readOnlyWhen.field]) || l && f.fields[l]?.readOnly) throw new m("read-only", null, `projected \`${o}\` is read-only`);
125
+ if (d === "set") {
126
+ if (!l) throw k("a projected set needs a field");
127
+ try {
128
+ se(e, o, l, n.value);
129
+ } catch (a) {
130
+ throw k(String(a));
131
+ }
132
+ }
133
+ const b = (l ? f.projectedOps?.[`${d}.${l}`] : void 0) ?? f.projectedOps?.[d];
134
+ if (!b) throw new m("read-only", null, `no projected command mapping for \`${o}.${d}\``);
135
+ const r = {};
136
+ for (const [a, w] of Object.entries(b.params ?? {})) {
137
+ let c = n;
138
+ for (const p of w.split(".")) c = c !== null && typeof c == "object" && Object.hasOwn(c, p) ? c[p] : void 0;
139
+ c !== void 0 && Z(r, a, c);
140
+ }
141
+ return { command: b.command, params: r, ...d === "create" ? { creates: o } : {} };
142
+ }
143
+ const ke = "KitObjectModel 1.0";
144
+ class m extends Error {
145
+ code;
146
+ index;
147
+ constructor(t, i, s) {
148
+ super(s), this.name = "BatchError", this.code = t, this.index = i;
149
+ }
150
+ toJSON() {
151
+ return { code: this.code, index: this.index, message: this.message };
152
+ }
153
+ }
154
+ function De(e) {
155
+ return {
156
+ replica: () => e,
157
+ transact: (t, i) => e.transact(t, i),
158
+ command: (t) => {
159
+ throw new Error(`unknown command \`${t}\`: this document runs no commands`);
160
+ }
161
+ };
162
+ }
163
+ const le = /* @__PURE__ */ new Set(["create", "set", "delete", "text.insert", "text.delete", "text.set", "text.mark", "text.unmark", "list.insert", "list.move", "list.remove", "map.set", "map.delete", "tree.insert", "tree.move", "command"]);
164
+ function Ae(e) {
165
+ if (e == null) return { kind: "extension", principal: "script" };
166
+ const t = () => new m("invalid-op", null, "origin is { kind: person | agent | extension, principal, label? }");
167
+ try {
168
+ me(e);
169
+ } catch {
170
+ throw t();
171
+ }
172
+ if (!["person", "agent", "extension"].includes(e.kind)) throw t();
173
+ return e.label === void 0 ? { kind: e.kind, principal: e.principal } : { kind: e.kind, principal: e.principal, label: e.label };
174
+ }
175
+ const P = (e) => typeof e == "object" && e !== null && !Array.isArray(e);
176
+ function R(e, t) {
177
+ if (typeof e == "string") return t.get(e) ?? e;
178
+ if (Array.isArray(e)) return e.map((i) => R(i, t));
179
+ if (P(e)) {
180
+ const i = {};
181
+ for (const [s, n] of Object.entries(e)) i[s] = R(n, t);
182
+ return i;
183
+ }
184
+ return e;
185
+ }
186
+ const L = (e) => e.objectsOfType(e.schema.root)[0] ?? null;
187
+ function ce(e, t) {
188
+ return t instanceof m ? t : t instanceof he ? new m(t.reason === "not-found" ? "not-found" : "invalid-value", e, t.message) : new m("invalid-value", e, t instanceof Error ? t.message : String(t));
189
+ }
190
+ const fe = (e, t) => new m(t.reason === "not-found" ? "not-found" : "invalid-op", e, t.message);
191
+ function H(e, t, i, s, n, d) {
192
+ if (typeof e != "string") throw new m("invalid-op", d, "`target` is a string");
193
+ if (e === "root") {
194
+ const o = n();
195
+ if (o === null) throw new m("not-found", d, "the document has no root object");
196
+ return o;
197
+ }
198
+ if (e.startsWith("$")) {
199
+ const o = t.get(e);
200
+ if (o === void 0) throw new m("not-found", d, `\`${e}\` is not bound in this batch`);
201
+ return o;
202
+ }
203
+ if (e === "") throw new m("invalid-op", d, "a target is not empty");
204
+ const h = i.resolve(e, s);
205
+ if (ae(h)) throw fe(d, h);
206
+ return h;
207
+ }
208
+ function G(e, t, i, s, n, d, h) {
209
+ if (typeof h != "string" || !i.isActive() || h === "") return h;
210
+ const o = t.types[n]?.fields[d];
211
+ if (!o || !(o.kind === "ref" || o.kind === "map" && o.of !== void 0)) return h;
212
+ const f = i.resolve(h, (l) => e.exists(l));
213
+ if (!ae(f)) return f;
214
+ if (f.reason === "not-found") return h;
215
+ throw fe(s, f);
216
+ }
217
+ function Ie(e, t, i) {
218
+ if (!e.isActive()) return i;
219
+ const s = (n) => typeof n == "string" ? e.show(n) : n;
220
+ return t === "ref" ? s(i) : t === "text" ? e.showText(i) : t === "list" && Array.isArray(i) ? i.map(s) : t === "tree" && Array.isArray(i) ? i.map((n) => ({ ...n, object: s(n.object), parent: s(n.parent) })) : t === "map" || t === "json" ? e.showJson(i) : i;
221
+ }
222
+ function de(e, t) {
223
+ const i = P(t) ? t.op : void 0;
224
+ if (typeof i != "string") throw new m("invalid-op", e, "an operation is an object with an `op`");
225
+ if (!le.has(i)) throw new m("invalid-op", e, `unknown operation \`${i}\``);
226
+ return i;
227
+ }
228
+ const T = (e, t, i) => {
229
+ const s = t[i];
230
+ if (typeof s != "string") throw new m("invalid-op", e, `\`${i}\` is a string`);
231
+ return s;
232
+ }, J = (e, t, i) => {
233
+ const s = t[i];
234
+ if (typeof s != "number" || !Number.isSafeInteger(s) || s < 0) throw new m("invalid-op", e, `\`${i}\` is a whole number`);
235
+ return s;
236
+ };
237
+ function oe(e, t, i) {
238
+ if (t == null || t === "end") return i;
239
+ if (typeof t != "number" || !Number.isSafeInteger(t) || t < 0) throw new m("invalid-op", e, '`at` is "end" or a whole number');
240
+ return t;
241
+ }
242
+ function B(e, t) {
243
+ let i = 0, s = 0;
244
+ for (const n of e) {
245
+ if (s === t) return i;
246
+ i += n.length, s += 1;
247
+ }
248
+ return i + (t - s);
249
+ }
250
+ async function Re(e, t) {
251
+ if (!P(t)) throw new m("invalid-op", null, "a batch is an object");
252
+ if (t.objects !== void 0 && typeof t.objects != "boolean") throw new m("invalid-op", null, "`objects` is a boolean");
253
+ const i = Ae(t.origin), s = (y, v) => {
254
+ if (y == null) return [];
255
+ if (!Array.isArray(y)) throw new m("invalid-op", null, `\`${v}\` is an array`);
256
+ return y;
257
+ }, n = s(t.ops, "ops"), d = s(t.load, "load");
258
+ let h = !1;
259
+ if (n.forEach((y, v) => {
260
+ h = le.has(de(v, y)) || h;
261
+ }), h && e.readOnly?.()) throw new m("read-only", null, "the document is read-only");
262
+ const o = (await e.replica()).schema, f = ie(o), l = async () => f ? F.ofReplica(await e.replica()) : F.none();
263
+ let u = /* @__PURE__ */ new Map();
264
+ const b = n.map(() => null), r = ve(o), a = async () => {
265
+ if (!e.projectedObjects) throw new m("invalid-value", null, "this adapter cannot read declared projected objects");
266
+ const y = await e.projectedObjects();
267
+ return new Oe(await e.replica(), y);
268
+ }, w = async (y) => {
269
+ if (!r || y.op === "command") return;
270
+ const v = await a(), W = await e.replica(), E = L(W);
271
+ return $e(o, v, E ? (await l()).show(E) : "", R(y, u));
272
+ };
273
+ let c = 0;
274
+ for (; c < n.length; ) {
275
+ const y = n[c], v = await w(y);
276
+ if (v) {
277
+ if (v.creates && y.ref !== void 0 && (typeof y.ref != "string" || !y.ref.startsWith("$") || u.has(y.ref))) throw new m("invalid-op", c, "a projected create ref starts with $ and is unique");
278
+ let S;
279
+ try {
280
+ S = await e.command(v.command, v.params, i);
281
+ } catch (A) {
282
+ throw new m("command-failed", c, String(A));
283
+ }
284
+ if (v.creates) {
285
+ const A = typeof S == "string" ? S : P(S) ? S.id : void 0;
286
+ if (typeof A != "string") throw new m("invalid-op", c, "a projected create command must return its id");
287
+ if ((await a()).objects.get(A)?.type !== v.creates) throw new m("invalid-op", c, "a projected create command returned no live object of its type");
288
+ typeof y.ref == "string" && u.set(y.ref, A);
289
+ }
290
+ b[c++] = S ?? null;
291
+ continue;
292
+ }
293
+ if (y.op === "command") {
294
+ if (typeof y.id != "string") throw new m("invalid-op", c, "a command needs an `id`");
295
+ let S = {};
296
+ if (y.params !== void 0 && f && u.size > 0) {
297
+ const A = await l();
298
+ S = R(y.params, new Map([...u].map(([q, D]) => [q, A.show(D)])));
299
+ } else y.params !== void 0 && (S = R(y.params, u));
300
+ try {
301
+ const A = y.at;
302
+ let q = u;
303
+ if (f && u.size > 0) {
304
+ const N = await l();
305
+ q = new Map([...u].map(([K, _]) => [K, N.show(_)]));
306
+ }
307
+ const D = Object.fromEntries(Object.entries({ on: y.on, part: A?.part, time: A?.time }).filter(([, N]) => N !== void 0).map(([N, K]) => [N, R(K, q)]));
308
+ if (Object.keys(D).length > 0 && !e.commandAt) throw new Error("this batch adapter cannot apply explicit command context");
309
+ if (b[c] = await (e.commandAt ? e.commandAt(y.id, S, D, i) : e.command(y.id, S, i)) ?? null, typeof y.ref == "string") {
310
+ const N = b[c], K = typeof N == "string" ? N : P(N) && typeof N.id == "string" ? N.id : void 0;
311
+ if (!K) throw new Error("a bound command must return its object id");
312
+ const _ = await e.replica(), ne = F.ofReplica(_).resolve(K, (pe) => _.exists(pe));
313
+ if (typeof ne != "string") throw new Error("a bound command returned no live object");
314
+ u.set(y.ref, ne);
315
+ }
316
+ } catch (A) {
317
+ throw new m("command-failed", c, A instanceof Error ? A.message : String(A));
318
+ }
319
+ c += 1;
320
+ continue;
321
+ }
322
+ const W = c;
323
+ for (; c < n.length && n[c].op !== "command" && !await w(n[c]); ) c += 1;
324
+ const E = c, I = await l();
325
+ let V = u, U = [];
326
+ await e.transact(i, (S) => {
327
+ V = new Map(u), U = [];
328
+ let A;
329
+ const q = () => A === void 0 ? A = S.objectsOfType(o.root)[0] ?? null : A;
330
+ for (let D = W; D < E; D += 1) U.push(We(S, o, I, D, n[D], V, q));
331
+ }), u = V, U.forEach((S, A) => {
332
+ b[W + A] = S;
333
+ });
334
+ }
335
+ const p = await l(), j = await e.replica(), g = r ? await a() : void 0, O = d.map((y, v) => {
336
+ try {
337
+ if (g) {
338
+ if (!P(y) || typeof y.target != "string") throw new m("invalid-op", null, "`target` is a string");
339
+ return g.load(L(j) ? p.show(L(j)) : null, u.get(y.target) ?? y.target, y.fields);
340
+ }
341
+ return Se(j, p, y, u);
342
+ } catch (W) {
343
+ const E = ce(null, W);
344
+ throw new m(E.code, null, `load ${v}: ${E.message}`);
345
+ }
346
+ });
347
+ p.isActive() && b.forEach((y, v) => {
348
+ b[v] = p.showJson(y);
349
+ });
350
+ const $ = { refs: Object.fromEntries([...u].map(([y, v]) => [y, p.show(v)])), results: b, loaded: O };
351
+ return t.objects && ($.objects = [...Object.keys(o.types).flatMap((y) => j.objectsOfType(y).map((v) => ({ id: p.show(v), type: y }))), ...[...g?.objects.values() ?? []].map((y) => ({ id: y.id, type: y.type, projected: !0, at: y.at }))].sort((y, v) => y.id < v.id ? -1 : y.id > v.id ? 1 : 0)), $;
352
+ }
353
+ function Y(e, t, i, s, n, d) {
354
+ const h = (r) => new m("invalid-op", s, `\`field\` \`${d}\`: ${r}`);
355
+ let o = n, f = d;
356
+ const l = d.split(".")[0];
357
+ if (l.includes("/") || l.includes("#")) {
358
+ let r = null;
359
+ const a = [n], w = /* @__PURE__ */ new Set();
360
+ for (; a.length > 0; ) {
361
+ const c = a.pop();
362
+ if (w.has(c) || w.size > 1e5) continue;
363
+ w.add(c);
364
+ const p = i.namedPath(c);
365
+ if (p !== null && d.startsWith(p)) {
366
+ const g = d.slice(p.length);
367
+ (g === "" || g.startsWith(".")) && (r === null || p.length > r[1]) && (r = [c, p.length]);
368
+ }
369
+ let j;
370
+ try {
371
+ j = e.objectType(c);
372
+ } catch {
373
+ continue;
374
+ }
375
+ if (Object.hasOwn(t.types, j))
376
+ for (const g of M(t.types[j].fields)) {
377
+ const O = t.types[j].fields[g];
378
+ if (O.kind === "list") a.push(...e.listItems(c, g));
379
+ else if (O.kind === "ref" && O.owns === !0) {
380
+ const $ = e.get(c, g);
381
+ typeof $ == "string" && $ !== "" && a.push($);
382
+ }
383
+ }
384
+ }
385
+ if (r === null) throw h("no object the target holds has that name in the kit's words");
386
+ o = r[0], f = d.slice(r[1]).replace(/^\.+/, "");
387
+ }
388
+ const u = f.split(".");
389
+ let b = 0;
390
+ for (; b < u.length; ) {
391
+ const r = u[b], a = e.objectType(o);
392
+ if (!Object.hasOwn(t.types, a) || !Object.hasOwn(t.types[a].fields, r)) throw h(`\`${a}\` has no field \`${r}\``);
393
+ const w = t.types[a].fields[r];
394
+ if (b + 1 === u.length || !["ref", "list", "map", "tree"].includes(w.kind)) {
395
+ const p = u.slice(b + 1);
396
+ if (p.length > 0 && w.kind !== "json") throw h(`\`${a}.${r}\` is no json value to go into`);
397
+ return [o, r, p];
398
+ }
399
+ let c = null;
400
+ if (w.kind === "ref" && w.owns === !0) {
401
+ const p = e.get(o, r);
402
+ c = typeof p == "string" && p !== "" ? p : null;
403
+ } else if (w.kind === "list") {
404
+ b += 1;
405
+ const p = u[b];
406
+ if (p === void 0 || !/^\d+$/.test(p)) throw h(`\`${r}\` is a list: give an index after it`);
407
+ const j = Number(p), g = e.listItems(o, r);
408
+ if (j >= g.length) throw h(`\`${r}\` has ${g.length} items, not ${j + 1}`);
409
+ c = g[j];
410
+ } else if (w.kind === "map" && w.owns === !0) {
411
+ b += 1;
412
+ const p = u[b];
413
+ if (p === void 0) throw h(`\`${r}\` is a map: give a key after it`);
414
+ const j = e.mapGet(o, r, p);
415
+ c = typeof j == "string" && j !== "" ? j : null;
416
+ } else throw w.kind === "ref" || w.kind === "map" ? h(`\`${a}.${r}\` does not own what it names: a path goes through owning fields only`) : h(`\`${a}.${r}\` is a tree: a path does not go through trees`);
417
+ if (c === null) throw h(`\`${a}.${r}\` holds nothing there`);
418
+ if (i.toolId(c) !== null) throw h(`it reaches \`${i.show(c)}\`, which has a tool id of its own: target it by that id`);
419
+ o = c, b += 1;
420
+ }
421
+ throw h("it names no field");
422
+ }
423
+ function Ee(e, t, i) {
424
+ if (t.length === 0) return i;
425
+ const n = e === void 0 ? null : JSON.parse(JSON.stringify(e));
426
+ let d = n;
427
+ for (const [h, o] of t.entries()) {
428
+ const f = h === t.length - 1;
429
+ if (Array.isArray(d) && /^\d+$/.test(o)) {
430
+ const l = Number(o);
431
+ if (l >= d.length) throw new Error(f ? `no item ${l}` : `no item ${l} at \`${o}\``);
432
+ f ? d[l] = i : d = d[l];
433
+ } else {
434
+ if (d === null || typeof d != "object" || Array.isArray(d)) throw new Error(`\`${o}\` is in no object`);
435
+ const l = d;
436
+ f ? l[o] = i : (Object.hasOwn(l, o) || (l[o] = null), d = l[o]);
437
+ }
438
+ }
439
+ return n;
440
+ }
441
+ function C(e, t, i, s, n, d) {
442
+ let h;
443
+ try {
444
+ h = e.objectType(n);
445
+ } catch {
446
+ return;
447
+ }
448
+ if (!Object.hasOwn(t.types, h)) return;
449
+ const o = t.types[h];
450
+ if (te(o, (l) => {
451
+ try {
452
+ return e.get(n, l);
453
+ } catch {
454
+ return;
455
+ }
456
+ })) throw new m("read-only", s, `\`${i.show(n)}\` is a read-only \`${h}\`: the kit keeps it as it is`);
457
+ if (d !== null && Object.hasOwn(o.fields, d) && o.fields[d].readOnly)
458
+ throw new m("read-only", s, `\`${h}.${d}\` is read-only: the kit keeps it as it is`);
459
+ }
460
+ function Te(e, t, i, s) {
461
+ if (!Object.hasOwn(e.types, i)) return;
462
+ const n = e.types[i];
463
+ if (te(n, (d) => Object.hasOwn(s, d) ? s[d] : void 0))
464
+ throw new m("read-only", t, `a new \`${i}\` would be read-only: only the kit makes one`);
465
+ }
466
+ function Q(e, t, i, s, n) {
467
+ if (!Object.hasOwn(t.types, s)) return;
468
+ const d = t.types[s];
469
+ for (const [h, o] of n) {
470
+ if (!Object.hasOwn(d.fields, h) || typeof o != "string") continue;
471
+ const f = d.fields[h];
472
+ if (!(f.kind === "ref" || f.kind === "map" && f.of !== void 0) || e.exists(o)) continue;
473
+ const l = f.of ?? "object";
474
+ throw new m(
475
+ "not-found",
476
+ i,
477
+ `\`${s}.${h}\` holds the id of a \`${l}\`, and no object has the id \`${o}\`: give the id of an existing \`${l}\` (a load or query answers them), or create one earlier in the batch and use its \`$name\``
478
+ );
479
+ }
480
+ }
481
+ function We(e, t, i, s, n, d, h) {
482
+ const o = de(s, n), f = (r) => e.exists(r) || o === "delete" && e.wasDeleted(r), l = (r) => H(n[r], d, i, f, h, s), u = (r, a) => {
483
+ const [w, c, p] = Y(e, t, i, s, r, a);
484
+ if (p.length) throw new m("invalid-op", s, "a container operation requires a container field, not a JSON member");
485
+ return [w, c];
486
+ }, b = (r, a) => {
487
+ if (!a.includes(".") && !a.includes("/")) return [r, a];
488
+ const [w, c, p] = Y(e, t, i, s, r, a);
489
+ if (p.length) throw new m("invalid-op", s, `\`field\` \`${a}\`: \`${o}\` ${o === "text.set" ? "replaces" : "edits"} a text field, not a path inside a value`);
490
+ return [w, c];
491
+ };
492
+ try {
493
+ switch (o) {
494
+ case "create": {
495
+ const r = T(s, n, "type");
496
+ if (n.fields !== void 0 && n.fields !== null && !P(n.fields)) throw new m("invalid-op", s, "`fields` is an object");
497
+ const a = R(n.fields ?? {}, d) ?? {}, w = {};
498
+ for (const g of M(a)) w[g] = G(e, t, i, s, r, g, a[g]);
499
+ const c = n.ref;
500
+ if (c != null && !(typeof c == "string" && c.length > 1 && c.startsWith("$")))
501
+ throw new m("invalid-op", s, "`ref` is a name starting with `$`");
502
+ let p = null;
503
+ if (n.in !== void 0 && n.in !== null) {
504
+ const g = n.in, O = typeof g == "string" ? g.indexOf(".") : -1;
505
+ if (typeof g != "string" || O < 0) throw new m("invalid-op", s, '`in` is "<target>.<field>"');
506
+ p = u(H(g.slice(0, O), d, i, f, h, s), g.slice(O + 1));
507
+ }
508
+ if (n.id !== void 0 && n.id !== null && typeof n.id != "string") throw new m("invalid-op", s, "`id` is a string");
509
+ Te(t, s, r, w), Q(e, t, s, r, M(w).map((g) => [g, w[g]]));
510
+ const j = e.create(r, w, typeof n.id == "string" ? { id: n.id } : void 0);
511
+ if (p) {
512
+ const [g, O] = p, $ = e.objectType(g), y = re(t, $, O).kind;
513
+ if (C(e, t, i, s, g, O), y === "list") e.listInsert(g, O, oe(s, n.at, e.listItems(g, O).length), j);
514
+ else if (y === "tree") {
515
+ if (n.at !== void 0 && n.at !== null && n.at !== "end") throw new m("invalid-op", s, "`at` places items in lists; a tree adds a node last among its siblings");
516
+ e.treeCreate(g, O, X(e, d, i, h, s, n, g, O), j);
517
+ } else throw new m("invalid-op", s, `\`${$}.${O}\` is not a list or a tree; create objects in lists and trees`);
518
+ } else if (n.parent !== void 0 && n.parent !== null) throw new m("invalid-op", s, "`parent` places an object in a tree: give `in` too");
519
+ return typeof c == "string" && d.set(c, j), j;
520
+ }
521
+ case "set": {
522
+ let r = l("target"), a = T(s, n, "field"), w = n.value === void 0 ? null : R(n.value, d);
523
+ if (a.includes(".") || a.includes("/")) {
524
+ const j = a, [g, O, $] = Y(e, t, i, s, r, j);
525
+ if ($.length > 0) {
526
+ let y = null;
527
+ try {
528
+ y = e.get(g, O);
529
+ } catch {
530
+ y = null;
531
+ }
532
+ try {
533
+ w = Ee(y, $, w);
534
+ } catch (v) {
535
+ throw new m("invalid-op", s, `\`field\` \`${j}\`: ${v.message}`);
536
+ }
537
+ }
538
+ r = g, a = O;
539
+ }
540
+ let c = w;
541
+ try {
542
+ c = G(e, t, i, s, e.objectType(r), a, w);
543
+ } catch (j) {
544
+ if (j instanceof m) throw j;
545
+ }
546
+ C(e, t, i, s, r, a);
547
+ let p = null;
548
+ try {
549
+ p = e.objectType(r);
550
+ } catch {
551
+ p = null;
552
+ }
553
+ if (p !== null && Object.hasOwn(t.types, p)) {
554
+ const j = t.types[p], g = (O) => {
555
+ if (O === a) return c;
556
+ try {
557
+ return e.get(r, O);
558
+ } catch {
559
+ return;
560
+ }
561
+ };
562
+ if (j.readOnlyWhen?.field === a && te(j, g))
563
+ throw new m("read-only", s, `\`${p}.${a}\` would make \`${i.show(r)}\` read-only: only the kit does`);
564
+ }
565
+ return p !== null && Q(e, t, s, p, [[a, c]]), e.set(r, a, c), null;
566
+ }
567
+ case "delete": {
568
+ const r = l("target");
569
+ if (!e.exists(r) && !e.wasDeleted(r)) throw new m("not-found", s, `object \`${r}\` not found`);
570
+ const a = [[r, !1]], w = /* @__PURE__ */ new Set(), c = [];
571
+ for (; a.length; ) {
572
+ const [p, j] = a.pop();
573
+ if (j) {
574
+ c.push(p);
575
+ continue;
576
+ }
577
+ if (!e.exists(p) || w.has(p)) continue;
578
+ w.add(p), a.push([p, !0]);
579
+ for (const O of M(t.types))
580
+ for (const [$, y] of Object.entries(z(t, O).fields))
581
+ if (y.cascade)
582
+ for (const v of e.objectsOfType(O)) {
583
+ const W = e.tree(v, $), E = W.filter((I) => I.object === p).map((I) => I.node);
584
+ a.push(...W.filter((I) => I.parent !== null && E.some((V) => V.equals(I.parent))).map((I) => [I.object, !1]));
585
+ }
586
+ const g = z(t, e.objectType(p));
587
+ for (const O of M(g.fields)) {
588
+ const $ = g.fields[O];
589
+ if (!be($)) continue;
590
+ let y = [];
591
+ switch ($.kind) {
592
+ case "list":
593
+ y = e.listItems(p, O);
594
+ break;
595
+ case "tree":
596
+ y = e.tree(p, O).map((v) => v.object);
597
+ break;
598
+ case "ref": {
599
+ const v = e.get(p, O);
600
+ typeof v == "string" && (y = [v]);
601
+ break;
602
+ }
603
+ case "map":
604
+ y = je(Object.fromEntries(e.mapEntries(p, O)), (v) => e.exists(v)).map(([v]) => v);
605
+ break;
606
+ }
607
+ a.push(...y.map((v) => [v, !1]));
608
+ }
609
+ }
610
+ for (const p of c) {
611
+ const j = e.objectType(p);
612
+ C(e, t, i, s, p, null);
613
+ for (const g of M(t.types)) {
614
+ const O = z(t, g);
615
+ for (const $ of M(O.fields)) {
616
+ const y = O.fields[$];
617
+ if (!(y.kind !== "list" || y.of !== j))
618
+ for (const v of e.objectsOfType(g)) {
619
+ const W = e.listItems(v, $);
620
+ W.includes(p) && C(e, t, i, s, v, $);
621
+ for (let E = W.length - 1; E >= 0; E -= 1) W[E] === p && e.listRemove(v, $, E);
622
+ }
623
+ }
624
+ }
625
+ for (const g of M(t.types)) {
626
+ const O = z(t, g);
627
+ for (const $ of M(O.fields)) {
628
+ const y = O.fields[$];
629
+ if (!(y.kind !== "tree" || y.of !== void 0 && y.of !== j))
630
+ for (const v of e.objectsOfType(g)) {
631
+ const W = e.tree(v, $), E = W.filter((I) => I.object === p).map((I) => I.node);
632
+ if (E.length && C(e, t, i, s, v, $), W.some((I) => I.parent !== null && E.some((V) => V.equals(I.parent)) && e.exists(I.object)))
633
+ throw new m("invalid-value", s, `\`${i.show(p)}\` has children in \`${i.show(v)}.${$}\`: delete them child-first or move them, before deleting this node in the same batch`);
634
+ }
635
+ }
636
+ }
637
+ e.delete(p);
638
+ }
639
+ return null;
640
+ }
641
+ case "tree.insert": {
642
+ const [r, a] = u(l("target"), T(s, n, "field"));
643
+ C(e, t, i, s, r, a);
644
+ const w = l("item");
645
+ if (ee(e, r, a, w)) throw new m("invalid-value", s, `\`${i.show(w)}\` is already in \`${i.show(r)}.${a}\`: move it with tree.move`);
646
+ return e.treeCreate(r, a, X(e, d, i, h, s, n, r, a), w), null;
647
+ }
648
+ case "tree.move": {
649
+ const [r, a] = u(l("target"), T(s, n, "field"));
650
+ C(e, t, i, s, r, a);
651
+ const w = l("item"), c = ee(e, r, a, w);
652
+ if (!c) throw new m("not-found", s, `\`${i.show(w)}\` is not in \`${i.show(r)}.${a}\``);
653
+ return e.treeMove(r, a, c, X(e, d, i, h, s, n, r, a)), null;
654
+ }
655
+ case "text.insert": {
656
+ const [r, a] = b(l("target"), T(s, n, "field"));
657
+ return C(e, t, i, s, r, a), e.textInsert(r, a, B(e.text(r, a), J(s, n, "index")), T(s, n, "text")), null;
658
+ }
659
+ case "text.delete": {
660
+ const [r, a] = b(l("target"), T(s, n, "field"));
661
+ C(e, t, i, s, r, a);
662
+ const w = e.text(r, a), c = J(s, n, "index"), p = B(w, c);
663
+ return e.textDelete(r, a, p, B(w, c + J(s, n, "count")) - p), null;
664
+ }
665
+ case "text.set": {
666
+ const [r, a] = b(l("target"), T(s, n, "field"));
667
+ C(e, t, i, s, r, a);
668
+ const w = T(s, n, "text"), c = e.textLength(r, a);
669
+ return c > 0 && e.textDelete(r, a, 0, c), w !== "" && e.textInsert(r, a, 0, w), null;
670
+ }
671
+ case "text.mark":
672
+ case "text.unmark": {
673
+ const r = l("target"), a = T(s, n, "field");
674
+ C(e, t, i, s, r, a);
675
+ const w = e.text(r, a), c = B(w, J(s, n, "start")), p = B(w, J(s, n, "end"));
676
+ return o === "text.mark" ? e.textMark(r, a, c, p, T(s, n, "key"), n.value ?? null) : e.textUnmark(r, a, c, p, T(s, n, "key")), null;
677
+ }
678
+ case "list.insert": {
679
+ const [r, a] = u(l("target"), T(s, n, "field"));
680
+ return C(e, t, i, s, r, a), e.listInsert(r, a, oe(s, n.index, e.listItems(r, a).length), l("item")), null;
681
+ }
682
+ case "list.move":
683
+ case "list.remove":
684
+ case "map.set":
685
+ case "map.delete": {
686
+ const [r, a] = o.startsWith("list.") ? u(l("target"), T(s, n, "field")) : [l("target"), T(s, n, "field")];
687
+ if (C(e, t, i, s, r, a), o === "list.move") {
688
+ const w = J(s, n, "from"), c = J(s, n, "to");
689
+ e.listMove(r, a, w, c > w ? c - 1 : c);
690
+ } else if (o === "list.remove") e.listRemove(r, a, J(s, n, "index"));
691
+ else if (o === "map.set") {
692
+ const w = e.objectType(r), c = G(e, t, i, s, w, a, n.value === void 0 ? null : R(n.value, d));
693
+ Q(e, t, s, w, [[a, c]]), e.mapSet(r, a, T(s, n, "key"), c);
694
+ } else e.mapDelete(r, a, T(s, n, "key"));
695
+ return null;
696
+ }
697
+ default:
698
+ throw new m("invalid-op", s, `\`${o}\` is not a model operation`);
699
+ }
700
+ } catch (r) {
701
+ throw ce(s, r);
702
+ }
703
+ }
704
+ function ee(e, t, i, s) {
705
+ return e.exists(s) ? e.tree(t, i).find((n) => n.object === s)?.node ?? null : null;
706
+ }
707
+ function X(e, t, i, s, n, d, h, o) {
708
+ if (d.parent === void 0 || d.parent === null) return null;
709
+ if (typeof d.parent != "string") throw new m("invalid-op", n, "`parent` is an object or null");
710
+ const f = H(d.parent, t, i, (u) => e.exists(u), s, n), l = ee(e, h, o, f);
711
+ if (!l) throw new m("not-found", n, `the parent \`${i.show(f)}\` is not in \`${i.show(h)}.${o}\``);
712
+ return l;
713
+ }
714
+ function Se(e, t, i, s) {
715
+ if (!P(i) || typeof i.target != "string") throw new m("invalid-op", null, "`target` is a string");
716
+ const n = H(i.target, s, t, (l) => e.exists(l), () => L(e), null), d = e.objectType(n), h = z(e.schema, d);
717
+ let o;
718
+ if (i.fields === void 0 || i.fields === null) o = M(h.fields);
719
+ else if (Array.isArray(i.fields) && i.fields.every((l) => typeof l == "string")) o = i.fields;
720
+ else throw new m("invalid-op", null, "`fields` lists names");
721
+ const f = {};
722
+ for (const l of o) {
723
+ const u = Object.hasOwn(h.fields, l) ? h.fields[l].kind : "json";
724
+ f[l] = Ie(t, u, Ce(e, n, d, l));
725
+ }
726
+ return { id: t.show(n), type: d, fields: f };
727
+ }
728
+ function Ce(e, t, i, s) {
729
+ const n = re(e.schema, i, s);
730
+ switch (n.kind) {
731
+ case "text":
732
+ return ge(n) ? e.textDelta(t, s) : e.text(t, s);
733
+ case "list":
734
+ return e.list(t, s);
735
+ case "map":
736
+ return Object.fromEntries(e.mapEntries(t, s));
737
+ case "tree": {
738
+ const d = [];
739
+ for (const h of e.tree(t, s))
740
+ (h.parent === null || d.some((f) => f.node.equals(h.parent))) && e.exists(h.object) && d.push(h);
741
+ return d.map((h) => ({ object: h.object, parent: d.find((o) => h.parent !== null && o.node.equals(h.parent))?.object ?? null }));
742
+ }
743
+ default:
744
+ return e.get(t, s);
745
+ }
746
+ }
747
+ function Je(e) {
748
+ const t = e.schema, i = {};
749
+ for (const d of M(t.types)) {
750
+ const h = {};
751
+ for (const [l, u] of Object.entries(t.types[d].fields)) {
752
+ const b = { kind: u.kind };
753
+ u.label !== void 0 && (b.label = u.label);
754
+ for (const r of ["minimum", "maximum", "multipleOf"]) u[r] !== void 0 && (b[r] = u[r]);
755
+ u.members !== void 0 && Object.keys(u.members).length > 0 && (b.members = u.members), u.of !== void 0 && (b.of = u.of), u.values?.length && (b.values = u.values), u.optional && (b.optional = !0), u.marks && Object.keys(u.marks).length > 0 && (b.marks = M(u.marks)), u.readOnly && (b.readOnly = !0), u.owns && (b.owns = !0), u.cascade && (b.cascade = !0), u.position && (b.position = !0), u.display !== void 0 && (b.display = { ...u.display }), u.sample !== void 0 && (b.sample = u.sample), h[l] = b;
756
+ }
757
+ const o = t.types[d], f = { fields: h };
758
+ o.projected && (f.projected = !0), o.projectedOps && Object.keys(o.projectedOps).length && (f.projectedOps = structuredClone(o.projectedOps)), o.readOnly && (f.readOnly = !0), o.readOnlyWhen && (f.readOnlyWhen = { field: o.readOnlyWhen.field, values: [...o.readOnlyWhen.values] }), o.toolId && (f.toolId = { field: o.toolId.field, prefix: o.toolId.prefix, ...o.toolId.parent === void 0 ? {} : { parent: o.toolId.parent } }), o.pathName !== void 0 && (f.pathName = o.pathName), o.label !== void 0 && (f.label = o.label), o.derived && (f.derived = Object.fromEntries(M(o.derived).map((l) => [l, { ...o.derived[l] }]))), o.text && (f.text = o.text), o.batchOps && Object.assign(f, { batchOps: structuredClone(o.batchOps) }), o.creation && Object.assign(f, { creation: structuredClone(o.creation) }), o.batchIndependent && (f.batchIndependent = !0), o.moveConsequences?.length && (f.moveConsequences = [...o.moveConsequences]), o.structuralConsequences?.length && (f.structuralConsequences = o.structuralConsequences.map((l) => ({ container: l.container, on: [...l.on], fields: [...l.fields] }))), i[d] = f;
759
+ }
760
+ const s = L(e), n = s !== null && ie(t) ? F.ofReplica(e).show(s) : s;
761
+ return { capability: ke, kit: t.kit, schemaVersion: t.schemaVersion, root: t.root, rootId: n, types: i };
762
+ }
763
+ function Pe(e) {
764
+ const t = e;
765
+ for (const s of ["kit", "schemaVersion", "root", "types"])
766
+ if (t[s] === void 0) throw ue.schema(`the kit's description has no \`${s}\``);
767
+ const i = {};
768
+ for (const [s, n] of Object.entries(t.types)) {
769
+ const d = {};
770
+ for (const [h, o] of Object.entries(n.fields ?? {}))
771
+ d[h] = Array.isArray(o.marks) ? { ...o, marks: Object.fromEntries(o.marks.map((f) => [f, { expand: "after" }])) } : o;
772
+ i[s] = { ...n, fields: d };
773
+ }
774
+ return we({ kdm: ye, kit: t.kit, schemaVersion: t.schemaVersion, root: t.root, types: i });
775
+ }
776
+ export {
777
+ m as B,
778
+ ke as O,
779
+ Oe as P,
780
+ De as a,
781
+ Ae as b,
782
+ R as c,
783
+ Je as d,
784
+ L as e,
785
+ Re as f,
786
+ ve as h,
787
+ Ce as r,
788
+ Pe as s
789
+ };