@voidhash/mimic 0.0.2 → 0.0.3

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 (273) hide show
  1. package/.turbo/turbo-build.log +257 -42
  2. package/dist/Document.cjs +152 -0
  3. package/dist/Document.d.cts +67 -0
  4. package/dist/Document.d.cts.map +1 -0
  5. package/dist/Document.d.mts +68 -0
  6. package/dist/Document.d.mts.map +1 -0
  7. package/dist/Document.mjs +147 -0
  8. package/dist/Document.mjs.map +1 -0
  9. package/dist/EffectSchema.cjs +180 -0
  10. package/dist/EffectSchema.d.cts +84 -0
  11. package/dist/EffectSchema.d.cts.map +1 -0
  12. package/dist/EffectSchema.d.mts +84 -0
  13. package/dist/EffectSchema.d.mts.map +1 -0
  14. package/dist/EffectSchema.mjs +176 -0
  15. package/dist/EffectSchema.mjs.map +1 -0
  16. package/dist/FractionalIndex.cjs +365 -0
  17. package/dist/FractionalIndex.mjs +364 -0
  18. package/dist/FractionalIndex.mjs.map +1 -0
  19. package/dist/Operation.cjs +53 -0
  20. package/dist/Operation.d.cts +39 -0
  21. package/dist/Operation.d.cts.map +1 -0
  22. package/dist/Operation.d.mts +39 -0
  23. package/dist/Operation.d.mts.map +1 -0
  24. package/dist/Operation.mjs +46 -0
  25. package/dist/Operation.mjs.map +1 -0
  26. package/dist/OperationDefinition.cjs +13 -0
  27. package/dist/OperationDefinition.d.cts +12 -0
  28. package/dist/OperationDefinition.d.cts.map +1 -0
  29. package/dist/OperationDefinition.d.mts +12 -0
  30. package/dist/OperationDefinition.d.mts.map +1 -0
  31. package/dist/OperationDefinition.mjs +13 -0
  32. package/dist/OperationDefinition.mjs.map +1 -0
  33. package/dist/OperationPath.cjs +148 -0
  34. package/dist/OperationPath.d.cts +60 -0
  35. package/dist/OperationPath.d.cts.map +1 -0
  36. package/dist/OperationPath.d.mts +60 -0
  37. package/dist/OperationPath.d.mts.map +1 -0
  38. package/dist/OperationPath.mjs +138 -0
  39. package/dist/OperationPath.mjs.map +1 -0
  40. package/dist/{Presence-gWrmGBeu.cjs → Presence.cjs} +4 -39
  41. package/dist/{Presence-N8u7Eppr.d.mts → Presence.d.cts} +2 -2
  42. package/dist/Presence.d.cts.map +1 -0
  43. package/dist/{Presence-DKKP4v5X.d.cts → Presence.d.mts} +2 -2
  44. package/dist/Presence.d.mts.map +1 -0
  45. package/dist/{Presence-DdMVKcOv.mjs → Presence.mjs} +3 -28
  46. package/dist/Presence.mjs.map +1 -0
  47. package/dist/Primitive.cjs +52 -0
  48. package/dist/Primitive.d.cts +20 -0
  49. package/dist/Primitive.d.cts.map +1 -0
  50. package/dist/Primitive.d.mts +20 -0
  51. package/dist/Primitive.d.mts.map +1 -0
  52. package/dist/Primitive.mjs +48 -0
  53. package/dist/Primitive.mjs.map +1 -0
  54. package/dist/ProxyEnvironment.cjs +34 -0
  55. package/dist/ProxyEnvironment.d.cts +31 -0
  56. package/dist/ProxyEnvironment.d.cts.map +1 -0
  57. package/dist/ProxyEnvironment.d.mts +31 -0
  58. package/dist/ProxyEnvironment.d.mts.map +1 -0
  59. package/dist/ProxyEnvironment.mjs +29 -0
  60. package/dist/ProxyEnvironment.mjs.map +1 -0
  61. package/dist/Transaction.cjs +66 -0
  62. package/dist/Transaction.d.cts +56 -0
  63. package/dist/Transaction.d.cts.map +1 -0
  64. package/dist/Transaction.d.mts +56 -0
  65. package/dist/Transaction.d.mts.map +1 -0
  66. package/dist/Transaction.mjs +58 -0
  67. package/dist/Transaction.mjs.map +1 -0
  68. package/dist/Transform.cjs +11 -0
  69. package/dist/Transform.d.cts +21 -0
  70. package/dist/Transform.d.cts.map +1 -0
  71. package/dist/Transform.d.mts +21 -0
  72. package/dist/Transform.d.mts.map +1 -0
  73. package/dist/Transform.mjs +6 -0
  74. package/dist/Transform.mjs.map +1 -0
  75. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
  76. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
  77. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
  78. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
  79. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
  80. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
  81. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
  82. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
  83. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
  84. package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
  85. package/dist/_virtual/rolldown_runtime.cjs +43 -0
  86. package/dist/{chunk-CLMFDpHK.mjs → _virtual/rolldown_runtime.mjs} +1 -1
  87. package/dist/client/ClientDocument.cjs +590 -0
  88. package/dist/client/ClientDocument.d.cts +161 -0
  89. package/dist/client/ClientDocument.d.cts.map +1 -0
  90. package/dist/client/ClientDocument.d.mts +162 -0
  91. package/dist/client/ClientDocument.d.mts.map +1 -0
  92. package/dist/client/ClientDocument.mjs +586 -0
  93. package/dist/client/ClientDocument.mjs.map +1 -0
  94. package/dist/client/Rebase.cjs +204 -0
  95. package/dist/client/Rebase.d.cts +87 -0
  96. package/dist/client/Rebase.d.cts.map +1 -0
  97. package/dist/client/Rebase.d.mts +88 -0
  98. package/dist/client/Rebase.d.mts.map +1 -0
  99. package/dist/client/Rebase.mjs +198 -0
  100. package/dist/client/Rebase.mjs.map +1 -0
  101. package/dist/client/StateMonitor.cjs +133 -0
  102. package/dist/client/StateMonitor.d.cts +122 -0
  103. package/dist/client/StateMonitor.d.cts.map +1 -0
  104. package/dist/client/StateMonitor.d.mts +122 -0
  105. package/dist/client/StateMonitor.d.mts.map +1 -0
  106. package/dist/client/StateMonitor.mjs +129 -0
  107. package/dist/client/StateMonitor.mjs.map +1 -0
  108. package/dist/client/Transport.cjs +11 -0
  109. package/dist/client/Transport.d.cts +237 -0
  110. package/dist/client/Transport.d.cts.map +1 -0
  111. package/dist/client/Transport.d.mts +237 -0
  112. package/dist/client/Transport.d.mts.map +1 -0
  113. package/dist/client/Transport.mjs +6 -0
  114. package/dist/client/Transport.mjs.map +1 -0
  115. package/dist/client/WebSocketTransport.cjs +396 -0
  116. package/dist/client/WebSocketTransport.d.cts +29 -0
  117. package/dist/client/WebSocketTransport.d.cts.map +1 -0
  118. package/dist/client/WebSocketTransport.d.mts +29 -0
  119. package/dist/client/WebSocketTransport.d.mts.map +1 -0
  120. package/dist/client/WebSocketTransport.mjs +392 -0
  121. package/dist/client/WebSocketTransport.mjs.map +1 -0
  122. package/dist/client/errors.cjs +135 -0
  123. package/dist/client/errors.d.cts +87 -0
  124. package/dist/client/errors.d.cts.map +1 -0
  125. package/dist/client/errors.d.mts +87 -0
  126. package/dist/client/errors.d.mts.map +1 -0
  127. package/dist/client/errors.mjs +127 -0
  128. package/dist/client/errors.mjs.map +1 -0
  129. package/dist/client/index.cjs +22 -1424
  130. package/dist/client/index.d.cts +8 -692
  131. package/dist/client/index.d.mts +8 -692
  132. package/dist/client/index.mjs +9 -1413
  133. package/dist/index.cjs +20 -2973
  134. package/dist/index.d.cts +12 -419
  135. package/dist/index.d.mts +12 -419
  136. package/dist/index.mjs +13 -2968
  137. package/dist/primitives/Array.cjs +302 -0
  138. package/dist/primitives/Array.d.cts +95 -0
  139. package/dist/primitives/Array.d.cts.map +1 -0
  140. package/dist/primitives/Array.d.mts +95 -0
  141. package/dist/primitives/Array.d.mts.map +1 -0
  142. package/dist/primitives/Array.mjs +301 -0
  143. package/dist/primitives/Array.mjs.map +1 -0
  144. package/dist/primitives/Boolean.cjs +95 -0
  145. package/dist/primitives/Boolean.d.cts +44 -0
  146. package/dist/primitives/Boolean.d.cts.map +1 -0
  147. package/dist/primitives/Boolean.d.mts +44 -0
  148. package/dist/primitives/Boolean.d.mts.map +1 -0
  149. package/dist/primitives/Boolean.mjs +94 -0
  150. package/dist/primitives/Boolean.mjs.map +1 -0
  151. package/dist/primitives/Either.cjs +200 -0
  152. package/dist/primitives/Either.d.cts +113 -0
  153. package/dist/primitives/Either.d.cts.map +1 -0
  154. package/dist/primitives/Either.d.mts +113 -0
  155. package/dist/primitives/Either.d.mts.map +1 -0
  156. package/dist/primitives/Either.mjs +199 -0
  157. package/dist/primitives/Either.mjs.map +1 -0
  158. package/dist/primitives/Lazy.cjs +46 -0
  159. package/dist/primitives/Lazy.d.cts +46 -0
  160. package/dist/primitives/Lazy.d.cts.map +1 -0
  161. package/dist/primitives/Lazy.d.mts +46 -0
  162. package/dist/primitives/Lazy.d.mts.map +1 -0
  163. package/dist/primitives/Lazy.mjs +46 -0
  164. package/dist/primitives/Lazy.mjs.map +1 -0
  165. package/dist/primitives/Literal.cjs +91 -0
  166. package/dist/primitives/Literal.d.cts +46 -0
  167. package/dist/primitives/Literal.d.cts.map +1 -0
  168. package/dist/primitives/Literal.d.mts +46 -0
  169. package/dist/primitives/Literal.d.mts.map +1 -0
  170. package/dist/primitives/Literal.mjs +90 -0
  171. package/dist/primitives/Literal.mjs.map +1 -0
  172. package/dist/primitives/Number.cjs +115 -0
  173. package/dist/primitives/Number.d.cts +54 -0
  174. package/dist/primitives/Number.d.cts.map +1 -0
  175. package/dist/primitives/Number.d.mts +54 -0
  176. package/dist/primitives/Number.d.mts.map +1 -0
  177. package/dist/primitives/Number.mjs +114 -0
  178. package/dist/primitives/Number.mjs.map +1 -0
  179. package/dist/primitives/String.cjs +127 -0
  180. package/dist/primitives/String.d.cts +56 -0
  181. package/dist/primitives/String.d.cts.map +1 -0
  182. package/dist/primitives/String.d.mts +56 -0
  183. package/dist/primitives/String.d.mts.map +1 -0
  184. package/dist/primitives/String.mjs +126 -0
  185. package/dist/primitives/String.mjs.map +1 -0
  186. package/dist/primitives/Struct.cjs +207 -0
  187. package/dist/primitives/Struct.d.cts +96 -0
  188. package/dist/primitives/Struct.d.cts.map +1 -0
  189. package/dist/primitives/Struct.d.mts +97 -0
  190. package/dist/primitives/Struct.d.mts.map +1 -0
  191. package/dist/primitives/Struct.mjs +206 -0
  192. package/dist/primitives/Struct.mjs.map +1 -0
  193. package/dist/primitives/Tree.cjs +575 -0
  194. package/dist/primitives/Tree.d.cts +185 -0
  195. package/dist/primitives/Tree.d.cts.map +1 -0
  196. package/dist/primitives/Tree.d.mts +185 -0
  197. package/dist/primitives/Tree.d.mts.map +1 -0
  198. package/dist/primitives/Tree.mjs +574 -0
  199. package/dist/primitives/Tree.mjs.map +1 -0
  200. package/dist/primitives/TreeNode.cjs +73 -0
  201. package/dist/primitives/TreeNode.d.cts +92 -0
  202. package/dist/primitives/TreeNode.d.cts.map +1 -0
  203. package/dist/primitives/TreeNode.d.mts +93 -0
  204. package/dist/primitives/TreeNode.d.mts.map +1 -0
  205. package/dist/primitives/TreeNode.mjs +72 -0
  206. package/dist/primitives/TreeNode.mjs.map +1 -0
  207. package/dist/primitives/Union.cjs +170 -0
  208. package/dist/primitives/Union.d.cts +81 -0
  209. package/dist/primitives/Union.d.cts.map +1 -0
  210. package/dist/primitives/Union.d.mts +81 -0
  211. package/dist/primitives/Union.d.mts.map +1 -0
  212. package/dist/primitives/Union.mjs +169 -0
  213. package/dist/primitives/Union.mjs.map +1 -0
  214. package/dist/primitives/shared.cjs +60 -0
  215. package/dist/primitives/shared.d.cts +147 -0
  216. package/dist/primitives/shared.d.cts.map +1 -0
  217. package/dist/primitives/shared.d.mts +147 -0
  218. package/dist/primitives/shared.d.mts.map +1 -0
  219. package/dist/primitives/shared.mjs +58 -0
  220. package/dist/primitives/shared.mjs.map +1 -0
  221. package/dist/server/ServerDocument.cjs +110 -0
  222. package/dist/server/ServerDocument.d.cts +98 -0
  223. package/dist/server/ServerDocument.d.cts.map +1 -0
  224. package/dist/server/ServerDocument.d.mts +99 -0
  225. package/dist/server/ServerDocument.d.mts.map +1 -0
  226. package/dist/server/ServerDocument.mjs +106 -0
  227. package/dist/server/ServerDocument.mjs.map +1 -0
  228. package/dist/server/errors.cjs +85 -0
  229. package/dist/server/errors.d.cts +53 -0
  230. package/dist/server/errors.d.cts.map +1 -0
  231. package/dist/server/errors.d.mts +53 -0
  232. package/dist/server/errors.d.mts.map +1 -0
  233. package/dist/server/errors.mjs +81 -0
  234. package/dist/server/errors.mjs.map +1 -0
  235. package/dist/server/index.cjs +9 -185
  236. package/dist/server/index.d.cts +3 -148
  237. package/dist/server/index.d.mts +3 -148
  238. package/dist/server/index.mjs +3 -181
  239. package/dist/types/index.cjs +16 -0
  240. package/dist/types/index.d.cts +16 -0
  241. package/dist/types/index.d.cts.map +1 -0
  242. package/dist/types/index.d.mts +16 -0
  243. package/dist/types/index.d.mts.map +1 -0
  244. package/dist/types/index.mjs +12 -0
  245. package/dist/types/index.mjs.map +1 -0
  246. package/dist/utils/tree-helpers.cjs +443 -0
  247. package/dist/utils/tree-helpers.d.cts +280 -0
  248. package/dist/utils/tree-helpers.d.cts.map +1 -0
  249. package/dist/utils/tree-helpers.d.mts +280 -0
  250. package/dist/utils/tree-helpers.d.mts.map +1 -0
  251. package/dist/utils/tree-helpers.mjs +439 -0
  252. package/dist/utils/tree-helpers.mjs.map +1 -0
  253. package/package.json +2 -2
  254. package/tsdown.config.ts +1 -1
  255. package/dist/Document-ChuFrTk1.cjs +0 -571
  256. package/dist/Document-CwiAFTIq.mjs +0 -438
  257. package/dist/Document-CwiAFTIq.mjs.map +0 -1
  258. package/dist/Presence-DKKP4v5X.d.cts.map +0 -1
  259. package/dist/Presence-DdMVKcOv.mjs.map +0 -1
  260. package/dist/Presence-N8u7Eppr.d.mts.map +0 -1
  261. package/dist/Primitive-DqQFc3Gu.d.mts +0 -1180
  262. package/dist/Primitive-DqQFc3Gu.d.mts.map +0 -1
  263. package/dist/Primitive-awpEjnKL.d.cts +0 -1180
  264. package/dist/Primitive-awpEjnKL.d.cts.map +0 -1
  265. package/dist/client/index.d.cts.map +0 -1
  266. package/dist/client/index.d.mts.map +0 -1
  267. package/dist/client/index.mjs.map +0 -1
  268. package/dist/index.d.cts.map +0 -1
  269. package/dist/index.d.mts.map +0 -1
  270. package/dist/index.mjs.map +0 -1
  271. package/dist/server/index.d.cts.map +0 -1
  272. package/dist/server/index.d.mts.map +0 -1
  273. package/dist/server/index.mjs.map +0 -1
