@valbuild/core 0.12.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.
Files changed (100) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/README.md +537 -0
  3. package/dist/createClass-012eebbf.esm.js +109 -0
  4. package/dist/createClass-a436dbfe.cjs.dev.js +116 -0
  5. package/dist/createClass-de7426aa.cjs.prod.js +116 -0
  6. package/dist/declarations/src/Json.d.ts +6 -0
  7. package/dist/declarations/src/expr/eval.d.ts +19 -0
  8. package/dist/declarations/src/expr/expr.d.ts +32 -0
  9. package/dist/declarations/src/expr/index.d.ts +3 -0
  10. package/dist/declarations/src/expr/parser.d.ts +8 -0
  11. package/dist/declarations/src/expr/tokenizer.d.ts +7 -0
  12. package/dist/declarations/src/fetchVal.d.ts +5 -0
  13. package/dist/declarations/src/fp/array.d.ts +8 -0
  14. package/dist/declarations/src/fp/index.d.ts +3 -0
  15. package/dist/declarations/src/fp/result.d.ts +67 -0
  16. package/dist/declarations/src/fp/util.d.ts +12 -0
  17. package/dist/declarations/src/index.d.ts +32 -0
  18. package/dist/declarations/src/initSchema.d.ts +57 -0
  19. package/dist/declarations/src/initVal.d.ts +39 -0
  20. package/dist/declarations/src/module.d.ts +38 -0
  21. package/dist/declarations/src/patch/deref.d.ts +13 -0
  22. package/dist/declarations/src/patch/index.d.ts +6 -0
  23. package/dist/declarations/src/patch/json.d.ts +11 -0
  24. package/dist/declarations/src/patch/operation.d.ts +59 -0
  25. package/dist/declarations/src/patch/ops.d.ts +40 -0
  26. package/dist/declarations/src/patch/parse.d.ts +19 -0
  27. package/dist/declarations/src/patch/patch.d.ts +6 -0
  28. package/dist/declarations/src/patch/util.d.ts +6 -0
  29. package/dist/declarations/src/schema/array.d.ts +18 -0
  30. package/dist/declarations/src/schema/boolean.d.ts +15 -0
  31. package/dist/declarations/src/schema/i18n.d.ts +21 -0
  32. package/dist/declarations/src/schema/image.d.ts +32 -0
  33. package/dist/declarations/src/schema/index.d.ts +23 -0
  34. package/dist/declarations/src/schema/literal.d.ts +17 -0
  35. package/dist/declarations/src/schema/number.d.ts +22 -0
  36. package/dist/declarations/src/schema/object.d.ts +25 -0
  37. package/dist/declarations/src/schema/oneOf.d.ts +22 -0
  38. package/dist/declarations/src/schema/richtext.d.ts +76 -0
  39. package/dist/declarations/src/schema/string.d.ts +22 -0
  40. package/dist/declarations/src/schema/union.d.ts +36 -0
  41. package/dist/declarations/src/selector/SelectorProxy.d.ts +7 -0
  42. package/dist/declarations/src/selector/array.d.ts +17 -0
  43. package/dist/declarations/src/selector/boolean.d.ts +2 -0
  44. package/dist/declarations/src/selector/file.d.ts +9 -0
  45. package/dist/declarations/src/selector/i18n.d.ts +11 -0
  46. package/dist/declarations/src/selector/index.d.ts +81 -0
  47. package/dist/declarations/src/selector/number.d.ts +2 -0
  48. package/dist/declarations/src/selector/object.d.ts +10 -0
  49. package/dist/declarations/src/selector/primitive.d.ts +9 -0
  50. package/dist/declarations/src/selector/remote.d.ts +7 -0
  51. package/dist/declarations/src/selector/string.d.ts +2 -0
  52. package/dist/declarations/src/source/file.d.ts +23 -0
  53. package/dist/declarations/src/source/i18n.d.ts +29 -0
  54. package/dist/declarations/src/source/index.d.ts +33 -0
  55. package/dist/declarations/src/source/remote.d.ts +29 -0
  56. package/dist/declarations/src/val/array.d.ts +9 -0
  57. package/dist/declarations/src/val/index.d.ts +53 -0
  58. package/dist/declarations/src/val/object.d.ts +9 -0
  59. package/dist/declarations/src/val/primitive.d.ts +7 -0
  60. package/dist/index-2f1631cf.cjs.prod.js +1031 -0
  61. package/dist/index-55916dcd.esm.js +1000 -0
  62. package/dist/index-b49a2c60.cjs.dev.js +1031 -0
  63. package/dist/ops-6fae92a1.esm.js +12 -0
  64. package/dist/ops-87cdbafc.cjs.dev.js +14 -0
  65. package/dist/ops-ae4d1bc2.cjs.prod.js +14 -0
  66. package/dist/result-26f67b40.cjs.prod.js +299 -0
  67. package/dist/result-48320acd.cjs.dev.js +299 -0
  68. package/dist/result-b96df128.esm.js +279 -0
  69. package/dist/util-030d8a1f.cjs.prod.js +29 -0
  70. package/dist/util-18613e99.esm.js +26 -0
  71. package/dist/util-b213092b.cjs.dev.js +29 -0
  72. package/dist/valbuild-core.cjs.d.ts +2 -0
  73. package/dist/valbuild-core.cjs.d.ts.map +1 -0
  74. package/dist/valbuild-core.cjs.dev.js +1276 -0
  75. package/dist/valbuild-core.cjs.js +7 -0
  76. package/dist/valbuild-core.cjs.prod.js +1276 -0
  77. package/dist/valbuild-core.esm.js +1265 -0
  78. package/expr/dist/valbuild-core-expr.cjs.d.ts +2 -0
  79. package/expr/dist/valbuild-core-expr.cjs.d.ts.map +1 -0
  80. package/expr/dist/valbuild-core-expr.cjs.dev.js +18 -0
  81. package/expr/dist/valbuild-core-expr.cjs.js +7 -0
  82. package/expr/dist/valbuild-core-expr.cjs.prod.js +18 -0
  83. package/expr/dist/valbuild-core-expr.esm.js +3 -0
  84. package/expr/package.json +4 -0
  85. package/fp/dist/valbuild-core-fp.cjs.d.ts +2 -0
  86. package/fp/dist/valbuild-core-fp.cjs.d.ts.map +1 -0
  87. package/fp/dist/valbuild-core-fp.cjs.dev.js +13 -0
  88. package/fp/dist/valbuild-core-fp.cjs.js +7 -0
  89. package/fp/dist/valbuild-core-fp.cjs.prod.js +13 -0
  90. package/fp/dist/valbuild-core-fp.esm.js +2 -0
  91. package/fp/package.json +4 -0
  92. package/package.json +43 -0
  93. package/patch/dist/valbuild-core-patch.cjs.d.ts +2 -0
  94. package/patch/dist/valbuild-core-patch.cjs.d.ts.map +1 -0
  95. package/patch/dist/valbuild-core-patch.cjs.dev.js +443 -0
  96. package/patch/dist/valbuild-core-patch.cjs.js +7 -0
  97. package/patch/dist/valbuild-core-patch.cjs.prod.js +443 -0
  98. package/patch/dist/valbuild-core-patch.esm.js +431 -0
  99. package/patch/package.json +4 -0
  100. package/tsconfig.json +7 -0
