@elaraai/east-ui-components 0.0.1-beta.17 → 0.0.1-beta.18

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 CHANGED
@@ -525,7 +525,7 @@ var q = {
525
525
  }, Q = ["fa-classic", "fa-duotone", "fa-sharp", "fa-sharp-duotone", "fa-thumbprint", "fa-whiteboard", "fa-notdog", "fa-notdog-duo", "fa-chisel", "fa-etch", "fa-jelly", "fa-jelly-fill", "fa-jelly-duo", "fa-slab", "fa-slab-press", "fa-utility", "fa-utility-duo", "fa-utility-fill"];
526
526
  var i = "classic", t = "duotone", d = "sharp", l = "sharp-duotone", f = "chisel", n = "etch", h = "jelly", o = "jelly-duo", u = "jelly-fill", g = "notdog", s$1 = "notdog-duo", y$1 = "slab", m = "slab-press", e = "thumbprint", p = "utility", a$1 = "utility-duo", w = "utility-fill", x$1 = "whiteboard", b = "Classic", c$1 = "Duotone", I = "Sharp", F = "Sharp Duotone", v = "Chisel", S = "Etch", A = "Jelly", P$1 = "Jelly Duo", j = "Jelly Fill", B = "Notdog", N = "Notdog Duo", k$1 = "Slab", D = "Slab Press", T = "Thumbprint", C = "Utility", W = "Utility Duo", K = "Utility Fill", R = "Whiteboard", rt = [i, t, d, l, f, n, h, o, u, g, s$1, y$1, m, e, p, a$1, w, x$1];
527
527
  _dt = {}, _defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_dt, i, b), t, c$1), d, I), l, F), f, v), n, S), h, A), o, P$1), u, j), g, B), _defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_defineProperty$V(_dt, s$1, N), y$1, k$1), m, D), e, T), p, C), a$1, W), w, K), x$1, R);
528
- var gt = {
528
+ var gt$1 = {
529
529
  classic: {
530
530
  900: "fas",
531
531
  400: "far",
@@ -1140,7 +1140,7 @@ familyProxy(_LONG_STYLE_TO_PREFIX);
1140
1140
  var ICON_SELECTION_SYNTAX_PATTERN = E;
1141
1141
  var LAYERS_TEXT_CLASSNAME = "fa-layers-text";
1142
1142
  var FONT_FAMILY_PATTERN = _;
1143
- var _FONT_WEIGHT_TO_PREFIX = _objectSpread2({}, gt);
1143
+ var _FONT_WEIGHT_TO_PREFIX = _objectSpread2({}, gt$1);
1144
1144
  familyProxy(_FONT_WEIGHT_TO_PREFIX);
1145
1145
  var ATTRIBUTES_WATCHED_FOR_MUTATION = ["class", "data-prefix", "data-icon", "data-fa-transform", "data-fa-mask"];
1146
1146
  var DUOTONE_CLASSES = H;
@@ -20831,10 +20831,10 @@ function requireIsPrimitive() {
20831
20831
  })(isPrimitive);
20832
20832
  return isPrimitive;
20833
20833
  }
20834
- var eq = {};
20834
+ var eq$1 = {};
20835
20835
  var hasRequiredEq;
20836
20836
  function requireEq() {
20837
- if (hasRequiredEq) return eq;
20837
+ if (hasRequiredEq) return eq$1;
20838
20838
  hasRequiredEq = 1;
20839
20839
  (function(exports$1) {
20840
20840
  Object.defineProperty(exports$1, Symbol.toStringTag, { value: "Module" });
@@ -20842,8 +20842,8 @@ function requireEq() {
20842
20842
  return value === other || Number.isNaN(value) && Number.isNaN(other);
20843
20843
  }
20844
20844
  exports$1.eq = eq2;
20845
- })(eq);
20846
- return eq;
20845
+ })(eq$1);
20846
+ return eq$1;
20847
20847
  }
20848
20848
  var hasRequiredIsMatchWith;