@@ -0,0 +1,574 @@
1
+ import { _defineProperty } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs";
2
+ import { _objectSpread2 } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs";
3
+ import { ValidationError, applyDefaults, runValidators } from "./shared.mjs";
4
+ import { make } from "../OperationDefinition.mjs";
5
+ import { isPrefix, pathsEqual, pathsOverlap } from "../OperationPath.mjs";
6
+ import { fromDefinition } from "../Operation.mjs";
7
+ import { base62CharSet, generateKeyBetween } from "../FractionalIndex.mjs";
8
+ import { Effect, Schema } from "effect";
9
+
10
+ //#region src/primitives/Tree.ts
11
+ /**
12
+ * Helper to get children sorted by position
13
+ */
14
+ const getOrderedChildren = (nodes, parentId) => {
15
+ return [...nodes].filter((n) => n.parentId === parentId).sort((a, b) => a.pos < b.pos ? -1 : a.pos > b.pos ? 1 : 0);
16
+ };
17
+ /**
18
+ * Get all descendant IDs of a node (recursive)
19
+ */
20
+ const getDescendantIds = (nodes, nodeId) => {
21
+ const children = nodes.filter((n) => n.parentId === nodeId);
22
+ const descendantIds = [];
23
+ for (const child of children) {
24
+ descendantIds.push(child.id);
25
+ descendantIds.push(...getDescendantIds(nodes, child.id));
26
+ }
27
+ return descendantIds;
28
+ };
29
+ /**
30
+ * Check if moving a node to a new parent would create a cycle
31
+ */
32
+ const wouldCreateCycle = (nodes, nodeId, newParentId) => {
33
+ if (newParentId === null) return false;
34
+ if (newParentId === nodeId) return true;
35
+ return getDescendantIds(nodes, nodeId).includes(newParentId);
36
+ };
37
+ /**
38
+ * Generate a fractional position between two positions
39
+ */
40
+ const generateTreePosBetween = (left, right) => {
41
+ const charSet = base62CharSet();
42
+ return Effect.runSync(generateKeyBetween(left, right, charSet));
43
+ };
44
+ var TreePrimitive = class TreePrimitive {
45
+ constructor(schema) {
46
+ _defineProperty(this, "_tag", "TreePrimitive");
47
+ _defineProperty(this, "_State", void 0);
48
+ _defineProperty(this, "_Proxy", void 0);
49
+ _defineProperty(this, "_TRequired", void 0);
50
+ _defineProperty(this, "_THasDefault", void 0);
51
+ _defineProperty(this, "TSetInput", void 0);
52
+ _defineProperty(this, "TUpdateInput", void 0);
53
+ _defineProperty(this, "_schema", void 0);
54
+ _defineProperty(this, "_nodeTypeRegistry", void 0);
55
+ _defineProperty(this, "_opDefinitions", {
56
+ set: make({
57
+ kind: "tree.set",
58
+ payload: Schema.Unknown,
59
+ target: Schema.Unknown,
60
+ apply: (payload) => payload
61
+ }),
62
+ insert: make({
63
+ kind: "tree.insert",
64
+ payload: Schema.Unknown,
65
+ target: Schema.Unknown,
66
+ apply: (payload) => payload
67
+ }),
68
+ remove: make({
69
+ kind: "tree.remove",
70
+ payload: Schema.Unknown,
71
+ target: Schema.Unknown,
72
+ apply: (payload) => payload
73
+ }),
74
+ move: make({
75
+ kind: "tree.move",
76
+ payload: Schema.Unknown,
77
+ target: Schema.Unknown,
78
+ apply: (payload) => payload
79
+ })
80
+ });
81
+ _defineProperty(this, "_internal", {
82
+ createProxy: (env, operationPath) => {
83
+ const getCurrentState = () => {
84
+ const state = env.getState(operationPath);
85
+ return state !== null && state !== void 0 ? state : [];
86
+ };
87
+ const getParentType = (parentId) => {
88
+ var _parent$type;
89
+ if (parentId === null) return null;
90
+ const parent = getCurrentState().find((n) => n.id === parentId);
91
+ return (_parent$type = parent === null || parent === void 0 ? void 0 : parent.type) !== null && _parent$type !== void 0 ? _parent$type : null;
92
+ };
93
+ const createNodeProxy = (nodeState) => {
94
+ return {
95
+ id: nodeState.id,
96
+ type: nodeState.type,
97
+ is: (nodeType) => {
98
+ return nodeState.type === nodeType.type;
99
+ },
100
+ as: (nodeType) => {
101
+ if (nodeState.type !== nodeType.type) throw new ValidationError(`Node is of type "${nodeState.type}", not "${nodeType.type}"`);
102
+ const nodePath = operationPath.append(nodeState.id);
103
+ const dataProxy = nodeType.data._internal.createProxy(env, nodePath);
104
+ return {
105
+ id: nodeState.id,
106
+ type: nodeType.type,
107
+ data: dataProxy,
108
+ get: () => nodeState,
109
+ update: (value) => {
110
+ dataProxy.update(value);
111
+ }
112
+ };
113
+ },
114
+ get: () => nodeState
115
+ };
116
+ };
117
+ const buildSnapshot = (nodeId, nodes) => {
118
+ const node = nodes.find((n) => n.id === nodeId);
119
+ if (!node) return void 0;
120
+ const childNodes = getOrderedChildren(nodes, nodeId);
121
+ const children = [];
122
+ for (const child of childNodes) {
123
+ const childSnapshot = buildSnapshot(child.id, nodes);
124
+ if (childSnapshot) children.push(childSnapshot);
125
+ }
126
+ return _objectSpread2(_objectSpread2({
127
+ id: node.id,
128
+ type: node.type
129
+ }, node.data), {}, { children });
130
+ };
131
+ return {
132
+ get: () => {
133
+ return getCurrentState();
134
+ },
135
+ set: (nodes) => {
136
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, nodes));
137
+ },
138
+ root: () => {
139
+ return getCurrentState().find((n) => n.parentId === null);
140
+ },
141
+ children: (parentId) => {
142
+ return getOrderedChildren(getCurrentState(), parentId);
143
+ },
144
+ node: (id) => {
145
+ const nodeState = getCurrentState().find((n) => n.id === id);
146
+ if (!nodeState) return void 0;
147
+ return createNodeProxy(nodeState);
148
+ },
149
+ insertFirst: (parentId, nodeType, data) => {
150
+ const state = getCurrentState();
151
+ const siblings = getOrderedChildren(state, parentId);
152
+ const pos = generateTreePosBetween(null, siblings.length > 0 ? siblings[0].pos : null);
153
+ const id = env.generateId();
154
+ if (parentId !== null && !state.find((n) => n.id === parentId)) throw new ValidationError(`Parent node not found: ${parentId}`);
155
+ const parentType = getParentType(parentId);
156
+ this._validateChildType(parentType, nodeType.type);
157
+ if (parentId === null && state.some((n) => n.parentId === null)) throw new ValidationError("Tree already has a root node");
158
+ const mergedData = applyDefaults(nodeType.data, data);
159
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
160
+ id,
161
+ type: nodeType.type,
162
+ parentId,
163
+ pos,
164
+ data: mergedData
165
+ }));
166
+ return id;
167
+ },
168
+ insertLast: (parentId, nodeType, data) => {
169
+ const state = getCurrentState();
170
+ const siblings = getOrderedChildren(state, parentId);
171
+ const pos = generateTreePosBetween(siblings.length > 0 ? siblings[siblings.length - 1].pos : null, null);
172
+ const id = env.generateId();
173
+ if (parentId !== null && !state.find((n) => n.id === parentId)) throw new ValidationError(`Parent node not found: ${parentId}`);
174
+ const parentType = getParentType(parentId);
175
+ this._validateChildType(parentType, nodeType.type);
176
+ if (parentId === null && state.some((n) => n.parentId === null)) throw new ValidationError("Tree already has a root node");
177
+ const mergedData = applyDefaults(nodeType.data, data);
178
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
179
+ id,
180
+ type: nodeType.type,
181
+ parentId,
182
+ pos,
183
+ data: mergedData
184
+ }));
185
+ return id;
186
+ },
187
+ insertAt: (parentId, index, nodeType, data) => {
188
+ const state = getCurrentState();
189
+ const siblings = getOrderedChildren(state, parentId);
190
+ const clampedIndex = Math.max(0, Math.min(index, siblings.length));
191
+ const pos = generateTreePosBetween(clampedIndex > 0 && siblings[clampedIndex - 1] ? siblings[clampedIndex - 1].pos : null, clampedIndex < siblings.length && siblings[clampedIndex] ? siblings[clampedIndex].pos : null);
192
+ const id = env.generateId();
193
+ if (parentId !== null && !state.find((n) => n.id === parentId)) throw new ValidationError(`Parent node not found: ${parentId}`);
194
+ const parentType = getParentType(parentId);
195
+ this._validateChildType(parentType, nodeType.type);
196
+ if (parentId === null && state.some((n) => n.parentId === null)) throw new ValidationError("Tree already has a root node");
197
+ const mergedData = applyDefaults(nodeType.data, data);
198
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
199
+ id,
200
+ type: nodeType.type,
201
+ parentId,
202
+ pos,
203
+ data: mergedData
204
+ }));
205
+ return id;
206
+ },
207
+ insertAfter: (siblingId, nodeType, data) => {
208
+ var _nextSibling$pos;
209
+ const state = getCurrentState();
210
+ const sibling = state.find((n) => n.id === siblingId);
211
+ if (!sibling) throw new ValidationError(`Sibling node not found: ${siblingId}`);
212
+ const parentId = sibling.parentId;
213
+ const siblings = getOrderedChildren(state, parentId);
214
+ const nextSibling = siblings[siblings.findIndex((n) => n.id === siblingId) + 1];
215
+ const pos = generateTreePosBetween(sibling.pos, (_nextSibling$pos = nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.pos) !== null && _nextSibling$pos !== void 0 ? _nextSibling$pos : null);
216
+ const id = env.generateId();
217
+ const parentType = getParentType(parentId);
218
+ this._validateChildType(parentType, nodeType.type);
219
+ const mergedData = applyDefaults(nodeType.data, data);
220
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
221
+ id,
222
+ type: nodeType.type,
223
+ parentId,
224
+ pos,
225
+ data: mergedData
226
+ }));
227
+ return id;
228
+ },
229
+ insertBefore: (siblingId, nodeType, data) => {
230
+ var _prevSibling$pos;
231
+ const state = getCurrentState();
232
+ const sibling = state.find((n) => n.id === siblingId);
233
+ if (!sibling) throw new ValidationError(`Sibling node not found: ${siblingId}`);
234
+ const parentId = sibling.parentId;
235
+ const siblings = getOrderedChildren(state, parentId);
236
+ const prevSibling = siblings[siblings.findIndex((n) => n.id === siblingId) - 1];
237
+ const pos = generateTreePosBetween((_prevSibling$pos = prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.pos) !== null && _prevSibling$pos !== void 0 ? _prevSibling$pos : null, sibling.pos);
238
+ const id = env.generateId();
239
+ const parentType = getParentType(parentId);
240
+ this._validateChildType(parentType, nodeType.type);
241
+ const mergedData = applyDefaults(nodeType.data, data);
242
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
243
+ id,
244
+ type: nodeType.type,
245
+ parentId,
246
+ pos,
247
+ data: mergedData
248
+ }));
249
+ return id;
250
+ },
251
+ remove: (id) => {
252
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.remove, { id }));
253
+ },
254
+ move: (nodeId, newParentId, toIndex) => {
255
+ var _state$find$type, _state$find;
256
+ const state = getCurrentState();
257
+ const node = state.find((n) => n.id === nodeId);
258
+ if (!node) throw new ValidationError(`Node not found: ${nodeId}`);
259
+ if (newParentId !== null && !state.find((n) => n.id === newParentId)) throw new ValidationError(`Parent node not found: ${newParentId}`);
260
+ if (wouldCreateCycle(state, nodeId, newParentId)) throw new ValidationError("Move would create a cycle in the tree");
261
+ const newParentType = newParentId === null ? null : (_state$find$type = (_state$find = state.find((n) => n.id === newParentId)) === null || _state$find === void 0 ? void 0 : _state$find.type) !== null && _state$find$type !== void 0 ? _state$find$type : null;
262
+ this._validateChildType(newParentType, node.type);
263
+ if (node.parentId === null && newParentId !== null) throw new ValidationError("Cannot move root node to have a parent");
264
+ const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
265
+ const clampedIndex = Math.max(0, Math.min(toIndex, siblings.length));
266
+ const pos = generateTreePosBetween(clampedIndex > 0 && siblings[clampedIndex - 1] ? siblings[clampedIndex - 1].pos : null, clampedIndex < siblings.length && siblings[clampedIndex] ? siblings[clampedIndex].pos : null);
267
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.move, {
268
+ id: nodeId,
269
+ parentId: newParentId,
270
+ pos
271
+ }));
272
+ },
273
+ moveAfter: (nodeId, siblingId) => {
274
+ var _state$find$type2, _state$find2, _nextSibling$pos2;
275
+ const state = getCurrentState();
276
+ const node = state.find((n) => n.id === nodeId);
277
+ const sibling = state.find((n) => n.id === siblingId);
278
+ if (!node) throw new ValidationError(`Node not found: ${nodeId}`);
279
+ if (!sibling) throw new ValidationError(`Sibling node not found: ${siblingId}`);
280
+ const newParentId = sibling.parentId;
281
+ if (wouldCreateCycle(state, nodeId, newParentId)) throw new ValidationError("Move would create a cycle in the tree");
282
+ const newParentType = newParentId === null ? null : (_state$find$type2 = (_state$find2 = state.find((n) => n.id === newParentId)) === null || _state$find2 === void 0 ? void 0 : _state$find2.type) !== null && _state$find$type2 !== void 0 ? _state$find$type2 : null;
283
+ this._validateChildType(newParentType, node.type);
284
+ if (node.parentId === null && newParentId !== null) throw new ValidationError("Cannot move root node to have a parent");
285
+ const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
286
+ const nextSibling = siblings[siblings.findIndex((n) => n.id === siblingId) + 1];
287
+ const pos = generateTreePosBetween(sibling.pos, (_nextSibling$pos2 = nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.pos) !== null && _nextSibling$pos2 !== void 0 ? _nextSibling$pos2 : null);
288
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.move, {
289
+ id: nodeId,
290
+ parentId: newParentId,
291
+ pos
292
+ }));
293
+ },
294
+ moveBefore: (nodeId, siblingId) => {
295
+ var _state$find$type3, _state$find3, _prevSibling$pos2;
296
+ const state = getCurrentState();
297
+ const node = state.find((n) => n.id === nodeId);
298
+ const sibling = state.find((n) => n.id === siblingId);
299
+ if (!node) throw new ValidationError(`Node not found: ${nodeId}`);
300
+ if (!sibling) throw new ValidationError(`Sibling node not found: ${siblingId}`);
301
+ const newParentId = sibling.parentId;
302
+ if (wouldCreateCycle(state, nodeId, newParentId)) throw new ValidationError("Move would create a cycle in the tree");
303
+ const newParentType = newParentId === null ? null : (_state$find$type3 = (_state$find3 = state.find((n) => n.id === newParentId)) === null || _state$find3 === void 0 ? void 0 : _state$find3.type) !== null && _state$find$type3 !== void 0 ? _state$find$type3 : null;
304
+ this._validateChildType(newParentType, node.type);
305
+ if (node.parentId === null && newParentId !== null) throw new ValidationError("Cannot move root node to have a parent");
306
+ const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
307
+ const prevSibling = siblings[siblings.findIndex((n) => n.id === siblingId) - 1];
308
+ const pos = generateTreePosBetween((_prevSibling$pos2 = prevSibling === null || prevSibling === void 0 ? void 0 : prevSibling.pos) !== null && _prevSibling$pos2 !== void 0 ? _prevSibling$pos2 : null, sibling.pos);
309
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.move, {
310
+ id: nodeId,
311
+ parentId: newParentId,
312
+ pos
313
+ }));
314
+ },
315
+ moveToFirst: (nodeId, newParentId) => {
316
+ var _state$find$type4, _state$find4;
317
+ const state = getCurrentState();
318
+ const node = state.find((n) => n.id === nodeId);
319
+ if (!node) throw new ValidationError(`Node not found: ${nodeId}`);
320
+ if (newParentId !== null && !state.find((n) => n.id === newParentId)) throw new ValidationError(`Parent node not found: ${newParentId}`);
321
+ if (wouldCreateCycle(state, nodeId, newParentId)) throw new ValidationError("Move would create a cycle in the tree");
322
+ const newParentType = newParentId === null ? null : (_state$find$type4 = (_state$find4 = state.find((n) => n.id === newParentId)) === null || _state$find4 === void 0 ? void 0 : _state$find4.type) !== null && _state$find$type4 !== void 0 ? _state$find$type4 : null;
323
+ this._validateChildType(newParentType, node.type);
324
+ if (node.parentId === null && newParentId !== null) throw new ValidationError("Cannot move root node to have a parent");
325
+ const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
326
+ const pos = generateTreePosBetween(null, siblings.length > 0 ? siblings[0].pos : null);
327
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.move, {
328
+ id: nodeId,
329
+ parentId: newParentId,
330
+ pos
331
+ }));
332
+ },
333
+ moveToLast: (nodeId, newParentId) => {
334
+ var _state$find$type5, _state$find5;
335
+ const state = getCurrentState();
336
+ const node = state.find((n) => n.id === nodeId);
337
+ if (!node) throw new ValidationError(`Node not found: ${nodeId}`);
338
+ if (newParentId !== null && !state.find((n) => n.id === newParentId)) throw new ValidationError(`Parent node not found: ${newParentId}`);
339
+ if (wouldCreateCycle(state, nodeId, newParentId)) throw new ValidationError("Move would create a cycle in the tree");
340
+ const newParentType = newParentId === null ? null : (_state$find$type5 = (_state$find5 = state.find((n) => n.id === newParentId)) === null || _state$find5 === void 0 ? void 0 : _state$find5.type) !== null && _state$find$type5 !== void 0 ? _state$find$type5 : null;
341
+ this._validateChildType(newParentType, node.type);
342
+ if (node.parentId === null && newParentId !== null) throw new ValidationError("Cannot move root node to have a parent");
343
+ const siblings = getOrderedChildren(state, newParentId).filter((n) => n.id !== nodeId);
344
+ const pos = generateTreePosBetween(siblings.length > 0 ? siblings[siblings.length - 1].pos : null, null);
345
+ env.addOperation(fromDefinition(operationPath, this._opDefinitions.move, {
346
+ id: nodeId,
347
+ parentId: newParentId,
348
+ pos
349
+ }));
350
+ },
351
+ at: (id, nodeType) => {
352
+ const node = getCurrentState().find((n) => n.id === id);
353
+ if (!node) throw new ValidationError(`Node not found: ${id}`);
354
+ if (node.type !== nodeType.type) throw new ValidationError(`Node is of type "${node.type}", not "${nodeType.type}"`);
355
+ const nodePath = operationPath.append(id);
356
+ return nodeType.data._internal.createProxy(env, nodePath);
357
+ },
358
+ updateAt: (id, nodeType, value) => {
359
+ const node = getCurrentState().find((n) => n.id === id);
360
+ if (!node) throw new ValidationError(`Node not found: ${id}`);
361
+ if (node.type !== nodeType.type) throw new ValidationError(`Node is of type "${node.type}", not "${nodeType.type}"`);
362
+ const nodePath = operationPath.append(id);
363
+ nodeType.data._internal.createProxy(env, nodePath).update(value);
364
+ },
365
+ toSnapshot: () => {
366
+ const state = getCurrentState();
367
+ const rootNode = state.find((n) => n.parentId === null);
368
+ if (!rootNode) return void 0;
369
+ return buildSnapshot(rootNode.id, state);
370
+ }
371
+ };
372
+ },
373
+ applyOperation: (state, operation) => {
374
+ const path = operation.path;
375
+ const tokens = path.toTokens().filter((t) => t !== "");
376
+ const currentState = state !== null && state !== void 0 ? state : [];
377
+ let newState;
378
+ if (tokens.length === 0) switch (operation.kind) {
379
+ case "tree.set": {
380
+ const payload = operation.payload;
381
+ if (!globalThis.Array.isArray(payload)) throw new ValidationError(`TreePrimitive.set requires an array payload`);
382
+ newState = payload;
383
+ break;
384
+ }
385
+ case "tree.insert": {
386
+ const { id, type, parentId, pos, data } = operation.payload;
387
+ newState = [...currentState, {
388
+ id,
389
+ type,
390
+ parentId,
391
+ pos,
392
+ data
393
+ }];
394
+ break;
395
+ }
396
+ case "tree.remove": {
397
+ const { id } = operation.payload;
398
+ const descendantIds = getDescendantIds(currentState, id);
399
+ const idsToRemove = new Set([id, ...descendantIds]);
400
+ newState = currentState.filter((node) => !idsToRemove.has(node.id));
401
+ break;
402
+ }
403
+ case "tree.move": {
404
+ const { id, parentId, pos } = operation.payload;
405
+ newState = currentState.map((node) => node.id === id ? _objectSpread2(_objectSpread2({}, node), {}, {
406
+ parentId,
407
+ pos
408
+ }) : node);
409
+ break;
410
+ }
411
+ default: throw new ValidationError(`TreePrimitive cannot apply operation of kind: ${operation.kind}`);
412
+ }
413
+ else {
414
+ const nodeId = tokens[0];
415
+ const nodeIndex = currentState.findIndex((node$1) => node$1.id === nodeId);
416
+ if (nodeIndex === -1) throw new ValidationError(`Tree node not found with ID: ${nodeId}`);
417
+ const node = currentState[nodeIndex];
418
+ const nodeTypePrimitive = this._getNodeTypePrimitive(node.type);
419
+ const remainingPath = path.shift();
420
+ const nodeOperation = _objectSpread2(_objectSpread2({}, operation), {}, { path: remainingPath });
421
+ const newData = nodeTypePrimitive.data._internal.applyOperation(node.data, nodeOperation);
422
+ const mutableState = [...currentState];
423
+ mutableState[nodeIndex] = _objectSpread2(_objectSpread2({}, node), {}, { data: newData });
424
+ newState = mutableState;
425
+ }
426
+ runValidators(newState, this._schema.validators);
427
+ return newState;
428
+ },
429
+ getInitialState: () => {
430
+ var _rootNodeType$data$_i;
431
+ if (this._schema.defaultValue !== void 0) return this._schema.defaultValue;
432
+ const rootNodeType = this._schema.root;
433
+ const rootData = (_rootNodeType$data$_i = rootNodeType.data._internal.getInitialState()) !== null && _rootNodeType$data$_i !== void 0 ? _rootNodeType$data$_i : {};
434
+ const rootId = crypto.randomUUID();
435
+ const rootPos = generateTreePosBetween(null, null);
436
+ return [{
437
+ id: rootId,
438
+ type: rootNodeType.type,
439
+ parentId: null,
440
+ pos: rootPos,
441
+ data: rootData
442
+ }];
443
+ },
444
+ transformOperation: (clientOp, serverOp) => {
445
+ const clientPath = clientOp.path;
446
+ const serverPath = serverOp.path;
447
+ if (!pathsOverlap(clientPath, serverPath)) return {
448
+ type: "transformed",
449
+ operation: clientOp
450
+ };
451
+ if (serverOp.kind === "tree.remove") {
452
+ const removedId = serverOp.payload.id;
453
+ const clientTokens$1 = clientPath.toTokens().filter((t) => t !== "");
454
+ const serverTokens$1 = serverPath.toTokens().filter((t) => t !== "");
455
+ if (clientOp.kind === "tree.move") {
456
+ const movePayload = clientOp.payload;
457
+ if (movePayload.id === removedId || movePayload.parentId === removedId) return { type: "noop" };
458
+ }
459
+ if (clientOp.kind === "tree.insert") {
460
+ if (clientOp.payload.parentId === removedId) return { type: "noop" };
461
+ }
462
+ if (clientTokens$1.length > serverTokens$1.length) {
463
+ if (clientTokens$1[serverTokens$1.length] === removedId) return { type: "noop" };
464
+ }
465
+ }
466
+ if (serverOp.kind === "tree.insert" && clientOp.kind === "tree.insert") return {
467
+ type: "transformed",
468
+ operation: clientOp
469
+ };
470
+ if (serverOp.kind === "tree.move" && clientOp.kind === "tree.move") {
471
+ if (serverOp.payload.id === clientOp.payload.id) return {
472
+ type: "transformed",
473
+ operation: clientOp
474
+ };
475
+ return {
476
+ type: "transformed",
477
+ operation: clientOp
478
+ };
479
+ }
480
+ if (pathsEqual(clientPath, serverPath)) return {
481
+ type: "transformed",
482
+ operation: clientOp
483
+ };
484
+ if (serverOp.kind === "tree.set" && isPrefix(serverPath, clientPath)) return {
485
+ type: "transformed",
486
+ operation: clientOp
487
+ };
488
+ const clientTokens = clientPath.toTokens().filter((t) => t !== "");
489
+ const serverTokens = serverPath.toTokens().filter((t) => t !== "");
490
+ if (clientTokens.length > 0 && serverTokens.length > 0) {
491
+ if (clientTokens[0] !== serverTokens[0]) return {
492
+ type: "transformed",
493
+ operation: clientOp
494
+ };
495
+ return {
496
+ type: "transformed",
497
+ operation: clientOp
498
+ };
499
+ }
500
+ return {
501
+ type: "transformed",
502
+ operation: clientOp
503
+ };
504
+ }
505
+ });
506
+ this._schema = schema;
507
+ }
508
+ /** Mark this tree as required */
509
+ required() {
510
+ return new TreePrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { required: true }));
511
+ }
512
+ /** Set a default value for this tree */
513
+ default(defaultValue) {
514
+ return new TreePrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue }));
515
+ }
516
+ /** Get the root node type */
517
+ get root() {
518
+ return this._schema.root;
519
+ }
520
+ /** Add a custom validation rule */
521
+ refine(fn, message) {
522
+ return new TreePrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { validators: [...this._schema.validators, {
523
+ validate: fn,
524
+ message
525
+ }] }));
526
+ }
527
+ /**
528
+ * Build a registry of all node types reachable from root
529
+ */
530
+ _buildNodeTypeRegistry() {
531
+ if (this._nodeTypeRegistry !== void 0) return this._nodeTypeRegistry;
532
+ const registry = /* @__PURE__ */ new Map();
533
+ const visited = /* @__PURE__ */ new Set();
534
+ const visit = (node) => {
535
+ if (visited.has(node.type)) return;
536
+ visited.add(node.type);
537
+ registry.set(node.type, node);
538
+ for (const child of node.children) visit(child);
539
+ };
540
+ visit(this._schema.root);
541
+ this._nodeTypeRegistry = registry;
542
+ return registry;
543
+ }
544
+ /**
545
+ * Get a node type primitive by its type string
546
+ */
547
+ _getNodeTypePrimitive(type) {
548
+ const nodeType = this._buildNodeTypeRegistry().get(type);
549
+ if (!nodeType) throw new ValidationError(`Unknown node type: ${type}`);
550
+ return nodeType;
551
+ }
552
+ /**
553
+ * Validate that a node type can be a child of a parent node type
554
+ */
555
+ _validateChildType(parentType, childType) {
556
+ if (parentType === null) {
557
+ if (childType !== this._schema.root.type) throw new ValidationError(`Root node must be of type "${this._schema.root.type}", got "${childType}"`);
558
+ return;
559
+ }
560
+ const parentNodePrimitive = this._getNodeTypePrimitive(parentType);
561
+ if (!parentNodePrimitive.isChildAllowed(childType)) throw new ValidationError(`Node type "${childType}" is not allowed as a child of "${parentType}". Allowed types: ${parentNodePrimitive.children.map((c) => c.type).join(", ") || "none"}`);
562
+ }
563
+ };
564
+ /** Creates a new TreePrimitive with the given root node type */
565
+ const Tree = (options) => new TreePrimitive({
566
+ required: false,
567
+ defaultValue: void 0,
568
+ root: options.root,
569
+ validators: []
570
+ });
571
+
572
+ //#endregion
573
+ export { Tree, TreePrimitive };
574
+ //# sourceMappingURL=Tree.mjs.map