@@ -0,0 +1,1265 @@
1
+ import { _ as _inherits, a as _createSuper, S as Schema, g as getValPath, n as newSelectorProxy, b as SourceOrExpr, E as Expr, I as ImageSchema, c as _objectSpread2, d as _defineProperty, V as VAL_EXTENSION, i as image, P as Path, e as createValPathOfItem, f as isSelector, h as isSerializedVal, j as file, k as isFile, F as FILE_REF_PROP, l as convertImageSource, m as getSchema } from './index-55916dcd.esm.js';
2
+ export { F as FILE_REF_PROP, G as GenericSelector, S as Schema, V as VAL_EXTENSION, o as expr } from './index-55916dcd.esm.js';
3
+ import { _ as _classCallCheck, a as _createClass, b as _typeof, c as _slicedToArray, d as _toConsumableArray } from './createClass-012eebbf.esm.js';
4
+ import { _ as _createForOfIteratorHelper, i as isErr, a as isOk, e as err, o as ok } from './result-b96df128.esm.js';
5
+ import { P as PatchError } from './ops-6fae92a1.esm.js';
6
+
7
+ var ObjectSchema = /*#__PURE__*/function (_Schema) {
8
+ _inherits(ObjectSchema, _Schema);
9
+ var _super = _createSuper(ObjectSchema);
10
+ function ObjectSchema(items) {
11
+ var _this;
12
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
13
+ _classCallCheck(this, ObjectSchema);
14
+ _this = _super.call(this);
15
+ _this.items = items;
16
+ _this.opt = opt;
17
+ return _this;
18
+ }
19
+ _createClass(ObjectSchema, [{
20
+ key: "validate",
21
+ value: function validate(src) {
22
+ throw new Error("Method not implemented.");
23
+ }
24
+ }, {
25
+ key: "match",
26
+ value: function match(src) {
27
+ if (this.opt && (src === null || src === undefined)) {
28
+ return true;
29
+ }
30
+ if (!src) {
31
+ return false;
32
+ }
33
+
34
+ // TODO: checks all props
35
+
36
+ return _typeof(src) === "object" && !Array.isArray(src);
37
+ }
38
+ }, {
39
+ key: "optional",
40
+ value: function optional() {
41
+ return new ObjectSchema(this.items, true);
42
+ }
43
+ }, {
44
+ key: "serialize",
45
+ value: function serialize() {
46
+ return {
47
+ type: "object",
48
+ items: Object.fromEntries(Object.entries(this.items).map(function (_ref) {
49
+ var _ref2 = _slicedToArray(_ref, 2),
50
+ key = _ref2[0],
51
+ schema = _ref2[1];
52
+ return [key, schema.serialize()];
53
+ })),
54
+ opt: this.opt
55
+ };
56
+ }
57
+ }]);
58
+ return ObjectSchema;
59
+ }(Schema);
60
+ var object = function object(schema) {
61
+ return new ObjectSchema(schema);
62
+ };
63
+
64
+ var ArraySchema = /*#__PURE__*/function (_Schema) {
65
+ _inherits(ArraySchema, _Schema);
66
+ var _super = _createSuper(ArraySchema);
67
+ function ArraySchema(item) {
68
+ var _this;
69
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
70
+ _classCallCheck(this, ArraySchema);
71
+ _this = _super.call(this);
72
+ _this.item = item;
73
+ _this.opt = opt;
74
+ return _this;
75
+ }
76
+ _createClass(ArraySchema, [{
77
+ key: "validate",
78
+ value: function validate(src) {
79
+ throw new Error("Method not implemented.");
80
+ }
81
+ }, {
82
+ key: "match",
83
+ value: function match(src) {
84
+ if (this.opt && (src === null || src === undefined)) {
85
+ return true;
86
+ }
87
+ if (!src) {
88
+ return false;
89
+ }
90
+
91
+ // TODO: checks all items
92
+ return _typeof(src) === "object" && Array.isArray(src);
93
+ }
94
+ }, {
95
+ key: "optional",
96
+ value: function optional() {
97
+ return new ArraySchema(this.item, true);
98
+ }
99
+ }, {
100
+ key: "serialize",
101
+ value: function serialize() {
102
+ return {
103
+ type: "array",
104
+ item: this.item.serialize(),
105
+ opt: this.opt
106
+ };
107
+ }
108
+ }]);
109
+ return ArraySchema;
110
+ }(Schema);
111
+ var array = function array(schema) {
112
+ return new ArraySchema(schema);
113
+ };
114
+
115
+ var I18nSchema = /*#__PURE__*/function (_Schema) {
116
+ _inherits(I18nSchema, _Schema);
117
+ var _super = _createSuper(I18nSchema);
118
+ function I18nSchema(locales, item) {
119
+ var _this;
120
+ var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
121
+ _classCallCheck(this, I18nSchema);
122
+ _this = _super.call(this);
123
+ _this.locales = locales;
124
+ _this.item = item;
125
+ _this.opt = opt;
126
+ return _this;
127
+ }
128
+ _createClass(I18nSchema, [{
129
+ key: "validate",
130
+ value: function validate(src) {
131
+ throw new Error("Method not implemented.");
132
+ }
133
+ }, {
134
+ key: "match",
135
+ value: function match(src) {
136
+ throw new Error("Method not implemented.");
137
+ }
138
+ }, {
139
+ key: "optional",
140
+ value: function optional() {
141
+ return new I18nSchema(this.locales, this.item, true);
142
+ }
143
+ }, {
144
+ key: "serialize",
145
+ value: function serialize() {
146
+ return {
147
+ type: "i18n",
148
+ item: this.item.serialize(),
149
+ locales: this.locales,
150
+ opt: this.opt
151
+ };
152
+ }
153
+ }]);
154
+ return I18nSchema;
155
+ }(Schema);
156
+ var i18n$1 = function i18n(locales) {
157
+ return function (schema) {
158
+ return new I18nSchema(locales, schema);
159
+ };
160
+ };
161
+
162
+ var UnionSchema = /*#__PURE__*/function (_Schema) {
163
+ _inherits(UnionSchema, _Schema);
164
+ var _super = _createSuper(UnionSchema);
165
+ function UnionSchema(key, items) {
166
+ var _this;
167
+ var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
168
+ _classCallCheck(this, UnionSchema);
169
+ _this = _super.call(this);
170
+ _this.key = key;
171
+ _this.items = items;
172
+ _this.opt = opt;
173
+ return _this;
174
+ }
175
+ _createClass(UnionSchema, [{
176
+ key: "validate",
177
+ value: function validate(src) {
178
+ throw new Error("Method not implemented.");
179
+ }
180
+ }, {
181
+ key: "match",
182
+ value: function match(src) {
183
+ throw new Error("Method not implemented.");
184
+ }
185
+ }, {
186
+ key: "optional",
187
+ value: function optional() {
188
+ throw new Error("Method not implemented.");
189
+ }
190
+ }, {
191
+ key: "serialize",
192
+ value: function serialize() {
193
+ return {
194
+ type: "union",
195
+ key: this.key,
196
+ items: this.items.map(function (o) {
197
+ return o.serialize();
198
+ }),
199
+ opt: this.opt
200
+ };
201
+ }
202
+ }]);
203
+ return UnionSchema;
204
+ }(Schema);
205
+ var union = function union(key) {
206
+ for (var _len = arguments.length, objects = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
207
+ objects[_key - 1] = arguments[_key];
208
+ }
209
+ return new UnionSchema(key, objects);
210
+ };
211
+
212
+ var OneOfSchema = /*#__PURE__*/function (_Schema) {
213
+ _inherits(OneOfSchema, _Schema);
214
+ var _super = _createSuper(OneOfSchema);
215
+ function OneOfSchema(selector) {
216
+ var _this;
217
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
218
+ _classCallCheck(this, OneOfSchema);
219
+ _this = _super.call(this);
220
+ _this.selector = selector;
221
+ _this.opt = opt;
222
+ return _this;
223
+ }
224
+ _createClass(OneOfSchema, [{
225
+ key: "validate",
226
+ value: function validate(src) {
227
+ throw new Error("Method not implemented.");
228
+ }
229
+ }, {
230
+ key: "match",
231
+ value: function match(src) {
232
+ throw new Error("Method not implemented.");
233
+ }
234
+ }, {
235
+ key: "optional",
236
+ value: function optional() {
237
+ return new OneOfSchema(this.selector, true);
238
+ }
239
+ }, {
240
+ key: "serialize",
241
+ value: function serialize() {
242
+ var path = getValPath(this.selector);
243
+ if (!path) {
244
+ throw new Error("Cannot serialize oneOf schema with empty selector. Make sure a Val module is used.");
245
+ }
246
+ return {
247
+ type: "oneOf",
248
+ selector: path,
249
+ opt: this.opt
250
+ };
251
+ }
252
+ }]);
253
+ return OneOfSchema;
254
+ }(Schema);
255
+ var oneOf = function oneOf(valModule) {
256
+ return new OneOfSchema(valModule);
257
+ };
258
+
259
+ var RichTextSchema = /*#__PURE__*/function (_Schema) {
260
+ _inherits(RichTextSchema, _Schema);
261
+ var _super = _createSuper(RichTextSchema);
262
+ function RichTextSchema() {
263
+ var _this;
264
+ var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
265
+ _classCallCheck(this, RichTextSchema);
266
+ _this = _super.call(this);
267
+ _this.opt = opt;
268
+ return _this;
269
+ }
270
+ _createClass(RichTextSchema, [{
271
+ key: "validate",
272
+ value: function validate(src) {
273
+ throw new Error("Method not implemented.");
274
+ }
275
+ }, {
276
+ key: "match",
277
+ value: function match(src) {
278
+ // TODO:
279
+ return true;
280
+ }
281
+ }, {
282
+ key: "optional",
283
+ value: function optional() {
284
+ return new RichTextSchema(true);
285
+ }
286
+ }, {
287
+ key: "serialize",
288
+ value: function serialize() {
289
+ return {
290
+ type: "richtext",
291
+ opt: this.opt
292
+ };
293
+ }
294
+ }]);
295
+ return RichTextSchema;
296
+ }(Schema);
297
+ var richtext = function richtext() {
298
+ return new RichTextSchema();
299
+ };
300
+ {
301
+ content("/test/richtext1", richtext(), {
302
+ children: [{
303
+ children: [{
304
+ detail: 0,
305
+ format: 0,
306
+ mode: "normal",
307
+ style: "",
308
+ text: "Heading 1",
309
+ type: "text",
310
+ version: 1
311
+ }],
312
+ direction: "ltr",
313
+ format: "",
314
+ indent: 0,
315
+ type: "heading",
316
+ version: 1,
317
+ tag: "h1"
318
+ }, {
319
+ children: [{
320
+ detail: 0,
321
+ format: 0,
322
+ mode: "normal",
323
+ style: "",
324
+ text: "Heading 2",
325
+ type: "text",
326
+ version: 1
327
+ }],
328
+ direction: "ltr",
329
+ format: "",
330
+ indent: 0,
331
+ type: "heading",
332
+ version: 1,
333
+ tag: "h2"
334
+ }, {
335
+ children: [],
336
+ direction: "ltr",
337
+ format: "",
338
+ indent: 0,
339
+ type: "paragraph",
340
+ version: 1
341
+ }, {
342
+ children: [{
343
+ detail: 0,
344
+ format: 0,
345
+ mode: "normal",
346
+ style: "",
347
+ text: "Normal",
348
+ type: "text",
349
+ version: 1
350
+ }],
351
+ direction: "ltr",
352
+ format: "",
353
+ indent: 0,
354
+ type: "paragraph",
355
+ version: 1
356
+ }, {
357
+ children: [{
358
+ detail: 0,
359
+ format: 0,
360
+ mode: "normal",
361
+ style: "font-size: 20px;",
362
+ text: "Normal Font size 20",
363
+ type: "text",
364
+ version: 1
365
+ }],
366
+ direction: "ltr",
367
+ format: "",
368
+ indent: 0,
369
+ type: "paragraph",
370
+ version: 1
371
+ }, {
372
+ children: [{
373
+ detail: 0,
374
+ format: 0,
375
+ mode: "normal",
376
+ style: "font-family: serif;",
377
+ text: "Normal font type serif",
378
+ type: "text",
379
+ version: 1
380
+ }],
381
+ direction: "ltr",
382
+ format: "",
383
+ indent: 0,
384
+ type: "paragraph",
385
+ version: 1
386
+ }, {
387
+ children: [{
388
+ detail: 0,
389
+ format: 1,
390
+ mode: "normal",
391
+ style: "font-family: serif;",
392
+ text: "Serif and bold",
393
+ type: "text",
394
+ version: 1
395
+ }],
396
+ direction: "ltr",
397
+ format: "",
398
+ indent: 0,
399
+ type: "paragraph",
400
+ version: 1
401
+ }, {
402
+ children: [{
403
+ detail: 0,
404
+ format: 2,
405
+ mode: "normal",
406
+ style: "",
407
+ text: "Arial and italic",
408
+ type: "text",
409
+ version: 1
410
+ }],
411
+ direction: "ltr",
412
+ format: "",
413
+ indent: 0,
414
+ type: "paragraph",
415
+ version: 1
416
+ }, {
417
+ children: [],
418
+ direction: null,
419
+ format: "",
420
+ indent: 0,
421
+ type: "paragraph",
422
+ version: 1
423
+ }, {
424
+ children: [{
425
+ children: [{
426
+ detail: 0,
427
+ format: 0,
428
+ mode: "normal",
429
+ style: "",
430
+ text: "Num list 1",
431
+ type: "text",
432
+ version: 1
433
+ }],
434
+ direction: "ltr",
435
+ format: "",
436
+ indent: 0,
437
+ type: "listitem",
438
+ version: 1,
439
+ value: 1
440
+ }, {
441
+ children: [{
442
+ detail: 0,
443
+ format: 0,
444
+ mode: "normal",
445
+ style: "",
446
+ text: "Num list 2",
447
+ type: "text",
448
+ version: 1
449
+ }
450
+ // TODO: image
451
+ // {
452
+ // altText: "",
453
+ // height: 0,
454
+ // maxWidth: 0,
455
+ // src: "https://picsum.photos/id/237/200/300",
456
+ // type: "image",
457
+ // version: 1,
458
+ // width: 0,
459
+ // },
460
+ ],
461
+
462
+ direction: "ltr",
463
+ format: "",
464
+ indent: 0,
465
+ type: "listitem",
466
+ version: 1,
467
+ value: 2
468
+ }],
469
+ direction: "ltr",
470
+ format: "",
471
+ indent: 0,
472
+ type: "list",
473
+ version: 1,
474
+ listType: "number",
475
+ start: 1,
476
+ tag: "ol"
477
+ }, {
478
+ children: [],
479
+ direction: null,
480
+ format: "",
481
+ indent: 0,
482
+ type: "paragraph",
483
+ version: 1
484
+ }, {
485
+ children: [{
486
+ children: [{
487
+ detail: 0,
488
+ format: 0,
489
+ mode: "normal",
490
+ style: "",
491
+ text: "Bullet list 1",
492
+ type: "text",
493
+ version: 1
494
+ }],
495
+ direction: "ltr",
496
+ format: "",
497
+ indent: 0,
498
+ type: "listitem",
499
+ version: 1,
500
+ value: 1
501
+ }, {
502
+ children: [{
503
+ detail: 0,
504
+ format: 0,
505
+ mode: "normal",
506
+ style: "",
507
+ text: "Bullet list 2",
508
+ type: "text",
509
+ version: 1
510
+ }],
511
+ direction: "ltr",
512
+ format: "",
513
+ indent: 0,
514
+ type: "listitem",
515
+ version: 1,
516
+ value: 2
517
+ }],
518
+ direction: "ltr",
519
+ format: "",
520
+ indent: 0,
521
+ type: "list",
522
+ version: 1,
523
+ listType: "bullet",
524
+ start: 1,
525
+ tag: "ul"
526
+ }, {
527
+ children: [],
528
+ direction: null,
529
+ format: "",
530
+ indent: 0,
531
+ type: "paragraph",
532
+ version: 1
533
+ }, {
534
+ children: [],
535
+ direction: "ltr",
536
+ format: "",
537
+ indent: 0,
538
+ type: "paragraph",
539
+ version: 1
540
+ }, {
541
+ children: [],
542
+ direction: null,
543
+ format: "",
544
+ indent: 0,
545
+ type: "paragraph",
546
+ version: 1
547
+ }],
548
+ direction: "ltr",
549
+ format: "",
550
+ indent: 0,
551
+ type: "root",
552
+ version: 1
553
+ });
554
+ }
555
+
556
+ function content(
557
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
558
+ id,
559
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
560
+ schema,
561
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
562
+ source) {
563
+ return newSelectorProxy(source, id, schema);
564
+ }
565
+ function getRawSource(valModule) {
566
+ var sourceOrExpr = valModule[SourceOrExpr];
567
+ if (sourceOrExpr instanceof Expr) {
568
+ throw Error("Cannot get raw source of an Expr");
569
+ }
570
+ var source = sourceOrExpr;
571
+ return source;
572
+ }
573
+ function splitModuleIdAndModulePath(path) {
574
+ return [path.slice(0, path.indexOf(".")), path.slice(path.indexOf(".") + 1)];
575
+ }
576
+ function isObjectSchema(schema) {
577
+ return schema instanceof ObjectSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "object";
578
+ }
579
+ function isArraySchema(schema) {
580
+ return schema instanceof ArraySchema || _typeof(schema) === "object" && "type" in schema && schema.type === "array";
581
+ }
582
+ function isI18nSchema(schema) {
583
+ return schema instanceof I18nSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "i18n";
584
+ }
585
+ function isUnionSchema(schema) {
586
+ return schema instanceof UnionSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "union";
587
+ }
588
+
589
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
590
+ function isRichTextSchema(schema) {
591
+ return schema instanceof RichTextSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "richtext";
592
+ }
593
+ function isImageSchema(schema) {
594
+ return schema instanceof ImageSchema || _typeof(schema) === "object" && "type" in schema && schema.type === "image";
595
+ }
596
+ function resolvePath(path, valModule, schema) {
597
+ var parts = parsePath(path);
598
+ var origParts = _toConsumableArray(parts);
599
+ var resolvedSchema = schema;
600
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
601
+ var resolvedSource = valModule;
602
+ var _loop = function _loop() {
603
+ var part = parts.shift();
604
+ if (part === undefined) {
605
+ throw Error("Unexpected undefined part");
606
+ }
607
+ if (isArraySchema(resolvedSchema)) {
608
+ if (Number.isNaN(Number(part))) {
609
+ throw Error("Invalid path: array schema ".concat(resolvedSchema, " must have ").concat(part, " a number as path"));
610
+ }
611
+ if (_typeof(resolvedSource) !== "object" && !Array.isArray(resolvedSource)) {
612
+ throw Error("Schema type error: expected source to be type of array, but got ".concat(_typeof(resolvedSource)));
613
+ }
614
+ if (!resolvedSource[part]) {
615
+ var _resolvedSource;
616
+ throw Error("Invalid path: array source (length: ".concat((_resolvedSource = resolvedSource) === null || _resolvedSource === void 0 ? void 0 : _resolvedSource.length, ") did not have index ").concat(part, " from path: ").concat(path));
617
+ }
618
+ resolvedSource = resolvedSource[part];
619
+ resolvedSchema = resolvedSchema.item;
620
+ } else if (isObjectSchema(resolvedSchema)) {
621
+ if (_typeof(resolvedSource) !== "object") {
622
+ throw Error("Schema type error: expected source to be type of object, but got ".concat(_typeof(resolvedSource)));
623
+ }
624
+ if (!resolvedSource[part]) {
625
+ throw Error("Invalid path: object source did not have key ".concat(part, " from path: ").concat(path));
626
+ }
627
+ resolvedSource = resolvedSource[part];
628
+ resolvedSchema = resolvedSchema.items[part];
629
+ } else if (isI18nSchema(resolvedSchema)) {
630
+ if (!resolvedSchema.locales.includes(part)) {
631
+ throw Error("Invalid path: i18n schema ".concat(resolvedSchema, " supports locales ").concat(resolvedSchema.locales.join(", "), ", but found: ").concat(part));
632
+ }
633
+ if (!Object.keys(resolvedSource).includes(part)) {
634
+ throw Error("Schema type error: expected source to be type of i18n with locale ".concat(part, ", but got ").concat(JSON.stringify(Object.keys(resolvedSource))));
635
+ }
636
+ resolvedSource = resolvedSource[part];
637
+ resolvedSchema = resolvedSchema.item;
638
+ } else if (isImageSchema(resolvedSchema)) {
639
+ return {
640
+ v: {
641
+ path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
642
+ return JSON.stringify(p);
643
+ }).join("."),
644
+ // TODO: create a function generate path from parts (not sure if this always works)
645
+ schema: resolvedSchema,
646
+ source: resolvedSource
647
+ }
648
+ };
649
+ } else if (isUnionSchema(resolvedSchema)) {
650
+ var _key = resolvedSchema.key;
651
+ var keyValue = resolvedSource[_key];
652
+ if (!keyValue) {
653
+ throw Error("Invalid path: union source ".concat(resolvedSchema, " did not have required key ").concat(_key, " in path: ").concat(path));
654
+ }
655
+ var schemaOfUnionKey = resolvedSchema.items.find(
656
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
657
+ function (child) {
658
+ var _child$items, _child$items$_key;
659
+ return (child === null || child === void 0 ? void 0 : (_child$items = child.items) === null || _child$items === void 0 ? void 0 : (_child$items$_key = _child$items[_key]) === null || _child$items$_key === void 0 ? void 0 : _child$items$_key.value) === keyValue;
660
+ });
661
+ if (!schemaOfUnionKey) {
662
+ throw Error("Invalid path: union schema ".concat(resolvedSchema, " did not have a child object with ").concat(_key, " of value ").concat(keyValue, " in path: ").concat(path));
663
+ }
664
+ resolvedSchema = schemaOfUnionKey.items[part];
665
+ resolvedSource = resolvedSource[part];
666
+ } else if (isRichTextSchema(resolvedSchema)) {
667
+ return {
668
+ v: {
669
+ path: origParts.slice(0, origParts.length - parts.length - 1).map(function (p) {
670
+ return JSON.stringify(p);
671
+ }).join("."),
672
+ // TODO: create a function generate path from parts (not sure if this always works)
673
+ schema: resolvedSchema,
674
+ source: resolvedSource
675
+ }
676
+ };
677
+ } else {
678
+ throw Error("Invalid path: ".concat(part, " resolved to an unexpected schema ").concat(JSON.stringify(resolvedSchema)));
679
+ }
680
+ };
681
+ while (parts.length > 0) {
682
+ var _ret = _loop();
683
+ if (_typeof(_ret) === "object") return _ret.v;
684
+ }
685
+ if (parts.length > 0) {
686
+ throw Error("Invalid path: ".concat(parts.join("."), " is not a valid path"));
687
+ }
688
+ return {
689
+ path: path,
690
+ schema: resolvedSchema,
691
+ source: resolvedSource
692
+ };
693
+ }
694
+ function parsePath(input) {
695
+ var result = [];
696
+ var i = 0;
697
+ while (i < input.length) {
698
+ var part = "";
699
+ if (input[i] === '"') {
700
+ // Parse a quoted string
701
+ i++;
702
+ while (i < input.length && input[i] !== '"') {
703
+ if (input[i] === "\\" && input[i + 1] === '"') {
704
+ // Handle escaped double quotes
705
+ part += '"';
706
+ i++;
707
+ } else {
708
+ part += input[i];
709
+ }
710
+ i++;
711
+ }
712
+ if (input[i] !== '"') {
713
+ var _input$i;
714
+ throw new Error("Invalid input (".concat(JSON.stringify(input), "): Missing closing double quote: ").concat((_input$i = input[i]) !== null && _input$i !== void 0 ? _input$i : "at end of string", " (char: ").concat(i, "; length: ").concat(input.length, ")"));
715
+ }
716
+ } else {
717
+ // Parse a regular string
718
+ while (i < input.length && input[i] !== ".") {
719
+ part += input[i];
720
+ i++;
721
+ }
722
+ }
723
+ if (part !== "") {
724
+ result.push(part);
725
+ }
726
+ i++;
727
+ }
728
+ return result;
729
+ }
730
+
731
+ /**
732
+ * I18n sources cannot have nested remote sources.
733
+ */
734
+
735
+ /**
736
+ * An i18n source is a map of locales to sources.
737
+ *
738
+ * Its selector will default to the underlying source. It is possible to call `.all` on i18n sources, which returns an object with all the locales
739
+ *
740
+ */
741
+
742
+ function i18n(
743
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
744
+ locales) {
745
+ return function (source) {
746
+ return _objectSpread2(_objectSpread2({}, source), {}, _defineProperty({}, VAL_EXTENSION, "i18n"));
747
+ };
748
+ }
749
+
750
+ var NumberSchema = /*#__PURE__*/function (_Schema) {
751
+ _inherits(NumberSchema, _Schema);
752
+ var _super = _createSuper(NumberSchema);
753
+ function NumberSchema(options) {
754
+ var _this;
755
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
756
+ _classCallCheck(this, NumberSchema);
757
+ _this = _super.call(this);
758
+ _this.options = options;
759
+ _this.opt = opt;
760
+ return _this;
761
+ }
762
+ _createClass(NumberSchema, [{
763
+ key: "validate",
764
+ value: function validate(src) {
765
+ throw new Error("Method not implemented.");
766
+ }
767
+ }, {
768
+ key: "match",
769
+ value: function match(src) {
770
+ if (this.opt && (src === null || src === undefined)) {
771
+ return true;
772
+ }
773
+ return typeof src === "number";
774
+ }
775
+ }, {
776
+ key: "optional",
777
+ value: function optional() {
778
+ return new NumberSchema(this.options, true);
779
+ }
780
+ }, {
781
+ key: "serialize",
782
+ value: function serialize() {
783
+ return {
784
+ type: "number",
785
+ options: this.options,
786
+ opt: this.opt
787
+ };
788
+ }
789
+ }]);
790
+ return NumberSchema;
791
+ }(Schema);
792
+ var number = function number(options) {
793
+ return new NumberSchema(options);
794
+ };
795
+
796
+ var StringSchema = /*#__PURE__*/function (_Schema) {
797
+ _inherits(StringSchema, _Schema);
798
+ var _super = _createSuper(StringSchema);
799
+ function StringSchema(options) {
800
+ var _this;
801
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
802
+ _classCallCheck(this, StringSchema);
803
+ _this = _super.call(this);
804
+ _this.options = options;
805
+ _this.opt = opt;
806
+ return _this;
807
+ }
808
+ _createClass(StringSchema, [{
809
+ key: "validate",
810
+ value: function validate(src) {
811
+ throw new Error("Method not implemented.");
812
+ }
813
+ }, {
814
+ key: "match",
815
+ value: function match(src) {
816
+ if (this.opt && (src === null || src === undefined)) {
817
+ return true;
818
+ }
819
+ return typeof src === "string";
820
+ }
821
+ }, {
822
+ key: "optional",
823
+ value: function optional() {
824
+ return new StringSchema(this.options, true);
825
+ }
826
+ }, {
827
+ key: "serialize",
828
+ value: function serialize() {
829
+ return {
830
+ type: "string",
831
+ options: this.options,
832
+ opt: this.opt
833
+ };
834
+ }
835
+ }]);
836
+ return StringSchema;
837
+ }(Schema);
838
+ var string = function string(options) {
839
+ return new StringSchema(options);
840
+ };
841
+
842
+ var BooleanSchema = /*#__PURE__*/function (_Schema) {
843
+ _inherits(BooleanSchema, _Schema);
844
+ var _super = _createSuper(BooleanSchema);
845
+ function BooleanSchema() {
846
+ var _this;
847
+ var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
848
+ _classCallCheck(this, BooleanSchema);
849
+ _this = _super.call(this);
850
+ _this.opt = opt;
851
+ return _this;
852
+ }
853
+ _createClass(BooleanSchema, [{
854
+ key: "validate",
855
+ value: function validate(src) {
856
+ throw new Error("Method not implemented.");
857
+ }
858
+ }, {
859
+ key: "match",
860
+ value: function match(src) {
861
+ if (this.opt && (src === null || src === undefined)) {
862
+ return true;
863
+ }
864
+ return typeof src === "boolean";
865
+ }
866
+ }, {
867
+ key: "optional",
868
+ value: function optional() {
869
+ return new BooleanSchema(true);
870
+ }
871
+ }, {
872
+ key: "serialize",
873
+ value: function serialize() {
874
+ return {
875
+ type: "boolean",
876
+ opt: this.opt
877
+ };
878
+ }
879
+ }]);
880
+ return BooleanSchema;
881
+ }(Schema);
882
+ var _boolean = function _boolean() {
883
+ return new BooleanSchema();
884
+ };
885
+
886
+ var LiteralSchema = /*#__PURE__*/function (_Schema) {
887
+ _inherits(LiteralSchema, _Schema);
888
+ var _super = _createSuper(LiteralSchema);
889
+ function LiteralSchema(value) {
890
+ var _this;
891
+ var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
892
+ _classCallCheck(this, LiteralSchema);
893
+ _this = _super.call(this);
894
+ _this.value = value;
895
+ _this.opt = opt;
896
+ return _this;
897
+ }
898
+ _createClass(LiteralSchema, [{
899
+ key: "validate",
900
+ value: function validate(src) {
901
+ throw new Error("Method not implemented.");
902
+ }
903
+ }, {
904
+ key: "match",
905
+ value: function match(src) {
906
+ if (this.opt && (src === null || src === undefined)) {
907
+ return true;
908
+ }
909
+ return typeof src === "string";
910
+ }
911
+ }, {
912
+ key: "optional",
913
+ value: function optional() {
914
+ return new LiteralSchema(this.value, true);
915
+ }
916
+ }, {
917
+ key: "serialize",
918
+ value: function serialize() {
919
+ return {
920
+ type: "literal",
921
+ value: this.value,
922
+ opt: this.opt
923
+ };
924
+ }
925
+ }]);
926
+ return LiteralSchema;
927
+ }(Schema);
928
+ var literal = function literal(value) {
929
+ return new LiteralSchema(value);
930
+ };
931
+
932
+ function initSchema(locales) {
933
+ return {
934
+ string: string,
935
+ "boolean": _boolean,
936
+ array: array,
937
+ object: object,
938
+ number: number,
939
+ union: union,
940
+ oneOf: oneOf,
941
+ richtext: richtext,
942
+ image: image,
943
+ literal: literal,
944
+ i18n: i18n$1(locales)
945
+ };
946
+ }
947
+
948
+ function fetchVal(selector, locale) {
949
+ return Promise.resolve(getVal(selector));
950
+ }
951
+ function getVal(selector,
952
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
953
+ locale) {
954
+ return newValProxy(serializedValOfSelectorSource(selector));
955
+ }
956
+
957
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
958
+ function isArrayOrArraySelector(child) {
959
+ if (isSelector(child)) {
960
+ return _typeof(child[SourceOrExpr]) === "object" && _typeof(child[SourceOrExpr]) !== null && Array.isArray(child[SourceOrExpr]);
961
+ }
962
+ return Array.isArray(child);
963
+ }
964
+
965
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
966
+ function isObjectOrObjectSelector(child) {
967
+ if (isSelector(child)) {
968
+ return _typeof(child[SourceOrExpr]) === "object" && _typeof(child[SourceOrExpr]) !== null && !Array.isArray(child[SourceOrExpr]);
969
+ }
970
+ return _typeof(child) === "object";
971
+ }
972
+ function serializedValOfSelectorSource(selector) {
973
+ var wrappedSelector = newSelectorProxy(selector); // NOTE: we do this if call-site uses a literal with selectors inside
974
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
975
+ function rec(child) {
976
+ var isArray = isArrayOrArraySelector(child);
977
+ var isObject = isObjectOrObjectSelector(child);
978
+ if (isArray) {
979
+ var array = SourceOrExpr in child ? child[SourceOrExpr] : child;
980
+ var valPath = Path in child ? child[Path] : undefined;
981
+ return {
982
+ val: array.map(function (item, i) {
983
+ return rec(isSelector(item) // NOTE: We do this since selectors currently do not create selectors of items unless specifically required.
984
+ ? item : newSelectorProxy(item, createValPathOfItem(valPath, i)));
985
+ }),
986
+ valPath: valPath
987
+ };
988
+ } else if (isObject) {
989
+ var obj = SourceOrExpr in child ? child[SourceOrExpr] : child;
990
+ var _valPath = Path in child ? child[Path] : undefined;
991
+ return {
992
+ val: obj !== null && Object.fromEntries(Object.entries(obj).map(function (_ref) {
993
+ var _ref2 = _slicedToArray(_ref, 2),
994
+ key = _ref2[0],
995
+ value = _ref2[1];
996
+ return [key, rec(isSelector(value) // NOTE: We do this since selectors currently do not create selectors of items unless specifically required.
997
+ ? value : newSelectorProxy(value, createValPathOfItem(_valPath, key)))];
998
+ })),
999
+ valPath: _valPath
1000
+ };
1001
+ } else if (isSelector(child)) {
1002
+ return {
1003
+ val: rec(child[SourceOrExpr]),
1004
+ valPath: child[Path]
1005
+ };
1006
+ } else {
1007
+ return child;
1008
+ }
1009
+ }
1010
+ return rec(wrappedSelector);
1011
+ }
1012
+ function strip(value) {
1013
+ var val = isSerializedVal(value) ? value.val : value;
1014
+ switch (_typeof(val)) {
1015
+ case "function":
1016
+ case "symbol":
1017
+ throw Error("Invalid val type: ".concat(_typeof(val)));
1018
+ case "object":
1019
+ if (val === null) {
1020
+ return null;
1021
+ } else if (Array.isArray(val)) {
1022
+ return val.map(strip);
1023
+ } else {
1024
+ return Object.fromEntries(Object.entries(val).map(function (_ref3) {
1025
+ var _ref4 = _slicedToArray(_ref3, 2),
1026
+ key = _ref4[0],
1027
+ value = _ref4[1];
1028
+ return [key, value && strip(value)];
1029
+ }));
1030
+ }
1031
+ // intentional fallthrough
1032
+ // eslint-disable-next-line no-fallthrough
1033
+ default:
1034
+ return val;
1035
+ }
1036
+ }
1037
+ function newValProxy(val) {
1038
+ var _ref5;
1039
+ var source = val.val;
1040
+ switch (_typeof(source)) {
1041
+ case "function":
1042
+ case "symbol":
1043
+ throw Error("Invalid val type: ".concat(_typeof(source)));
1044
+ case "object":
1045
+ if (source !== null) {
1046
+ // Handles both objects and arrays!
1047
+ return new Proxy(source, {
1048
+ has: function has(target, prop) {
1049
+ if (prop === "val") {
1050
+ return true;
1051
+ }
1052
+ if (prop === Path) {
1053
+ return true;
1054
+ }
1055
+ return hasOwn(target, prop);
1056
+ },
1057
+ get: function get(target, prop) {
1058
+ if (prop === Path) {
1059
+ return val.valPath;
1060
+ }
1061
+ if (prop === "val") {
1062
+ return strip(val);
1063
+ }
1064
+ if (Array.isArray(target) && prop === "length") {
1065
+ return target.length;
1066
+ }
1067
+ if (hasOwn(source, prop)) {
1068
+ var _Reflect$get$valPath, _Reflect$get;
1069
+ return newValProxy({
1070
+ val: Reflect.get(target, prop).val,
1071
+ valPath: (_Reflect$get$valPath = (_Reflect$get = Reflect.get(target, prop)) === null || _Reflect$get === void 0 ? void 0 : _Reflect$get.valPath) !== null && _Reflect$get$valPath !== void 0 ? _Reflect$get$valPath : createValPathOfItem(val.valPath, Array.isArray(target) ? Number(prop) : prop)
1072
+ });
1073
+ }
1074
+ return Reflect.get(target, prop);
1075
+ }
1076
+ });
1077
+ }
1078
+ // intentional fallthrough
1079
+ // eslint-disable-next-line no-fallthrough
1080
+ default:
1081
+ return _ref5 = {}, _defineProperty(_ref5, Path, val.valPath), _defineProperty(_ref5, "val", val.val), _ref5;
1082
+ }
1083
+ }
1084
+ function hasOwn(obj, prop) {
1085
+ return Object.prototype.hasOwnProperty.call(obj, prop);
1086
+ }
1087
+
1088
+ /**
1089
+ * Remote sources cannot include other remote sources.
1090
+ */
1091
+
1092
+ var REMOTE_REF_PROP = "_ref"; // TODO: same as FILE_REF_PROP so use same prop?
1093
+
1094
+ /**
1095
+ * A remote source is a hash that represents a remote object.
1096
+ *
1097
+ * It will be resolved into a ValRemote object.
1098
+ */
1099
+
1100
+ function remote(ref) {
1101
+ var _ref;
1102
+ return _ref = {}, _defineProperty(_ref, REMOTE_REF_PROP, ref), _defineProperty(_ref, VAL_EXTENSION, "remote"), _ref;
1103
+ }
1104
+ function isRemote(obj) {
1105
+ return _typeof(obj) === "object" && obj !== null && VAL_EXTENSION in obj && obj[VAL_EXTENSION] === "remote" && REMOTE_REF_PROP in obj && typeof obj[REMOTE_REF_PROP] === "string";
1106
+ }
1107
+
1108
+ var initVal = function initVal(options) {
1109
+ var locales = options === null || options === void 0 ? void 0 : options.locales;
1110
+ var s = initSchema(locales === null || locales === void 0 ? void 0 : locales.required);
1111
+ if (locales !== null && locales !== void 0 && locales.required) {
1112
+ console.error("Locales / i18n currently not implemented");
1113
+ return {
1114
+ val: {
1115
+ content: content,
1116
+ i18n: i18n,
1117
+ remote: remote,
1118
+ getPath: getValPath,
1119
+ key: getValPath,
1120
+ file: file
1121
+ },
1122
+ fetchVal: fetchVal,
1123
+ s: s
1124
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1125
+ };
1126
+ }
1127
+
1128
+ return {
1129
+ val: {
1130
+ content: content,
1131
+ remote: remote,
1132
+ getPath: getValPath,
1133
+ key: getValPath,
1134
+ file: file
1135
+ },
1136
+ fetchVal: fetchVal,
1137
+ s: _objectSpread2(_objectSpread2({}, s), {}, {
1138
+ i18n: undefined
1139
+ })
1140
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1141
+ };
1142
+ };
1143
+
1144
+ function derefPath(path) {
1145
+ var dereffedPath = [];
1146
+ var referencedPath = null;
1147
+ var _iterator = _createForOfIteratorHelper(path),
1148
+ _step;
1149
+ try {
1150
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1151
+ var segment = _step.value;
1152
+ if (segment.startsWith("$")) {
1153
+ var dereffedSegment = segment.slice(1);
1154
+ dereffedPath.push(dereffedSegment);
1155
+ referencedPath = [];
1156
+ var _iterator2 = _createForOfIteratorHelper(path.slice(dereffedPath.length)),
1157
+ _step2;
1158
+ try {
1159
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
1160
+ var _segment = _step2.value;
1161
+ if (_segment.startsWith("$")) {
1162
+ return err(new PatchError("Cannot reference within reference: ".concat(_segment, ". Path: ").concat(path.join("/"))));
1163
+ }
1164
+ referencedPath.push(_segment);
1165
+ }
1166
+ } catch (err) {
1167
+ _iterator2.e(err);
1168
+ } finally {
1169
+ _iterator2.f();
1170
+ }
1171
+ break;
1172
+ } else {
1173
+ dereffedPath.push(segment);
1174
+ }
1175
+ }
1176
+ } catch (err) {
1177
+ _iterator.e(err);
1178
+ } finally {
1179
+ _iterator.f();
1180
+ }
1181
+ return ok([dereffedPath, referencedPath]);
1182
+ }
1183
+ function derefPatch(patch, document, ops) {
1184
+ var remotePatches = {};
1185
+ var fileUpdates = {};
1186
+ var dereferencedPatch = [];
1187
+ var _iterator3 = _createForOfIteratorHelper(patch),
1188
+ _step3;
1189
+ try {
1190
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
1191
+ var op = _step3.value;
1192
+ if (op.op === "replace") {
1193
+ var maybeDerefRes = derefPath(op.path);
1194
+ if (isErr(maybeDerefRes)) {
1195
+ return maybeDerefRes;
1196
+ }
1197
+ var _maybeDerefRes$value = _slicedToArray(maybeDerefRes.value, 2),
1198
+ dereffedPath = _maybeDerefRes$value[0],
1199
+ referencedPath = _maybeDerefRes$value[1];
1200
+ if (referencedPath) {
1201
+ var maybeValue = ops.get(document, dereffedPath);
1202
+ if (isOk(maybeValue)) {
1203
+ var value = maybeValue.value;
1204
+ if (isFile(value)) {
1205
+ if (referencedPath.length > 0) {
1206
+ return err(new PatchError("Cannot sub-reference file reference at path: ".concat(dereffedPath.join("/"))));
1207
+ }
1208
+ if (typeof op.value !== "string") {
1209
+ return err(new PatchError("Expected base64 encoded string value for file reference, got ".concat(JSON.stringify(op.value))));
1210
+ }
1211
+ fileUpdates[value[FILE_REF_PROP]] = op.value;
1212
+ } else if (isRemote(value)) {
1213
+ if (!remotePatches[value[REMOTE_REF_PROP]]) {
1214
+ remotePatches[value[REMOTE_REF_PROP]] = [];
1215
+ }
1216
+ remotePatches[value[REMOTE_REF_PROP]].push({
1217
+ op: "replace",
1218
+ path: referencedPath,
1219
+ value: op.value
1220
+ });
1221
+ } else {
1222
+ return err(new PatchError("Unknown reference: ".concat(JSON.stringify(op), " at path: ").concat(dereffedPath.join("/"))));
1223
+ }
1224
+ } else {
1225
+ return maybeValue;
1226
+ }
1227
+ } else {
1228
+ dereferencedPatch.push(op);
1229
+ }
1230
+ } else {
1231
+ var _maybeDerefRes = derefPath(op.path);
1232
+ if (isErr(_maybeDerefRes)) {
1233
+ return _maybeDerefRes;
1234
+ }
1235
+ var _maybeDerefRes$value2 = _slicedToArray(_maybeDerefRes.value, 2),
1236
+ _referencedPath = _maybeDerefRes$value2[1];
1237
+ if (_referencedPath) {
1238
+ throw new Error("Unimplemented operation: ".concat(JSON.stringify(op)));
1239
+ }
1240
+ dereferencedPatch.push(op);
1241
+ }
1242
+ }
1243
+ } catch (err) {
1244
+ _iterator3.e(err);
1245
+ } finally {
1246
+ _iterator3.f();
1247
+ }
1248
+ return ok({
1249
+ remotePatches: remotePatches,
1250
+ fileUpdates: fileUpdates,
1251
+ dereferencedPatch: dereferencedPatch
1252
+ });
1253
+ }
1254
+
1255
+ var Internal = {
1256
+ convertImageSource: convertImageSource,
1257
+ getSchema: getSchema,
1258
+ getValPath: getValPath,
1259
+ getVal: getVal,
1260
+ getRawSource: getRawSource,
1261
+ resolvePath: resolvePath,
1262
+ splitModuleIdAndModulePath: splitModuleIdAndModulePath
1263
+ };
1264
+
1265
+ export { Internal, derefPatch, fetchVal, initVal };