20849
20849
  function requireIsMatchWith() {
@@ -33989,15 +33989,15 @@ var selectSmallestDistanceBetweenValues = createSelector(selectAllAppliedValues,
33989
33989
  if (sortedValues.length < 2) {
33990
33990
  return Infinity;
33991
33991
  }
33992
- var diff = sortedValues[sortedValues.length - 1] - sortedValues[0];
33993
- if (diff === 0) {
33992
+ var diff2 = sortedValues[sortedValues.length - 1] - sortedValues[0];
33993
+ if (diff2 === 0) {
33994
33994
  return Infinity;
33995
33995
  }
33996
33996
  for (var i2 = 0; i2 < sortedValues.length - 1; i2++) {
33997
33997
  var distance = sortedValues[i2 + 1] - sortedValues[i2];
33998
33998
  smallestDistanceBetweenValues = Math.min(smallestDistanceBetweenValues, distance);
33999
33999
  }
34000
- return smallestDistanceBetweenValues / diff;
34000
+ return smallestDistanceBetweenValues / diff2;
34001
34001
  });
34002
34002
  var selectCalculatedPadding = createSelector(selectSmallestDistanceBetweenValues, selectChartLayout, selectBarCategoryGap, selectChartOffsetInternal, (_1, _2, _3, padding) => padding, (smallestDistanceInPercent, layout, barCategoryGap, offset, padding) => {
34003
34003
  if (!isWellBehavedNumber(smallestDistanceInPercent)) {
@@ -53698,10 +53698,10 @@ const arrIncludesSome = (row, columnId, filterValue) => {
53698
53698
  });
53699
53699
  };
53700
53700
  arrIncludesSome.autoRemove = (val) => testFalsey(val) || !(val != null && val.length);
53701
- const equals = (row, columnId, filterValue) => {
53701
+ const equals$1 = (row, columnId, filterValue) => {
53702
53702
  return row.getValue(columnId) === filterValue;
53703
53703
  };
53704
- equals.autoRemove = (val) => testFalsey(val);
53704
+ equals$1.autoRemove = (val) => testFalsey(val);
53705
53705
  const weakEquals = (row, columnId, filterValue) => {
53706
53706
  return row.getValue(columnId) == filterValue;
53707
53707
  };
@@ -53732,7 +53732,7 @@ const filterFns = {
53732
53732
  arrIncludes,
53733
53733
  arrIncludesAll,
53734
53734
  arrIncludesSome,
53735
- equals,
53735
+ equals: equals$1,
53736
53736
  weakEquals,
53737
53737
  inNumberRange
53738
53738
  };
@@ -71229,6 +71229,28 @@ const Builtins = {
71229
71229
  inputs: ["T", "T"],
71230
71230
  output: BooleanType
71231
71231
  },
71232
+ // Patch operations - P is the computed PatchType passed from expression building
71233
+ // We use two type parameters because the patch type structure varies based on T
71234
+ Diff: {
71235
+ type_parameters: ["T", "P"],
71236
+ inputs: ["T", "T"],
71237
+ output: "P"
71238
+ },
71239
+ ApplyPatch: {
71240
+ type_parameters: ["T", "P"],
71241
+ inputs: ["T", "P"],
71242
+ output: "T"
71243
+ },
71244
+ ComposePatch: {
71245
+ type_parameters: ["T", "P"],
71246
+ inputs: ["P", "P"],
71247
+ output: "P"
71248
+ },
71249
+ InvertPatch: {
71250
+ type_parameters: ["T", "P"],
71251
+ inputs: ["P"],
71252
+ output: "P"
71253
+ },
71232
71254
  BooleanNot: {
71233
71255
  type_parameters: [],
71234
71256
  inputs: [BooleanType],
@@ -72705,6 +72727,17 @@ function valueOrExprToAstTyped(value, type, visited, location = get_location(2))
72705
72727
  class BooleanExpr extends Expr {
72706
72728
  constructor(ast, createExpr) {
72707
72729
  super(BooleanType, ast, createExpr);
72730
+ // ============================================================================
72731
+ // Aliases for comparison operations
72732
+ // ============================================================================
72733
+ /** Alias for {@link equals} */
72734
+ __publicField(this, "eq", this.equals);
72735
+ /** Alias for {@link equals} */
72736
+ __publicField(this, "equal", this.equals);
72737
+ /** Alias for {@link notEquals} */
72738
+ __publicField(this, "ne", this.notEquals);
72739
+ /** Alias for {@link notEquals} */
72740
+ __publicField(this, "notEqual", this.notEquals);
72708
72741
  }
72709
72742
  /**
72710
72743
  * Performs logical NOT operation (!x).
@@ -72975,6 +73008,58 @@ class BooleanExpr extends Expr {
72975
73008
  class IntegerExpr extends Expr {
72976
73009
  constructor(ast, createExpr) {
72977
73010
  super(IntegerType, ast, createExpr);
73011
+ // ============================================================================
73012
+ // Aliases for comparison operations
73013
+ // ============================================================================
73014
+ /** Alias for {@link equals} */
73015
+ __publicField(this, "eq", this.equals);
73016
+ /** Alias for {@link equals} */
73017
+ __publicField(this, "equal", this.equals);
73018
+ /** Alias for {@link notEquals} */
73019
+ __publicField(this, "ne", this.notEquals);
73020
+ /** Alias for {@link notEquals} */
73021
+ __publicField(this, "notEqual", this.notEquals);
73022
+ /** Alias for {@link greaterThan} */
73023
+ __publicField(this, "gt", this.greaterThan);
73024
+ /** Alias for {@link greaterThan} */
73025
+ __publicField(this, "greater", this.greaterThan);
73026
+ /** Alias for {@link lessThan} */
73027
+ __publicField(this, "lt", this.lessThan);
73028
+ /** Alias for {@link lessThan} */
73029
+ __publicField(this, "less", this.lessThan);
73030
+ /** Alias for {@link greaterThanOrEqual} */
73031
+ __publicField(this, "gte", this.greaterThanOrEqual);
73032
+ /** Alias for {@link greaterThanOrEqual} */
73033
+ __publicField(this, "ge", this.greaterThanOrEqual);
73034
+ /** Alias for {@link greaterThanOrEqual} */
73035
+ __publicField(this, "greaterEqual", this.greaterThanOrEqual);
73036
+ /** Alias for {@link lessThanOrEqual} */
73037
+ __publicField(this, "lte", this.lessThanOrEqual);
73038
+ /** Alias for {@link lessThanOrEqual} */
73039
+ __publicField(this, "le", this.lessThanOrEqual);
73040
+ /** Alias for {@link lessThanOrEqual} */
73041
+ __publicField(this, "lessEqual", this.lessThanOrEqual);
73042
+ // ============================================================================
73043
+ // Aliases for arithmetic operations
73044
+ // ============================================================================
73045
+ /** Alias for {@link add} */
73046
+ __publicField(this, "plus", this.add);
73047
+ /** Alias for {@link subtract} */
73048
+ __publicField(this, "sub", this.subtract);
73049
+ /** Alias for {@link subtract} */
73050
+ __publicField(this, "minus", this.subtract);
73051
+ /** Alias for {@link multiply} */
73052
+ __publicField(this, "mul", this.multiply);
73053
+ /** Alias for {@link multiply} */
73054
+ __publicField(this, "times", this.multiply);
73055
+ /** Alias for {@link divide} */
73056
+ __publicField(this, "div", this.divide);
73057
+ /** Alias for {@link remainder} */
73058
+ __publicField(this, "mod", this.remainder);
73059
+ /** Alias for {@link remainder} */
73060
+ __publicField(this, "rem", this.remainder);
73061
+ /** Alias for {@link remainder} */
73062
+ __publicField(this, "modulo", this.remainder);
72978
73063
  }
72979
73064
  /**
72980
73065
  * Negates an integer value (-x).
@@ -73366,6 +73451,58 @@ class IntegerExpr extends Expr {
73366
73451
  class FloatExpr extends Expr {
73367
73452
  constructor(ast, createExpr) {
73368
73453
  super(FloatType, ast, createExpr);
73454
+ // ============================================================================
73455
+ // Aliases for comparison operations
73456
+ // ============================================================================
73457
+ /** Alias for {@link equals} */
73458
+ __publicField(this, "eq", this.equals);
73459
+ /** Alias for {@link equals} */
73460
+ __publicField(this, "equal", this.equals);
73461
+ /** Alias for {@link notEquals} */
73462
+ __publicField(this, "ne", this.notEquals);
73463
+ /** Alias for {@link notEquals} */
73464
+ __publicField(this, "notEqual", this.notEquals);
73465
+ /** Alias for {@link greaterThan} */
73466
+ __publicField(this, "gt", this.greaterThan);
73467
+ /** Alias for {@link greaterThan} */
73468
+ __publicField(this, "greater", this.greaterThan);
73469
+ /** Alias for {@link lessThan} */
73470
+ __publicField(this, "lt", this.lessThan);
73471
+ /** Alias for {@link lessThan} */
73472
+ __publicField(this, "less", this.lessThan);
73473
+ /** Alias for {@link greaterThanOrEqual} */
73474
+ __publicField(this, "gte", this.greaterThanOrEqual);
73475
+ /** Alias for {@link greaterThanOrEqual} */
73476
+ __publicField(this, "ge", this.greaterThanOrEqual);
73477
+ /** Alias for {@link greaterThanOrEqual} */
73478
+ __publicField(this, "greaterEqual", this.greaterThanOrEqual);
73479
+ /** Alias for {@link lessThanOrEqual} */
73480
+ __publicField(this, "lte", this.lessThanOrEqual);
73481
+ /** Alias for {@link lessThanOrEqual} */
73482
+ __publicField(this, "le", this.lessThanOrEqual);
73483
+ /** Alias for {@link lessThanOrEqual} */
73484
+ __publicField(this, "lessEqual", this.lessThanOrEqual);
73485
+ // ============================================================================
73486
+ // Aliases for arithmetic operations
73487
+ // ============================================================================
73488
+ /** Alias for {@link add} */
73489
+ __publicField(this, "plus", this.add);
73490
+ /** Alias for {@link subtract} */
73491
+ __publicField(this, "sub", this.subtract);
73492
+ /** Alias for {@link subtract} */
73493
+ __publicField(this, "minus", this.subtract);
73494
+ /** Alias for {@link multiply} */
73495
+ __publicField(this, "mul", this.multiply);
73496
+ /** Alias for {@link multiply} */
73497
+ __publicField(this, "times", this.multiply);
73498
+ /** Alias for {@link divide} */
73499
+ __publicField(this, "div", this.divide);
73500
+ /** Alias for {@link remainder} */
73501
+ __publicField(this, "mod", this.remainder);
73502
+ /** Alias for {@link remainder} */
73503
+ __publicField(this, "rem", this.remainder);
73504
+ /** Alias for {@link remainder} */
73505
+ __publicField(this, "modulo", this.remainder);
73369
73506
  }
73370
73507
  /**
73371
73508
  * Negates a floating-point number (-x).
@@ -74151,6 +74288,37 @@ if (import.meta.url === `file://${process.argv[1]}`) {
74151
74288
  class StringExpr extends Expr {
74152
74289
  constructor(ast, createExpr) {
74153
74290
  super(StringType, ast, createExpr);
74291
+ // ============================================================================
74292
+ // Aliases for comparison operations
74293
+ // ============================================================================
74294
+ /** Alias for {@link equals} */
74295
+ __publicField(this, "eq", this.equals);
74296
+ /** Alias for {@link equals} */
74297
+ __publicField(this, "equal", this.equals);
74298
+ /** Alias for {@link notEquals} */
74299
+ __publicField(this, "ne", this.notEquals);
74300
+ /** Alias for {@link notEquals} */
74301
+ __publicField(this, "notEqual", this.notEquals);
74302
+ /** Alias for {@link greaterThan} */
74303
+ __publicField(this, "gt", this.greaterThan);
74304
+ /** Alias for {@link greaterThan} */
74305
+ __publicField(this, "greater", this.greaterThan);
74306
+ /** Alias for {@link lessThan} */
74307
+ __publicField(this, "lt", this.lessThan);
74308
+ /** Alias for {@link lessThan} */
74309
+ __publicField(this, "less", this.lessThan);
74310
+ /** Alias for {@link greaterThanOrEqual} */
74311
+ __publicField(this, "gte", this.greaterThanOrEqual);
74312
+ /** Alias for {@link greaterThanOrEqual} */
74313
+ __publicField(this, "ge", this.greaterThanOrEqual);
74314
+ /** Alias for {@link greaterThanOrEqual} */
74315
+ __publicField(this, "greaterEqual", this.greaterThanOrEqual);
74316
+ /** Alias for {@link lessThanOrEqual} */
74317
+ __publicField(this, "lte", this.lessThanOrEqual);
74318
+ /** Alias for {@link lessThanOrEqual} */
74319
+ __publicField(this, "le", this.lessThanOrEqual);
74320
+ /** Alias for {@link lessThanOrEqual} */
74321
+ __publicField(this, "lessEqual", this.lessThanOrEqual);
74154
74322
  }
74155
74323
  /**
74156
74324
  * Parses a value of the given type from the string using East's canonical text format.
@@ -74988,6 +75156,37 @@ function tokenizeDateTimeFormat(format2) {
74988
75156
  class DateTimeExpr extends Expr {
74989
75157
  constructor(ast, createExpr) {
74990
75158
  super(DateTimeType, ast, createExpr);
75159
+ // ============================================================================
75160
+ // Aliases for comparison operations
75161
+ // ============================================================================
75162
+ /** Alias for {@link equals} */
75163
+ __publicField(this, "eq", this.equals);
75164
+ /** Alias for {@link equals} */
75165
+ __publicField(this, "equal", this.equals);
75166
+ /** Alias for {@link notEquals} */
75167
+ __publicField(this, "ne", this.notEquals);
75168
+ /** Alias for {@link notEquals} */
75169
+ __publicField(this, "notEqual", this.notEquals);
75170
+ /** Alias for {@link greaterThan} */
75171
+ __publicField(this, "gt", this.greaterThan);
75172
+ /** Alias for {@link greaterThan} */
75173
+ __publicField(this, "greater", this.greaterThan);
75174
+ /** Alias for {@link lessThan} */
75175
+ __publicField(this, "lt", this.lessThan);
75176
+ /** Alias for {@link lessThan} */
75177
+ __publicField(this, "less", this.lessThan);
75178
+ /** Alias for {@link greaterThanOrEqual} */
75179
+ __publicField(this, "gte", this.greaterThanOrEqual);
75180
+ /** Alias for {@link greaterThanOrEqual} */
75181
+ __publicField(this, "ge", this.greaterThanOrEqual);
75182
+ /** Alias for {@link greaterThanOrEqual} */
75183
+ __publicField(this, "greaterEqual", this.greaterThanOrEqual);
75184
+ /** Alias for {@link lessThanOrEqual} */
75185
+ __publicField(this, "lte", this.lessThanOrEqual);
75186
+ /** Alias for {@link lessThanOrEqual} */
75187
+ __publicField(this, "le", this.lessThanOrEqual);
75188
+ /** Alias for {@link lessThanOrEqual} */
75189
+ __publicField(this, "lessEqual", this.lessThanOrEqual);
74991
75190
  }
74992
75191
  /**
74993
75192
  * Extracts the year component from the DateTime.
@@ -77235,6 +77434,17 @@ function encodeCsvFor(structType, config2) {
77235
77434
  class BlobExpr extends Expr {
77236
77435
  constructor(ast, createExpr) {
77237
77436
  super(ast.type, ast, createExpr);
77437
+ // ============================================================================
77438
+ // Aliases for comparison operations
77439
+ // ============================================================================
77440
+ /** Alias for {@link equals} */
77441
+ __publicField(this, "eq", this.equals);
77442
+ /** Alias for {@link equals} */
77443
+ __publicField(this, "equal", this.equals);
77444
+ /** Alias for {@link notEquals} */
77445
+ __publicField(this, "ne", this.notEquals);
77446
+ /** Alias for {@link notEquals} */
77447
+ __publicField(this, "notEqual", this.notEquals);
77238
77448
  }
77239
77449
  /**
77240
77450
  * Returns the size of the blob in bytes.
@@ -77479,6 +77689,17 @@ class ArrayExpr extends Expr {
77479
77689
  constructor(value_type, ast, createExpr) {
77480
77690
  super(ast.type, ast, createExpr);
77481
77691
  __publicField(this, "value_type");
77692
+ // ============================================================================
77693
+ // Aliases for comparison operations
77694
+ // ============================================================================
77695
+ /** Alias for {@link equals} */
77696
+ __publicField(this, "eq", this.equals);
77697
+ /** Alias for {@link equals} */
77698
+ __publicField(this, "equal", this.equals);
77699
+ /** Alias for {@link notEquals} */
77700
+ __publicField(this, "ne", this.notEquals);
77701
+ /** Alias for {@link notEquals} */
77702
+ __publicField(this, "notEqual", this.notEquals);
77482
77703
  this.value_type = value_type;
77483
77704
  }
77484
77705
  /**
@@ -79324,6 +79545,17 @@ class SetExpr extends Expr {
79324
79545
  constructor(key_type, ast, createExpr) {
79325
79546
  super(ast.type, ast, createExpr);
79326
79547
  __publicField(this, "key_type");
79548
+ // ============================================================================
79549
+ // Aliases for comparison operations
79550
+ // ============================================================================
79551
+ /** Alias for {@link equals} */
79552
+ __publicField(this, "eq", this.equals);
79553
+ /** Alias for {@link equals} */
79554
+ __publicField(this, "equal", this.equals);
79555
+ /** Alias for {@link notEquals} */
79556
+ __publicField(this, "ne", this.notEquals);
79557
+ /** Alias for {@link notEquals} */
79558
+ __publicField(this, "notEqual", this.notEquals);
79327
79559
  this.key_type = key_type;
79328
79560
  }
79329
79561
  /**
@@ -80436,6 +80668,17 @@ class DictExpr extends Expr {
80436
80668
  super(ast.type, ast, createExpr);
80437
80669
  __publicField(this, "key_type");
80438
80670
  __publicField(this, "value_type");
80671
+ // ============================================================================
80672
+ // Aliases for comparison operations
80673
+ // ============================================================================
80674
+ /** Alias for {@link equals} */
80675
+ __publicField(this, "eq", this.equals);
80676
+ /** Alias for {@link equals} */
80677
+ __publicField(this, "equal", this.equals);
80678
+ /** Alias for {@link notEquals} */
80679
+ __publicField(this, "ne", this.notEquals);
80680
+ /** Alias for {@link notEquals} */
80681
+ __publicField(this, "notEqual", this.notEquals);
80439
80682
  this.key_type = key_type;
