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