@automerge/automerge-repo-solid-primitives 2.2.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,912 @@
1
+ import { onCleanup as Y, createMemo as G, createContext as X, useContext as J, createResource as Z, createEffect as Q } from "solid-js";
2
+ import { createStore as ee, produce as te, reconcile as re } from "solid-js/store";
3
+ const m = Symbol.for("_am_meta"), w = Symbol.for("_am_trace"), y = Symbol.for("_am_objectId"), O = Symbol.for("_am_isProxy"), A = Symbol.for("_am_clearCache"), ne = Symbol.for("_am_uint"), oe = Symbol.for("_am_int"), se = Symbol.for("_am_f64"), T = Symbol.for("_am_counter"), W = Symbol.for("_am_immutableString");
4
+ class ie {
5
+ constructor(e) {
6
+ this.value = e || 0, Reflect.defineProperty(this, T, { value: !0 });
7
+ }
8
+ /**
9
+ * A peculiar JavaScript language feature from its early days: if the object
10
+ * `x` has a `valueOf()` method that returns a number, you can use numerical
11
+ * operators on the object `x` directly, such as `x + 1` or `x < 4`.
12
+ * This method is also called when coercing a value to a string by
13
+ * concatenating it with another string, as in `x + ''`.
14
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
15
+ */
16
+ valueOf() {
17
+ return this.value;
18
+ }
19
+ /**
20
+ * Returns the counter value as a decimal string. If `x` is a counter object,
21
+ * this method is called e.g. when you do `['value: ', x].join('')` or when
22
+ * you use string interpolation: `value: ${x}`.
23
+ */
24
+ toString() {
25
+ return this.valueOf().toString();
26
+ }
27
+ /**
28
+ * Returns the counter value, so that a JSON serialization of an Automerge
29
+ * document represents the counter simply as an integer.
30
+ */
31
+ toJSON() {
32
+ return this.value;
33
+ }
34
+ /**
35
+ * Increases the value of the counter by `delta`. If `delta` is not given,
36
+ * increases the value of the counter by 1.
37
+ *
38
+ * Will throw an error if used outside of a change callback.
39
+ */
40
+ increment(e) {
41
+ throw new Error("Counters should not be incremented outside of a change callback");
42
+ }
43
+ /**
44
+ * Decreases the value of the counter by `delta`. If `delta` is not given,
45
+ * decreases the value of the counter by 1.
46
+ *
47
+ * Will throw an error if used outside of a change callback.
48
+ */
49
+ decrement(e) {
50
+ throw new Error("Counters should not be decremented outside of a change callback");
51
+ }
52
+ }
53
+ class ue extends ie {
54
+ constructor(e, r, o, s, n) {
55
+ super(e), this.context = r, this.path = o, this.objectId = s, this.key = n;
56
+ }
57
+ /**
58
+ * Increases the value of the counter by `delta`. If `delta` is not given,
59
+ * increases the value of the counter by 1.
60
+ */
61
+ increment(e) {
62
+ return e = typeof e == "number" ? e : 1, this.context.increment(this.objectId, this.key, e), this.value += e, this.value;
63
+ }
64
+ /**
65
+ * Decreases the value of the counter by `delta`. If `delta` is not given,
66
+ * decreases the value of the counter by 1.
67
+ */
68
+ decrement(e) {
69
+ return this.increment(typeof e == "number" ? -e : -1);
70
+ }
71
+ }
72
+ function ae(t, e, r, o, s) {
73
+ return new ue(t, e, r, o, s);
74
+ }
75
+ var z;
76
+ class ce {
77
+ constructor(e) {
78
+ this[z] = !0, this.val = e;
79
+ }
80
+ /**
81
+ * Returns the content of the ImmutableString object as a simple string
82
+ */
83
+ toString() {
84
+ return this.val;
85
+ }
86
+ toJSON() {
87
+ return this.val;
88
+ }
89
+ }
90
+ z = W;
91
+ function h(t) {
92
+ if (typeof t == "string" && /^[0-9]+$/.test(t) && (t = parseInt(t, 10)), typeof t != "number")
93
+ return t;
94
+ if (t < 0 || isNaN(t) || t === 1 / 0 || t === -1 / 0)
95
+ throw new RangeError("A list index must be positive, but you passed " + t);
96
+ return t;
97
+ }
98
+ function p(t, e) {
99
+ const { context: r, objectId: o, path: s } = t, n = r.getWithType(o, e);
100
+ if (n === null)
101
+ return;
102
+ const i = n[0], u = n[1];
103
+ switch (i) {
104
+ case void 0:
105
+ return;
106
+ case "map":
107
+ return P(r, u, [...s, e]);
108
+ case "list":
109
+ return _(r, u, [...s, e]);
110
+ case "text":
111
+ return r.text(u);
112
+ case "str":
113
+ return new ce(u);
114
+ case "uint":
115
+ return u;
116
+ case "int":
117
+ return u;
118
+ case "f64":
119
+ return u;
120
+ case "boolean":
121
+ return u;
122
+ case "null":
123
+ return null;
124
+ case "bytes":
125
+ return u;
126
+ case "timestamp":
127
+ return u;
128
+ case "counter":
129
+ return ae(u, r, s, o, e);
130
+ default:
131
+ throw RangeError(`datatype ${i} unimplemented`);
132
+ }
133
+ }
134
+ function S(t, e, r) {
135
+ const o = typeof t;
136
+ switch (o) {
137
+ case "object":
138
+ if (t == null)
139
+ return [null, "null"];
140
+ if (t[ne])
141
+ return [t.value, "uint"];
142
+ if (t[oe])
143
+ return [t.value, "int"];
144
+ if (t[se])
145
+ return [t.value, "f64"];
146
+ if (t[T])
147
+ return [t.value, "counter"];
148
+ if (t instanceof Date)
149
+ return [t.getTime(), "timestamp"];
150
+ if (pe(t))
151
+ return [t.toString(), "str"];
152
+ if (t instanceof Uint8Array)
153
+ return [t, "bytes"];
154
+ if (t instanceof Array)
155
+ return [t, "list"];
156
+ if (Object.prototype.toString.call(t) === "[object Object]")
157
+ return [t, "map"];
158
+ throw E(t, r) ? new RangeError("Cannot create a reference to an existing document object") : new RangeError(`Cannot assign unknown object: ${t}`);
159
+ case "boolean":
160
+ return [t, "boolean"];
161
+ case "number":
162
+ return Number.isInteger(t) ? [t, "int"] : [t, "f64"];
163
+ case "string":
164
+ return [t, "text"];
165
+ case "undefined":
166
+ throw new RangeError([
167
+ `Cannot assign undefined value at ${M(e)}, `,
168
+ "because `undefined` is not a valid JSON data type. ",
169
+ "You might consider setting the property's value to `null`, ",
170
+ "or using `delete` to remove it altogether."
171
+ ].join(""));
172
+ default:
173
+ throw new RangeError([
174
+ `Cannot assign ${o} value at ${M(e)}. `,
175
+ "All JSON primitive datatypes (object, array, string, number, boolean, null) ",
176
+ `are supported in an Automerge document; ${o} values are not. `
177
+ ].join(""));
178
+ }
179
+ }
180
+ function E(t, e) {
181
+ var r, o;
182
+ return t instanceof Date ? !1 : !!(t && ((o = (r = t[m]) === null || r === void 0 ? void 0 : r.handle) === null || o === void 0 ? void 0 : o.__wbg_ptr) === e.__wbg_ptr);
183
+ }
184
+ const fe = {
185
+ get(t, e) {
186
+ const { context: r, objectId: o, cache: s } = t;
187
+ return e === Symbol.toStringTag ? t[Symbol.toStringTag] : e === y ? o : e === O ? !0 : e === w ? t.trace : e === m ? { handle: r } : (s[e] || (s[e] = p(t, e)), s[e]);
188
+ },
189
+ set(t, e, r) {
190
+ const { context: o, objectId: s, path: n } = t;
191
+ if (t.cache = {}, E(r, o))
192
+ throw new RangeError("Cannot create a reference to an existing document object");
193
+ if (e === w)
194
+ return t.trace = r, !0;
195
+ if (e === A)
196
+ return !0;
197
+ const [i, u] = S(r, [...n, e], o);
198
+ switch (u) {
199
+ case "list": {
200
+ const a = o.putObject(s, e, []), f = _(o, a, [...n, e]);
201
+ for (let c = 0; c < i.length; c++)
202
+ f[c] = i[c];
203
+ break;
204
+ }
205
+ case "text": {
206
+ o.putObject(s, e, i);
207
+ break;
208
+ }
209
+ case "map": {
210
+ const a = o.putObject(s, e, {}), f = P(o, a, [...n, e]);
211
+ for (const c in i)
212
+ f[c] = i[c];
213
+ break;
214
+ }
215
+ default:
216
+ o.put(s, e, i, u);
217
+ }
218
+ return !0;
219
+ },
220
+ deleteProperty(t, e) {
221
+ const { context: r, objectId: o } = t;
222
+ return t.cache = {}, r.delete(o, e), !0;
223
+ },
224
+ has(t, e) {
225
+ return this.get(t, e) !== void 0;
226
+ },
227
+ getOwnPropertyDescriptor(t, e) {
228
+ const r = this.get(t, e);
229
+ if (typeof r < "u")
230
+ return {
231
+ configurable: !0,
232
+ enumerable: !0,
233
+ value: r
234
+ };
235
+ },
236
+ ownKeys(t) {
237
+ const { context: e, objectId: r } = t, o = e.keys(r);
238
+ return [...new Set(o)];
239
+ }
240
+ }, F = {
241
+ get(t, e) {
242
+ const { context: r, objectId: o } = t;
243
+ return e = h(e), e === Symbol.hasInstance ? (s) => Array.isArray(s) : e === Symbol.toStringTag ? t[Symbol.toStringTag] : e === y ? o : e === O ? !0 : e === w ? t.trace : e === m ? { handle: r } : e === "length" ? r.length(o) : typeof e == "number" ? p(t, e) : C(t)[e];
244
+ },
245
+ set(t, e, r) {
246
+ const { context: o, objectId: s, path: n } = t;
247
+ if (e = h(e), E(r, o))
248
+ throw new RangeError("Cannot create a reference to an existing document object");
249
+ if (e === A)
250
+ return !0;
251
+ if (e === w)
252
+ return t.trace = r, !0;
253
+ if (typeof e == "string")
254
+ throw new RangeError("list index must be a number");
255
+ const [i, u] = S(r, [...n, e], o);
256
+ switch (u) {
257
+ case "list": {
258
+ let a;
259
+ e >= o.length(s) ? a = o.insertObject(s, e, []) : a = o.putObject(s, e, []), _(o, a, [...n, e]).splice(0, 0, ...i);
260
+ break;
261
+ }
262
+ case "text": {
263
+ e >= o.length(s) ? o.insertObject(s, e, i) : o.putObject(s, e, i);
264
+ break;
265
+ }
266
+ case "map": {
267
+ let a;
268
+ e >= o.length(s) ? a = o.insertObject(s, e, {}) : a = o.putObject(s, e, {});
269
+ const f = P(o, a, [...n, e]);
270
+ for (const c in i)
271
+ f[c] = i[c];
272
+ break;
273
+ }
274
+ default:
275
+ e >= o.length(s) ? o.insert(s, e, i, u) : o.put(s, e, i, u);
276
+ }
277
+ return !0;
278
+ },
279
+ deleteProperty(t, e) {
280
+ const { context: r, objectId: o } = t;
281
+ e = h(e);
282
+ const s = r.get(o, e);
283
+ if (s != null && s[0] == "counter")
284
+ throw new TypeError("Unsupported operation: deleting a counter from a list");
285
+ return r.delete(o, e), !0;
286
+ },
287
+ has(t, e) {
288
+ const { context: r, objectId: o } = t;
289
+ return e = h(e), typeof e == "number" ? e < r.length(o) : e === "length";
290
+ },
291
+ getOwnPropertyDescriptor(t, e) {
292
+ const { context: r, objectId: o } = t;
293
+ return e === "length" ? { writable: !0, value: r.length(o) } : e === y ? { configurable: !1, enumerable: !1, value: o } : (e = h(e), { configurable: !0, enumerable: !0, value: p(t, e) });
294
+ },
295
+ getPrototypeOf(t) {
296
+ return Object.getPrototypeOf(t);
297
+ },
298
+ ownKeys() {
299
+ const t = [];
300
+ return t.push("length"), t;
301
+ }
302
+ };
303
+ Object.assign({}, F, {
304
+ get(t, e) {
305
+ const { context: r, objectId: o } = t;
306
+ return e = h(e), e === Symbol.hasInstance ? (s) => Array.isArray(s) : e === Symbol.toStringTag ? t[Symbol.toStringTag] : e === y ? o : e === O ? !0 : e === w ? t.trace : e === m ? { handle: r } : e === "length" ? r.length(o) : typeof e == "number" ? p(t, e) : le(t)[e] || C(t)[e];
307
+ },
308
+ getPrototypeOf() {
309
+ return Object.getPrototypeOf(new Text());
310
+ }
311
+ });
312
+ function P(t, e, r) {
313
+ const o = {
314
+ context: t,
315
+ objectId: e,
316
+ path: r || [],
317
+ cache: {}
318
+ }, s = {};
319
+ return Object.assign(s, o), new Proxy(s, fe);
320
+ }
321
+ function _(t, e, r) {
322
+ const o = {
323
+ context: t,
324
+ objectId: e,
325
+ path: r || [],
326
+ cache: {}
327
+ }, s = [];
328
+ return Object.assign(s, o), new Proxy(s, F);
329
+ }
330
+ function C(t) {
331
+ const { context: e, objectId: r, path: o } = t;
332
+ return {
333
+ at(n) {
334
+ return p(t, n);
335
+ },
336
+ deleteAt(n, i) {
337
+ return typeof i == "number" ? e.splice(r, n, i) : e.delete(r, n), this;
338
+ },
339
+ fill(n, i, u) {
340
+ const [a, f] = S(n, [...o, i], e), c = e.length(r);
341
+ i = h(i || 0), u = h(u || c);
342
+ for (let l = i; l < Math.min(u, c); l++)
343
+ f === "list" || f === "map" || f === "text" ? e.putObject(r, l, a) : e.put(r, l, a, f);
344
+ return this;
345
+ },
346
+ indexOf(n, i = 0) {
347
+ const u = e.length(r);
348
+ for (let a = i; a < u; a++) {
349
+ const f = e.getWithType(r, a);
350
+ if (!f)
351
+ continue;
352
+ const [c, l] = f;
353
+ if (!["map", "list", "text"].includes(c)) {
354
+ if (l === n)
355
+ return a;
356
+ continue;
357
+ }
358
+ if (c === "text" && typeof n == "string" && n === p(t, a) || n[y] === l)
359
+ return a;
360
+ }
361
+ return -1;
362
+ },
363
+ insertAt(n, ...i) {
364
+ return this.splice(n, 0, ...i), this;
365
+ },
366
+ pop() {
367
+ const n = e.length(r);
368
+ if (n == 0)
369
+ return;
370
+ const i = p(t, n - 1);
371
+ return e.delete(r, n - 1), i;
372
+ },
373
+ push(...n) {
374
+ const i = e.length(r);
375
+ return this.splice(i, 0, ...n), e.length(r);
376
+ },
377
+ shift() {
378
+ if (e.length(r) == 0)
379
+ return;
380
+ const n = p(t, 0);
381
+ return e.delete(r, 0), n;
382
+ },
383
+ splice(n, i, ...u) {
384
+ n = h(n), typeof i != "number" && (i = e.length(r) - n), i = h(i);
385
+ for (const c of u)
386
+ if (E(c, e))
387
+ throw new RangeError("Cannot create a reference to an existing document object");
388
+ const a = [];
389
+ for (let c = 0; c < i; c++) {
390
+ const l = p(t, n);
391
+ l !== void 0 && a.push(l), e.delete(r, n);
392
+ }
393
+ const f = u.map((c, l) => {
394
+ try {
395
+ return S(c, [...o], e);
396
+ } catch (g) {
397
+ throw g instanceof RangeError ? new RangeError(`${g.message} (at index ${l} in the input)`) : g;
398
+ }
399
+ });
400
+ for (const [c, l] of f) {
401
+ switch (l) {
402
+ case "list": {
403
+ const g = e.insertObject(r, n, []);
404
+ _(e, g, [...o, n]).splice(0, 0, ...c);
405
+ break;
406
+ }
407
+ case "text": {
408
+ e.insertObject(r, n, c);
409
+ break;
410
+ }
411
+ case "map": {
412
+ const g = e.insertObject(r, n, {}), L = P(e, g, [...o, n]);
413
+ for (const D in c)
414
+ L[D] = c[D];
415
+ break;
416
+ }
417
+ default:
418
+ e.insert(r, n, c, l);
419
+ }
420
+ n += 1;
421
+ }
422
+ return a;
423
+ },
424
+ unshift(...n) {
425
+ return this.splice(0, 0, ...n), e.length(r);
426
+ },
427
+ entries() {
428
+ let n = 0;
429
+ return {
430
+ next: () => {
431
+ const u = p(t, n);
432
+ return u === void 0 ? { value: void 0, done: !0 } : { value: [n++, u], done: !1 };
433
+ },
434
+ [Symbol.iterator]() {
435
+ return this;
436
+ }
437
+ };
438
+ },
439
+ keys() {
440
+ let n = 0;
441
+ const i = e.length(r);
442
+ return {
443
+ next: () => n < i ? { value: n++, done: !1 } : { value: void 0, done: !0 },
444
+ [Symbol.iterator]() {
445
+ return this;
446
+ }
447
+ };
448
+ },
449
+ values() {
450
+ let n = 0;
451
+ return {
452
+ next: () => {
453
+ const u = p(t, n++);
454
+ return u === void 0 ? { value: void 0, done: !0 } : { value: u, done: !1 };
455
+ },
456
+ [Symbol.iterator]() {
457
+ return this;
458
+ }
459
+ };
460
+ },
461
+ toArray() {
462
+ const n = [];
463
+ let i;
464
+ do
465
+ i = p(t, n.length), i !== void 0 && n.push(i);
466
+ while (i !== void 0);
467
+ return n;
468
+ },
469
+ map(n) {
470
+ return this.toArray().map(n);
471
+ },
472
+ toString() {
473
+ return this.toArray().toString();
474
+ },
475
+ toLocaleString() {
476
+ return this.toArray().toLocaleString();
477
+ },
478
+ forEach(n) {
479
+ return this.toArray().forEach(n);
480
+ },
481
+ // todo: real concat function is different
482
+ concat(n) {
483
+ return this.toArray().concat(n);
484
+ },
485
+ every(n) {
486
+ return this.toArray().every(n);
487
+ },
488
+ filter(n) {
489
+ return this.toArray().filter(n);
490
+ },
491
+ find(n) {
492
+ let i = 0;
493
+ for (const u of this) {
494
+ if (n(u, i))
495
+ return u;
496
+ i += 1;
497
+ }
498
+ },
499
+ findIndex(n) {
500
+ let i = 0;
501
+ for (const u of this) {
502
+ if (n(u, i))
503
+ return i;
504
+ i += 1;
505
+ }
506
+ return -1;
507
+ },
508
+ includes(n) {
509
+ return this.find((i) => i === n) !== void 0;
510
+ },
511
+ join(n) {
512
+ return this.toArray().join(n);
513
+ },
514
+ reduce(n, i) {
515
+ return this.toArray().reduce(n, i);
516
+ },
517
+ reduceRight(n, i) {
518
+ return this.toArray().reduceRight(n, i);
519
+ },
520
+ lastIndexOf(n, i = 1 / 0) {
521
+ return this.toArray().lastIndexOf(n, i);
522
+ },
523
+ slice(n, i) {
524
+ return this.toArray().slice(n, i);
525
+ },
526
+ some(n) {
527
+ let i = 0;
528
+ for (const u of this) {
529
+ if (n(u, i))
530
+ return !0;
531
+ i += 1;
532
+ }
533
+ return !1;
534
+ },
535
+ [Symbol.iterator]: function* () {
536
+ let n = 0, i = p(t, n);
537
+ for (; i !== void 0; )
538
+ yield i, n += 1, i = p(t, n);
539
+ }
540
+ };
541
+ }
542
+ function le(t) {
543
+ const { context: e, objectId: r } = t;
544
+ return {
545
+ set(s, n) {
546
+ return this[s] = n;
547
+ },
548
+ get(s) {
549
+ return this[s];
550
+ },
551
+ toString() {
552
+ return e.text(r).replace(//g, "");
553
+ },
554
+ toSpans() {
555
+ const s = [];
556
+ let n = "";
557
+ const i = e.length(r);
558
+ for (let u = 0; u < i; u++) {
559
+ const a = this[u];
560
+ typeof a == "string" ? n += a : (n.length > 0 && (s.push(n), n = ""), s.push(a));
561
+ }
562
+ return n.length > 0 && s.push(n), s;
563
+ },
564
+ toJSON() {
565
+ return this.toString();
566
+ },
567
+ indexOf(s, n = 0) {
568
+ return e.text(r).indexOf(s, n);
569
+ },
570
+ insertAt(s, ...n) {
571
+ n.every((i) => typeof i == "string") ? e.splice(r, s, 0, n.join("")) : C(t).insertAt(s, ...n);
572
+ }
573
+ };
574
+ }
575
+ function M(t) {
576
+ const e = t.map((r) => {
577
+ if (typeof r == "number")
578
+ return r.toString();
579
+ if (typeof r == "string")
580
+ return r.replace(/~/g, "~0").replace(/\//g, "~1");
581
+ });
582
+ return t.length === 0 ? "" : "/" + e.join("/");
583
+ }
584
+ function pe(t) {
585
+ return typeof t == "object" && t !== null && Object.prototype.hasOwnProperty.call(t, W);
586
+ }
587
+ function he(t) {
588
+ return typeof t == "object" && t !== null && Object.prototype.hasOwnProperty.call(t, T);
589
+ }
590
+ let B;
591
+ const v = typeof TextEncoder < "u" ? new TextEncoder("utf-8") : { encode: () => {
592
+ throw Error("TextEncoder not available");
593
+ } };
594
+ v.encodeInto;
595
+ const be = typeof TextDecoder < "u" ? new TextDecoder("utf-8", { ignoreBOM: !0, fatal: !0 }) : { decode: () => {
596
+ throw Error("TextDecoder not available");
597
+ } };
598
+ typeof TextDecoder < "u" && be.decode();
599
+ typeof FinalizationRegistry > "u" || new FinalizationRegistry((t) => B.__wbg_automerge_free(t >>> 0, 1));
600
+ typeof FinalizationRegistry > "u" || new FinalizationRegistry((t) => B.__wbg_syncstate_free(t >>> 0, 1));
601
+ function j(t, e = !0) {
602
+ if (typeof t != "object")
603
+ throw new RangeError("must be the document root");
604
+ const r = Reflect.get(t, m);
605
+ if (r === void 0 || r == null || e && x(t) !== "_root")
606
+ throw new RangeError("must be the document root");
607
+ return r;
608
+ }
609
+ function ge(t) {
610
+ Reflect.set(t, A, !0);
611
+ }
612
+ function x(t) {
613
+ return typeof t != "object" || t === null ? null : Reflect.get(t, y);
614
+ }
615
+ function k(t) {
616
+ return !!Reflect.get(t, O);
617
+ }
618
+ function ye(t, e) {
619
+ let r = Ee(t, e.path);
620
+ if (e.action === "put")
621
+ me(t, r, e);
622
+ else if (e.action === "insert")
623
+ de(t, r, e);
624
+ else if (e.action === "del")
625
+ we(t, r, e);
626
+ else if (e.action === "splice")
627
+ je(t, r, e);
628
+ else if (e.action === "inc")
629
+ Re(t, r, e);
630
+ else if (e.action === "mark")
631
+ Se(t, r, e);
632
+ else if (e.action === "unmark")
633
+ Oe(t, r, e);
634
+ else if (e.action !== "conflict") throw new RangeError(`unsupported patch: ${e}`);
635
+ }
636
+ function me(t, e, r) {
637
+ let { obj: o, prop: s } = b(e, -1);
638
+ o[s] = r.value;
639
+ }
640
+ function de(t, e, r) {
641
+ let { obj: o, prop: s } = b(e, -1);
642
+ if (!Array.isArray(o))
643
+ throw new RangeError("target is not an array for patch");
644
+ if (typeof s != "number")
645
+ throw new RangeError("index is not a number for patch");
646
+ o.splice(s, 0, ...r.values);
647
+ }
648
+ function we(t, e, r) {
649
+ let { obj: o, prop: s, parentPath: n } = b(e, -1);
650
+ if (typeof s != "number")
651
+ throw new RangeError("index is not a number for patch");
652
+ if (Array.isArray(o))
653
+ o.splice(s, r.length || 1);
654
+ else if (typeof o == "string")
655
+ if (R(t))
656
+ K(t, n, s, r.length || 1);
657
+ else {
658
+ let { obj: i, prop: u } = b(e, -2);
659
+ if (typeof s != "number")
660
+ throw new RangeError("index is not a number for patch");
661
+ let a = i[u];
662
+ if (a == null || typeof a != "string")
663
+ throw new RangeError("target is not a string for patch");
664
+ let f = a.slice(0, s) + a.slice(s + (r.length || 1));
665
+ i[u] = f;
666
+ }
667
+ else
668
+ throw new RangeError("target is not an array or string for patch");
669
+ }
670
+ function je(t, e, r) {
671
+ if (R(t)) {
672
+ let { obj: o, prop: s, parentPath: n } = b(e, -1);
673
+ if (typeof s != "number")
674
+ throw new RangeError("index is not a number for patch");
675
+ K(t, n, s, 0, r.value);
676
+ } else {
677
+ let { obj: o, prop: s } = b(e, -1), { obj: n, prop: i } = b(e, -2);
678
+ if (typeof s != "number")
679
+ throw new RangeError("index is not a number for patch");
680
+ let u = n[i];
681
+ if (u == null || typeof u != "string")
682
+ throw new RangeError("target is not a string for patch");
683
+ let a = u.slice(0, s) + r.value + u.slice(s);
684
+ n[i] = a;
685
+ }
686
+ }
687
+ function Re(t, e, r) {
688
+ let { obj: o, prop: s } = b(e, -1);
689
+ const n = o[s];
690
+ if (R(t)) {
691
+ if (!he(n))
692
+ throw new RangeError("target is not a counter for patch");
693
+ n.increment(r.value);
694
+ } else {
695
+ if (typeof n != "number")
696
+ throw new RangeError("target is not a number for patch");
697
+ o[s] = n + r.value;
698
+ }
699
+ }
700
+ function Se(t, e, r) {
701
+ let { obj: o, prop: s } = b(e, -1);
702
+ if (R(t))
703
+ for (const n of r.marks)
704
+ Ae(
705
+ t,
706
+ r.path,
707
+ // TODO: add mark expansion to patches. This will require emitting
708
+ // the expand values in patches.
709
+ { start: n.start, end: n.end, expand: "none" },
710
+ n.name,
711
+ n.value
712
+ );
713
+ }
714
+ function Oe(t, e, r) {
715
+ R(t) && Te(t, r.path, { start: r.start, end: r.end, expand: "none" }, r.name);
716
+ }
717
+ function q(t, e) {
718
+ for (const r of e)
719
+ ye(t, r);
720
+ }
721
+ function Ee(t, e) {
722
+ const r = [];
723
+ let o = t, s = [];
724
+ for (const [n, i] of e.entries())
725
+ if (r.push({ obj: o, prop: i, parentPath: s.slice() }), s.push(i), n !== e.length - 1) {
726
+ if (o == null || typeof o != "object")
727
+ throw new Error(`Invalid path: ${e}`);
728
+ o = o[i];
729
+ } else
730
+ break;
731
+ return r;
732
+ }
733
+ function b(t, e) {
734
+ let r = t.at(e);
735
+ if (r == null)
736
+ throw new Error("invalid path");
737
+ return r;
738
+ }
739
+ function Pe(t, e, r) {
740
+ if (typeof r == "string") {
741
+ if (/^-?[0-9]+@[0-9a-zA-Z]+$|^[se]$/.test(r))
742
+ return t.handle.getCursorPosition(e, r);
743
+ throw new RangeError("index must be a number or cursor");
744
+ } else
745
+ return r;
746
+ }
747
+ function _e(t, e) {
748
+ return x(t);
749
+ }
750
+ function ve(t, e, r) {
751
+ N(e, "before"), N(r, "after");
752
+ const o = j(t);
753
+ return o.mostRecentPatch && I(o.mostRecentPatch.before, e) && I(o.mostRecentPatch.after, r) ? o.mostRecentPatch.patches : o.handle.diff(e, r);
754
+ }
755
+ function N(t, e) {
756
+ if (!Array.isArray(t))
757
+ throw new Error(`${e} must be an array`);
758
+ }
759
+ function I(t, e) {
760
+ if (!U(t) || !U(e))
761
+ return t === e;
762
+ const r = Object.keys(t).sort(), o = Object.keys(e).sort();
763
+ if (r.length !== o.length)
764
+ return !1;
765
+ for (let s = 0; s < r.length; s++)
766
+ if (r[s] !== o[s] || !I(t[r[s]], e[o[s]]))
767
+ return !1;
768
+ return !0;
769
+ }
770
+ function Ie(t) {
771
+ const e = j(t);
772
+ return e.heads || e.handle.getHeads();
773
+ }
774
+ function R(t) {
775
+ return typeof t == "object" && t !== null ? _e(t) === "_root" && !!Reflect.get(t, m) : !1;
776
+ }
777
+ function U(t) {
778
+ return typeof t == "object" && t !== null;
779
+ }
780
+ function K(t, e, r, o, s) {
781
+ const n = $(t, e, "splice");
782
+ if (!k(t))
783
+ throw new RangeError("object cannot be modified outside of a change block");
784
+ const i = j(t, !1);
785
+ ge(t), r = Pe(i, n, r);
786
+ try {
787
+ return i.handle.splice(n, r, o, s);
788
+ } catch (u) {
789
+ throw new RangeError(`Cannot splice: ${u}`);
790
+ }
791
+ }
792
+ function Ae(t, e, r, o, s) {
793
+ const n = $(t, e, "mark");
794
+ if (!k(t))
795
+ throw new RangeError("object cannot be modified outside of a change block");
796
+ const i = j(t, !1);
797
+ try {
798
+ return i.handle.mark(n, r, o, s);
799
+ } catch (u) {
800
+ throw new RangeError(`Cannot mark: ${u}`);
801
+ }
802
+ }
803
+ function Te(t, e, r, o) {
804
+ const s = $(t, e, "unmark");
805
+ if (!k(t))
806
+ throw new RangeError("object cannot be modified outside of a change block");
807
+ const n = j(t, !1);
808
+ try {
809
+ return n.handle.unmark(s, r, o);
810
+ } catch (i) {
811
+ throw new RangeError(`Cannot unmark: ${i}`);
812
+ }
813
+ }
814
+ function $(t, e, r) {
815
+ e = e.slice();
816
+ const o = x(t);
817
+ if (!o)
818
+ throw new RangeError(`invalid object for ${r}`);
819
+ return e.unshift(o), e.join("/");
820
+ }
821
+ function Ce(t) {
822
+ return (e) => q(e, t.patches);
823
+ }
824
+ const d = /* @__PURE__ */ new WeakMap();
825
+ function xe(t) {
826
+ const e = {};
827
+ return q(e, ve(t.doc(), [], Ie(t.doc()))), e;
828
+ }
829
+ function ke(t) {
830
+ if (Y(() => {
831
+ const n = d.get(t);
832
+ n && (n.refs-- || n.cleanup());
833
+ }), d.has(t)) {
834
+ const n = d.get(t);
835
+ return n.refs++, n.store;
836
+ }
837
+ const [e, r] = ee(xe(t));
838
+ d.set(t, {
839
+ refs: 0,
840
+ store: e,
841
+ cleanup() {
842
+ t.off("change", o), t.off("delete", s), d.delete(t);
843
+ }
844
+ });
845
+ function o(n) {
846
+ r(te(Ce(n)));
847
+ }
848
+ function s() {
849
+ r(re({}));
850
+ }
851
+ return t.on("change", o), t.on("delete", s), e;
852
+ }
853
+ function $e(t) {
854
+ return G(() => {
855
+ const r = typeof t == "function" ? t() : t;
856
+ return r && ke(r);
857
+ });
858
+ }
859
+ const V = X(
860
+ null
861
+ ), H = ["ready", "deleted", "unavailable"], Le = ["deleted", "unavailable"];
862
+ function De(t, e) {
863
+ const r = J(V);
864
+ if (!e?.repo && !r)
865
+ throw new Error("use outside <RepoContext> requires options.repo");
866
+ const o = e?.repo || r;
867
+ function s() {
868
+ if (e?.["~skipInitialValue"]) return;
869
+ const u = typeof t == "function" ? t() : t;
870
+ if (u)
871
+ try {
872
+ const a = new URL(u).pathname, f = o.handles[a];
873
+ if (f?.isReady())
874
+ return f;
875
+ } catch (a) {
876
+ console.error("Error parsing URL:", a);
877
+ }
878
+ }
879
+ const [n, { mutate: i }] = Z(
880
+ t,
881
+ async (u) => {
882
+ const a = await o.find(u, {
883
+ allowableStates: H
884
+ }), f = (c) => Promise.reject(new Error(`document not available: [${c}]`));
885
+ return a.isReady() ? a : a.inState(Le) ? f(a.state) : a.whenReady(H).then(() => a.isReady() ? a : f(a.state));
886
+ },
887
+ {
888
+ initialValue: s()
889
+ }
890
+ );
891
+ return Q(() => {
892
+ (typeof t == "function" ? t() : t) || i();
893
+ }), n;
894
+ }
895
+ function Ue(t, e) {
896
+ const r = De(t, e);
897
+ return [$e(r), r];
898
+ }
899
+ function He() {
900
+ const t = J(V);
901
+ if (!t) throw new Error("Please wrap me in a <RepoContext value={repo}>");
902
+ return t;
903
+ }
904
+ export {
905
+ V as RepoContext,
906
+ Ce as autoproduce,
907
+ $e as createDocumentProjection,
908
+ ke as makeDocumentProjection,
909
+ De as useDocHandle,
910
+ Ue as useDocument,
911
+ He as useRepo
912
+ };