80440
80683
  this.value_type = value_type;
80441
80684
  }
@@ -81911,6 +82154,17 @@ class VariantExpr extends Expr {
81911
82154
  constructor(cases, ast, factory) {
81912
82155
  super(ast.type, ast, factory);
81913
82156
  __publicField(this, "cases");
82157
+ // ============================================================================
82158
+ // Aliases for comparison operations
82159
+ // ============================================================================
82160
+ /** Alias for {@link equals} */
82161
+ __publicField(this, "eq", this.equals);
82162
+ /** Alias for {@link equals} */
82163
+ __publicField(this, "equal", this.equals);
82164
+ /** Alias for {@link notEquals} */
82165
+ __publicField(this, "ne", this.notEquals);
82166
+ /** Alias for {@link notEquals} */
82167
+ __publicField(this, "notEqual", this.notEquals);
81914
82168
  this.cases = cases;
81915
82169
  }
81916
82170
  /**
@@ -82249,7 +82503,7 @@ function ast_to_ir(ast, ctx = { local_ctx: /* @__PURE__ */ new Map(), parent_ctx
82249
82503
  throw new Error(`Builtin ${builtin_name} with type parameters [${ast.type_parameters.map((tp) => printType(tp)).join(", ")}] argument ${i2} of type ${printType(arg.type)} is not compatible with expected type ${printType(expectedType)} at ${printLocation(ast.location)}`);
82250
82504
  }
82251
82505
  arg_ir = variant("As", {
82252
- type: toEastTypeValue(arg.type),
82506
+ type: toEastTypeValue(expectedType),
82253
82507
  value: arg_ir,
82254
82508
  location: toLocationValue(ast.location)
82255
82509
  });
@@ -82981,6 +83235,318 @@ function ast_to_ir(ast, ctx = { local_ctx: /* @__PURE__ */ new Map(), parent_ctx
82981
83235
  throw e3;
82982
83236
  }
82983
83237
  }
