@kubohiroya/turbowarp-extension-data-editor 0.2.0 → 0.2.2

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 (68) hide show
  1. package/README.md +9 -6
  2. package/dist/build-info.json +3 -3
  3. package/dist/fixed-input-branches.d.ts +12 -0
  4. package/dist/fixed-input-branches.js +180 -0
  5. package/dist/fixed-input-editor.d.ts +39 -0
  6. package/dist/fixed-input-editor.js +538 -0
  7. package/dist/fixed-input-schema.d.ts +15 -0
  8. package/dist/fixed-input-schema.js +21 -0
  9. package/dist/fixed-input-snapshot.d.ts +7 -0
  10. package/dist/fixed-input-snapshot.js +180 -0
  11. package/dist/fixed-input-types.d.ts +138 -0
  12. package/dist/fixed-input-types.js +23 -0
  13. package/dist/fixed-input-view.d.ts +14 -0
  14. package/dist/fixed-input-view.js +168 -0
  15. package/dist/index.d.ts +6 -0
  16. package/dist/index.js +4 -0
  17. package/dist/react.css +168 -0
  18. package/dist/react.d.ts +8 -0
  19. package/dist/react.js +19 -2
  20. package/dist/registry.d.ts +4 -0
  21. package/dist/registry.js +45 -7
  22. package/dist/rjsf-templates.d.ts +11 -0
  23. package/dist/rjsf-templates.js +129 -0
  24. package/dist/rjsf.d.ts +24 -1
  25. package/dist/rjsf.js +35 -3
  26. package/dist/schema.d.ts +2 -0
  27. package/dist/schema.js +1 -0
  28. package/dist/session.d.ts +3 -1
  29. package/dist/session.js +205 -7
  30. package/dist/turbowarp/bridge.d.ts +2 -1
  31. package/dist/turbowarp/bridge.js +37 -5
  32. package/dist/turbowarp/fixed-fields.d.ts +82 -0
  33. package/dist/turbowarp/fixed-fields.js +311 -0
  34. package/dist/turbowarp/fixed-transaction.d.ts +18 -0
  35. package/dist/turbowarp/fixed-transaction.js +460 -0
  36. package/dist/turbowarp/host.d.ts +3 -8
  37. package/dist/turbowarp/host.js +68 -1
  38. package/dist/turbowarp.d.ts +4 -0
  39. package/dist/turbowarp.js +1 -1
  40. package/dist/types.d.ts +5 -0
  41. package/dist/view.js +4 -0
  42. package/dist/webpack4/index.js +22 -5
  43. package/dist/webpack4/metafile.json +592 -299
  44. package/dist/webpack4/react.js +20 -6
  45. package/dist/webpack4/react.js.map +2 -2
  46. package/dist/webpack4/rjsf.js +450 -60
  47. package/dist/webpack4/rjsf.js.map +4 -4
  48. package/dist/webpack4/schema.js +6 -3
  49. package/dist/webpack4/{shared-KQOUMWJ4.js → shared-75QIWLZY.js} +1129 -1244
  50. package/dist/webpack4/{shared-KQOUMWJ4.js.map → shared-75QIWLZY.js.map} +4 -4
  51. package/dist/webpack4/shared-FYUILHED.js +1 -0
  52. package/dist/webpack4/shared-GQLPPRT2.js +210 -0
  53. package/dist/webpack4/shared-GQLPPRT2.js.map +7 -0
  54. package/dist/webpack4/shared-P2JBRZFP.js +2206 -0
  55. package/dist/webpack4/shared-P2JBRZFP.js.map +7 -0
  56. package/dist/webpack4/turbowarp.js +913 -9
  57. package/dist/webpack4/turbowarp.js.map +4 -4
  58. package/docs/api.md +7 -0
  59. package/docs/fixed-input-contracts.md +77 -0
  60. package/docs/package-distribution.md +5 -0
  61. package/docs/stage-2-publication.ja.md +6 -0
  62. package/package.json +2 -1
  63. package/dist/webpack4/shared-CQB3NG2A.js +0 -1
  64. package/dist/webpack4/shared-MRPGBJIH.js +0 -75
  65. package/dist/webpack4/shared-MRPGBJIH.js.map +0 -7
  66. package/dist/webpack4/shared-N6YQE2X6.js +0 -1029
  67. package/dist/webpack4/shared-N6YQE2X6.js.map +0 -7
  68. /package/dist/webpack4/{shared-CQB3NG2A.js.map → shared-FYUILHED.js.map} +0 -0