83238
+ class ConflictError extends Error {
83239
+ constructor(message) {
83240
+ super(message);
83241
+ this.name = "ConflictError";
83242
+ }
83243
+ }
83244
+ function computeLCS(before, after, equal2) {
83245
+ const m2 = before.length;
83246
+ const n2 = after.length;
83247
+ const dp = Array.from({ length: m2 + 1 }, () => Array(n2 + 1).fill(0));
83248
+ for (let i3 = 1; i3 <= m2; i3++) {
83249
+ for (let j3 = 1; j3 <= n2; j3++) {
83250
+ if (equal2(before[i3 - 1], after[j3 - 1])) {
83251
+ dp[i3][j3] = dp[i3 - 1][j3 - 1] + 1;
83252
+ } else {
83253
+ dp[i3][j3] = Math.max(dp[i3 - 1][j3], dp[i3][j3 - 1]);
83254
+ }
83255
+ }
83256
+ }
83257
+ const beforeIndices = [];
83258
+ const afterIndices = [];
83259
+ let i2 = m2;
83260
+ let j2 = n2;
83261
+ while (i2 > 0 && j2 > 0) {
83262
+ if (equal2(before[i2 - 1], after[j2 - 1])) {
83263
+ beforeIndices.unshift(i2 - 1);
83264
+ afterIndices.unshift(j2 - 1);
83265
+ i2--;
83266
+ j2--;
83267
+ } else if (dp[i2 - 1][j2] > dp[i2][j2 - 1]) {
83268
+ i2--;
83269
+ } else {
83270
+ j2--;
83271
+ }
83272
+ }
83273
+ return { beforeIndices, afterIndices };
83274
+ }
83275
+ function diffFor(type, ctx = { diff: [], types: [], equal: [] }) {
83276
+ const t2 = isVariant(type) ? type : toEastTypeValue(type);
83277
+ if (t2.type === "Never") {
83278
+ return (_before, _after) => {
83279
+ throw new Error("Cannot diff values of type Never");
83280
+ };
83281
+ } else if (t2.type === "Null" || t2.type === "Boolean" || t2.type === "Integer" || t2.type === "Float" || t2.type === "String" || t2.type === "DateTime" || t2.type === "Blob") {
83282
+ const equal2 = equalFor(t2);
83283
+ return (before, after) => {
83284
+ if (equal2(before, after)) {
83285
+ return variant("unchanged", null);
83286
+ }
83287
+ return variant("replace", { before, after });
83288
+ };
83289
+ } else if (t2.type === "Array") {
83290
+ let elementEqual;
83291
+ let is2;
83292
+ const ret = (before, after) => {
83293
+ if (is2(before, after)) {
83294
+ return variant("unchanged", null);
83295
+ }
83296
+ const { beforeIndices, afterIndices } = computeLCS(before, after, elementEqual);
83297
+ const operations = [];
83298
+ let beforePtr = 0;
83299
+ let afterPtr = 0;
83300
+ let lcsPtr = 0;
83301
+ let deleteCount = 0;
83302
+ let insertCount = 0;
83303
+ while (beforePtr < before.length || afterPtr < after.length) {
83304
+ const nextBeforeLCS = lcsPtr < beforeIndices.length ? beforeIndices[lcsPtr] : before.length;
83305
+ const nextAfterLCS = lcsPtr < afterIndices.length ? afterIndices[lcsPtr] : after.length;
83306
+ while (beforePtr < nextBeforeLCS) {
83307
+ const actualPosition = beforePtr - deleteCount + insertCount;
83308
+ operations.push({
83309
+ key: BigInt(actualPosition),
83310
+ offset: 0n,
83311
+ operation: variant("delete", before[beforePtr])
83312
+ });
83313
+ deleteCount++;
83314
+ beforePtr++;
83315
+ }
83316
+ while (afterPtr < nextAfterLCS) {
83317
+ operations.push({
83318
+ key: BigInt(afterPtr),
83319
+ offset: 0n,
83320
+ operation: variant("insert", after[afterPtr])
83321
+ });
83322
+ insertCount++;
83323
+ afterPtr++;
83324
+ }
83325
+ if (lcsPtr < beforeIndices.length) {
83326
+ beforePtr++;
83327
+ afterPtr++;
83328
+ lcsPtr++;
83329
+ }
83330
+ }
83331
+ if (operations.length === 0) {
83332
+ return variant("unchanged", null);
83333
+ }
83334
+ return variant("patch", operations);
83335
+ };
83336
+ const arrayEqual = equalFor(t2, ctx.equal);
83337
+ ctx.diff.push(ret);
83338
+ ctx.types.push(t2);
83339
+ ctx.equal.push(arrayEqual);
83340
+ is2 = isFor(t2, ctx.equal);
83341
+ elementEqual = equalFor(t2.value, ctx.equal);
83342
+ ctx.diff.pop();
83343
+ ctx.types.pop();
83344
+ ctx.equal.pop();
83345
+ return ret;
83346
+ } else if (t2.type === "Set") {
83347
+ const is2 = isFor(t2);
83348
+ const keyCompare = compareFor(t2.value);
83349
+ return (before, after) => {
83350
+ if (is2(before, after)) {
83351
+ return variant("unchanged", null);
83352
+ }
83353
+ const operations = new SortedMap(void 0, keyCompare);
83354
+ for (const key of before) {
83355
+ if (!after.has(key)) {
83356
+ operations.set(key, variant("delete", null));
83357
+ }
83358
+ }
83359
+ for (const key of after) {
83360
+ if (!before.has(key)) {
83361
+ operations.set(key, variant("insert", null));
83362
+ }
83363
+ }
83364
+ if (operations.size === 0) {
83365
+ return variant("unchanged", null);
83366
+ }
83367
+ let deleteCount = 0;
83368
+ let insertCount = 0;
83369
+ for (const op of operations.values()) {
83370
+ if (op.type === "delete")
83371
+ deleteCount++;
83372
+ if (op.type === "insert")
83373
+ insertCount++;
83374
+ }
83375
+ if (deleteCount === before.size && insertCount === after.size && before.size > 0) {
83376
+ return variant("replace", { before, after });
83377
+ }
83378
+ return variant("patch", operations);
83379
+ };
83380
+ } else if (t2.type === "Dict") {
83381
+ let valueDiff;
83382
+ let valueEqual;
83383
+ let is2;
83384
+ const keyCompare = compareFor(t2.value.key);
83385
+ const ret = (before, after) => {
83386
+ if (is2(before, after)) {
83387
+ return variant("unchanged", null);
83388
+ }
83389
+ const operations = new SortedMap(void 0, keyCompare);
83390
+ for (const [key, beforeValue] of before) {
83391
+ if (!after.has(key)) {
83392
+ operations.set(key, variant("delete", beforeValue));
83393
+ } else {
83394
+ const afterValue = after.get(key);
83395
+ if (!valueEqual(beforeValue, afterValue)) {
83396
+ const patch = valueDiff(beforeValue, afterValue);
83397
+ operations.set(key, variant("update", patch));
83398
+ }
83399
+ }
83400
+ }
83401
+ for (const [key, afterValue] of after) {
83402
+ if (!before.has(key)) {
83403
+ operations.set(key, variant("insert", afterValue));
83404
+ }
83405
+ }
83406
+ if (operations.size === 0) {
83407
+ return variant("unchanged", null);
83408
+ }
83409
+ let insertCount = 0;
83410
+ let deleteCount = 0;
83411
+ for (const op of operations.values()) {
83412
+ if (op.type === "insert")
83413
+ insertCount++;
83414
+ if (op.type === "delete")
83415
+ deleteCount++;
83416
+ }
83417
+ if (insertCount === after.size && deleteCount === before.size && before.size > 0) {
83418
+ return variant("replace", { before, after });
83419
+ }
83420
+ return variant("patch", operations);
83421
+ };
83422
+ const dictEqual = equalFor(t2, ctx.equal);
83423
+ ctx.diff.push(ret);
83424
+ ctx.types.push(t2);
83425
+ ctx.equal.push(dictEqual);
83426
+ is2 = isFor(t2, ctx.equal);
83427
+ valueDiff = diffFor(t2.value.value, ctx);
83428
+ valueEqual = equalFor(t2.value.value, ctx.equal);
83429
+ ctx.diff.pop();
83430
+ ctx.types.pop();
83431
+ ctx.equal.pop();
83432
+ return ret;
83433
+ } else if (t2.type === "Struct") {
83434
+ const fieldDiffs = {};
83435
+ const fieldEquals = {};
83436
+ const ret = (before, after) => {
83437
+ if (before === after) {
83438
+ return variant("unchanged", null);
83439
+ }
83440
+ const patchFields = {};
83441
+ let allUnchanged = true;
83442
+ for (const { name } of t2.value) {
83443
+ const beforeValue = before[name];
83444
+ const afterValue = after[name];
83445
+ if (fieldEquals[name](beforeValue, afterValue)) {
83446
+ patchFields[name] = variant("unchanged", null);
83447
+ } else {
83448
+ patchFields[name] = fieldDiffs[name](beforeValue, afterValue);
83449
+ allUnchanged = false;
83450
+ }
83451
+ }
83452
+ if (allUnchanged) {
83453
+ return variant("unchanged", null);
83454
+ }
83455
+ return variant("patch", patchFields);
83456
+ };
83457
+ const structEqual = equalFor(t2, ctx.equal);
83458
+ ctx.diff.push(ret);
83459
+ ctx.types.push(t2);
83460
+ ctx.equal.push(structEqual);
83461
+ for (const { name, type: fieldType } of t2.value) {
83462
+ fieldDiffs[name] = diffFor(fieldType, ctx);
83463
+ fieldEquals[name] = equalFor(fieldType, ctx.equal);
83464
+ }
83465
+ ctx.diff.pop();
83466
+ ctx.types.pop();
83467
+ ctx.equal.pop();
83468
+ return ret;
83469
+ } else if (t2.type === "Variant") {
83470
+ const caseDiffs = {};
83471
+ const caseEquals = {};
83472
+ const ret = (before, after) => {
83473
+ if (before === after) {
83474
+ return variant("unchanged", null);
83475
+ }
83476
+ if (before.type !== after.type) {
83477
+ return variant("replace", { before, after });
83478
+ }
83479
+ const caseName = before.type;
83480
+ if (caseEquals[caseName](before.value, after.value)) {
83481
+ return variant("unchanged", null);
83482
+ }
83483
+ const casePatch = caseDiffs[caseName](before.value, after.value);
83484
+ if (casePatch.type === "unchanged") {
83485
+ return variant("unchanged", null);
83486
+ }
83487
+ return variant("patch", variant(caseName, casePatch));
83488
+ };
83489
+ const variantEqual = equalFor(t2, ctx.equal);
83490
+ ctx.diff.push(ret);
83491
+ ctx.types.push(t2);
83492
+ ctx.equal.push(variantEqual);
83493
+ for (const { name, type: caseType } of t2.value) {
83494
+ caseDiffs[name] = diffFor(caseType, ctx);
83495
+ caseEquals[name] = equalFor(caseType, ctx.equal);
83496
+ }
83497
+ ctx.diff.pop();
83498
+ ctx.types.pop();
83499
+ ctx.equal.pop();
83500
+ return ret;
83501
+ } else if (t2.type === "Ref") {
83502
+ let innerDiff;
83503
+ let innerEqual;
83504
+ let is2;
83505
+ const ret = (before, after) => {
83506
+ if (is2(before, after)) {
83507
+ return variant("unchanged", null);
83508
+ }
83509
+ if (innerEqual(before.value, after.value)) {
83510
+ return variant("unchanged", null);
83511
+ }
83512
+ const innerPatch = innerDiff(before.value, after.value);
83513
+ if (innerPatch.type === "unchanged") {
83514
+ return variant("unchanged", null);
83515
+ }
83516
+ return variant("patch", innerPatch);
83517
+ };
83518
+ const refEqual = equalFor(t2, ctx.equal);
83519
+ ctx.diff.push(ret);
83520
+ ctx.types.push(t2);
83521
+ ctx.equal.push(refEqual);
83522
+ is2 = isFor(t2, ctx.equal);
83523
+ innerDiff = diffFor(t2.value, ctx);
83524
+ innerEqual = equalFor(t2.value, ctx.equal);
83525
+ ctx.diff.pop();
83526
+ ctx.types.pop();
83527
+ ctx.equal.pop();
83528
+ return ret;
83529
+ } else if (t2.type === "Recursive") {
83530
+ const resolvedType = ctx.types[ctx.types.length - Number(t2.value)];
83531
+ if (resolvedType === void 0) {
83532
+ throw new Error(`Internal error: Recursive type context not found in diffFor`);
83533
+ }
83534
+ const equal2 = ctx.equal[ctx.equal.length - Number(t2.value)];
83535
+ if (equal2 === void 0) {
83536
+ throw new Error(`Internal error: Recursive equal context not found in diffFor`);
83537
+ }
83538
+ return (before, after) => {
83539
+ if (equal2(before, after)) {
83540
+ return variant("unchanged", null);
83541
+ }
83542
+ return variant("replace", { before, after });
83543
+ };
83544
+ } else if (t2.type === "Function" || t2.type === "AsyncFunction") {
83545
+ return (_before, _after) => variant("unchanged", null);
83546
+ } else {
83547
+ throw new Error(`Unhandled type in diffFor: ${t2.type}`);
83548
+ }
83549
+ }
82984
83550
  class ParseError extends Error {
82985
83551
  constructor(message, position, path = "") {
82986
83552
  super(message);
@@ -84047,6 +84613,1007 @@ const createVariantParser = (cases, frozen, typeCtx) => {
84047
84613
  typeCtx.pop();
84048
84614
  return ret;
84049
84615
  };
84616
+ function applyFor(type, ctx = { apply: [], types: [], equal: [], print: [] }) {
84617
+ const t2 = isVariant(type) ? type : toEastTypeValue(type);
84618
+ if (t2.type === "Never") {
84619
+ return (_base, _patch) => {
84620
+ throw new Error("Cannot apply patch to values of type Never");
84621
+ };
84622
+ } else if (t2.type === "Null" || t2.type === "Boolean" || t2.type === "Integer" || t2.type === "Float" || t2.type === "String" || t2.type === "DateTime" || t2.type === "Blob") {
84623
+ const equal2 = equalFor(t2);
84624
+ const print2 = printFor(t2);
84625
+ return (base, patch) => {
84626
+ if (patch.type === "unchanged") {
84627
+ return base;
84628
+ } else if (patch.type === "replace") {
84629
+ if (!equal2(base, patch.value.before)) {
84630
+ throw new ConflictError(`Cannot apply replace - expected ${print2(patch.value.before)}, found ${print2(base)}`);
84631
+ }
84632
+ return patch.value.after;
84633
+ } else {
84634
+ throw new Error(`Invalid patch type for primitive: ${patch.type}`);
84635
+ }
84636
+ };
84637
+ } else if (t2.type === "Array") {
84638
+ let elementApply;
84639
+ let elementEqual;
84640
+ let elementPrint;
84641
+ let arrayEqual;
84642
+ const ret = (base, patch) => {
84643
+ if (patch.type === "unchanged") {
84644
+ return base;
84645
+ } else if (patch.type === "replace") {
84646
+ if (!arrayEqual(base, patch.value.before)) {
84647
+ throw new ConflictError("Cannot apply replace - base array does not match expected");
84648
+ }
84649
+ return [...patch.value.after];
84650
+ } else if (patch.type === "patch") {
84651
+ const result = [...base];
84652
+ const operations = patch.value;
84653
+ for (const op of operations) {
84654
+ const key = Number(op.key);
84655
+ const offset = Number(op.offset);
84656
+ const oldKey = key + offset;
84657
+ if (op.operation.type === "delete") {
84658
+ if (oldKey < 0 || oldKey >= result.length) {
84659
+ throw new ConflictError(`Cannot delete at index ${oldKey} - array length is ${result.length}`);
84660
+ }
84661
+ if (!elementEqual(result[oldKey], op.operation.value)) {
84662
+ throw new ConflictError(`Cannot delete at index ${oldKey} - expected ${elementPrint(op.operation.value)}, found ${elementPrint(result[oldKey])}`);
84663
+ }
84664
+ result.splice(oldKey, 1);
84665
+ } else if (op.operation.type === "insert") {
84666
+ if (key < 0 || key > result.length) {
84667
+ throw new ConflictError(`Cannot insert at index ${key} - array length is ${result.length}`);
84668
+ }
84669
+ result.splice(key, 0, op.operation.value);
84670
+ } else if (op.operation.type === "update") {
84671
+ if (oldKey < 0 || oldKey >= result.length) {
84672
+ throw new ConflictError(`Cannot update at index ${oldKey} - array length is ${result.length}`);
84673
+ }
84674
+ result[oldKey] = elementApply(result[oldKey], op.operation.value);
84675
+ }
84676
+ }
84677
+ return result;
84678
+ } else {
84679
+ throw new Error(`Invalid patch type for array: ${patch.type}`);
84680
+ }
84681
+ };
84682
+ const arrayPrint = printFor(t2, ctx.print);
84683
+ arrayEqual = equalFor(t2, ctx.equal);
84684
+ ctx.apply.push(ret);
84685
+ ctx.types.push(t2);
84686
+ ctx.equal.push(arrayEqual);
84687
+ ctx.print.push(arrayPrint);
84688
+ elementApply = applyFor(t2.value, ctx);
84689
+ elementEqual = equalFor(t2.value, ctx.equal);
84690
+ elementPrint = printFor(t2.value, ctx.print);
84691
+ ctx.apply.pop();
84692
+ ctx.types.pop();
84693
+ ctx.equal.pop();
84694
+ ctx.print.pop();
84695
+ return ret;
84696
+ } else if (t2.type === "Set") {
84697
+ const keyPrint = printFor(t2.value);
84698
+ const setEqual = equalFor(t2, ctx.equal);
84699
+ const keyCompare = compareFor(t2.value);
84700
+ return (base, patch) => {
84701
+ if (patch.type === "unchanged") {
84702
+ return base;
84703
+ } else if (patch.type === "replace") {
84704
+ if (!setEqual(base, patch.value.before)) {
84705
+ throw new ConflictError("Cannot apply replace - base set does not match expected");
84706
+ }
84707
+ return new SortedSet(patch.value.after, keyCompare);
84708
+ } else if (patch.type === "patch") {
84709
+ const result = new SortedSet(base, keyCompare);
84710
+ const operations = patch.value;
84711
+ for (const [key, op] of operations) {
84712
+ if (op.type === "delete") {
84713
+ if (!result.has(key)) {
84714
+ throw new ConflictError(`Cannot delete key ${keyPrint(key)} - key does not exist`);
84715
+ }
84716
+ result.delete(key);
84717
+ } else if (op.type === "insert") {
84718
+ if (result.has(key)) {
84719
+ throw new ConflictError(`Cannot insert key ${keyPrint(key)} - key already exists`);
84720
+ }
84721
+ result.add(key);
84722
+ }
84723
+ }
84724
+ return result;
84725
+ } else {
84726
+ throw new Error(`Invalid patch type for set: ${patch.type}`);
84727
+ }
84728
+ };
84729
+ } else if (t2.type === "Dict") {
84730
+ let valueApply;
84731
+ let valueEqual;
84732
+ let dictEqual;
84733
+ let keyPrint;
84734
+ let valuePrint;
84735
+ const keyCompare = compareFor(t2.value.key);
84736
+ const ret = (base, patch) => {
84737
+ if (patch.type === "unchanged") {
84738
+ return base;
84739
+ } else if (patch.type === "replace") {
84740
+ if (!dictEqual(base, patch.value.before)) {
84741
+ throw new ConflictError("Cannot apply replace - base dict does not match expected");
84742
+ }
84743
+ return new SortedMap(patch.value.after, keyCompare);
84744
+ } else if (patch.type === "patch") {
84745
+ const result = new SortedMap(base, keyCompare);
84746
+ const operations = patch.value;
84747
+ for (const [key, op] of operations) {
84748
+ if (op.type === "delete") {
84749
+ if (!result.has(key)) {
84750
+ throw new ConflictError(`Cannot delete key ${keyPrint(key)} - key does not exist`);
84751
+ }
84752
+ if (!valueEqual(result.get(key), op.value)) {
84753
+ throw new ConflictError(`Cannot delete key ${keyPrint(key)} - expected value ${valuePrint(op.value)}, found ${valuePrint(result.get(key))}`);
84754
+ }
84755
+ result.delete(key);
84756
+ } else if (op.type === "insert") {
84757
+ if (result.has(key)) {
84758
+ throw new ConflictError(`Cannot insert key ${keyPrint(key)} - key already exists with value ${valuePrint(result.get(key))}`);
84759
+ }
84760
+ result.set(key, op.value);
84761
+ } else if (op.type === "update") {
84762
+ if (!result.has(key)) {
84763
+ throw new ConflictError(`Cannot update key ${keyPrint(key)} - key does not exist`);
84764
+ }
84765
+ result.set(key, valueApply(result.get(key), op.value));
84766
+ }
84767
+ }
84768
+ return result;
84769
+ } else {
84770
+ throw new Error(`Invalid patch type for dict: ${patch.type}`);
84771
+ }
84772
+ };
84773
+ const dictPrint = printFor(t2, ctx.print);
84774
+ dictEqual = equalFor(t2, ctx.equal);
84775
+ ctx.apply.push(ret);
84776
+ ctx.types.push(t2);
84777
+ ctx.equal.push(dictEqual);
84778
+ ctx.print.push(dictPrint);
84779
+ valueApply = applyFor(t2.value.value, ctx);
84780
+ valueEqual = equalFor(t2.value.value, ctx.equal);
84781
+ keyPrint = printFor(t2.value.key);
84782
+ valuePrint = printFor(t2.value.value, ctx.print);
84783
+ ctx.apply.pop();
84784
+ ctx.types.pop();
84785
+ ctx.equal.pop();
84786
+ ctx.print.pop();
84787
+ return ret;
84788
+ } else if (t2.type === "Struct") {
84789
+ const fieldApplies = {};
84790
+ let equal2;
84791
+ const ret = (base, patch) => {
84792
+ if (patch.type === "unchanged") {
84793
+ return base;
84794
+ } else if (patch.type === "replace") {
84795
+ if (!equal2(base, patch.value.before)) {
84796
+ throw new ConflictError("Cannot apply replace - base struct does not match expected");
84797
+ }
84798
+ return { ...patch.value.after };
84799
+ } else if (patch.type === "patch") {
84800
+ const result = {};
84801
+ for (const { name } of t2.value) {
84802
+ const fieldPatch = patch.value[name];
84803
+ result[name] = fieldApplies[name](base[name], fieldPatch);
84804
+ }
84805
+ return result;
84806
+ } else {
84807
+ throw new Error(`Invalid patch type for struct: ${patch.type}`);
84808
+ }
84809
+ };
84810
+ const structPrint = printFor(t2, ctx.print);
84811
+ equal2 = equalFor(t2, ctx.equal);
84812
+ ctx.apply.push(ret);
84813
+ ctx.types.push(t2);
84814
+ ctx.equal.push(equal2);
84815
+ ctx.print.push(structPrint);
84816
+ for (const { name, type: fieldType } of t2.value) {
84817
+ fieldApplies[name] = applyFor(fieldType, ctx);
84818
+ }
84819
+ ctx.apply.pop();
84820
+ ctx.types.pop();
84821
+ ctx.equal.pop();
84822
+ ctx.print.pop();
84823
+ return ret;
84824
+ } else if (t2.type === "Variant") {
84825
+ const caseApplies = {};
84826
+ let equal2;
84827
+ const ret = (base, patch) => {
84828
+ if (patch.type === "unchanged") {
84829
+ return base;
84830
+ } else if (patch.type === "replace") {
84831
+ if (!equal2(base, patch.value.before)) {
84832
+ throw new ConflictError("Cannot apply replace - base variant does not match expected");
84833
+ }
84834
+ return patch.value.after;
84835
+ } else if (patch.type === "patch") {
84836
+ const caseName = patch.value.type;
84837
+ if (base.type !== caseName) {
84838
+ throw new ConflictError(`Cannot apply patch for case ${caseName} to variant with case ${base.type}`);
84839
+ }
84840
+ const casePatch = patch.value.value;
84841
+ const newValue = caseApplies[caseName](base.value, casePatch);
84842
+ return variant(caseName, newValue);
84843
+ } else {
84844
+ throw new Error(`Invalid patch type for variant: ${patch.type}`);
84845
+ }
84846
+ };
84847
+ const variantPrint = printFor(t2, ctx.print);
84848
+ equal2 = equalFor(t2, ctx.equal);
84849
+ ctx.apply.push(ret);
84850
+ ctx.types.push(t2);
84851
+ ctx.equal.push(equal2);
84852
+ ctx.print.push(variantPrint);
84853
+ for (const { name, type: caseType } of t2.value) {
84854
+ caseApplies[name] = applyFor(caseType, ctx);
84855
+ }
84856
+ ctx.apply.pop();
84857
+ ctx.types.pop();
84858
+ ctx.equal.pop();
84859
+ ctx.print.pop();
84860
+ return ret;
84861
+ } else if (t2.type === "Ref") {
84862
+ let innerApply;
84863
+ let equal2;
84864
+ const ret = (base, patch) => {
84865
+ if (patch.type === "unchanged") {
84866
+ return base;
84867
+ } else if (patch.type === "replace") {
84868
+ if (!equal2(base, patch.value.before)) {
84869
+ throw new ConflictError("Cannot apply replace - base ref does not match expected");
84870
+ }
84871
+ return { value: patch.value.after.value };
84872
+ } else if (patch.type === "patch") {
84873
+ const newValue = innerApply(base.value, patch.value);
84874
+ return { value: newValue };
84875
+ } else {
84876
+ throw new Error(`Invalid patch type for ref: ${patch.type}`);
84877
+ }
84878
+ };
84879
+ const refPrint = printFor(t2, ctx.print);
84880
+ equal2 = equalFor(t2, ctx.equal);
84881
+ ctx.apply.push(ret);
84882
+ ctx.types.push(t2);
84883
+ ctx.equal.push(equal2);
84884
+ ctx.print.push(refPrint);
84885
+ innerApply = applyFor(t2.value, ctx);
84886
+ ctx.apply.pop();
84887
+ ctx.types.pop();
84888
+ ctx.equal.pop();
84889
+ ctx.print.pop();
84890
+ return ret;
84891
+ } else if (t2.type === "Recursive") {
84892
+ return (base, patch) => {
84893
+ if (patch.type === "unchanged") {
84894
+ return base;
84895
+ } else if (patch.type === "replace") {
84896
+ return patch.value.after;
84897
+ } else {
84898
+ throw new Error(`Invalid patch type for recursive type: ${patch.type}`);
84899
+ }
84900
+ };
84901
+ } else if (t2.type === "Function" || t2.type === "AsyncFunction") {
84902
+ return (base, patch) => {
84903
+ if (patch.type === "unchanged") {
84904
+ return base;
84905
+ } else if (patch.type === "replace") {
84906
+ return patch.value.after;
84907
+ } else {
84908
+ throw new Error(`Invalid patch type for function: ${patch.type}`);
84909
+ }
84910
+ };
84911
+ } else {
84912
+ throw new Error(`Unhandled type in applyFor: ${t2.type}`);
84913
+ }
84914
+ }
84915
+ function invertFor(type, ctx = { invert: [], types: [], equal: [] }) {
84916
+ const t2 = isVariant(type) ? type : toEastTypeValue(type);
84917
+ if (t2.type === "Never") {
84918
+ return (_patch) => {
84919
+ throw new Error("Cannot invert patches for type Never");
84920
+ };
84921
+ } else if (t2.type === "Null" || t2.type === "Boolean" || t2.type === "Integer" || t2.type === "Float" || t2.type === "String" || t2.type === "DateTime" || t2.type === "Blob") {
84922
+ return (patch) => {
84923
+ if (patch.type === "unchanged") {
84924
+ return patch;
84925
+ } else if (patch.type === "replace") {
84926
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
84927
+ } else {
84928
+ throw new Error(`Invalid patch type for primitive inversion: ${patch.type}`);
84929
+ }
84930
+ };
84931
+ } else if (t2.type === "Array") {
84932
+ let elementInvert;
84933
+ const ret = (patch) => {
84934
+ if (patch.type === "unchanged") {
84935
+ return patch;
84936
+ } else if (patch.type === "replace") {
84937
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
84938
+ } else if (patch.type === "patch") {
84939
+ const operations = patch.value;
84940
+ const inverted = [];
84941
+ for (let i2 = operations.length - 1; i2 >= 0; i2--) {
84942
+ const op = operations[i2];
84943
+ if (op.operation.type === "delete") {
84944
+ inverted.push({
84945
+ key: op.key,
84946
+ offset: 0n,
84947
+ operation: variant("insert", op.operation.value)
84948
+ });
84949
+ } else if (op.operation.type === "insert") {
84950
+ inverted.push({
84951
+ key: op.key,
84952
+ offset: 0n,
84953
+ operation: variant("delete", op.operation.value)
84954
+ });
84955
+ } else if (op.operation.type === "update") {
84956
+ inverted.push({
84957
+ key: op.key,
84958
+ offset: 0n,
84959
+ operation: variant("update", elementInvert(op.operation.value))
84960
+ });
84961
+ }
84962
+ }
84963
+ return variant("patch", inverted);
84964
+ } else {
84965
+ throw new Error(`Invalid patch type for array inversion: ${patch.type}`);
84966
+ }
84967
+ };
84968
+ const arrayEqual = equalFor(t2, ctx.equal);
84969
+ ctx.invert.push(ret);
84970
+ ctx.types.push(t2);
84971
+ ctx.equal.push(arrayEqual);
84972
+ elementInvert = invertFor(t2.value, ctx);
84973
+ ctx.invert.pop();
84974
+ ctx.types.pop();
84975
+ ctx.equal.pop();
84976
+ return ret;
84977
+ } else if (t2.type === "Set") {
84978
+ const keyCompare = compareFor(t2.value);
84979
+ return (patch) => {
84980
+ if (patch.type === "unchanged") {
84981
+ return patch;
84982
+ } else if (patch.type === "replace") {
84983
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
84984
+ } else if (patch.type === "patch") {
84985
+ const operations = patch.value;
84986
+ const inverted = new SortedMap(void 0, keyCompare);
84987
+ for (const [key, op] of operations) {
84988
+ if (op.type === "delete") {
84989
+ inverted.set(key, variant("insert", null));
84990
+ } else if (op.type === "insert") {
84991
+ inverted.set(key, variant("delete", null));
84992
+ }
84993
+ }
84994
+ return variant("patch", inverted);
84995
+ } else {
84996
+ throw new Error(`Invalid patch type for set inversion: ${patch.type}`);
84997
+ }
84998
+ };
84999
+ } else if (t2.type === "Dict") {
85000
+ let valueInvert;
85001
+ const keyCompare = compareFor(t2.value.key);
85002
+ const ret = (patch) => {
85003
+ if (patch.type === "unchanged") {
85004
+ return patch;
85005
+ } else if (patch.type === "replace") {
85006
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
85007
+ } else if (patch.type === "patch") {
85008
+ const operations = patch.value;
85009
+ const inverted = new SortedMap(void 0, keyCompare);
85010
+ for (const [key, op] of operations) {
85011
+ if (op.type === "delete") {
85012
+ inverted.set(key, variant("insert", op.value));
85013
+ } else if (op.type === "insert") {
85014
+ inverted.set(key, variant("delete", op.value));
85015
+ } else if (op.type === "update") {
85016
+ inverted.set(key, variant("update", valueInvert(op.value)));
85017
+ }
85018
+ }
85019
+ return variant("patch", inverted);
85020
+ } else {
85021
+ throw new Error(`Invalid patch type for dict inversion: ${patch.type}`);
85022
+ }
85023
+ };
85024
+ const dictEqual = equalFor(t2, ctx.equal);
85025
+ ctx.invert.push(ret);
85026
+ ctx.types.push(t2);
85027
+ ctx.equal.push(dictEqual);
85028
+ valueInvert = invertFor(t2.value.value, ctx);
85029
+ ctx.invert.pop();
85030
+ ctx.types.pop();
85031
+ ctx.equal.pop();
85032
+ return ret;
85033
+ } else if (t2.type === "Struct") {
85034
+ const fieldInverts = {};
85035
+ const ret = (patch) => {
85036
+ if (patch.type === "unchanged") {
85037
+ return patch;
85038
+ } else if (patch.type === "replace") {
85039
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
85040
+ } else if (patch.type === "patch") {
85041
+ const result = {};
85042
+ let allUnchanged = true;
85043
+ for (const { name } of t2.value) {
85044
+ const inverted = fieldInverts[name](patch.value[name]);
85045
+ result[name] = inverted;
85046
+ if (inverted.type !== "unchanged") {
85047
+ allUnchanged = false;
85048
+ }
85049
+ }
85050
+ if (allUnchanged) {
85051
+ return variant("unchanged", null);
85052
+ }
85053
+ return variant("patch", result);
85054
+ } else {
85055
+ throw new Error(`Invalid patch type for struct inversion: ${patch.type}`);
85056
+ }
85057
+ };
85058
+ const structEqual = equalFor(t2, ctx.equal);
85059
+ ctx.invert.push(ret);
85060
+ ctx.types.push(t2);
85061
+ ctx.equal.push(structEqual);
85062
+ for (const { name, type: fieldType } of t2.value) {
85063
+ fieldInverts[name] = invertFor(fieldType, ctx);
85064
+ }
85065
+ ctx.invert.pop();
85066
+ ctx.types.pop();
85067
+ ctx.equal.pop();
85068
+ return ret;
85069
+ } else if (t2.type === "Variant") {
85070
+ const caseInverts = {};
85071
+ const ret = (patch) => {
85072
+ if (patch.type === "unchanged") {
85073
+ return patch;
85074
+ } else if (patch.type === "replace") {
85075
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
85076
+ } else if (patch.type === "patch") {
85077
+ const caseName = patch.value.type;
85078
+ const inverted = caseInverts[caseName](patch.value.value);
85079
+ if (inverted.type === "unchanged") {
85080
+ return variant("unchanged", null);
85081
+ }
85082
+ return variant("patch", variant(caseName, inverted));
85083
+ } else {
85084
+ throw new Error(`Invalid patch type for variant inversion: ${patch.type}`);
85085
+ }
85086
+ };
85087
+ const variantEqual = equalFor(t2, ctx.equal);
85088
+ ctx.invert.push(ret);
85089
+ ctx.types.push(t2);
85090
+ ctx.equal.push(variantEqual);
85091
+ for (const { name, type: caseType } of t2.value) {
85092
+ caseInverts[name] = invertFor(caseType, ctx);
85093
+ }
85094
+ ctx.invert.pop();
85095
+ ctx.types.pop();
85096
+ ctx.equal.pop();
85097
+ return ret;
85098
+ } else if (t2.type === "Ref") {
85099
+ let innerInvert;
85100
+ const ret = (patch) => {
85101
+ if (patch.type === "unchanged") {
85102
+ return patch;
85103
+ } else if (patch.type === "replace") {
85104
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
85105
+ } else if (patch.type === "patch") {
85106
+ const inverted = innerInvert(patch.value);
85107
+ if (inverted.type === "unchanged") {
85108
+ return variant("unchanged", null);
85109
+ }
85110
+ return variant("patch", inverted);
85111
+ } else {
85112
+ throw new Error(`Invalid patch type for ref inversion: ${patch.type}`);
85113
+ }
85114
+ };
85115
+ const refEqual = equalFor(t2, ctx.equal);
85116
+ ctx.invert.push(ret);
85117
+ ctx.types.push(t2);
85118
+ ctx.equal.push(refEqual);
85119
+ innerInvert = invertFor(t2.value, ctx);
85120
+ ctx.invert.pop();
85121
+ ctx.types.pop();
85122
+ ctx.equal.pop();
85123
+ return ret;
85124
+ } else if (t2.type === "Recursive") {
85125
+ return (patch) => {
85126
+ if (patch.type === "unchanged") {
85127
+ return patch;
85128
+ } else if (patch.type === "replace") {
85129
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
85130
+ } else {
85131
+ throw new Error(`Invalid patch type for recursive type inversion: ${patch.type}`);
85132
+ }
85133
+ };
85134
+ } else if (t2.type === "Function" || t2.type === "AsyncFunction") {
85135
+ return (patch) => {
85136
+ if (patch.type === "unchanged") {
85137
+ return patch;
85138
+ } else if (patch.type === "replace") {
85139
+ return variant("replace", { before: patch.value.after, after: patch.value.before });
85140
+ } else {
85141
+ throw new Error(`Invalid patch type for function inversion: ${patch.type}`);
85142
+ }
85143
+ };
85144
+ } else {
85145
+ throw new Error(`Unhandled type in invertFor: ${t2.type}`);
85146
+ }
85147
+ }
85148
+ function composeFor(type, ctx = { compose: [], apply: [], invert: [], types: [], equal: [], print: [] }) {
85149
+ const t2 = isVariant(type) ? type : toEastTypeValue(type);
85150
+ if (t2.type === "Never") {
85151
+ return (_first, _second) => {
85152
+ throw new Error("Cannot compose patches for type Never");
85153
+ };
85154
+ } else if (t2.type === "Null" || t2.type === "Boolean" || t2.type === "Integer" || t2.type === "Float" || t2.type === "String" || t2.type === "DateTime" || t2.type === "Blob") {
85155
+ return (first, second2) => {
85156
+ if (first.type === "unchanged") {
85157
+ return second2;
85158
+ } else if (second2.type === "unchanged") {
85159
+ return first;
85160
+ } else if (first.type === "replace" && second2.type === "replace") {
85161
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85162
+ } else {
85163
+ throw new Error("Invalid patch composition for primitive");
85164
+ }
85165
+ };
85166
+ } else if (t2.type === "Array") {
85167
+ let applyRet;
85168
+ let invertRet;
85169
+ const composeRet = (first, second2) => {
85170
+ if (first.type === "unchanged") {
85171
+ return second2;
85172
+ } else if (second2.type === "unchanged") {
85173
+ return first;
85174
+ } else if (first.type === "replace" && second2.type === "replace") {
85175
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85176
+ } else if (first.type === "replace" && second2.type === "patch") {
85177
+ const afterSecond = applyRet(first.value.after, second2);
85178
+ return variant("replace", { before: first.value.before, after: afterSecond });
85179
+ } else if (first.type === "patch" && second2.type === "replace") {
85180
+ const invertedFirst = invertRet(first);
85181
+ const originalBefore = applyRet(second2.value.before, invertedFirst);
85182
+ return variant("replace", { before: originalBefore, after: second2.value.after });
85183
+ } else {
85184
+ const p1Ops = first.value;
85185
+ const p2Ops = second2.value;
85186
+ const result = [...p1Ops, ...p2Ops];
85187
+ if (result.length === 0) {
85188
+ return variant("unchanged", null);
85189
+ }
85190
+ return variant("patch", result);
85191
+ }
85192
+ };
85193
+ const arrayEqual = equalFor(t2, ctx.equal);
85194
+ const arrayPrint = printFor(t2, ctx.print);
85195
+ ctx.compose.push(composeRet);
85196
+ ctx.types.push(t2);
85197
+ ctx.equal.push(arrayEqual);
85198
+ ctx.print.push(arrayPrint);
85199
+ applyRet = applyFor(t2, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85200
+ invertRet = invertFor(t2, { invert: ctx.invert, types: ctx.types, equal: ctx.equal });
85201
+ ctx.apply.push(applyRet);
85202
+ ctx.invert.push(invertRet);
85203
+ composeFor(t2.value, ctx);
85204
+ ctx.compose.pop();
85205
+ ctx.apply.pop();
85206
+ ctx.invert.pop();
85207
+ ctx.types.pop();
85208
+ ctx.equal.pop();
85209
+ ctx.print.pop();
85210
+ return composeRet;
85211
+ } else if (t2.type === "Set") {
85212
+ const keyPrint = printFor(t2.value);
85213
+ const keyCompare = compareFor(t2.value);
85214
+ const apply = applyFor(t2, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85215
+ const invert = invertFor(t2, { invert: ctx.invert, types: ctx.types, equal: ctx.equal });
85216
+ return (first, second2) => {
85217
+ if (first.type === "unchanged") {
85218
+ return second2;
85219
+ } else if (second2.type === "unchanged") {
85220
+ return first;
85221
+ } else if (first.type === "replace" && second2.type === "replace") {
85222
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85223
+ } else if (first.type === "patch" && second2.type === "patch") {
85224
+ const result = new SortedMap(void 0, keyCompare);
85225
+ for (const [key, op] of first.value) {
85226
+ result.set(key, op);
85227
+ }
85228
+ for (const [key, op] of second2.value) {
85229
+ if (result.has(key)) {
85230
+ const firstOp = result.get(key);
85231
+ if (firstOp.type === "insert" && op.type === "delete") {
85232
+ result.delete(key);
85233
+ } else if (firstOp.type === "delete" && op.type === "insert") {
85234
+ result.delete(key);
85235
+ } else {
85236
+ throw new ConflictError(`Cannot compose patches - conflicting operations on key ${keyPrint(key)}`);
85237
+ }
85238
+ } else {
85239
+ result.set(key, op);
85240
+ }
85241
+ }
85242
+ if (result.size === 0) {
85243
+ return variant("unchanged", null);
85244
+ }
85245
+ return variant("patch", result);
85246
+ } else {
85247
+ if (first.type === "replace") {
85248
+ const afterSecond = apply(first.value.after, second2);
85249
+ return variant("replace", { before: first.value.before, after: afterSecond });
85250
+ } else {
85251
+ const invertedFirst = invert(first);
85252
+ const originalBefore = apply(second2.value.before, invertedFirst);
85253
+ return variant("replace", { before: originalBefore, after: second2.value.after });
85254
+ }
85255
+ }
85256
+ };
85257
+ } else if (t2.type === "Dict") {
85258
+ let valueCompose;
85259
+ let valueApply;
85260
+ let applyRet;
85261
+ let invertRet;
85262
+ const keyPrint = printFor(t2.value.key);
85263
+ const keyCompare = compareFor(t2.value.key);
85264
+ const composeRet = (first, second2) => {
85265
+ if (first.type === "unchanged") {
85266
+ return second2;
85267
+ } else if (second2.type === "unchanged") {
85268
+ return first;
85269
+ } else if (first.type === "replace" && second2.type === "replace") {
85270
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85271
+ } else if (first.type === "patch" && second2.type === "patch") {
85272
+ const result = new SortedMap(void 0, keyCompare);
85273
+ for (const [key, op] of first.value) {
85274
+ result.set(key, op);
85275
+ }
85276
+ for (const [key, op] of second2.value) {
85277
+ if (result.has(key)) {
85278
+ const firstOp = result.get(key);
85279
+ if (firstOp.type === "insert" && op.type === "delete") {
85280
+ result.delete(key);
85281
+ } else if (firstOp.type === "insert" && op.type === "update") {
85282
+ const newValue = valueApply(firstOp.value, op.value);
85283
+ result.set(key, variant("insert", newValue));
85284
+ } else if (firstOp.type === "delete" && op.type === "insert") {
85285
+ result.set(key, variant("update", variant("replace", { before: firstOp.value, after: op.value })));
85286
+ } else if (firstOp.type === "update" && op.type === "delete") {
85287
+ throw new ConflictError(`Cannot compose patches - update then delete on key ${keyPrint(key)}`);
85288
+ } else if (firstOp.type === "update" && op.type === "update") {
85289
+ const composed = valueCompose(firstOp.value, op.value);
85290
+ result.set(key, variant("update", composed));
85291
+ } else {
85292
+ throw new ConflictError(`Cannot compose patches - conflicting operations on key ${keyPrint(key)}`);
85293
+ }
85294
+ } else {
85295
+ result.set(key, op);
85296
+ }
85297
+ }
85298
+ if (result.size === 0) {
85299
+ return variant("unchanged", null);
85300
+ }
85301
+ return variant("patch", result);
85302
+ } else {
85303
+ if (first.type === "replace") {
85304
+ const afterSecond = applyRet(first.value.after, second2);
85305
+ return variant("replace", { before: first.value.before, after: afterSecond });
85306
+ } else {
85307
+ const invertedFirst = invertRet(first);
85308
+ const originalBefore = applyRet(second2.value.before, invertedFirst);
85309
+ return variant("replace", { before: originalBefore, after: second2.value.after });
85310
+ }
85311
+ }
85312
+ };
85313
+ const dictPrint = printFor(t2, ctx.print);
85314
+ const dictEqual = equalFor(t2, ctx.equal);
85315
+ ctx.compose.push(composeRet);
85316
+ ctx.types.push(t2);
85317
+ ctx.equal.push(dictEqual);
85318
+ ctx.print.push(dictPrint);
85319
+ applyRet = applyFor(t2, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85320
+ invertRet = invertFor(t2, { invert: ctx.invert, types: ctx.types, equal: ctx.equal });
85321
+ ctx.apply.push(applyRet);
85322
+ ctx.invert.push(invertRet);
85323
+ valueCompose = composeFor(t2.value.value, ctx);
85324
+ valueApply = applyFor(t2.value.value, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85325
+ ctx.compose.pop();
85326
+ ctx.apply.pop();
85327
+ ctx.invert.pop();
85328
+ ctx.types.pop();
85329
+ ctx.equal.pop();
85330
+ ctx.print.pop();
85331
+ return composeRet;
85332
+ } else if (t2.type === "Struct") {
85333
+ const fieldComposes = {};
85334
+ let applyRet;
85335
+ let invertRet;
85336
+ const composeRet = (first, second2) => {
85337
+ if (first.type === "unchanged") {
85338
+ return second2;
85339
+ } else if (second2.type === "unchanged") {
85340
+ return first;
85341
+ } else if (first.type === "replace" && second2.type === "replace") {
85342
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85343
+ } else if (first.type === "patch" && second2.type === "patch") {
85344
+ const result = {};
85345
+ let allUnchanged = true;
85346
+ for (const { name } of t2.value) {
85347
+ const composed = fieldComposes[name](first.value[name], second2.value[name]);
85348
+ result[name] = composed;
85349
+ if (composed.type !== "unchanged") {
85350
+ allUnchanged = false;
85351
+ }
85352
+ }
85353
+ if (allUnchanged) {
85354
+ return variant("unchanged", null);
85355
+ }
85356
+ return variant("patch", result);
85357
+ } else {
85358
+ if (first.type === "replace") {
85359
+ const afterSecond = applyRet(first.value.after, second2);
85360
+ return variant("replace", { before: first.value.before, after: afterSecond });
85361
+ } else {
85362
+ const invertedFirst = invertRet(first);
85363
+ const originalBefore = applyRet(second2.value.before, invertedFirst);
85364
+ return variant("replace", { before: originalBefore, after: second2.value.after });
85365
+ }
85366
+ }
85367
+ };
85368
+ const structEqual = equalFor(t2, ctx.equal);
85369
+ const structPrint = printFor(t2, ctx.print);
85370
+ ctx.compose.push(composeRet);
85371
+ ctx.types.push(t2);
85372
+ ctx.equal.push(structEqual);
85373
+ ctx.print.push(structPrint);
85374
+ applyRet = applyFor(t2, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85375
+ invertRet = invertFor(t2, { invert: ctx.invert, types: ctx.types, equal: ctx.equal });
85376
+ ctx.apply.push(applyRet);
85377
+ ctx.invert.push(invertRet);
85378
+ for (const { name, type: fieldType } of t2.value) {
85379
+ fieldComposes[name] = composeFor(fieldType, ctx);
85380
+ }
85381
+ ctx.compose.pop();
85382
+ ctx.apply.pop();
85383
+ ctx.invert.pop();
85384
+ ctx.types.pop();
85385
+ ctx.equal.pop();
85386
+ ctx.print.pop();
85387
+ return composeRet;
85388
+ } else if (t2.type === "Variant") {
85389
+ const caseComposes = {};
85390
+ let applyRet;
85391
+ let invertRet;
85392
+ const composeRet = (first, second2) => {
85393
+ if (first.type === "unchanged") {
85394
+ return second2;
85395
+ } else if (second2.type === "unchanged") {
85396
+ return first;
85397
+ } else if (first.type === "replace" && second2.type === "replace") {
85398
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85399
+ } else if (first.type === "patch" && second2.type === "patch") {
85400
+ if (first.value.type !== second2.value.type) {
85401
+ throw new ConflictError(`Cannot compose variant patches for different cases: ${first.value.type} and ${second2.value.type}`);
85402
+ }
85403
+ const caseName = first.value.type;
85404
+ const composed = caseComposes[caseName](first.value.value, second2.value.value);
85405
+ if (composed.type === "unchanged") {
85406
+ return variant("unchanged", null);
85407
+ }
85408
+ return variant("patch", variant(caseName, composed));
85409
+ } else {
85410
+ if (first.type === "replace") {
85411
+ const afterSecond = applyRet(first.value.after, second2);
85412
+ return variant("replace", { before: first.value.before, after: afterSecond });
85413
+ } else {
85414
+ const invertedFirst = invertRet(first);
85415
+ const originalBefore = applyRet(second2.value.before, invertedFirst);
85416
+ return variant("replace", { before: originalBefore, after: second2.value.after });
85417
+ }
85418
+ }
85419
+ };
85420
+ const variantEqual = equalFor(t2, ctx.equal);
85421
+ const variantPrint = printFor(t2, ctx.print);
85422
+ ctx.compose.push(composeRet);
85423
+ ctx.types.push(t2);
85424
+ ctx.equal.push(variantEqual);
85425
+ ctx.print.push(variantPrint);
85426
+ applyRet = applyFor(t2, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85427
+ invertRet = invertFor(t2, { invert: ctx.invert, types: ctx.types, equal: ctx.equal });
85428
+ ctx.apply.push(applyRet);
85429
+ ctx.invert.push(invertRet);
85430
+ for (const { name, type: caseType } of t2.value) {
85431
+ caseComposes[name] = composeFor(caseType, ctx);
85432
+ }
85433
+ ctx.compose.pop();
85434
+ ctx.apply.pop();
85435
+ ctx.invert.pop();
85436
+ ctx.types.pop();
85437
+ ctx.equal.pop();
85438
+ ctx.print.pop();
85439
+ return composeRet;
85440
+ } else if (t2.type === "Ref") {
85441
+ let innerCompose;
85442
+ let applyRet;
85443
+ let invertRet;
85444
+ const composeRet = (first, second2) => {
85445
+ if (first.type === "unchanged") {
85446
+ return second2;
85447
+ } else if (second2.type === "unchanged") {
85448
+ return first;
85449
+ } else if (first.type === "replace" && second2.type === "replace") {
85450
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85451
+ } else if (first.type === "patch" && second2.type === "patch") {
85452
+ const composed = innerCompose(first.value, second2.value);
85453
+ if (composed.type === "unchanged") {
85454
+ return variant("unchanged", null);
85455
+ }
85456
+ return variant("patch", composed);
85457
+ } else {
85458
+ if (first.type === "replace") {
85459
+ const afterSecond = applyRet(first.value.after, second2);
85460
+ return variant("replace", { before: first.value.before, after: afterSecond });
85461
+ } else {
85462
+ const invertedFirst = invertRet(first);
85463
+ const originalBefore = applyRet(second2.value.before, invertedFirst);
85464
+ return variant("replace", { before: originalBefore, after: second2.value.after });
85465
+ }
85466
+ }
85467
+ };
85468
+ const refPrint = printFor(t2, ctx.print);
85469
+ const refEqual = equalFor(t2, ctx.equal);
85470
+ ctx.compose.push(composeRet);
85471
+ ctx.types.push(t2);
85472
+ ctx.equal.push(refEqual);
85473
+ ctx.print.push(refPrint);
85474
+ applyRet = applyFor(t2, { apply: ctx.apply, types: ctx.types, equal: ctx.equal, print: ctx.print });
85475
+ invertRet = invertFor(t2, { invert: ctx.invert, types: ctx.types, equal: ctx.equal });
85476
+ ctx.apply.push(applyRet);
85477
+ ctx.invert.push(invertRet);
85478
+ innerCompose = composeFor(t2.value, ctx);
85479
+ ctx.compose.pop();
85480
+ ctx.apply.pop();
85481
+ ctx.invert.pop();
85482
+ ctx.types.pop();
85483
+ ctx.equal.pop();
85484
+ ctx.print.pop();
85485
+ return composeRet;
85486
+ } else if (t2.type === "Recursive") {
85487
+ return (first, second2) => {
85488
+ if (first.type === "unchanged") {
85489
+ return second2;
85490
+ } else if (second2.type === "unchanged") {
85491
+ return first;
85492
+ } else if (first.type === "replace" && second2.type === "replace") {
85493
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85494
+ } else {
85495
+ throw new Error(`Invalid patch types for recursive type composition: ${first.type}, ${second2.type}`);
85496
+ }
85497
+ };
85498
+ } else if (t2.type === "Function" || t2.type === "AsyncFunction") {
85499
+ return (first, second2) => {
85500
+ if (first.type === "unchanged") {
85501
+ return second2;
85502
+ } else if (second2.type === "unchanged") {
85503
+ return first;
85504
+ } else {
85505
+ return variant("replace", { before: first.value.before, after: second2.value.after });
85506
+ }
85507
+ };
85508
+ } else {
85509
+ throw new Error(`Unhandled type in composeFor: ${t2.type}`);
85510
+ }
85511
+ }
85512
+ function PatchType(type, ctx) {
85513
+ const context = ctx ?? /* @__PURE__ */ new Map();
85514
+ const cached = context.get(type);
85515
+ if (cached !== void 0) {
85516
+ return cached;
85517
+ }
85518
+ const t2 = type;
85519
+ if (t2.type === "Never" || t2.type === "Null" || t2.type === "Boolean" || t2.type === "Integer" || t2.type === "Float" || t2.type === "String" || t2.type === "DateTime" || t2.type === "Blob") {
85520
+ return VariantType({
85521
+ unchanged: NullType,
85522
+ replace: StructType({ before: type, after: type })
85523
+ });
85524
+ } else if (t2.type === "Array") {
85525
+ const elementType = t2.value;
85526
+ const elementPatchType = PatchType(elementType, context);
85527
+ const operationType = VariantType({
85528
+ delete: elementType,
85529
+ insert: elementType,
85530
+ update: elementPatchType
85531
+ });
85532
+ const entryType = StructType({
85533
+ key: IntegerType,
85534
+ offset: IntegerType,
85535
+ operation: operationType
85536
+ });
85537
+ return VariantType({
85538
+ unchanged: NullType,
85539
+ replace: StructType({ before: type, after: type }),
85540
+ patch: ArrayType(entryType)
85541
+ });
85542
+ } else if (t2.type === "Set") {
85543
+ const keyType = t2.key;
85544
+ const operationType = VariantType({
85545
+ delete: NullType,
85546
+ insert: NullType
85547
+ });
85548
+ return VariantType({
85549
+ unchanged: NullType,
85550
+ replace: StructType({ before: type, after: type }),
85551
+ patch: DictType(keyType, operationType)
85552
+ });
85553
+ } else if (t2.type === "Dict") {
85554
+ const keyType = t2.key;
85555
+ const valueType = t2.value;
85556
+ const valuePatchType = PatchType(valueType, context);
85557
+ const operationType = VariantType({
85558
+ delete: valueType,
85559
+ insert: valueType,
85560
+ update: valuePatchType
85561
+ });
85562
+ return VariantType({
85563
+ unchanged: NullType,
85564
+ replace: StructType({ before: type, after: type }),
85565
+ patch: DictType(keyType, operationType)
85566
+ });
85567
+ } else if (t2.type === "Struct") {
85568
+ const fieldTypes = t2.fields;
85569
+ const patchFields = {};
85570
+ for (const [name, fieldType] of Object.entries(fieldTypes)) {
85571
+ patchFields[name] = PatchType(fieldType, context);
85572
+ }
85573
+ return VariantType({
85574
+ unchanged: NullType,
85575
+ replace: StructType({ before: type, after: type }),
85576
+ patch: StructType(patchFields)
85577
+ });
85578
+ } else if (t2.type === "Variant") {
85579
+ const caseTypes = t2.cases;
85580
+ const patchCases = {};
85581
+ for (const [name, caseType] of Object.entries(caseTypes)) {
85582
+ patchCases[name] = PatchType(caseType, context);
85583
+ }
85584
+ return VariantType({
85585
+ unchanged: NullType,
85586
+ replace: StructType({ before: type, after: type }),
85587
+ patch: VariantType(patchCases)
85588
+ });
85589
+ } else if (t2.type === "Ref") {
85590
+ const innerType = t2.value;
85591
+ const innerPatchType = PatchType(innerType, context);
85592
+ return VariantType({
85593
+ unchanged: NullType,
85594
+ replace: StructType({ before: type, after: type }),
85595
+ patch: innerPatchType
85596
+ });
85597
+ } else if (t2.type === "Recursive") {
85598
+ const cached2 = context.get(type);
85599
+ if (cached2 !== void 0) {
85600
+ return cached2;
85601
+ }
85602
+ const replaceOnlyType = VariantType({
85603
+ unchanged: NullType,
85604
+ replace: StructType({ before: type, after: type })
85605
+ });
85606
+ context.set(type, replaceOnlyType);
85607
+ return PatchType(t2.node, context);
85608
+ } else if (t2.type === "Function" || t2.type === "AsyncFunction") {
85609
+ return VariantType({
85610
+ unchanged: NullType,
85611
+ replace: StructType({ before: type, after: type })
85612
+ });
85613
+ } else {
85614
+ throw new Error(`Unhandled type in PatchType: ${t2.type}`);
85615
+ }
85616
+ }
84050
85617
  class EastError extends Error {
84051
85618
  constructor(message, options) {
84052
85619
  super(message, options.cause === void 0 ? void 0 : { cause: options.cause });
@@ -88967,6 +90534,34 @@ const builtin_evaluators = {
88967
90534
  LessEqual: (_location, _platformDef, T2) => lessEqualFor(T2),
88968
90535
  Greater: (_location, _platformDef, T2) => greaterFor(T2),
88969
90536
  GreaterEqual: (_location, _platformDef, T2) => greaterEqualFor(T2),
90537
+ Diff: (_location, _platformDef, T2) => diffFor(T2),
90538
+ ApplyPatch: (location, _platformDef, T2) => {
90539
+ const apply = applyFor(T2);
90540
+ return (base, patch) => {
90541
+ try {
90542
+ return apply(base, patch);
90543
+ } catch (e3) {
90544
+ if (e3 instanceof ConflictError) {
90545
+ throw new EastError(e3.message, { location });
90546
+ }
90547
+ throw e3;
90548
+ }
90549
+ };
90550
+ },
90551
+ ComposePatch: (location, _platformDef, T2) => {
90552
+ const compose3 = composeFor(T2);
90553
+ return (first, second2) => {
90554
+ try {
90555
+ return compose3(first, second2);
90556
+ } catch (e3) {
90557
+ if (e3 instanceof ConflictError) {
90558
+ throw new EastError(e3.message, { location });
90559
+ }
90560
+ throw e3;
90561
+ }
90562
+ };
90563
+ },
90564
+ InvertPatch: (_location, _platformDef, T2) => invertFor(T2),
88970
90565
  BooleanNot: (_location) => (x2) => !x2,
88971
90566
  BooleanOr: (_location) => (x2, y2) => x2 || y2,
88972
90567
  BooleanAnd: (_location) => (x2, y2) => x2 && y2,
@@ -91716,6 +93311,20 @@ function greaterEqual(left, right) {
91716
93311
  arguments: [Expr.ast(left), rightAst]
91717
93312
  });
91718
93313
  }
93314
+ const equals = equal;
93315
+ const eq = equal;
93316
+ const notEquals = notEqual;
93317
+ const ne = notEqual;
93318
+ const lessThan = less;
93319
+ const lt = less;
93320
+ const lessThanOrEqual = lessEqual;
93321
+ const lte = lessEqual;
93322
+ const le = lessEqual;
93323
+ const greaterThan = greater;
93324
+ const gt = greater;
93325
+ const greaterThanOrEqual = greaterEqual;
93326
+ const gte = greaterEqual;
93327
+ const ge = greaterEqual;
91719
93328
  function is(left, right) {
91720
93329
  const rightAst = valueOrExprToAstTyped(right, Expr.type(left));
91721
93330
  return fromAst({
@@ -91738,6 +93347,61 @@ function print(value) {
91738
93347
  arguments: [valueAst]
91739
93348
  });
91740
93349
  }
93350
+ function diff(before, after) {
93351
+ const beforeAst = Expr.ast(before);
93352
+ const afterAst = Expr.ast(after);
93353
+ const valueType = beforeAst.type;
93354
+ const patchType = PatchType(valueType);
93355
+ return fromAst({
93356
+ ast_type: "Builtin",
93357
+ type: patchType,
93358
+ location: get_location(2),
93359
+ builtin: "Diff",
93360
+ type_parameters: [valueType, patchType],
93361
+ arguments: [beforeAst, afterAst]
93362
+ });
93363
+ }
93364
+ function applyPatch(value, patch) {
93365
+ const valueAst = Expr.ast(value);
93366
+ const patchAst = Expr.ast(patch);
93367
+ const valueType = valueAst.type;
93368
+ const patchType = patchAst.type;
93369
+ return fromAst({
93370
+ ast_type: "Builtin",
93371
+ type: valueType,
93372
+ location: get_location(2),
93373
+ builtin: "ApplyPatch",
93374
+ type_parameters: [valueType, patchType],
93375
+ arguments: [valueAst, patchAst]
93376
+ });
93377
+ }
93378
+ function composePatch(first, second2, type) {
93379
+ const firstAst = Expr.ast(first);
93380
+ const secondAst = Expr.ast(second2);
93381
+ const valueType = type;
93382
+ const patchType = PatchType(valueType);
93383
+ return fromAst({
93384
+ ast_type: "Builtin",
93385
+ type: patchType,
93386
+ location: get_location(2),
93387
+ builtin: "ComposePatch",
93388
+ type_parameters: [valueType, patchType],
93389
+ arguments: [firstAst, secondAst]
93390
+ });
93391
+ }
93392
+ function invertPatch(patch, type) {
93393
+ const patchAst = Expr.ast(patch);
93394
+ const valueType = type;
93395
+ const patchType = PatchType(valueType);
93396
+ return fromAst({
93397
+ ast_type: "Builtin",
93398
+ type: patchType,
93399
+ location: get_location(2),
93400
+ builtin: "InvertPatch",
93401
+ type_parameters: [valueType, patchType],
93402
+ arguments: [patchAst]
93403
+ });
93404
+ }
91741
93405
  const BlockBuilder = (return_type) => {
91742
93406
  const statements = [];
91743
93407
  const $ = ((expr) => {
@@ -92418,7 +94082,27 @@ Object.assign(Expr, {
92418
94082
  greater,
92419
94083
  greaterEqual,
92420
94084
  is,
92421
- match: matchExpr
94085
+ match: matchExpr,
94086
+ // Comparison aliases
94087
+ equals,
94088
+ eq,
94089
+ notEquals,
94090
+ ne,
94091
+ lessThan,
94092
+ lt,
94093
+ lessThanOrEqual,
94094
+ lte,
94095
+ le,
94096
+ greaterThan,
94097
+ gt,
94098
+ greaterThanOrEqual,
94099
+ gte,
94100
+ ge,
94101
+ // Patch operations
94102
+ diff,
94103
+ applyPatch,
94104
+ composePatch,
94105
+ invertPatch
92422
94106
  });
92423
94107
  const OverlayManagerContext = createContext(null);
92424
94108
  let globalOverlayManager = null;