@@ -0,0 +1,2206 @@
1
+ import {
2
+ BlockEditorConflictError,
3
+ BlockEditorStructureError,
4
+ DEFAULT_DESCENDANT_LIMITS,
5
+ DescendantNodeError,
6
+ EDITOR_API_VERSION,
7
+ supportsDescendantEditing
8
+ } from "./shared-BKTRZUZH.js";
9
+ import {
10
+ compileSchemaBundle,
11
+ isJsonRecord,
12
+ parseFixedNumberInput,
13
+ snapshotJson
14
+ } from "./shared-75QIWLZY.js";
15
+
16
+ // dist/data.js
17
+ function isLiteral(value) {
18
+ return typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
19
+ }
20
+ function cloneJson(value) {
21
+ const ancestors = /* @__PURE__ */ new Set();
22
+ const visit = (item, depth) => {
23
+ if (depth > 100)
24
+ throw new TypeError("JSON data exceeds the maximum depth.");
25
+ if (item === null || isLiteral(item))
26
+ return item;
27
+ if (typeof item !== "object")
28
+ throw new TypeError("Expected JSON-compatible data.");
29
+ if (ancestors.has(item))
30
+ throw new TypeError("JSON data must not contain cycles.");
31
+ ancestors.add(item);
32
+ try {
33
+ if (Array.isArray(item))
34
+ return Array.from(item, (child) => visit(child, depth + 1));
35
+ const prototype = Object.getPrototypeOf(item);
36
+ if (prototype !== Object.prototype && prototype !== null) {
37
+ throw new TypeError("JSON objects must be plain objects.");
38
+ }
39
+ if (Object.getOwnPropertySymbols(item).length > 0) {
40
+ throw new TypeError("JSON objects must not have symbol properties.");
41
+ }
42
+ return Object.fromEntries(Object.entries(item).map(([key, child]) => [key, visit(child, depth + 1)]));
43
+ } finally {
44
+ ancestors.delete(item);
45
+ }
46
+ };
47
+ return visit(value, 0);
48
+ }
49
+ function captureReference(reference) {
50
+ if (!reference || typeof reference.targetId !== "string" || !reference.targetId || typeof reference.blockId !== "string" || !reference.blockId) {
51
+ throw new TypeError("A target ID and block ID are required.");
52
+ }
53
+ return Object.freeze({ targetId: reference.targetId, blockId: reference.blockId });
54
+ }
55
+ function captureSnapshot(snapshot, reference) {
56
+ if (snapshot.targetId !== reference.targetId || snapshot.blockId !== reference.blockId || typeof snapshot.opcode !== "string" || !snapshot.opcode || typeof snapshot.revision !== "string" || !snapshot.revision || !snapshot.arguments || typeof snapshot.arguments !== "object" || Array.isArray(snapshot.arguments)) {
57
+ throw new TypeError("The host returned an invalid block snapshot.");
58
+ }
59
+ const entries = Object.entries(snapshot.arguments).map(([name, slot]) => {
60
+ if (!slot || slot.kind !== "reporter" && slot.kind !== "literal" || slot.kind === "literal" && !isLiteral(slot.value)) {
61
+ throw new TypeError(`Invalid argument slot: ${name}`);
62
+ }
63
+ const copy = slot.kind === "reporter" ? Object.freeze({ kind: "reporter" }) : Object.freeze({ kind: "literal", value: slot.value });
64
+ return [name, copy];
65
+ });
66
+ return Object.freeze({
67
+ ...captureReference(reference),
68
+ opcode: snapshot.opcode,
69
+ revision: snapshot.revision,
70
+ arguments: Object.freeze(Object.fromEntries(entries))
71
+ });
72
+ }
73
+ function sameSnapshot(left, right) {
74
+ const names3 = Object.keys(left.arguments);
75
+ return left.targetId === right.targetId && left.blockId === right.blockId && left.opcode === right.opcode && left.revision === right.revision && names3.length === Object.keys(right.arguments).length && names3.every((name) => {
76
+ const a = left.arguments[name];
77
+ const b = Object.hasOwn(right.arguments, name) ? right.arguments[name] : void 0;
78
+ return b !== void 0 && a.kind === b.kind && (a.kind === "reporter" || b.kind === "literal" && a.value === b.value);
79
+ });
80
+ }
81
+
82
+ // dist/fixed-input-branches.js
83
+ var fail = () => {
84
+ throw new TypeError("Invalid fixed branch declaration.");
85
+ };
86
+ var names = (value) => {
87
+ if (!Array.isArray(value) || !value.length || value.some((item) => typeof item !== "string" || !item) || new Set(value).size !== value.length)
88
+ return fail();
89
+ return Object.freeze(value);
90
+ };
91
+ var object = (value) => {
92
+ if (!value || typeof value !== "object" || Array.isArray(value))
93
+ return fail();
94
+ return value;
95
+ };
96
+ var normalize = (value, rule) => {
97
+ var _a, _b;
98
+ const text2 = ((_a = rule.normalize) === null || _a === void 0 ? void 0 : _a.trim) ? value.trim() : value;
99
+ return ((_b = rule.normalize) === null || _b === void 0 ? void 0 : _b.lowercase) ? text2.toLowerCase() : text2;
100
+ };
101
+ function captureFixedBranchRules(value, dependencies2, readOnlyArguments) {
102
+ if (value === void 0)
103
+ return Object.freeze([]);
104
+ const captured = snapshotJson(value);
105
+ if (!Array.isArray(captured))
106
+ return fail();
107
+ const selectors = /* @__PURE__ */ new Set();
108
+ const occupied = /* @__PURE__ */ new Set();
109
+ const result = [];
110
+ for (const item of captured) {
111
+ const rule = object(item);
112
+ if (typeof rule.selector !== "string" || selectors.has(rule.selector))
113
+ return fail();
114
+ const group = dependencies2.find((dependency) => dependency.selector === rule.selector);
115
+ if (!group)
116
+ return fail();
117
+ for (const argument of [group.selector, ...group.dependents]) {
118
+ if (occupied.has(argument) || dependencies2.some((other) => other !== group && (other.selector === argument || other.dependents.includes(argument))))
119
+ return fail();
120
+ occupied.add(argument);
121
+ }
122
+ selectors.add(rule.selector);
123
+ let normalization;
124
+ if (rule.normalize !== void 0) {
125
+ const input = object(rule.normalize);
126
+ if (Object.keys(input).some((key) => key !== "trim" && key !== "lowercase") || Object.values(input).some((value2) => typeof value2 !== "boolean"))
127
+ return fail();
128
+ normalization = Object.freeze({ ...input });
129
+ }
130
+ const shell = { selector: rule.selector, cases: [], ...normalization ? { normalize: normalization } : {} };
131
+ if (!Array.isArray(rule.cases) || !rule.cases.length)
132
+ return fail();
133
+ const values = /* @__PURE__ */ new Set();
134
+ const cases = [];
135
+ for (const input of rule.cases) {
136
+ const branch = object(input);
137
+ const caseValues = names(branch.values);
138
+ const normalized = caseValues.map((value2) => normalize(value2, shell));
139
+ if (normalized.some((value2) => !value2 || values.has(value2)))
140
+ return fail();
141
+ for (const value2 of normalized)
142
+ values.add(value2);
143
+ if (new Set(normalized).size !== normalized.length)
144
+ return fail();
145
+ if (!Array.isArray(branch.activeDependents))
146
+ return fail();
147
+ const active = branch.activeDependents.length ? names(branch.activeDependents) : Object.freeze([]);
148
+ if (active.some((argument) => !group.dependents.includes(argument)))
149
+ return fail();
150
+ const requirements = [];
151
+ if (branch.requireOnSelectorChange !== void 0) {
152
+ if (!Array.isArray(branch.requireOnSelectorChange))
153
+ return fail();
154
+ const required = /* @__PURE__ */ new Set();
155
+ for (const input2 of branch.requireOnSelectorChange) {
156
+ const requirement = object(input2);
157
+ if (typeof requirement.argument !== "string" || !readOnlyArguments.includes(requirement.argument) || required.has(requirement.argument) || requirement.rule !== "literal" && requirement.rule !== "nonempty-string")
158
+ return fail();
159
+ required.add(requirement.argument);
160
+ requirements.push(Object.freeze({ argument: requirement.argument, rule: requirement.rule }));
161
+ }
162
+ }
163
+ cases.push(Object.freeze({
164
+ values: caseValues,
165
+ activeDependents: active,
166
+ ...branch.requireOnSelectorChange === void 0 ? {} : { requireOnSelectorChange: Object.freeze(requirements) }
167
+ }));
168
+ }
169
+ result.push(Object.freeze({ ...shell, cases: Object.freeze(cases) }));
170
+ }
171
+ return Object.freeze(result);
172
+ }
173
+ function resolveFixedBranches(declaration, snapshot, arguments_ = snapshot.arguments) {
174
+ var _a;
175
+ return Object.freeze(((_a = declaration.branchRules) !== null && _a !== void 0 ? _a : []).map((rule) => {
176
+ const metadata = snapshot.fixedContext.inputs[rule.selector];
177
+ const slot = arguments_[rule.selector];
178
+ let status;
179
+ if (!metadata || metadata.source === "missing")
180
+ status = "missing";
181
+ else if (metadata.source === "reporter" || (slot === null || slot === void 0 ? void 0 : slot.kind) === "reporter")
182
+ status = "reporter";
183
+ else if (metadata.source !== "field" && metadata.source !== "shadow" || metadata.role !== "editable")
184
+ status = "unsupported";
185
+ else if ((slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal" || typeof slot.value !== "string")
186
+ status = "unknown";
187
+ else {
188
+ const value = normalize(slot.value, rule);
189
+ const selected = rule.cases.find((branch) => branch.values.some((item) => normalize(item, rule) === value));
190
+ if (selected)
191
+ return Object.freeze({
192
+ selector: rule.selector,
193
+ rule,
194
+ selected,
195
+ status: slot.value === selected.values[0] ? "known" : "alias",
196
+ value: selected.values[0]
197
+ });
198
+ status = "unknown";
199
+ }
200
+ return Object.freeze({ selector: rule.selector, rule, status });
201
+ }));
202
+ }
203
+ var writableLiteral = (snapshot, arguments_, name) => {
204
+ var _a;
205
+ const metadata = snapshot.fixedContext.inputs[name];
206
+ return ((_a = arguments_[name]) === null || _a === void 0 ? void 0 : _a.kind) === "literal" && (metadata === null || metadata === void 0 ? void 0 : metadata.role) === "editable" && (metadata.source === "field" || metadata.source === "shadow");
207
+ };
208
+ var diagnostic = (declaration, code, argument) => Object.freeze({
209
+ message: `Fixed branch validation failed: ${code}; contract=${JSON.stringify(declaration.contractId)}.`,
210
+ path: `/${argument.replace(/~/g, "~0").replace(/\//g, "~1")}`
211
+ });
212
+ function validateFixedBranchChanges(declaration, snapshot, changes) {
213
+ var _a;
214
+ const issues = [];
215
+ const finalArguments = { ...snapshot.arguments };
216
+ const changed = /* @__PURE__ */ new Set();
217
+ const seen = /* @__PURE__ */ new Set();
218
+ for (const change of changes) {
219
+ if (seen.has(change.argument) || !["string", "number", "boolean"].includes(typeof change.value) || !declaration.editableArguments.includes(change.argument) || !writableLiteral(snapshot, snapshot.arguments, change.argument)) {
220
+ issues.push(diagnostic(declaration, "disallowed-change", change.argument));
221
+ continue;
222
+ }
223
+ seen.add(change.argument);
224
+ const original = snapshot.arguments[change.argument];
225
+ if ((original === null || original === void 0 ? void 0 : original.kind) === "literal" && Object.is(original.value, change.value))
226
+ continue;
227
+ changed.add(change.argument);
228
+ Object.defineProperty(finalArguments, change.argument, { value: { kind: "literal", value: change.value }, enumerable: true, configurable: true });
229
+ }
230
+ const resolved = resolveFixedBranches(declaration, snapshot, finalArguments);
231
+ for (const group of declaration.dependencies) {
232
+ if (![group.selector, ...group.dependents].some((name) => changed.has(name)))
233
+ continue;
234
+ const branch = resolved.find((item) => item.selector === group.selector);
235
+ if (!(branch === null || branch === void 0 ? void 0 : branch.selected)) {
236
+ issues.push(diagnostic(declaration, branch ? "selector-unavailable" : "fixed-dependency-validation-unavailable", group.selector));
237
+ continue;
238
+ }
239
+ for (const argument of group.dependents) {
240
+ if (changed.has(argument) && !branch.selected.activeDependents.includes(argument)) {
241
+ issues.push(diagnostic(declaration, "inactive-change", argument));
242
+ }
243
+ }
244
+ if (!changed.has(group.selector))
245
+ continue;
246
+ for (const argument of branch.selected.activeDependents) {
247
+ if (!writableLiteral(snapshot, finalArguments, argument))
248
+ issues.push(diagnostic(declaration, "dependency-unverifiable", argument));
249
+ }
250
+ for (const requirement of (_a = branch.selected.requireOnSelectorChange) !== null && _a !== void 0 ? _a : []) {
251
+ const metadata = snapshot.fixedContext.inputs[requirement.argument];
252
+ const slot = finalArguments[requirement.argument];
253
+ if (!metadata || metadata.role !== "readonly" || metadata.source !== "field" && metadata.source !== "shadow" || (slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal") {
254
+ issues.push(diagnostic(declaration, "dependency-unverifiable", requirement.argument));
255
+ } else if (requirement.rule === "nonempty-string" && (typeof slot.value !== "string" || !slot.value.trim())) {
256
+ issues.push(diagnostic(declaration, "dependency-invalid", requirement.argument));
257
+ }
258
+ }
259
+ }
260
+ return Object.freeze(issues);
261
+ }
262
+
263
+ // dist/fixed-input-snapshot.js
264
+ function fail2() {
265
+ throw new TypeError("Invalid fixed-input contract or snapshot.");
266
+ }
267
+ function own(value, key) {
268
+ if (!value || typeof value !== "object")
269
+ return fail2();
270
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
271
+ if (!descriptor || !("value" in descriptor))
272
+ return fail2();
273
+ return descriptor.value;
274
+ }
275
+ function text(value) {
276
+ if (typeof value !== "string" || value.length === 0)
277
+ return fail2();
278
+ return value;
279
+ }
280
+ function record(value) {
281
+ if (!value || typeof value !== "object" || Array.isArray(value))
282
+ return fail2();
283
+ const prototype = Object.getPrototypeOf(value);
284
+ if (prototype !== Object.prototype && prototype !== null)
285
+ return fail2();
286
+ if (Object.getOwnPropertySymbols(value).length)
287
+ return fail2();
288
+ return value;
289
+ }
290
+ function names2(value) {
291
+ if (!Array.isArray(value))
292
+ return fail2();
293
+ const items = [];
294
+ for (let i = 0; i < value.length; i++)
295
+ items.push(text(own(value, String(i))));
296
+ if (new Set(items).size !== items.length)
297
+ return fail2();
298
+ return Object.freeze(items);
299
+ }
300
+ function dependencies(value, editable) {
301
+ if (!Array.isArray(value))
302
+ return fail2();
303
+ const selectors = /* @__PURE__ */ new Set();
304
+ const result = [];
305
+ for (let i = 0; i < value.length; i++) {
306
+ const item = record(own(value, String(i)));
307
+ const selector = text(own(item, "selector"));
308
+ const dependents = names2(own(item, "dependents"));
309
+ if (!editable.has(selector) || selectors.has(selector) || dependents.length === 0 || dependents.some((name) => name === selector || !editable.has(name)))
310
+ return fail2();
311
+ selectors.add(selector);
312
+ result.push(Object.freeze({ selector, dependents }));
313
+ }
314
+ return Object.freeze(result);
315
+ }
316
+ function captureFixedInputDeclaration(value) {
317
+ const policy = Object.getOwnPropertyDescriptor(value, "branchRules");
318
+ if (policy && !("value" in policy))
319
+ return fail2();
320
+ const captured = record(snapshotJson({
321
+ contractId: own(value, "contractId"),
322
+ contractRevision: own(value, "contractRevision"),
323
+ editableArguments: own(value, "editableArguments"),
324
+ readOnlyArguments: own(value, "readOnlyArguments"),
325
+ dependencies: own(value, "dependencies")
326
+ }));
327
+ const editableArguments = names2(captured.editableArguments);
328
+ const readOnlyArguments = names2(captured.readOnlyArguments);
329
+ const editable = new Set(editableArguments);
330
+ if (editable.size === 0 || readOnlyArguments.some((name) => editable.has(name)))
331
+ return fail2();
332
+ const groups = dependencies(captured.dependencies, editable);
333
+ const branchRules = captureFixedBranchRules(policy === null || policy === void 0 ? void 0 : policy.value, groups, readOnlyArguments);
334
+ return Object.freeze({
335
+ contractId: text(captured.contractId),
336
+ contractRevision: text(captured.contractRevision),
337
+ editableArguments,
338
+ readOnlyArguments,
339
+ dependencies: groups,
340
+ ...(policy === null || policy === void 0 ? void 0 : policy.value) === void 0 ? {} : { branchRules }
341
+ });
342
+ }
343
+ function canonical(value) {
344
+ if (value === null || typeof value !== "object")
345
+ return JSON.stringify(value);
346
+ if (Array.isArray(value))
347
+ return `[${value.map(canonical).join(",")}]`;
348
+ const item = value;
349
+ return `{${Object.keys(item).sort().map((key) => `${JSON.stringify(key)}:${canonical(item[key])}`).join(",")}}`;
350
+ }
351
+ function sameNames(left, right) {
352
+ return left.length === right.length && left.every((name) => right.includes(name));
353
+ }
354
+ function captureFixedSnapshot(value, reference, declaration) {
355
+ record(value);
356
+ const targetId = text(own(value, "targetId"));
357
+ const blockId = text(own(value, "blockId"));
358
+ if (reference && (targetId !== own(reference, "targetId") || blockId !== own(reference, "blockId")))
359
+ return fail2();
360
+ const opcode = text(own(value, "opcode"));
361
+ const revision = text(own(value, "revision"));
362
+ const inputArguments = record(own(value, "arguments"));
363
+ const argumentsCopy = /* @__PURE__ */ Object.create(null);
364
+ for (const name of Object.keys(inputArguments)) {
365
+ const item = record(own(inputArguments, name));
366
+ const kind = own(item, "kind");
367
+ if (kind === "reporter")
368
+ argumentsCopy[name] = Object.freeze({ kind });
369
+ else if (kind === "literal") {
370
+ const raw = own(item, "value");
371
+ if (typeof raw !== "string" && typeof raw !== "number" && typeof raw !== "boolean")
372
+ return fail2();
373
+ argumentsCopy[name] = Object.freeze({ kind, value: raw });
374
+ } else
375
+ return fail2();
376
+ }
377
+ const context = record(snapshotJson(own(value, "fixedContext")));
378
+ const contractId = text(context.contractId);
379
+ const contractRevision = text(context.contractRevision);
380
+ const generation = text(context.generation);
381
+ const readOnlyArguments = names2(context.readOnlyArguments);
382
+ const inputMetadata = record(context.inputs);
383
+ const editable = /* @__PURE__ */ new Set();
384
+ const inputs = /* @__PURE__ */ Object.create(null);
385
+ for (const name of Object.keys(inputMetadata)) {
386
+ const item = record(inputMetadata[name]);
387
+ if (item.role !== "editable" && item.role !== "readonly")
388
+ return fail2();
389
+ const role = item.role;
390
+ if (role === "readonly" !== readOnlyArguments.includes(name))
391
+ return fail2();
392
+ if (role === "editable")
393
+ editable.add(name);
394
+ const source = item.source;
395
+ if (source !== "shadow" && source !== "field" && source !== "reporter" && source !== "unsupported" && source !== "missing")
396
+ return fail2();
397
+ const slot = argumentsCopy[name];
398
+ if ((source === "shadow" || source === "field") && (slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal")
399
+ return fail2();
400
+ if (source === "reporter" && (slot === null || slot === void 0 ? void 0 : slot.kind) !== "reporter")
401
+ return fail2();
402
+ if (source === "missing" && (slot === null || slot === void 0 ? void 0 : slot.kind) === "literal")
403
+ return fail2();
404
+ let field;
405
+ if (source === "shadow" || source === "field") {
406
+ const capturedField = record(item.field);
407
+ field = Object.freeze({ blockId: text(capturedField.blockId), name: text(capturedField.name), type: text(capturedField.type) });
408
+ } else if (item.field !== void 0)
409
+ return fail2();
410
+ if (item.reason !== void 0 && typeof item.reason !== "string")
411
+ return fail2();
412
+ if ((source === "unsupported" || source === "missing") && !item.reason)
413
+ return fail2();
414
+ inputs[name] = Object.freeze({
415
+ role,
416
+ source,
417
+ identity: text(item.identity),
418
+ ...field ? { field } : {},
419
+ ...item.reason === void 0 ? {} : { reason: item.reason }
420
+ });
421
+ }
422
+ if (readOnlyArguments.some((name) => !Object.hasOwn(inputs, name)))
423
+ return fail2();
424
+ const capturedDependencies = dependencies(context.dependencies, editable);
425
+ if (declaration) {
426
+ const expected = captureFixedInputDeclaration(declaration);
427
+ if (contractId !== expected.contractId || contractRevision !== expected.contractRevision || !sameNames([...editable], expected.editableArguments) || !sameNames(readOnlyArguments, expected.readOnlyArguments) || canonical(capturedDependencies) !== canonical(expected.dependencies))
428
+ return fail2();
429
+ }
430
+ return Object.freeze({
431
+ targetId,
432
+ blockId,
433
+ opcode,
434
+ revision,
435
+ arguments: Object.freeze(argumentsCopy),
436
+ fixedContext: Object.freeze({
437
+ contractId,
438
+ contractRevision,
439
+ generation,
440
+ readOnlyArguments,
441
+ dependencies: capturedDependencies,
442
+ inputs: Object.freeze(inputs)
443
+ })
444
+ });
445
+ }
446
+ function sameFixedSnapshot(left, right) {
447
+ const a = captureFixedSnapshot(left);
448
+ const b = captureFixedSnapshot(right);
449
+ const keys = Object.keys(a.arguments);
450
+ return a.targetId === b.targetId && a.blockId === b.blockId && a.opcode === b.opcode && a.revision === b.revision && canonical(a.fixedContext) === canonical(b.fixedContext) && keys.length === Object.keys(b.arguments).length && keys.every((name) => {
451
+ const x = a.arguments[name];
452
+ const y = b.arguments[name];
453
+ return !!y && x.kind === y.kind && (x.kind === "reporter" || y.kind === "literal" && Object.is(x.value, y.value));
454
+ });
455
+ }
456
+
457
+ // dist/fixed-input-editor.js
458
+ function fixedDeclarationError() {
459
+ throw new TypeError("Invalid fixed editor declaration.");
460
+ }
461
+ function plainDeclaration(value) {
462
+ if (!value || typeof value !== "object" || Array.isArray(value) || Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null || Object.getOwnPropertySymbols(value).length)
463
+ fixedDeclarationError();
464
+ }
465
+ function ownData(value, key, optional = false) {
466
+ if (!value || typeof value !== "object")
467
+ return fixedDeclarationError();
468
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
469
+ if (!descriptor) {
470
+ if (optional)
471
+ return void 0;
472
+ return fixedDeclarationError();
473
+ }
474
+ if (!("value" in descriptor))
475
+ return fixedDeclarationError();
476
+ return descriptor.value;
477
+ }
478
+ function declarationText(value) {
479
+ if (typeof value !== "string" || !value)
480
+ return fixedDeclarationError();
481
+ return value;
482
+ }
483
+ function declarationArray(value) {
484
+ if (!Array.isArray(value) || Object.getOwnPropertySymbols(value).length)
485
+ return fixedDeclarationError();
486
+ const length = ownData(value, "length");
487
+ if (typeof length !== "number" || !Number.isSafeInteger(length) || length < 0 || length > 1e5)
488
+ return fixedDeclarationError();
489
+ for (const key of Object.getOwnPropertyNames(value)) {
490
+ if (key !== "length" && (!/^(0|[1-9][0-9]*)$/.test(key) || Number(key) >= length))
491
+ return fixedDeclarationError();
492
+ }
493
+ const items = [];
494
+ for (let i = 0; i < length; i++)
495
+ items.push(ownData(value, String(i)));
496
+ return Object.freeze(items);
497
+ }
498
+ function declarationJson(value) {
499
+ try {
500
+ return snapshotJson(value);
501
+ } catch (_a) {
502
+ return fixedDeclarationError();
503
+ }
504
+ }
505
+ function fixedDeclaration(value) {
506
+ try {
507
+ return captureFixedInputDeclaration(value);
508
+ } catch (_a) {
509
+ return fixedDeclarationError();
510
+ }
511
+ }
512
+ function captureOpcodes(value, extensionId) {
513
+ const items = declarationJson(value);
514
+ if (!Array.isArray(items) || !items.length || items.some((opcode) => typeof opcode !== "string" || !opcode.startsWith(`${extensionId}_`) || opcode === `${extensionId}_`) || new Set(items).size !== items.length)
515
+ return fixedDeclarationError();
516
+ return Object.freeze(items);
517
+ }
518
+ function captureFixedInputEditorDefinition(value) {
519
+ plainDeclaration(value);
520
+ const apiVersion = ownData(value, "apiVersion");
521
+ const extensionId = declarationText(ownData(value, "extensionId"));
522
+ const editorId = declarationText(ownData(value, "editorId"));
523
+ const saveContract = ownData(value, "saveContract");
524
+ if (apiVersion !== 1 || saveContract !== "prepared-fixed-v1")
525
+ return fixedDeclarationError();
526
+ const opcodes = captureOpcodes(ownData(value, "opcodes"), extensionId);
527
+ const branchRules = ownData(value, "branchRules", true);
528
+ const declaration = fixedDeclaration({
529
+ contractId: declarationText(ownData(value, "contractId")),
530
+ contractRevision: declarationText(ownData(value, "contractRevision")),
531
+ editableArguments: ownData(value, "editableArguments"),
532
+ readOnlyArguments: ownData(value, "readOnlyArguments"),
533
+ dependencies: ownData(value, "dependencies"),
534
+ ...branchRules === void 0 ? {} : { branchRules }
535
+ });
536
+ const read = ownData(value, "read");
537
+ const validate = ownData(value, "validate");
538
+ const createPatch = ownData(value, "createPatch");
539
+ const readFixed = ownData(value, "readFixed");
540
+ const prepareFixedSave = ownData(value, "prepareFixedSave");
541
+ const mount = ownData(value, "mount", true);
542
+ const describeFixed = ownData(value, "describeFixed", true);
543
+ if (typeof read !== "function" || typeof validate !== "function" || typeof createPatch !== "function" || typeof readFixed !== "function" || typeof prepareFixedSave !== "function" || mount !== void 0 && typeof mount !== "function" || describeFixed !== void 0 && typeof describeFixed !== "function")
544
+ return fixedDeclarationError();
545
+ return Object.freeze({
546
+ apiVersion,
547
+ extensionId,
548
+ editorId,
549
+ opcodes,
550
+ saveContract,
551
+ ...declaration,
552
+ read,
553
+ validate,
554
+ createPatch,
555
+ readFixed,
556
+ prepareFixedSave,
557
+ ...describeFixed === void 0 ? {} : { describeFixed },
558
+ ...mount === void 0 ? {} : { mount }
559
+ });
560
+ }
561
+ function drain(value) {
562
+ try {
563
+ void Promise.prototype.then.call(value, () => void 0, () => void 0);
564
+ } catch (_a) {
565
+ }
566
+ }
567
+ function copySync(value) {
568
+ try {
569
+ return snapshotJson(value);
570
+ } catch (error) {
571
+ drain(value);
572
+ throw error;
573
+ }
574
+ }
575
+ function freeze(value) {
576
+ if (value && typeof value === "object" && !Object.isFrozen(value)) {
577
+ Object.values(value).forEach(freeze);
578
+ Object.freeze(value);
579
+ }
580
+ }
581
+ function sameProjection(left, right) {
582
+ if (Object.is(left, right))
583
+ return true;
584
+ if (left === null || right === null || typeof left !== "object" || typeof right !== "object")
585
+ return false;
586
+ if (Array.isArray(left) || Array.isArray(right)) {
587
+ return Array.isArray(left) && Array.isArray(right) && left.length === right.length && left.every((value, index) => sameProjection(value, right[index]));
588
+ }
589
+ const names3 = Object.keys(left);
590
+ return names3.length === Object.keys(right).length && names3.every((name) => Object.hasOwn(right, name) && sameProjection(left[name], right[name]));
591
+ }
592
+ var pointer = (name) => `/${name.replace(/~/g, "~0").replace(/\//g, "~1")}`;
593
+ function isFixedInputEditor(definition) {
594
+ return definition.saveContract === "prepared-fixed-v1";
595
+ }
596
+ function captureFixedPreparedSave(definition, draft, snapshot) {
597
+ const value = copySync(definition.prepareFixedSave(draft, snapshot));
598
+ if (!isJsonRecord(value))
599
+ throw new TypeError("Invalid fixed prepared save.");
600
+ if (value.kind === "invalid" && Array.isArray(value.issues) && value.issues.length > 0) {
601
+ const issues = [];
602
+ for (let i = 0; i < value.issues.length; i++) {
603
+ const issue = value.issues[i];
604
+ if (!isJsonRecord(issue) || typeof issue.message !== "string" || issue.path !== void 0 && typeof issue.path !== "string")
605
+ throw new TypeError("Invalid fixed issue.");
606
+ issues.push(Object.freeze({ message: issue.message, ...issue.path === void 0 ? {} : { path: issue.path } }));
607
+ }
608
+ return Object.freeze({ kind: "invalid", issues: Object.freeze(issues) });
609
+ }
610
+ if (value.kind !== "ready" || !Array.isArray(value.changes) || !isJsonRecord(value.context) || typeof value.context.consumerId !== "string" || !value.context.consumerId || typeof value.context.nowMs !== "number" || !Number.isFinite(value.context.nowMs) || !Object.hasOwn(value.context, "settings"))
611
+ throw new TypeError("Invalid fixed prepared save.");
612
+ const changes = [];
613
+ for (let i = 0; i < value.changes.length; i++) {
614
+ const change = value.changes[i];
615
+ if (!isJsonRecord(change) || typeof change.argument !== "string" || !isLiteral(change.value))
616
+ throw new TypeError("Invalid fixed change.");
617
+ changes.push(Object.freeze({ argument: change.argument, value: change.value }));
618
+ }
619
+ freeze(value.context);
620
+ return Object.freeze({ kind: "ready", changes: Object.freeze(changes), context: value.context });
621
+ }
622
+ function createFixedInputEditor(options) {
623
+ var _a;
624
+ plainDeclaration(options);
625
+ const apiVersion = ownData(options, "apiVersion");
626
+ const extensionId = declarationText(ownData(options, "extensionId"));
627
+ const editorId = declarationText(ownData(options, "editorId"));
628
+ const contractId = declarationText(ownData(options, "contractId"));
629
+ const contractRevision = declarationText(ownData(options, "contractRevision"));
630
+ const contextProvider = ownData(options, "validationContext");
631
+ const opcodes = captureOpcodes(ownData(options, "opcodes"), extensionId);
632
+ const inputBindings = declarationArray(ownData(options, "bindings"));
633
+ const readOnlyArguments = ownData(options, "readOnlyArguments", true);
634
+ const dependencies2 = ownData(options, "dependencies", true);
635
+ const branchPolicies = ownData(options, "branchPolicies", true);
636
+ if (apiVersion !== 1 || !inputBindings.length || typeof contextProvider !== "function")
637
+ throw new TypeError("Invalid fixed editor options.");
638
+ const provider = contextProvider;
639
+ const compileBinding = (binding) => {
640
+ plainDeclaration(binding);
641
+ const argument = declarationText(ownData(binding, "argument"));
642
+ const read = ownData(binding, "read");
643
+ const encode = ownData(binding, "encode");
644
+ const validateCandidate = ownData(binding, "validateCandidate");
645
+ const schemaBundle = declarationJson(ownData(binding, "schemaBundle"));
646
+ const inputControl = ownData(binding, "control", true);
647
+ let control;
648
+ if (inputControl !== void 0) {
649
+ const item = declarationJson(inputControl);
650
+ if (!isJsonRecord(item) || !["text", "number", "enum", "color"].includes(item.kind) || item.label !== void 0 && typeof item.label !== "string" || item.choices !== void 0 && (!Array.isArray(item.choices) || item.choices.some((choice) => !isJsonRecord(choice) || !Object.hasOwn(choice, "value") || choice.label !== void 0 && typeof choice.label !== "string")))
651
+ return fixedDeclarationError();
652
+ freeze(item);
653
+ control = item;
654
+ }
655
+ if (typeof read !== "function" || typeof encode !== "function" || typeof validateCandidate !== "function")
656
+ throw new TypeError("Invalid fixed binding callbacks.");
657
+ const result = compileSchemaBundle(schemaBundle);
658
+ if (result.kind !== "compiled")
659
+ throw new TypeError("Invalid fixed input schema.");
660
+ freeze(schemaBundle);
661
+ return Object.freeze({
662
+ argument,
663
+ read,
664
+ encode,
665
+ validateCandidate,
666
+ validator: result.validator,
667
+ schemaBundle,
668
+ ...control === void 0 ? {} : { control }
669
+ });
670
+ };
671
+ const bindings = inputBindings.map(compileBinding);
672
+ const policyBindings = [];
673
+ const policyRules = [];
674
+ if (branchPolicies !== void 0) {
675
+ for (const input of declarationArray(branchPolicies)) {
676
+ plainDeclaration(input);
677
+ const selector = declarationText(ownData(input, "selector"));
678
+ const normalization = ownData(input, "normalize", true);
679
+ const cases = [];
680
+ const variants = [];
681
+ for (const caseInput of declarationArray(ownData(input, "cases"))) {
682
+ plainDeclaration(caseInput);
683
+ const requirements = ownData(caseInput, "requireOnSelectorChange", true);
684
+ const overrides = ownData(caseInput, "bindings", true);
685
+ cases.push({
686
+ values: declarationJson(ownData(caseInput, "values")),
687
+ activeDependents: declarationJson(ownData(caseInput, "activeDependents")),
688
+ ...requirements === void 0 ? {} : { requireOnSelectorChange: declarationJson(requirements) }
689
+ });
690
+ variants.push(overrides === void 0 ? [] : declarationArray(overrides).map(compileBinding));
691
+ }
692
+ policyRules.push({ selector, cases, ...normalization === void 0 ? {} : { normalize: declarationJson(normalization) } });
693
+ policyBindings.push(variants);
694
+ }
695
+ }
696
+ const declaration = fixedDeclaration({
697
+ contractId,
698
+ contractRevision,
699
+ editableArguments: bindings.map((binding) => binding.argument),
700
+ readOnlyArguments: readOnlyArguments !== null && readOnlyArguments !== void 0 ? readOnlyArguments : [],
701
+ dependencies: dependencies2 !== null && dependencies2 !== void 0 ? dependencies2 : [],
702
+ ...branchPolicies === void 0 ? {} : { branchRules: policyRules }
703
+ });
704
+ for (const [index, rule] of ((_a = declaration.branchRules) !== null && _a !== void 0 ? _a : []).entries()) {
705
+ const group = declaration.dependencies.find((item) => item.selector === rule.selector);
706
+ for (const variants of policyBindings[index]) {
707
+ if (new Set(variants.map((binding) => binding.argument)).size !== variants.length || variants.some((binding) => !group.dependents.includes(binding.argument)))
708
+ return fixedDeclarationError();
709
+ }
710
+ }
711
+ const diagnostic2 = (code, argument) => Object.freeze({
712
+ message: `Fixed input validation failed: ${code}; contract=${JSON.stringify(contractId)}${argument === void 0 ? "" : `; argument=${JSON.stringify(argument)}`}.`,
713
+ ...argument === void 0 ? {} : { path: pointer(argument) }
714
+ });
715
+ const literal = (snapshot, argument) => {
716
+ var _a2;
717
+ const metadata = snapshot.fixedContext.inputs[argument];
718
+ return ((_a2 = snapshot.arguments[argument]) === null || _a2 === void 0 ? void 0 : _a2.kind) === "literal" && (metadata === null || metadata === void 0 ? void 0 : metadata.role) === "editable" && (metadata.source === "field" || metadata.source === "shadow");
719
+ };
720
+ const project = (binding, snapshot) => {
721
+ const slot = snapshot.arguments[binding.argument];
722
+ if ((slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal" || !literal(snapshot, binding.argument))
723
+ return { readOnly: true };
724
+ try {
725
+ return { value: copySync(binding.read(slot.value)), changed: false };
726
+ } catch (_a2) {
727
+ return { readOnly: true };
728
+ }
729
+ };
730
+ const entryFor = (draft, argument) => {
731
+ const entry = isJsonRecord(draft.inputs) && Object.hasOwn(draft.inputs, argument) ? draft.inputs[argument] : void 0;
732
+ return isJsonRecord(entry) ? entry : void 0;
733
+ };
734
+ const selection = (snapshot, draft) => {
735
+ var _a2;
736
+ const arguments_ = { ...snapshot.arguments };
737
+ if (draft)
738
+ for (const rule of (_a2 = declaration.branchRules) !== null && _a2 !== void 0 ? _a2 : []) {
739
+ const entry = entryFor(draft, rule.selector);
740
+ if ((entry === null || entry === void 0 ? void 0 : entry.changed) === true && Object.hasOwn(entry, "value") && isLiteral(entry.value)) {
741
+ Object.defineProperty(arguments_, rule.selector, { value: { kind: "literal", value: entry.value }, enumerable: true });
742
+ }
743
+ }
744
+ const branches = resolveFixedBranches(declaration, snapshot, arguments_);
745
+ const selected = new Map(bindings.map((binding) => [binding.argument, binding]));
746
+ const states = /* @__PURE__ */ new Map();
747
+ const unruled = /* @__PURE__ */ new Set();
748
+ for (const group of declaration.dependencies) {
749
+ if (branches.some((branch) => branch.selector === group.selector))
750
+ continue;
751
+ for (const argument of [group.selector, ...group.dependents]) {
752
+ states.set(argument, "readonly");
753
+ unruled.add(argument);
754
+ }
755
+ }
756
+ for (const [policyIndex, branch] of branches.entries()) {
757
+ const group = declaration.dependencies.find((item) => item.selector === branch.selector);
758
+ if (!branch.selected) {
759
+ for (const argument of group.dependents)
760
+ states.set(argument, "readonly");
761
+ continue;
762
+ }
763
+ for (const argument of group.dependents)
764
+ if (!branch.selected.activeDependents.includes(argument))
765
+ states.set(argument, "hidden");
766
+ const caseIndex = branch.rule.cases.indexOf(branch.selected);
767
+ for (const binding of policyBindings[policyIndex][caseIndex])
768
+ selected.set(binding.argument, binding);
769
+ }
770
+ return { branches, bindings: [...selected.values()], states, unruled };
771
+ };
772
+ const initialProjection = (argument, snapshot) => {
773
+ const binding = selection(snapshot).bindings.find((item) => item.argument === argument);
774
+ return project(binding, snapshot);
775
+ };
776
+ const parseEntry = (_binding, entry, original) => {
777
+ if (original.readOnly === true) {
778
+ if (sameProjection(original, entry))
779
+ return { changed: false, value: null };
780
+ if (entry.repair !== true)
781
+ throw new TypeError();
782
+ }
783
+ if (!Object.hasOwn(entry, "value") || typeof entry.changed !== "boolean" || entry.readOnly === true)
784
+ throw new TypeError();
785
+ let value = copySync(entry.value);
786
+ let token;
787
+ if (Object.hasOwn(entry, "buffer")) {
788
+ if (!entry.changed || typeof entry.buffer !== "string")
789
+ throw new TypeError();
790
+ const parsed = parseFixedNumberInput(entry.buffer);
791
+ if (parsed.kind !== "valid" || typeof value !== "number" || !Object.is(parsed.value, value))
792
+ throw new TypeError();
793
+ token = parsed.token;
794
+ value = token;
795
+ }
796
+ if (token === void 0 && original.readOnly !== true && sameProjection(original.value, value))
797
+ return { changed: false, value };
798
+ if (!entry.changed)
799
+ throw new TypeError();
800
+ return { changed: true, value, ...token === void 0 ? {} : { token } };
801
+ };
802
+ const validateExact = (binding, candidate, context, snapshot) => {
803
+ const projectedCandidate = copySync(binding.read(candidate));
804
+ if (binding.validator.validate(projectedCandidate, { consumerId: context.consumerId }).kind !== "valid")
805
+ return diagnostic2("schema-data-invalid", binding.argument);
806
+ const owner = copySync(binding.validateCandidate(candidate, context, snapshot));
807
+ if (!Array.isArray(owner))
808
+ throw new TypeError();
809
+ for (const issue of owner) {
810
+ if (!isJsonRecord(issue) || typeof issue.message !== "string" || issue.path !== void 0 && typeof issue.path !== "string")
811
+ throw new TypeError();
812
+ }
813
+ return owner.length ? diagnostic2("owner-input-invalid", binding.argument) : void 0;
814
+ };
815
+ const description = (draft, snapshot) => {
816
+ const current = selection(snapshot, draft);
817
+ const inputs = /* @__PURE__ */ Object.create(null);
818
+ const issues = [];
819
+ const changes = [];
820
+ const describe = (argument, binding) => {
821
+ var _a2, _b, _c, _d;
822
+ const metadata = snapshot.fixedContext.inputs[argument];
823
+ const slot = snapshot.arguments[argument];
824
+ const entry = entryFor(draft, argument);
825
+ const projected = binding ? project(binding, snapshot) : void 0;
826
+ const schema = binding === null || binding === void 0 ? void 0 : binding.schemaBundle.root.document;
827
+ const schemaType = schema && typeof schema === "object" && !Array.isArray(schema) ? schema.type : void 0;
828
+ const value = (_a2 = entry === null || entry === void 0 ? void 0 : entry.value) !== null && _a2 !== void 0 ? _a2 : projected === null || projected === void 0 ? void 0 : projected.value;
829
+ const valueType = schemaType === "number" || schemaType === "integer" ? "number" : schemaType === "string" ? "string" : typeof value === "number" ? "number" : "string";
830
+ const explicit = binding === null || binding === void 0 ? void 0 : binding.control;
831
+ const state = (_b = current.states.get(argument)) !== null && _b !== void 0 ? _b : binding && literal(snapshot, argument) ? "editable" : "readonly";
832
+ const schemaEnum = schema && typeof schema === "object" && !Array.isArray(schema) ? schema.enum : void 0;
833
+ const choices = (_c = explicit === null || explicit === void 0 ? void 0 : explicit.choices) !== null && _c !== void 0 ? _c : Array.isArray(schemaEnum) ? schemaEnum.map((value2) => ({ value: value2 })) : void 0;
834
+ const control = (_d = explicit === null || explicit === void 0 ? void 0 : explicit.kind) !== null && _d !== void 0 ? _d : choices ? "enum" : valueType === "number" ? "number" : "text";
835
+ const rawDisplay = (slot === null || slot === void 0 ? void 0 : slot.kind) === "literal" ? String(slot.value) : void 0;
836
+ const reason = state === "hidden" ? "Inactive branch; its draft is retained and is not saved." : current.states.get(argument) === "readonly" ? "The selector is unavailable; the dependent input is read-only." : state === "readonly" ? "This input is read-only; connected expressions are not evaluated." : void 0;
837
+ inputs[argument] = Object.freeze({
838
+ state,
839
+ control,
840
+ valueType,
841
+ ...rawDisplay === void 0 ? {} : { rawDisplay },
842
+ ...(explicit === null || explicit === void 0 ? void 0 : explicit.label) === void 0 ? {} : { label: explicit.label },
843
+ ...choices === void 0 ? {} : { choices },
844
+ ...explicit && Object.hasOwn(explicit, "reset") ? { reset: explicit.reset } : {},
845
+ ...reason === void 0 ? {} : { reason },
846
+ ...(metadata === null || metadata === void 0 ? void 0 : metadata.source) === "reporter" ? { unverified: true } : {}
847
+ });
848
+ if (!binding || state !== "editable" || !entry)
849
+ return;
850
+ try {
851
+ const parsed = parseEntry(binding, entry, initialProjection(argument, snapshot));
852
+ if (!parsed.changed)
853
+ return;
854
+ const projection = parsed.token === void 0 ? parsed.value : parseFixedNumberInput(parsed.token);
855
+ const data = typeof projection === "object" && projection !== null && "kind" in projection && projection.kind === "valid" ? projection.value : projection;
856
+ if (binding.validator.validate(data, { consumerId: contractId }).kind !== "valid")
857
+ issues.push(diagnostic2("schema-data-invalid", argument));
858
+ if (isLiteral(parsed.value))
859
+ changes.push({ argument, value: parsed.value });
860
+ } catch (_e) {
861
+ issues.push(diagnostic2("fixed-input-invalid", argument));
862
+ }
863
+ };
864
+ for (const binding of current.bindings)
865
+ describe(binding.argument, binding);
866
+ for (const argument of declaration.readOnlyArguments)
867
+ describe(argument);
868
+ issues.push(...validateFixedBranchChanges(declaration, snapshot, changes));
869
+ for (const branch of current.branches) {
870
+ if (!branch.selected || !changes.some((change) => change.argument === branch.selector))
871
+ continue;
872
+ for (const argument of branch.selected.activeDependents) {
873
+ if (changes.some((change) => change.argument === argument))
874
+ continue;
875
+ const binding = current.bindings.find((binding2) => binding2.argument === argument);
876
+ const slot = snapshot.arguments[argument];
877
+ if ((slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal")
878
+ continue;
879
+ try {
880
+ const projected = copySync(binding.read(slot.value));
881
+ if (binding.validator.validate(projected, { consumerId: contractId }).kind !== "valid")
882
+ issues.push(diagnostic2("schema-data-invalid", argument));
883
+ } catch (_a2) {
884
+ issues.push(diagnostic2("fixed-input-invalid", argument));
885
+ }
886
+ }
887
+ }
888
+ return Object.freeze({ inputs: Object.freeze(inputs), issues: Object.freeze(issues), branches: Object.freeze(current.branches.map(({ selector, status, value }) => Object.freeze({ selector, status, ...value === void 0 ? {} : { value } }))) });
889
+ };
890
+ return Object.freeze({
891
+ apiVersion,
892
+ extensionId,
893
+ editorId,
894
+ opcodes,
895
+ ...declaration,
896
+ saveContract: "prepared-fixed-v1",
897
+ read: () => {
898
+ throw new TypeError("Fixed editors require a compatible host.");
899
+ },
900
+ validate: () => [diagnostic2("fixed-host-required")],
901
+ createPatch: () => {
902
+ throw new TypeError("Fixed editors require a compatible host.");
903
+ },
904
+ readFixed: (snapshot) => {
905
+ const inputs = {};
906
+ for (const binding of selection(snapshot).bindings)
907
+ Object.defineProperty(inputs, binding.argument, { value: project(binding, snapshot), enumerable: true });
908
+ return { inputs };
909
+ },
910
+ describeFixed: description,
911
+ prepareFixedSave: (draft, snapshot) => {
912
+ const issues = [];
913
+ const changes = [];
914
+ let context;
915
+ try {
916
+ const value = copySync(provider());
917
+ if (!isJsonRecord(value) || typeof value.consumerId !== "string" || !value.consumerId || typeof value.nowMs !== "number" || !Number.isFinite(value.nowMs) || !Object.hasOwn(value, "settings"))
918
+ throw new TypeError();
919
+ freeze(value);
920
+ context = value;
921
+ } catch (_a2) {
922
+ return { kind: "invalid", issues: [diagnostic2("owner-context-invalid")] };
923
+ }
924
+ const current = selection(snapshot, draft);
925
+ const candidates = /* @__PURE__ */ new Map();
926
+ const finalArguments = { ...snapshot.arguments };
927
+ for (const binding of current.bindings) {
928
+ const { argument } = binding;
929
+ try {
930
+ const entry = entryFor(draft, argument);
931
+ if (!entry)
932
+ throw new TypeError();
933
+ if (current.states.get(argument) === "hidden")
934
+ continue;
935
+ if (current.states.get(argument) === "readonly") {
936
+ if (sameProjection(project(binding, snapshot), entry))
937
+ continue;
938
+ if (current.unruled.has(argument)) {
939
+ issues.push(diagnostic2("fixed-dependency-validation-unavailable", argument));
940
+ continue;
941
+ }
942
+ throw new TypeError();
943
+ }
944
+ const original = initialProjection(argument, snapshot);
945
+ if (!literal(snapshot, argument)) {
946
+ if (sameProjection(original, entry))
947
+ continue;
948
+ throw new TypeError();
949
+ }
950
+ const parsed = parseEntry(binding, entry, original);
951
+ if (!parsed.changed)
952
+ continue;
953
+ const raw = binding.encode(parsed.value, context);
954
+ if (!isLiteral(raw)) {
955
+ drain(raw);
956
+ throw new TypeError();
957
+ }
958
+ if (parsed.token !== void 0 && raw !== parsed.token)
959
+ throw new TypeError();
960
+ candidates.set(argument, raw);
961
+ Object.defineProperty(finalArguments, argument, { value: { kind: "literal", value: raw }, enumerable: true, configurable: true });
962
+ const slot = snapshot.arguments[argument];
963
+ if ((slot === null || slot === void 0 ? void 0 : slot.kind) === "literal" && !Object.is(raw, slot.value))
964
+ changes.push(Object.freeze({ argument, value: raw }));
965
+ } catch (_b) {
966
+ issues.push(diagnostic2("fixed-input-invalid", argument));
967
+ }
968
+ }
969
+ issues.push(...validateFixedBranchChanges(declaration, snapshot, changes));
970
+ const finalBranches = resolveFixedBranches(declaration, snapshot, finalArguments);
971
+ for (let index = 0; index < current.branches.length; index++) {
972
+ if (current.branches[index].selected !== finalBranches[index].selected)
973
+ issues.push(diagnostic2("selector-codec-mismatch", current.branches[index].selector));
974
+ }
975
+ const validateNames = new Set(candidates.keys());
976
+ for (const branch of finalBranches)
977
+ if (branch.selected && changes.some((change) => change.argument === branch.selector)) {
978
+ for (const argument of branch.selected.activeDependents)
979
+ validateNames.add(argument);
980
+ }
981
+ const finalSnapshot = captureFixedSnapshot({ ...snapshot, arguments: finalArguments }, snapshot, declaration);
982
+ for (const argument of validateNames) {
983
+ const binding = current.bindings.find((binding2) => binding2.argument === argument);
984
+ const slot = finalArguments[argument];
985
+ if ((slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal")
986
+ continue;
987
+ try {
988
+ const issue = validateExact(binding, slot.value, context, finalSnapshot);
989
+ if (issue)
990
+ issues.push(issue);
991
+ } catch (_c) {
992
+ issues.push(diagnostic2("fixed-input-invalid", argument));
993
+ }
994
+ }
995
+ return issues.length ? { kind: "invalid", issues } : { kind: "ready", changes, context };
996
+ }
997
+ });
998
+ }
999
+
1000
+ // dist/fixed-input-types.js
1001
+ var FIXED_INPUTS_CAPABILITY = "fixed-inputs-v1";
1002
+ function supportsFixedInputs(host) {
1003
+ const data = (object2, key) => {
1004
+ let item = object2;
1005
+ for (let depth = 0; item && (typeof item === "object" || typeof item === "function") && depth < 64; depth++) {
1006
+ const descriptor = Object.getOwnPropertyDescriptor(item, key);
1007
+ if (descriptor)
1008
+ return "value" in descriptor ? descriptor.value : void 0;
1009
+ item = Object.getPrototypeOf(item);
1010
+ }
1011
+ return void 0;
1012
+ };
1013
+ try {
1014
+ const port = data(host, "fixedInputs");
1015
+ return data(port, "capability") === FIXED_INPUTS_CAPABILITY && typeof data(port, "readBlock") === "function" && typeof data(port, "commit") === "function";
1016
+ } catch (_a) {
1017
+ return false;
1018
+ }
1019
+ }
1020
+ var supportsFixedInputEditing = supportsFixedInputs;
1021
+
1022
+ // dist/view.js
1023
+ var __classPrivateFieldGet = function(receiver, state, kind, f) {
1024
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
1025
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
1026
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
1027
+ };
1028
+ var __classPrivateFieldSet = function(receiver, state, value, kind, f) {
1029
+ if (kind === "m") throw new TypeError("Private method is not writable");
1030
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
1031
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
1032
+ return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
1033
+ };
1034
+ var _EditorView_unmount;
1035
+ var _EditorView_mounting;
1036
+ var EditorView = class {
1037
+ constructor() {
1038
+ _EditorView_unmount.set(this, void 0);
1039
+ _EditorView_mounting.set(this, false);
1040
+ }
1041
+ mount(container, mount, owner) {
1042
+ if (owner.getState().status !== "editing" || __classPrivateFieldGet(this, _EditorView_unmount, "f") || __classPrivateFieldGet(this, _EditorView_mounting, "f")) {
1043
+ throw new Error("The session cannot mount another view.");
1044
+ }
1045
+ if (!mount)
1046
+ throw new Error("No editor view is registered.");
1047
+ let active = true;
1048
+ const subscriptions = /* @__PURE__ */ new Set();
1049
+ const assertMounted = () => {
1050
+ if (!active)
1051
+ throw new Error("The editor view is unmounted.");
1052
+ };
1053
+ const release = () => {
1054
+ active = false;
1055
+ for (const unsubscribe of subscriptions)
1056
+ unsubscribe();
1057
+ subscriptions.clear();
1058
+ };
1059
+ const context = Object.freeze({
1060
+ getDraft: () => owner.getDraft(),
1061
+ ...owner.getFixedInputView ? { getFixedInputView: () => {
1062
+ assertMounted();
1063
+ return owner.getFixedInputView();
1064
+ } } : {},
1065
+ setDraft: (draft) => {
1066
+ assertMounted();
1067
+ owner.setDraft(draft);
1068
+ },
1069
+ setValidationIssues: (issues) => {
1070
+ assertMounted();
1071
+ owner.setValidationIssues(issues);
1072
+ },
1073
+ getState: () => owner.getState(),
1074
+ subscribe: (listener) => {
1075
+ assertMounted();
1076
+ const unsubscribe = owner.subscribe(() => {
1077
+ if (active)
1078
+ listener();
1079
+ });
1080
+ const remove = () => {
1081
+ subscriptions.delete(remove);
1082
+ unsubscribe();
1083
+ };
1084
+ subscriptions.add(remove);
1085
+ return remove;
1086
+ }
1087
+ });
1088
+ __classPrivateFieldSet(this, _EditorView_mounting, true, "f");
1089
+ let cleanup;
1090
+ try {
1091
+ cleanup = mount(container, context);
1092
+ if (typeof cleanup !== "function")
1093
+ throw new TypeError("mount() must return a cleanup function.");
1094
+ } catch (error) {
1095
+ release();
1096
+ throw error;
1097
+ } finally {
1098
+ __classPrivateFieldSet(this, _EditorView_mounting, false, "f");
1099
+ }
1100
+ if (owner.getState().status !== "editing" && owner.getState().status !== "saving") {
1101
+ release();
1102
+ cleanup();
1103
+ return () => {
1104
+ };
1105
+ }
1106
+ const unmount = () => {
1107
+ if (!active)
1108
+ return;
1109
+ release();
1110
+ __classPrivateFieldSet(this, _EditorView_unmount, void 0, "f");
1111
+ cleanup();
1112
+ };
1113
+ __classPrivateFieldSet(this, _EditorView_unmount, unmount, "f");
1114
+ return unmount;
1115
+ }
1116
+ unmount() {
1117
+ var _a;
1118
+ (_a = __classPrivateFieldGet(this, _EditorView_unmount, "f")) === null || _a === void 0 ? void 0 : _a.call(this);
1119
+ }
1120
+ };
1121
+ _EditorView_unmount = /* @__PURE__ */ new WeakMap(), _EditorView_mounting = /* @__PURE__ */ new WeakMap();
1122
+
1123
+ // dist/prepared-save.js
1124
+ function isPreparedEditor(definition) {
1125
+ return definition.saveContract === "prepared-source-v1";
1126
+ }
1127
+ function capturePreparedSave(definition, draft, snapshot) {
1128
+ const output = definition.prepareSave(draft, snapshot);
1129
+ try {
1130
+ const value = snapshotJson(output);
1131
+ if (!value || typeof value !== "object" || Array.isArray(value))
1132
+ throw new TypeError("Invalid prepared save.");
1133
+ const result = value;
1134
+ if (result.kind === "ready" && Array.isArray(result.changes)) {
1135
+ const changes = [];
1136
+ for (let i = 0; i < result.changes.length; i++) {
1137
+ const change = result.changes[i];
1138
+ if (!change || typeof change.argument !== "string" || !(typeof change.value === "string" || typeof change.value === "boolean" || typeof change.value === "number" && Number.isFinite(change.value))) {
1139
+ throw new TypeError("Invalid prepared change.");
1140
+ }
1141
+ changes.push(Object.freeze({ argument: change.argument, value: change.value }));
1142
+ }
1143
+ return Object.freeze({ kind: "ready", changes: Object.freeze(changes) });
1144
+ }
1145
+ if (result.kind === "invalid" && Array.isArray(result.issues) && result.issues.length > 0) {
1146
+ const issues = [];
1147
+ for (let i = 0; i < result.issues.length; i++) {
1148
+ const issue = result.issues[i];
1149
+ if (!issue || typeof issue.message !== "string" || issue.path !== void 0 && typeof issue.path !== "string") {
1150
+ throw new TypeError("Invalid prepared issue.");
1151
+ }
1152
+ issues.push(Object.freeze({ message: issue.message, ...issue.path === void 0 ? {} : { path: issue.path } }));
1153
+ }
1154
+ return Object.freeze({ kind: "invalid", issues: Object.freeze(issues) });
1155
+ }
1156
+ throw new TypeError("Invalid prepared save.");
1157
+ } catch (error) {
1158
+ try {
1159
+ void Promise.prototype.then.call(output, () => void 0, () => void 0);
1160
+ } catch (_a) {
1161
+ }
1162
+ throw error;
1163
+ }
1164
+ }
1165
+
1166
+ // dist/session.js
1167
+ var __classPrivateFieldSet2 = function(receiver, state, value, kind, f) {
1168
+ if (kind === "m") throw new TypeError("Private method is not writable");
1169
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
1170
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
1171
+ return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
1172
+ };
1173
+ var __classPrivateFieldGet2 = function(receiver, state, kind, f) {
1174
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
1175
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
1176
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
1177
+ };
1178
+ var _EditorSession_instances;
1179
+ var _EditorSession_draft;
1180
+ var _EditorSession_status;
1181
+ var _EditorSession_issues;
1182
+ var _EditorSession_error;
1183
+ var _EditorSession_listeners;
1184
+ var _EditorSession_view;
1185
+ var _EditorSession_definition;
1186
+ var _EditorSession_snapshot;
1187
+ var _EditorSession_host;
1188
+ var _EditorSession_fixedPort;
1189
+ var _EditorSession_isRegistered;
1190
+ var _EditorSession_subscribeRegistrationChanges;
1191
+ var _EditorSession_unsubscribeFixedScope;
1192
+ var _EditorSession_unsubscribeFixedRegistration;
1193
+ var _EditorSession_fixedSavingController;
1194
+ var _EditorSession_saveFixed;
1195
+ var _EditorSession_prepareChanges;
1196
+ var _EditorSession_copyIssues;
1197
+ var _EditorSession_copyDraft;
1198
+ var _EditorSession_assertOpen;
1199
+ var _EditorSession_finish;
1200
+ var _EditorSession_notify;
1201
+ var _EditorSession_reportObserverError;
1202
+ var EditorSession = class {
1203
+ /** Use EditorRegistry.open() to construct sessions from a captured host snapshot. */
1204
+ constructor(definition, snapshot, host, isRegistered, subscribeRegistrationChanges) {
1205
+ _EditorSession_instances.add(this);
1206
+ _EditorSession_draft.set(this, void 0);
1207
+ _EditorSession_status.set(this, "editing");
1208
+ _EditorSession_issues.set(this, []);
1209
+ _EditorSession_error.set(this, void 0);
1210
+ _EditorSession_listeners.set(this, /* @__PURE__ */ new Set());
1211
+ _EditorSession_view.set(this, new EditorView());
1212
+ _EditorSession_definition.set(this, void 0);
1213
+ _EditorSession_snapshot.set(this, void 0);
1214
+ _EditorSession_host.set(this, void 0);
1215
+ _EditorSession_fixedPort.set(this, void 0);
1216
+ _EditorSession_isRegistered.set(this, void 0);
1217
+ _EditorSession_subscribeRegistrationChanges.set(this, void 0);
1218
+ _EditorSession_unsubscribeFixedScope.set(this, void 0);
1219
+ _EditorSession_unsubscribeFixedRegistration.set(this, void 0);
1220
+ _EditorSession_fixedSavingController.set(this, void 0);
1221
+ __classPrivateFieldSet2(this, _EditorSession_isRegistered, isRegistered, "f");
1222
+ __classPrivateFieldSet2(this, _EditorSession_subscribeRegistrationChanges, subscribeRegistrationChanges, "f");
1223
+ __classPrivateFieldSet2(this, _EditorSession_definition, definition, "f");
1224
+ __classPrivateFieldSet2(this, _EditorSession_snapshot, isFixedInputEditor(definition) ? captureFixedSnapshot(snapshot, captureReference(snapshot), definition) : captureSnapshot(snapshot, captureReference(snapshot)), "f");
1225
+ __classPrivateFieldSet2(this, _EditorSession_host, host, "f");
1226
+ __classPrivateFieldSet2(this, _EditorSession_fixedPort, isFixedInputEditor(definition) && supportsFixedInputEditing(host) ? host.fixedInputs : void 0, "f");
1227
+ if (isFixedInputEditor(definition) && !supportsFixedInputEditing(host))
1228
+ throw new TypeError("Fixed input editors require a compatible host.");
1229
+ __classPrivateFieldSet2(this, _EditorSession_draft, __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyDraft).call(this, isFixedInputEditor(definition) ? definition.readFixed(__classPrivateFieldGet2(this, _EditorSession_snapshot, "f")) : definition.read(__classPrivateFieldGet2(this, _EditorSession_snapshot, "f"))), "f");
1230
+ if (isFixedInputEditor(definition) && __classPrivateFieldGet2(this, _EditorSession_subscribeRegistrationChanges, "f")) {
1231
+ const unsubscribe = __classPrivateFieldGet2(this, _EditorSession_subscribeRegistrationChanges, "f").call(this, () => {
1232
+ var _a;
1233
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this) && (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "editing" || __classPrivateFieldGet2(this, _EditorSession_status, "f") === "saving")) {
1234
+ (_a = __classPrivateFieldGet2(this, _EditorSession_fixedSavingController, "f")) === null || _a === void 0 ? void 0 : _a.abort(new BlockEditorConflictError());
1235
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "disposed");
1236
+ }
1237
+ });
1238
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "editing")
1239
+ __classPrivateFieldSet2(this, _EditorSession_unsubscribeFixedRegistration, unsubscribe, "f");
1240
+ else
1241
+ unsubscribe();
1242
+ }
1243
+ try {
1244
+ if (isFixedInputEditor(definition) && __classPrivateFieldGet2(this, _EditorSession_fixedPort, "f")) {
1245
+ let subscribe;
1246
+ for (let item = __classPrivateFieldGet2(this, _EditorSession_fixedPort, "f"), depth = 0; item && depth < 64; item = Object.getPrototypeOf(item), depth++) {
1247
+ const property = Object.getOwnPropertyDescriptor(item, "subscribeInvalidation");
1248
+ if (property) {
1249
+ subscribe = "value" in property ? property.value : void 0;
1250
+ break;
1251
+ }
1252
+ }
1253
+ if (typeof subscribe === "function") {
1254
+ const unsubscribe = subscribe.call(__classPrivateFieldGet2(this, _EditorSession_fixedPort, "f"), this.reference, definition, () => {
1255
+ var _a;
1256
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "editing" && __classPrivateFieldGet2(this, _EditorSession_status, "f") !== "saving")
1257
+ return;
1258
+ (_a = __classPrivateFieldGet2(this, _EditorSession_fixedSavingController, "f")) === null || _a === void 0 ? void 0 : _a.abort(new BlockEditorConflictError());
1259
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "disposed");
1260
+ });
1261
+ if (typeof unsubscribe !== "function")
1262
+ throw new TypeError("Invalid fixed scope subscription.");
1263
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "editing")
1264
+ __classPrivateFieldSet2(this, _EditorSession_unsubscribeFixedScope, unsubscribe, "f");
1265
+ else
1266
+ unsubscribe();
1267
+ }
1268
+ }
1269
+ } catch (error) {
1270
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "disposed");
1271
+ throw error;
1272
+ }
1273
+ }
1274
+ /** Stable instance identity remains available after the session closes. */
1275
+ get reference() {
1276
+ return captureReference(__classPrivateFieldGet2(this, _EditorSession_snapshot, "f"));
1277
+ }
1278
+ getDraft() {
1279
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_assertOpen).call(this);
1280
+ return cloneJson(__classPrivateFieldGet2(this, _EditorSession_draft, "f"));
1281
+ }
1282
+ getFixedInputView() {
1283
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_assertOpen).call(this);
1284
+ const definition = __classPrivateFieldGet2(this, _EditorSession_definition, "f");
1285
+ if (!isFixedInputEditor(definition) || !definition.describeFixed)
1286
+ return void 0;
1287
+ let description;
1288
+ try {
1289
+ description = definition.describeFixed(__classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyDraft).call(this, __classPrivateFieldGet2(this, _EditorSession_draft, "f")), __classPrivateFieldGet2(this, _EditorSession_snapshot, "f"));
1290
+ const captured = snapshotJson(description);
1291
+ if (!isJsonRecord(captured) || !isJsonRecord(captured.inputs) || !Array.isArray(captured.issues) || !Array.isArray(captured.branches)) {
1292
+ throw new TypeError("Invalid fixed input presentation.");
1293
+ }
1294
+ const freeze2 = (value) => {
1295
+ if (value && typeof value === "object") {
1296
+ Object.values(value).forEach(freeze2);
1297
+ Object.freeze(value);
1298
+ }
1299
+ };
1300
+ freeze2(captured);
1301
+ return captured;
1302
+ } catch (_a) {
1303
+ try {
1304
+ void Promise.prototype.then.call(description, () => void 0, () => void 0);
1305
+ } catch (_b) {
1306
+ }
1307
+ throw new TypeError("Invalid fixed input presentation.");
1308
+ }
1309
+ }
1310
+ setDraft(draft) {
1311
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "editing")
1312
+ throw new Error("The session is not editable.");
1313
+ __classPrivateFieldSet2(this, _EditorSession_draft, __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyDraft).call(this, draft), "f");
1314
+ __classPrivateFieldSet2(this, _EditorSession_issues, [], "f");
1315
+ __classPrivateFieldSet2(this, _EditorSession_error, void 0, "f");
1316
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_notify).call(this);
1317
+ }
1318
+ setValidationIssues(issues) {
1319
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "editing")
1320
+ throw new Error("The session is not editable.");
1321
+ __classPrivateFieldSet2(this, _EditorSession_issues, __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyIssues).call(this, issues), "f");
1322
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_notify).call(this);
1323
+ }
1324
+ getState() {
1325
+ return Object.freeze({
1326
+ status: __classPrivateFieldGet2(this, _EditorSession_status, "f"),
1327
+ canSave: __classPrivateFieldGet2(this, _EditorSession_status, "f") === "editing" && __classPrivateFieldGet2(this, _EditorSession_issues, "f").length === 0,
1328
+ issues: __classPrivateFieldGet2(this, _EditorSession_issues, "f"),
1329
+ error: __classPrivateFieldGet2(this, _EditorSession_error, "f")
1330
+ });
1331
+ }
1332
+ subscribe(listener) {
1333
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_assertOpen).call(this);
1334
+ __classPrivateFieldGet2(this, _EditorSession_listeners, "f").add(listener);
1335
+ return () => {
1336
+ __classPrivateFieldGet2(this, _EditorSession_listeners, "f").delete(listener);
1337
+ };
1338
+ }
1339
+ mount(container) {
1340
+ return __classPrivateFieldGet2(this, _EditorSession_view, "f").mount(container, __classPrivateFieldGet2(this, _EditorSession_definition, "f").mount, this);
1341
+ }
1342
+ async save() {
1343
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "saving")
1344
+ return { status: "busy" };
1345
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "editing")
1346
+ return { status: "closed" };
1347
+ __classPrivateFieldSet2(this, _EditorSession_status, "saving", "f");
1348
+ __classPrivateFieldSet2(this, _EditorSession_issues, [], "f");
1349
+ __classPrivateFieldSet2(this, _EditorSession_error, void 0, "f");
1350
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_notify).call(this);
1351
+ try {
1352
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1353
+ throw new BlockEditorConflictError("The source editor registration expired.");
1354
+ if (isFixedInputEditor(__classPrivateFieldGet2(this, _EditorSession_definition, "f")))
1355
+ return await __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_saveFixed).call(this);
1356
+ const prepared = isPreparedEditor(__classPrivateFieldGet2(this, _EditorSession_definition, "f")) ? capturePreparedSave(__classPrivateFieldGet2(this, _EditorSession_definition, "f"), cloneJson(__classPrivateFieldGet2(this, _EditorSession_draft, "f")), __classPrivateFieldGet2(this, _EditorSession_snapshot, "f")) : void 0;
1357
+ const issues = prepared ? prepared.kind === "invalid" ? prepared.issues : [] : await __classPrivateFieldGet2(this, _EditorSession_definition, "f").validate(cloneJson(__classPrivateFieldGet2(this, _EditorSession_draft, "f")), __classPrivateFieldGet2(this, _EditorSession_snapshot, "f"));
1358
+ if (!Array.isArray(issues))
1359
+ throw new TypeError("validate() must return an issue array.");
1360
+ if (issues.length > 0) {
1361
+ __classPrivateFieldSet2(this, _EditorSession_issues, __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyIssues).call(this, issues), "f");
1362
+ return { status: "invalid", issues: __classPrivateFieldGet2(this, _EditorSession_issues, "f") };
1363
+ }
1364
+ const changes = __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_prepareChanges).call(this, (prepared === null || prepared === void 0 ? void 0 : prepared.kind) === "ready" ? prepared.changes : __classPrivateFieldGet2(this, _EditorSession_definition, "f").createPatch(cloneJson(__classPrivateFieldGet2(this, _EditorSession_draft, "f")), __classPrivateFieldGet2(this, _EditorSession_snapshot, "f")));
1365
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1366
+ throw new BlockEditorConflictError("The source editor registration expired.");
1367
+ const current = __classPrivateFieldGet2(this, _EditorSession_host, "f").readBlock(__classPrivateFieldGet2(this, _EditorSession_snapshot, "f"));
1368
+ if (!current || !sameSnapshot(__classPrivateFieldGet2(this, _EditorSession_snapshot, "f"), captureSnapshot(current, __classPrivateFieldGet2(this, _EditorSession_snapshot, "f")))) {
1369
+ throw new BlockEditorConflictError();
1370
+ }
1371
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1372
+ throw new BlockEditorConflictError("The source editor registration expired.");
1373
+ if (changes.length > 0) {
1374
+ await __classPrivateFieldGet2(this, _EditorSession_host, "f").commit(Object.freeze({ expected: __classPrivateFieldGet2(this, _EditorSession_snapshot, "f"), changes }));
1375
+ }
1376
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "saved");
1377
+ return { status: "saved", changed: changes.length > 0 };
1378
+ } catch (error) {
1379
+ __classPrivateFieldSet2(this, _EditorSession_error, error, "f");
1380
+ return error instanceof BlockEditorConflictError ? { status: "conflict", error } : { status: "failed", error };
1381
+ } finally {
1382
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "saving") {
1383
+ __classPrivateFieldSet2(this, _EditorSession_status, "editing", "f");
1384
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_notify).call(this);
1385
+ }
1386
+ }
1387
+ }
1388
+ /** Closing is refused while saving; the host must keep the modal open. */
1389
+ cancel() {
1390
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "editing")
1391
+ return false;
1392
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "cancelled");
1393
+ return true;
1394
+ }
1395
+ dispose() {
1396
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "saving")
1397
+ return false;
1398
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") === "editing")
1399
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "disposed");
1400
+ return true;
1401
+ }
1402
+ };
1403
+ _EditorSession_draft = /* @__PURE__ */ new WeakMap(), _EditorSession_status = /* @__PURE__ */ new WeakMap(), _EditorSession_issues = /* @__PURE__ */ new WeakMap(), _EditorSession_error = /* @__PURE__ */ new WeakMap(), _EditorSession_listeners = /* @__PURE__ */ new WeakMap(), _EditorSession_view = /* @__PURE__ */ new WeakMap(), _EditorSession_definition = /* @__PURE__ */ new WeakMap(), _EditorSession_snapshot = /* @__PURE__ */ new WeakMap(), _EditorSession_host = /* @__PURE__ */ new WeakMap(), _EditorSession_fixedPort = /* @__PURE__ */ new WeakMap(), _EditorSession_isRegistered = /* @__PURE__ */ new WeakMap(), _EditorSession_subscribeRegistrationChanges = /* @__PURE__ */ new WeakMap(), _EditorSession_unsubscribeFixedScope = /* @__PURE__ */ new WeakMap(), _EditorSession_unsubscribeFixedRegistration = /* @__PURE__ */ new WeakMap(), _EditorSession_fixedSavingController = /* @__PURE__ */ new WeakMap(), _EditorSession_instances = /* @__PURE__ */ new WeakSet(), _EditorSession_saveFixed = async function _EditorSession_saveFixed2() {
1404
+ var _a;
1405
+ const definition = __classPrivateFieldGet2(this, _EditorSession_definition, "f");
1406
+ if (!isFixedInputEditor(definition))
1407
+ throw new TypeError("Fixed input capability is unavailable.");
1408
+ if (!supportsFixedInputEditing(__classPrivateFieldGet2(this, _EditorSession_host, "f")) || __classPrivateFieldGet2(this, _EditorSession_host, "f").fixedInputs !== __classPrivateFieldGet2(this, _EditorSession_fixedPort, "f")) {
1409
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "disposed");
1410
+ throw new BlockEditorConflictError("The fixed input capability changed.");
1411
+ }
1412
+ const snapshot = __classPrivateFieldGet2(this, _EditorSession_snapshot, "f");
1413
+ const prepared = captureFixedPreparedSave(definition, __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyDraft).call(this, __classPrivateFieldGet2(this, _EditorSession_draft, "f")), snapshot);
1414
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "saving")
1415
+ throw new BlockEditorConflictError();
1416
+ if (prepared.kind === "invalid") {
1417
+ __classPrivateFieldSet2(this, _EditorSession_issues, __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_copyIssues).call(this, prepared.issues), "f");
1418
+ return { status: "invalid", issues: __classPrivateFieldGet2(this, _EditorSession_issues, "f") };
1419
+ }
1420
+ const changes = __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_prepareChanges).call(this, prepared.changes);
1421
+ const port = __classPrivateFieldGet2(this, _EditorSession_host, "f").fixedInputs;
1422
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1423
+ throw new BlockEditorConflictError("The fixed editor registration expired.");
1424
+ const current = port.readBlock(snapshot, definition);
1425
+ if (!current)
1426
+ throw new BlockEditorConflictError();
1427
+ try {
1428
+ const captured = captureFixedSnapshot(current, snapshot, definition);
1429
+ if (captured.fixedContext.generation !== snapshot.fixedContext.generation) {
1430
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "disposed");
1431
+ throw new BlockEditorConflictError();
1432
+ }
1433
+ if (!sameFixedSnapshot(snapshot, captured))
1434
+ throw new BlockEditorConflictError();
1435
+ } catch (_b) {
1436
+ throw new BlockEditorConflictError();
1437
+ }
1438
+ if (!supportsFixedInputEditing(__classPrivateFieldGet2(this, _EditorSession_host, "f")) || __classPrivateFieldGet2(this, _EditorSession_host, "f").fixedInputs !== port || __classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1439
+ throw new BlockEditorConflictError("The fixed editor registration expired.");
1440
+ if (changes.length > 0) {
1441
+ const controller = new AbortController();
1442
+ __classPrivateFieldSet2(this, _EditorSession_fixedSavingController, controller, "f");
1443
+ const context = prepared.context;
1444
+ const unsubscribe = (_a = __classPrivateFieldGet2(this, _EditorSession_subscribeRegistrationChanges, "f")) === null || _a === void 0 ? void 0 : _a.call(this, () => {
1445
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1446
+ controller.abort(new BlockEditorConflictError("The fixed editor registration expired."));
1447
+ });
1448
+ try {
1449
+ if (__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f") && !__classPrivateFieldGet2(this, _EditorSession_isRegistered, "f").call(this))
1450
+ throw new BlockEditorConflictError("The fixed editor registration expired.");
1451
+ await port.commit(Object.freeze({ expected: snapshot, changes, context, signal: controller.signal }));
1452
+ if (controller.signal.aborted)
1453
+ throw new BlockEditorConflictError("The fixed editor registration expired.");
1454
+ } catch (error) {
1455
+ if (controller.signal.aborted)
1456
+ throw new BlockEditorConflictError("The fixed editor registration expired.");
1457
+ throw error;
1458
+ } finally {
1459
+ unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
1460
+ __classPrivateFieldSet2(this, _EditorSession_fixedSavingController, void 0, "f");
1461
+ }
1462
+ }
1463
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_finish).call(this, "saved");
1464
+ return { status: "saved", changed: changes.length > 0 };
1465
+ }, _EditorSession_prepareChanges = function _EditorSession_prepareChanges2(patch) {
1466
+ if (!Array.isArray(patch))
1467
+ throw new TypeError("createPatch() must return an array.");
1468
+ const seen = /* @__PURE__ */ new Set();
1469
+ const changes = [];
1470
+ for (const change of patch) {
1471
+ if (!change || !__classPrivateFieldGet2(this, _EditorSession_definition, "f").editableArguments.includes(change.argument) || seen.has(change.argument) || !isLiteral(change.value)) {
1472
+ throw new TypeError("Patch contains an invalid, duplicate or disallowed argument.");
1473
+ }
1474
+ seen.add(change.argument);
1475
+ const slot = Object.hasOwn(__classPrivateFieldGet2(this, _EditorSession_snapshot, "f").arguments, change.argument) ? __classPrivateFieldGet2(this, _EditorSession_snapshot, "f").arguments[change.argument] : void 0;
1476
+ if (!slot || slot.kind !== "literal") {
1477
+ throw new TypeError("Only existing literal arguments can be changed.");
1478
+ }
1479
+ if (isFixedInputEditor(__classPrivateFieldGet2(this, _EditorSession_definition, "f"))) {
1480
+ const metadata = __classPrivateFieldGet2(this, _EditorSession_snapshot, "f").fixedContext.inputs[change.argument];
1481
+ if (!metadata || metadata.role !== "editable" || metadata.source !== "field" && metadata.source !== "shadow")
1482
+ throw new TypeError("Fixed patches require editable literal fields.");
1483
+ if (!Object.is(slot.value, change.value)) {
1484
+ changes.push(Object.freeze({ argument: change.argument, value: change.value }));
1485
+ }
1486
+ } else if (slot.value !== change.value)
1487
+ changes.push(Object.freeze({ ...change }));
1488
+ }
1489
+ if (isFixedInputEditor(__classPrivateFieldGet2(this, _EditorSession_definition, "f")) && validateFixedBranchChanges(__classPrivateFieldGet2(this, _EditorSession_definition, "f"), __classPrivateFieldGet2(this, _EditorSession_snapshot, "f"), changes).length)
1490
+ throw new TypeError("Fixed dependency validation is unavailable.");
1491
+ return Object.freeze(changes);
1492
+ }, _EditorSession_copyIssues = function _EditorSession_copyIssues2(issues) {
1493
+ if (isFixedInputEditor(__classPrivateFieldGet2(this, _EditorSession_definition, "f")))
1494
+ issues = snapshotJson(issues);
1495
+ if (!Array.isArray(issues))
1496
+ throw new TypeError("Expected a validation issue array.");
1497
+ return Object.freeze(issues.map((issue) => {
1498
+ if (!issue || typeof issue.message !== "string" || issue.path !== void 0 && typeof issue.path !== "string") {
1499
+ throw new TypeError("Invalid validation issue.");
1500
+ }
1501
+ return Object.freeze({ ...issue });
1502
+ }));
1503
+ }, _EditorSession_copyDraft = function _EditorSession_copyDraft2(draft) {
1504
+ if (!draft || typeof draft !== "object" || Array.isArray(draft)) {
1505
+ throw new TypeError("The editor draft must be a JSON object.");
1506
+ }
1507
+ if (!isFixedInputEditor(__classPrivateFieldGet2(this, _EditorSession_definition, "f")))
1508
+ return cloneJson(draft);
1509
+ try {
1510
+ return snapshotJson(draft);
1511
+ } catch (error) {
1512
+ try {
1513
+ void Promise.prototype.then.call(draft, () => void 0, () => void 0);
1514
+ } catch (_a) {
1515
+ }
1516
+ throw error;
1517
+ }
1518
+ }, _EditorSession_assertOpen = function _EditorSession_assertOpen2() {
1519
+ if (__classPrivateFieldGet2(this, _EditorSession_status, "f") !== "editing" && __classPrivateFieldGet2(this, _EditorSession_status, "f") !== "saving") {
1520
+ throw new Error("The session is closed.");
1521
+ }
1522
+ }, _EditorSession_finish = function _EditorSession_finish2(status) {
1523
+ __classPrivateFieldSet2(this, _EditorSession_status, status, "f");
1524
+ __classPrivateFieldSet2(this, _EditorSession_draft, {}, "f");
1525
+ const unsubscribe = __classPrivateFieldGet2(this, _EditorSession_unsubscribeFixedScope, "f");
1526
+ __classPrivateFieldSet2(this, _EditorSession_unsubscribeFixedScope, void 0, "f");
1527
+ try {
1528
+ unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
1529
+ } catch (error) {
1530
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_reportObserverError).call(this, error);
1531
+ }
1532
+ const unsubscribeRegistration = __classPrivateFieldGet2(this, _EditorSession_unsubscribeFixedRegistration, "f");
1533
+ __classPrivateFieldSet2(this, _EditorSession_unsubscribeFixedRegistration, void 0, "f");
1534
+ try {
1535
+ unsubscribeRegistration === null || unsubscribeRegistration === void 0 ? void 0 : unsubscribeRegistration();
1536
+ } catch (error) {
1537
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_reportObserverError).call(this, error);
1538
+ }
1539
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_notify).call(this);
1540
+ try {
1541
+ __classPrivateFieldGet2(this, _EditorSession_view, "f").unmount();
1542
+ } catch (error) {
1543
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_reportObserverError).call(this, error);
1544
+ }
1545
+ __classPrivateFieldGet2(this, _EditorSession_listeners, "f").clear();
1546
+ }, _EditorSession_notify = function _EditorSession_notify2() {
1547
+ for (const listener of [...__classPrivateFieldGet2(this, _EditorSession_listeners, "f")]) {
1548
+ try {
1549
+ listener();
1550
+ } catch (error) {
1551
+ __classPrivateFieldGet2(this, _EditorSession_instances, "m", _EditorSession_reportObserverError).call(this, error);
1552
+ }
1553
+ }
1554
+ }, _EditorSession_reportObserverError = function _EditorSession_reportObserverError2(error) {
1555
+ console.error("Block editor view callback failed:", error);
1556
+ };
1557
+
1558
+ // dist/subtree.js
1559
+ function descendantLimits(limits = {}) {
1560
+ const result = { ...DEFAULT_DESCENDANT_LIMITS, ...limits };
1561
+ if (!Number.isSafeInteger(result.maxDepth) || result.maxDepth < 0 || result.maxDepth > DEFAULT_DESCENDANT_LIMITS.maxDepth || !Number.isSafeInteger(result.maxNodes) || result.maxNodes < 1 || result.maxNodes > DEFAULT_DESCENDANT_LIMITS.maxNodes) {
1562
+ throw new BlockEditorStructureError("Limits must be integers: depth 0\u201332, nodes 1\u2013500.", "limit");
1563
+ }
1564
+ return Object.freeze(result);
1565
+ }
1566
+ function captureSubtree(source, reference, limits) {
1567
+ const invalid = (message) => {
1568
+ throw new BlockEditorStructureError(message, "invalid-structure");
1569
+ };
1570
+ if (!source || source.targetId !== reference.targetId || source.blockId !== reference.blockId || typeof source.generation !== "string" || !source.generation || !Array.isArray(source.nodes) || source.nodes.length === 0)
1571
+ invalid("The host returned an invalid subtree snapshot.");
1572
+ if (source.nodes.length > limits.maxNodes) {
1573
+ throw new BlockEditorStructureError("The subtree exceeds the node limit.", "limit");
1574
+ }
1575
+ const entries = /* @__PURE__ */ new Map();
1576
+ let edges = 0;
1577
+ for (const node of source.nodes) {
1578
+ if (!(node === null || node === void 0 ? void 0 : node.block) || !Array.isArray(node.branches))
1579
+ invalid("Invalid subtree node.");
1580
+ const block = (() => {
1581
+ try {
1582
+ return captureSnapshot(node.block, { targetId: reference.targetId, blockId: node.block.blockId });
1583
+ } catch (_a) {
1584
+ return invalid("Invalid subtree block snapshot or target identity.");
1585
+ }
1586
+ })();
1587
+ if (entries.has(block.blockId))
1588
+ invalid("Duplicate block ID in subtree.");
1589
+ const inputs = /* @__PURE__ */ new Set();
1590
+ const branches = node.branches.map((branch) => {
1591
+ if (!branch || typeof branch.input !== "string" || !branch.input || inputs.has(branch.input) || !Array.isArray(branch.blockIds))
1592
+ invalid("Invalid or duplicate statement input.");
1593
+ inputs.add(branch.input);
1594
+ edges += branch.blockIds.length;
1595
+ if (edges >= source.nodes.length)
1596
+ invalid("Subtree connections contain a cycle or duplicate parent.");
1597
+ const blockIds = branch.blockIds.map((id) => {
1598
+ if (typeof id !== "string" || !id)
1599
+ invalid("Invalid statement block ID.");
1600
+ return id;
1601
+ });
1602
+ return Object.freeze({ input: branch.input, blockIds: Object.freeze(blockIds) });
1603
+ });
1604
+ entries.set(block.blockId, Object.freeze({ block, branches: Object.freeze(branches) }));
1605
+ }
1606
+ const paths = /* @__PURE__ */ new Map();
1607
+ const ordered = [];
1608
+ const stack = [{ id: reference.blockId, path: Object.freeze([]) }];
1609
+ while (stack.length) {
1610
+ const { id, path } = stack.pop();
1611
+ if (paths.has(id))
1612
+ throw new BlockEditorStructureError("Cycle or repeated subtree node.", "cycle");
1613
+ if (path.length > limits.maxDepth) {
1614
+ throw new BlockEditorStructureError("The subtree exceeds the depth limit.", "limit");
1615
+ }
1616
+ const node = entries.get(id);
1617
+ if (!node)
1618
+ invalid(`Missing subtree node: ${id}`);
1619
+ paths.set(id, path);
1620
+ ordered.push(node);
1621
+ for (let branchIndex = node.branches.length - 1; branchIndex >= 0; branchIndex--) {
1622
+ const branch = node.branches[branchIndex];
1623
+ for (let index = branch.blockIds.length - 1; index >= 0; index--) {
1624
+ stack.push({ id: branch.blockIds[index], path: Object.freeze([
1625
+ ...path,
1626
+ Object.freeze({ blockId: id, input: branch.input, index })
1627
+ ]) });
1628
+ }
1629
+ }
1630
+ }
1631
+ if (paths.size !== entries.size)
1632
+ invalid("Subtree includes disconnected or out-of-scope nodes.");
1633
+ return {
1634
+ snapshot: Object.freeze({
1635
+ ...captureReference(reference),
1636
+ generation: source.generation,
1637
+ nodes: Object.freeze(ordered)
1638
+ }),
1639
+ paths
1640
+ };
1641
+ }
1642
+ function sameSubtree(left, right) {
1643
+ return left.targetId === right.targetId && left.blockId === right.blockId && left.generation === right.generation && left.nodes.length === right.nodes.length && left.nodes.every((node, index) => {
1644
+ const other = right.nodes[index];
1645
+ return sameSnapshot(node.block, other.block) && node.branches.length === other.branches.length && node.branches.every((branch, branchIndex) => {
1646
+ const b = other.branches[branchIndex];
1647
+ return branch.input === b.input && branch.blockIds.length === b.blockIds.length && branch.blockIds.every((id, i) => id === b.blockIds[i]);
1648
+ });
1649
+ });
1650
+ }
1651
+
1652
+ // dist/descendants.js
1653
+ var __classPrivateFieldSet3 = function(receiver, state, value, kind, f) {
1654
+ if (kind === "m") throw new TypeError("Private method is not writable");
1655
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
1656
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
1657
+ return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
1658
+ };
1659
+ var __classPrivateFieldGet3 = function(receiver, state, kind, f) {
1660
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
1661
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
1662
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
1663
+ };
1664
+ var _DescendantSession_instances;
1665
+ var _DescendantSession_snapshot;
1666
+ var _DescendantSession_host;
1667
+ var _DescendantSession_limits;
1668
+ var _DescendantSession_lookup;
1669
+ var _DescendantSession_bindings;
1670
+ var _DescendantSession_records;
1671
+ var _DescendantSession_nodes;
1672
+ var _DescendantSession_listeners;
1673
+ var _DescendantSession_detach;
1674
+ var _DescendantSession_abort;
1675
+ var _DescendantSession_status;
1676
+ var _DescendantSession_committing;
1677
+ var _DescendantSession_error;
1678
+ var _DescendantSession_createRecord;
1679
+ var _DescendantSession_isOpen;
1680
+ var _DescendantSession_closedSaveResult;
1681
+ var _DescendantSession_assertOpen;
1682
+ var _DescendantSession_assertEditable;
1683
+ var _DescendantSession_bindingsCurrent;
1684
+ var _DescendantSession_checkBindings;
1685
+ var _DescendantSession_assertSaving;
1686
+ var _DescendantSession_notify;
1687
+ var _DescendantSession_watch;
1688
+ var _DescendantSession_finish;
1689
+ function copyDraft(draft) {
1690
+ if (!draft || typeof draft !== "object" || Array.isArray(draft)) {
1691
+ throw new TypeError("The editor draft must be a JSON object.");
1692
+ }
1693
+ return cloneJson(draft);
1694
+ }
1695
+ function copyIssues(issues) {
1696
+ if (!Array.isArray(issues))
1697
+ throw new TypeError("Expected a validation issue array.");
1698
+ return Object.freeze(issues.map((issue) => {
1699
+ if (!issue || typeof issue.message !== "string" || issue.path !== void 0 && typeof issue.path !== "string") {
1700
+ throw new TypeError("Invalid validation issue.");
1701
+ }
1702
+ return Object.freeze({ message: issue.message, ...issue.path === void 0 ? {} : { path: issue.path } });
1703
+ }));
1704
+ }
1705
+ function observe(listeners) {
1706
+ for (const listener of [...listeners]) {
1707
+ try {
1708
+ listener();
1709
+ } catch (error) {
1710
+ report(error);
1711
+ }
1712
+ }
1713
+ }
1714
+ function report(error) {
1715
+ console.error("Block editor view callback failed:", error);
1716
+ }
1717
+ function abortable(value, signal) {
1718
+ return new Promise((resolve, reject) => {
1719
+ const abort = () => reject(signal.reason);
1720
+ signal.addEventListener("abort", abort, { once: true });
1721
+ Promise.resolve(value).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
1722
+ if (signal.aborted) {
1723
+ signal.removeEventListener("abort", abort);
1724
+ abort();
1725
+ }
1726
+ });
1727
+ }
1728
+ var DescendantSession = class {
1729
+ /** Use EditorRegistry.openDescendants() to negotiate and capture the scope. */
1730
+ constructor(captured, host, lookup, subscribeRegistry, limits) {
1731
+ _DescendantSession_instances.add(this);
1732
+ _DescendantSession_snapshot.set(this, void 0);
1733
+ _DescendantSession_host.set(this, void 0);
1734
+ _DescendantSession_limits.set(this, void 0);
1735
+ _DescendantSession_lookup.set(this, void 0);
1736
+ _DescendantSession_bindings.set(this, /* @__PURE__ */ new Map());
1737
+ _DescendantSession_records.set(this, /* @__PURE__ */ new Map());
1738
+ _DescendantSession_nodes.set(this, void 0);
1739
+ _DescendantSession_listeners.set(this, /* @__PURE__ */ new Set());
1740
+ _DescendantSession_detach.set(this, /* @__PURE__ */ new Set());
1741
+ _DescendantSession_abort.set(this, new AbortController());
1742
+ _DescendantSession_status.set(this, "editing");
1743
+ _DescendantSession_committing.set(this, false);
1744
+ _DescendantSession_error.set(this, void 0);
1745
+ __classPrivateFieldSet3(this, _DescendantSession_snapshot, captured.snapshot, "f");
1746
+ __classPrivateFieldSet3(this, _DescendantSession_host, host, "f");
1747
+ __classPrivateFieldSet3(this, _DescendantSession_lookup, lookup, "f");
1748
+ __classPrivateFieldSet3(this, _DescendantSession_limits, limits, "f");
1749
+ for (const node of captured.snapshot.nodes)
1750
+ __classPrivateFieldGet3(this, _DescendantSession_bindings, "f").set(node.block.opcode, lookup(node.block.opcode));
1751
+ __classPrivateFieldSet3(this, _DescendantSession_nodes, Object.freeze(captured.snapshot.nodes.map((node) => {
1752
+ const definition = __classPrivateFieldGet3(this, _DescendantSession_bindings, "f").get(node.block.opcode);
1753
+ const descriptor = Object.freeze({
1754
+ reference: captureReference(node.block),
1755
+ opcode: node.block.opcode,
1756
+ inputPath: captured.paths.get(node.block.blockId),
1757
+ ...definition ? { editor: Object.freeze({
1758
+ apiVersion: definition.apiVersion,
1759
+ extensionId: definition.extensionId,
1760
+ editorId: definition.editorId,
1761
+ opcodes: definition.opcodes,
1762
+ editableArguments: definition.editableArguments
1763
+ }) } : {}
1764
+ });
1765
+ if (definition) {
1766
+ try {
1767
+ __classPrivateFieldGet3(this, _DescendantSession_records, "f").set(node.block.blockId, __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_createRecord).call(this, descriptor, definition, node.block));
1768
+ } catch (cause) {
1769
+ throw new DescendantNodeError(descriptor.reference, descriptor.inputPath, cause);
1770
+ }
1771
+ }
1772
+ return descriptor;
1773
+ })), "f");
1774
+ try {
1775
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_watch).call(this, () => subscribeRegistry(() => {
1776
+ if (!__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_bindingsCurrent).call(this))
1777
+ this.invalidate(new BlockEditorConflictError("An editor registration changed."));
1778
+ }));
1779
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_checkBindings).call(this);
1780
+ if (host.subscribeInvalidation) {
1781
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_watch).call(this, () => host.subscribeInvalidation(this.reference, (reason) => this.invalidate(reason)));
1782
+ }
1783
+ } catch (error) {
1784
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_finish).call(this, "disposed");
1785
+ throw error;
1786
+ }
1787
+ }
1788
+ get reference() {
1789
+ return captureReference(__classPrivateFieldGet3(this, _DescendantSession_snapshot, "f"));
1790
+ }
1791
+ get generation() {
1792
+ return __classPrivateFieldGet3(this, _DescendantSession_snapshot, "f").generation;
1793
+ }
1794
+ getNodes() {
1795
+ return __classPrivateFieldGet3(this, _DescendantSession_nodes, "f");
1796
+ }
1797
+ getEditor(blockId) {
1798
+ var _a;
1799
+ return (_a = __classPrivateFieldGet3(this, _DescendantSession_records, "f").get(blockId)) === null || _a === void 0 ? void 0 : _a.editor;
1800
+ }
1801
+ getState() {
1802
+ const issues = Object.freeze([...__classPrivateFieldGet3(this, _DescendantSession_records, "f").values()].flatMap((record2) => record2.issues.map((issue) => Object.freeze({ ...issue, reference: record2.descriptor.reference, inputPath: record2.descriptor.inputPath }))));
1803
+ return Object.freeze({
1804
+ status: __classPrivateFieldGet3(this, _DescendantSession_status, "f"),
1805
+ canSave: __classPrivateFieldGet3(this, _DescendantSession_status, "f") === "editing" && issues.length === 0,
1806
+ issues,
1807
+ error: __classPrivateFieldGet3(this, _DescendantSession_error, "f")
1808
+ });
1809
+ }
1810
+ subscribe(listener) {
1811
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertOpen).call(this);
1812
+ __classPrivateFieldGet3(this, _DescendantSession_listeners, "f").add(listener);
1813
+ return () => {
1814
+ __classPrivateFieldGet3(this, _DescendantSession_listeners, "f").delete(listener);
1815
+ };
1816
+ }
1817
+ async save() {
1818
+ if (__classPrivateFieldGet3(this, _DescendantSession_status, "f") === "saving")
1819
+ return { status: "busy" };
1820
+ if (__classPrivateFieldGet3(this, _DescendantSession_status, "f") !== "editing")
1821
+ return { status: "closed" };
1822
+ __classPrivateFieldSet3(this, _DescendantSession_status, "saving", "f");
1823
+ __classPrivateFieldSet3(this, _DescendantSession_error, void 0, "f");
1824
+ for (const record2 of __classPrivateFieldGet3(this, _DescendantSession_records, "f").values())
1825
+ record2.issues = [];
1826
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_notify).call(this);
1827
+ try {
1828
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1829
+ const prepared = /* @__PURE__ */ new Map();
1830
+ for (const record2 of __classPrivateFieldGet3(this, _DescendantSession_records, "f").values()) {
1831
+ try {
1832
+ const result = isPreparedEditor(record2.definition) ? capturePreparedSave(record2.definition, cloneJson(record2.draft), record2.snapshot) : void 0;
1833
+ if (result)
1834
+ prepared.set(record2.snapshot.blockId, result);
1835
+ const issues2 = result ? result.kind === "invalid" ? result.issues : [] : await abortable(record2.definition.validate(cloneJson(record2.draft), record2.snapshot), __classPrivateFieldGet3(this, _DescendantSession_abort, "f").signal);
1836
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1837
+ record2.issues = copyIssues(issues2);
1838
+ } catch (cause) {
1839
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1840
+ throw new DescendantNodeError(record2.descriptor.reference, record2.descriptor.inputPath, cause);
1841
+ }
1842
+ }
1843
+ const issues = this.getState().issues;
1844
+ if (issues.length)
1845
+ return { status: "invalid", issues };
1846
+ const changes = [];
1847
+ const destinations = /* @__PURE__ */ new Set();
1848
+ for (const record2 of __classPrivateFieldGet3(this, _DescendantSession_records, "f").values()) {
1849
+ try {
1850
+ const result = prepared.get(record2.snapshot.blockId);
1851
+ const patch = (result === null || result === void 0 ? void 0 : result.kind) === "ready" ? result.changes : record2.definition.createPatch(cloneJson(record2.draft), record2.snapshot);
1852
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1853
+ if (!Array.isArray(patch))
1854
+ throw new TypeError("createPatch() must return an array.");
1855
+ for (const change of patch) {
1856
+ if (!change || !record2.definition.editableArguments.includes(change.argument) || !isLiteral(change.value)) {
1857
+ throw new TypeError("Patch contains an invalid or disallowed argument.");
1858
+ }
1859
+ const destination = JSON.stringify([record2.snapshot.blockId, change.argument]);
1860
+ if (destinations.has(destination))
1861
+ throw new TypeError("Duplicate subtree write destination.");
1862
+ destinations.add(destination);
1863
+ const slot = Object.hasOwn(record2.snapshot.arguments, change.argument) ? record2.snapshot.arguments[change.argument] : void 0;
1864
+ if ((slot === null || slot === void 0 ? void 0 : slot.kind) !== "literal")
1865
+ throw new TypeError("Only existing literal arguments can be changed.");
1866
+ if (slot.value !== change.value)
1867
+ changes.push(Object.freeze({
1868
+ blockId: record2.snapshot.blockId,
1869
+ argument: change.argument,
1870
+ value: change.value
1871
+ }));
1872
+ }
1873
+ } catch (cause) {
1874
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1875
+ throw new DescendantNodeError(record2.descriptor.reference, record2.descriptor.inputPath, cause);
1876
+ }
1877
+ }
1878
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1879
+ const live = __classPrivateFieldGet3(this, _DescendantSession_host, "f").readSubtree(this.reference);
1880
+ let matches = false;
1881
+ if (live) {
1882
+ try {
1883
+ matches = sameSubtree(__classPrivateFieldGet3(this, _DescendantSession_snapshot, "f"), captureSubtree(live, this.reference, __classPrivateFieldGet3(this, _DescendantSession_limits, "f")).snapshot);
1884
+ } catch (_a) {
1885
+ }
1886
+ }
1887
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1888
+ if (!matches)
1889
+ throw new BlockEditorConflictError("The subtree changed or is no longer editable.");
1890
+ if (changes.length) {
1891
+ __classPrivateFieldSet3(this, _DescendantSession_committing, true, "f");
1892
+ await abortable(__classPrivateFieldGet3(this, _DescendantSession_host, "f").commitSubtree(Object.freeze({
1893
+ expected: __classPrivateFieldGet3(this, _DescendantSession_snapshot, "f"),
1894
+ changes: Object.freeze(changes),
1895
+ signal: __classPrivateFieldGet3(this, _DescendantSession_abort, "f").signal
1896
+ })), __classPrivateFieldGet3(this, _DescendantSession_abort, "f").signal);
1897
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertSaving).call(this);
1898
+ }
1899
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_finish).call(this, "saved");
1900
+ return { status: "saved", changed: changes.length > 0 };
1901
+ } catch (error) {
1902
+ if (!__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this))
1903
+ return __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_closedSaveResult).call(this);
1904
+ __classPrivateFieldSet3(this, _DescendantSession_error, error, "f");
1905
+ return error instanceof BlockEditorConflictError ? { status: "conflict", error } : { status: "failed", error };
1906
+ } finally {
1907
+ __classPrivateFieldSet3(this, _DescendantSession_committing, false, "f");
1908
+ if (__classPrivateFieldGet3(this, _DescendantSession_status, "f") === "saving") {
1909
+ __classPrivateFieldSet3(this, _DescendantSession_status, "editing", "f");
1910
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_notify).call(this);
1911
+ }
1912
+ }
1913
+ }
1914
+ /** Validation may be cancelled; an already dispatched commit must finish atomically. */
1915
+ cancel() {
1916
+ if (!__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this) || __classPrivateFieldGet3(this, _DescendantSession_committing, "f"))
1917
+ return false;
1918
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_finish).call(this, "cancelled");
1919
+ return true;
1920
+ }
1921
+ dispose() {
1922
+ if (__classPrivateFieldGet3(this, _DescendantSession_committing, "f") && __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this))
1923
+ return false;
1924
+ if (__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this))
1925
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_finish).call(this, "disposed");
1926
+ return true;
1927
+ }
1928
+ /** The host must observe the abort signal before any deferred write. */
1929
+ invalidate(reason = new BlockEditorConflictError("The subtree session was invalidated.")) {
1930
+ if (!__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this))
1931
+ return false;
1932
+ __classPrivateFieldSet3(this, _DescendantSession_error, reason, "f");
1933
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_finish).call(this, "invalidated");
1934
+ return true;
1935
+ }
1936
+ };
1937
+ _DescendantSession_snapshot = /* @__PURE__ */ new WeakMap(), _DescendantSession_host = /* @__PURE__ */ new WeakMap(), _DescendantSession_limits = /* @__PURE__ */ new WeakMap(), _DescendantSession_lookup = /* @__PURE__ */ new WeakMap(), _DescendantSession_bindings = /* @__PURE__ */ new WeakMap(), _DescendantSession_records = /* @__PURE__ */ new WeakMap(), _DescendantSession_nodes = /* @__PURE__ */ new WeakMap(), _DescendantSession_listeners = /* @__PURE__ */ new WeakMap(), _DescendantSession_detach = /* @__PURE__ */ new WeakMap(), _DescendantSession_abort = /* @__PURE__ */ new WeakMap(), _DescendantSession_status = /* @__PURE__ */ new WeakMap(), _DescendantSession_committing = /* @__PURE__ */ new WeakMap(), _DescendantSession_error = /* @__PURE__ */ new WeakMap(), _DescendantSession_instances = /* @__PURE__ */ new WeakSet(), _DescendantSession_createRecord = function _DescendantSession_createRecord2(descriptor, definition, snapshot) {
1938
+ const view = new EditorView();
1939
+ const listeners = /* @__PURE__ */ new Set();
1940
+ const editor = Object.freeze({
1941
+ reference: descriptor.reference,
1942
+ getDraft: () => {
1943
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertOpen).call(this);
1944
+ return cloneJson(record2.draft);
1945
+ },
1946
+ setDraft: (draft) => {
1947
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertEditable).call(this);
1948
+ record2.draft = copyDraft(draft);
1949
+ record2.issues = [];
1950
+ __classPrivateFieldSet3(this, _DescendantSession_error, void 0, "f");
1951
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_notify).call(this, record2);
1952
+ },
1953
+ setValidationIssues: (issues) => {
1954
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertEditable).call(this);
1955
+ record2.issues = copyIssues(issues);
1956
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_notify).call(this, record2);
1957
+ },
1958
+ getState: () => Object.freeze({
1959
+ status: __classPrivateFieldGet3(this, _DescendantSession_status, "f") === "invalidated" ? "disposed" : __classPrivateFieldGet3(this, _DescendantSession_status, "f"),
1960
+ canSave: __classPrivateFieldGet3(this, _DescendantSession_status, "f") === "editing" && record2.issues.length === 0,
1961
+ issues: record2.issues,
1962
+ error: __classPrivateFieldGet3(this, _DescendantSession_error, "f")
1963
+ }),
1964
+ subscribe: (listener) => {
1965
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_assertOpen).call(this);
1966
+ listeners.add(listener);
1967
+ return () => {
1968
+ listeners.delete(listener);
1969
+ };
1970
+ },
1971
+ mount: (container) => view.mount(container, definition.mount, editor)
1972
+ });
1973
+ const record2 = {
1974
+ descriptor,
1975
+ definition,
1976
+ snapshot,
1977
+ view,
1978
+ listeners,
1979
+ editor,
1980
+ draft: copyDraft(definition.read(snapshot)),
1981
+ issues: []
1982
+ };
1983
+ return record2;
1984
+ }, _DescendantSession_isOpen = function _DescendantSession_isOpen2() {
1985
+ return __classPrivateFieldGet3(this, _DescendantSession_status, "f") === "editing" || __classPrivateFieldGet3(this, _DescendantSession_status, "f") === "saving";
1986
+ }, _DescendantSession_closedSaveResult = function _DescendantSession_closedSaveResult2() {
1987
+ return __classPrivateFieldGet3(this, _DescendantSession_status, "f") === "invalidated" ? { status: "invalidated" } : { status: "closed" };
1988
+ }, _DescendantSession_assertOpen = function _DescendantSession_assertOpen2() {
1989
+ if (!__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this))
1990
+ throw new Error("The session is closed.");
1991
+ }, _DescendantSession_assertEditable = function _DescendantSession_assertEditable2() {
1992
+ if (__classPrivateFieldGet3(this, _DescendantSession_status, "f") !== "editing")
1993
+ throw new Error("The session is not editable.");
1994
+ }, _DescendantSession_bindingsCurrent = function _DescendantSession_bindingsCurrent2() {
1995
+ return [...__classPrivateFieldGet3(this, _DescendantSession_bindings, "f")].every(([opcode, definition]) => __classPrivateFieldGet3(this, _DescendantSession_lookup, "f").call(this, opcode) === definition);
1996
+ }, _DescendantSession_checkBindings = function _DescendantSession_checkBindings2() {
1997
+ if (!__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_bindingsCurrent).call(this))
1998
+ throw new BlockEditorConflictError("An editor registration changed.");
1999
+ }, _DescendantSession_assertSaving = function _DescendantSession_assertSaving2() {
2000
+ if (__classPrivateFieldGet3(this, _DescendantSession_status, "f") !== "saving")
2001
+ throw new Error("The save was cancelled or invalidated.");
2002
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_checkBindings).call(this);
2003
+ }, _DescendantSession_notify = function _DescendantSession_notify2(record2) {
2004
+ if (record2)
2005
+ observe(record2.listeners);
2006
+ else
2007
+ for (const node of __classPrivateFieldGet3(this, _DescendantSession_records, "f").values())
2008
+ observe(node.listeners);
2009
+ observe(__classPrivateFieldGet3(this, _DescendantSession_listeners, "f"));
2010
+ }, _DescendantSession_watch = function _DescendantSession_watch2(subscribe) {
2011
+ const detach = subscribe();
2012
+ if (typeof detach !== "function")
2013
+ throw new TypeError("Invalidation subscriptions must return a cleanup function.");
2014
+ if (__classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_isOpen).call(this))
2015
+ __classPrivateFieldGet3(this, _DescendantSession_detach, "f").add(detach);
2016
+ else
2017
+ detach();
2018
+ }, _DescendantSession_finish = function _DescendantSession_finish2(status) {
2019
+ __classPrivateFieldSet3(this, _DescendantSession_status, status, "f");
2020
+ __classPrivateFieldGet3(this, _DescendantSession_abort, "f").abort(__classPrivateFieldGet3(this, _DescendantSession_error, "f"));
2021
+ __classPrivateFieldGet3(this, _DescendantSession_instances, "m", _DescendantSession_notify).call(this);
2022
+ for (const record2 of __classPrivateFieldGet3(this, _DescendantSession_records, "f").values()) {
2023
+ try {
2024
+ record2.view.unmount();
2025
+ } catch (error) {
2026
+ report(error);
2027
+ }
2028
+ record2.draft = {};
2029
+ record2.issues = [];
2030
+ record2.listeners.clear();
2031
+ }
2032
+ for (const detach of __classPrivateFieldGet3(this, _DescendantSession_detach, "f")) {
2033
+ try {
2034
+ detach();
2035
+ } catch (error) {
2036
+ report(error);
2037
+ }
2038
+ }
2039
+ __classPrivateFieldGet3(this, _DescendantSession_detach, "f").clear();
2040
+ __classPrivateFieldGet3(this, _DescendantSession_listeners, "f").clear();
2041
+ };
2042
+
2043
+ // dist/registry.js
2044
+ var __classPrivateFieldGet4 = function(receiver, state, kind, f) {
2045
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
2046
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
2047
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
2048
+ };
2049
+ var _EditorRegistry_instances;
2050
+ var _EditorRegistry_editors;
2051
+ var _EditorRegistry_opcodes;
2052
+ var _EditorRegistry_fixedDeclarations;
2053
+ var _EditorRegistry_changes;
2054
+ var _EditorRegistry_register;
2055
+ var _EditorRegistry_notifyChanges;
2056
+ var EditorRegistry = class {
2057
+ constructor() {
2058
+ _EditorRegistry_instances.add(this);
2059
+ _EditorRegistry_editors.set(this, /* @__PURE__ */ new Map());
2060
+ _EditorRegistry_opcodes.set(this, /* @__PURE__ */ new Map());
2061
+ _EditorRegistry_fixedDeclarations.set(this, /* @__PURE__ */ new Map());
2062
+ _EditorRegistry_changes.set(this, /* @__PURE__ */ new Set());
2063
+ }
2064
+ register(definition) {
2065
+ if (isFixedInputEditor(definition))
2066
+ throw new TypeError("Fixed input editors require registerFixed().");
2067
+ if (isPreparedEditor(definition))
2068
+ throw new TypeError("Prepared editors require registerPrepared().");
2069
+ return __classPrivateFieldGet4(this, _EditorRegistry_instances, "m", _EditorRegistry_register).call(this, definition);
2070
+ }
2071
+ registerPrepared(definition) {
2072
+ if (!isPreparedEditor(definition) || typeof definition.prepareSave !== "function") {
2073
+ throw new TypeError("Expected a prepared source editor.");
2074
+ }
2075
+ return __classPrivateFieldGet4(this, _EditorRegistry_instances, "m", _EditorRegistry_register).call(this, definition);
2076
+ }
2077
+ registerFixed(definition) {
2078
+ return __classPrivateFieldGet4(this, _EditorRegistry_instances, "m", _EditorRegistry_register).call(this, captureFixedInputEditorDefinition(definition));
2079
+ }
2080
+ resolve(opcode) {
2081
+ const entry = __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(opcode);
2082
+ if (!entry)
2083
+ return void 0;
2084
+ return Object.freeze({
2085
+ apiVersion: entry.apiVersion,
2086
+ extensionId: entry.extensionId,
2087
+ editorId: entry.editorId,
2088
+ opcodes: entry.opcodes,
2089
+ editableArguments: entry.editableArguments
2090
+ });
2091
+ }
2092
+ /** Stable, callback-free authority for this exact registration, never caller metadata. */
2093
+ resolveFixed(opcode) {
2094
+ const entry = __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(opcode);
2095
+ return entry ? __classPrivateFieldGet4(this, _EditorRegistry_fixedDeclarations, "f").get(entry) : void 0;
2096
+ }
2097
+ open(reference, host) {
2098
+ const capturedReference = captureReference(reference);
2099
+ const live = host.readBlock(capturedReference);
2100
+ if (!live)
2101
+ return void 0;
2102
+ const entry = __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(live.opcode);
2103
+ if (!entry) {
2104
+ captureSnapshot(live, capturedReference);
2105
+ return void 0;
2106
+ }
2107
+ if (isFixedInputEditor(entry)) {
2108
+ if (!supportsFixedInputEditing(host))
2109
+ return void 0;
2110
+ const fixed = host.fixedInputs.readBlock(capturedReference, entry);
2111
+ if (!fixed || fixed.opcode !== live.opcode)
2112
+ return void 0;
2113
+ const snapshot2 = captureFixedSnapshot(fixed, capturedReference, entry);
2114
+ return new EditorSession(entry, snapshot2, host, () => __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(snapshot2.opcode) === entry, (listener) => {
2115
+ __classPrivateFieldGet4(this, _EditorRegistry_changes, "f").add(listener);
2116
+ return () => {
2117
+ __classPrivateFieldGet4(this, _EditorRegistry_changes, "f").delete(listener);
2118
+ };
2119
+ });
2120
+ }
2121
+ const snapshot = captureSnapshot(live, capturedReference);
2122
+ return new EditorSession(entry, snapshot, host, isPreparedEditor(entry) ? () => __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(snapshot.opcode) === entry : void 0);
2123
+ }
2124
+ openDescendants(reference, host, limits) {
2125
+ if (!supportsDescendantEditing(host))
2126
+ return void 0;
2127
+ const port = host.descendants;
2128
+ const capturedReference = captureReference(reference);
2129
+ const resolvedLimits = descendantLimits(limits);
2130
+ const live = port.readSubtree(capturedReference);
2131
+ if (!live)
2132
+ return void 0;
2133
+ const captured = captureSubtree(live, capturedReference, resolvedLimits);
2134
+ const root = __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(captured.snapshot.nodes[0].block.opcode);
2135
+ if (!root || isFixedInputEditor(root))
2136
+ return void 0;
2137
+ return new DescendantSession(captured, port, (opcode) => {
2138
+ const entry = __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").get(opcode);
2139
+ return entry && !isFixedInputEditor(entry) ? entry : void 0;
2140
+ }, (listener) => {
2141
+ __classPrivateFieldGet4(this, _EditorRegistry_changes, "f").add(listener);
2142
+ return () => {
2143
+ __classPrivateFieldGet4(this, _EditorRegistry_changes, "f").delete(listener);
2144
+ };
2145
+ }, resolvedLimits);
2146
+ }
2147
+ };
2148
+ _EditorRegistry_editors = /* @__PURE__ */ new WeakMap(), _EditorRegistry_opcodes = /* @__PURE__ */ new WeakMap(), _EditorRegistry_fixedDeclarations = /* @__PURE__ */ new WeakMap(), _EditorRegistry_changes = /* @__PURE__ */ new WeakMap(), _EditorRegistry_instances = /* @__PURE__ */ new WeakSet(), _EditorRegistry_register = function _EditorRegistry_register2(definition) {
2149
+ if (definition.apiVersion !== EDITOR_API_VERSION)
2150
+ throw new TypeError("Unsupported editor API version.");
2151
+ if (!/^[a-z0-9]+$/i.test(definition.extensionId) || !/^[a-z0-9][a-z0-9._-]*$/i.test(definition.editorId)) {
2152
+ throw new TypeError("Invalid extension ID or editor ID.");
2153
+ }
2154
+ if (!Array.isArray(definition.opcodes) || definition.opcodes.length === 0 || new Set(definition.opcodes).size !== definition.opcodes.length || definition.opcodes.some((opcode) => typeof opcode !== "string" || !opcode.startsWith(`${definition.extensionId}_`) || opcode === `${definition.extensionId}_`)) {
2155
+ throw new TypeError("Expected distinct full opcodes belonging to the extension.");
2156
+ }
2157
+ if (!Array.isArray(definition.editableArguments) || new Set(definition.editableArguments).size !== definition.editableArguments.length || definition.editableArguments.some((name) => typeof name !== "string" || !name)) {
2158
+ throw new TypeError("Expected distinct editable argument names.");
2159
+ }
2160
+ if (typeof definition.read !== "function" || typeof definition.validate !== "function" || typeof definition.createPatch !== "function" || definition.mount !== void 0 && typeof definition.mount !== "function") {
2161
+ throw new TypeError("Invalid editor callbacks.");
2162
+ }
2163
+ const key = JSON.stringify([definition.extensionId, definition.editorId]);
2164
+ if (__classPrivateFieldGet4(this, _EditorRegistry_editors, "f").has(key) || definition.opcodes.some((opcode) => __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").has(opcode))) {
2165
+ throw new Error("The editor ID or an opcode is already registered.");
2166
+ }
2167
+ const entry = Object.freeze({
2168
+ ...definition,
2169
+ opcodes: Object.freeze([...definition.opcodes]),
2170
+ editableArguments: Object.freeze([...definition.editableArguments])
2171
+ });
2172
+ if (isFixedInputEditor(entry))
2173
+ __classPrivateFieldGet4(this, _EditorRegistry_fixedDeclarations, "f").set(entry, captureFixedInputDeclaration(entry));
2174
+ __classPrivateFieldGet4(this, _EditorRegistry_editors, "f").set(key, entry);
2175
+ for (const opcode of entry.opcodes)
2176
+ __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").set(opcode, entry);
2177
+ __classPrivateFieldGet4(this, _EditorRegistry_instances, "m", _EditorRegistry_notifyChanges).call(this);
2178
+ return () => {
2179
+ if (__classPrivateFieldGet4(this, _EditorRegistry_editors, "f").get(key) !== entry)
2180
+ return;
2181
+ __classPrivateFieldGet4(this, _EditorRegistry_editors, "f").delete(key);
2182
+ for (const opcode of entry.opcodes)
2183
+ __classPrivateFieldGet4(this, _EditorRegistry_opcodes, "f").delete(opcode);
2184
+ __classPrivateFieldGet4(this, _EditorRegistry_fixedDeclarations, "f").delete(entry);
2185
+ __classPrivateFieldGet4(this, _EditorRegistry_instances, "m", _EditorRegistry_notifyChanges).call(this);
2186
+ };
2187
+ }, _EditorRegistry_notifyChanges = function _EditorRegistry_notifyChanges2() {
2188
+ for (const listener of [...__classPrivateFieldGet4(this, _EditorRegistry_changes, "f")])
2189
+ listener();
2190
+ };
2191
+
2192
+ export {
2193
+ cloneJson,
2194
+ validateFixedBranchChanges,
2195
+ captureFixedInputDeclaration,
2196
+ captureFixedSnapshot,
2197
+ sameFixedSnapshot,
2198
+ createFixedInputEditor,
2199
+ FIXED_INPUTS_CAPABILITY,
2200
+ supportsFixedInputs,
2201
+ supportsFixedInputEditing,
2202
+ EditorSession,
2203
+ DescendantSession,
2204
+ EditorRegistry
2205
+ };
2206
+ //# sourceMappingURL=shared-P2JBRZFP.js.map