d5-mermaid 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,4079 @@
1
+ // src/d5-domain/detector.ts
2
+ function detectDomain(text) {
3
+ return text.trimStart().startsWith("d5-domain");
4
+ }
5
+
6
+ // src/shared/direction.ts
7
+ function normalizeDirection(value) {
8
+ const d = value.trim().toUpperCase();
9
+ const normalized = d === "TD" ? "TB" : d;
10
+ return normalized === "LR" || normalized === "RL" || normalized === "TB" || normalized === "BT" ? normalized : null;
11
+ }
12
+ var DIRECTION_RE = /^\s*direction\s+(LR|RL|TB|TD|BT)\s*$/i;
13
+
14
+ // src/d5-domain/db.ts
15
+ var DEFAULT_DIRECTION = "TB";
16
+ var D5DomainDb = class {
17
+ domain;
18
+ subdomains = [];
19
+ relationships = [];
20
+ direction = DEFAULT_DIRECTION;
21
+ title;
22
+ accTitle;
23
+ accDescription;
24
+ getDomain() {
25
+ return this.domain;
26
+ }
27
+ setDomain(id, label) {
28
+ this.domain = { id, label };
29
+ }
30
+ getSubdomains() {
31
+ return this.subdomains;
32
+ }
33
+ addSubdomain(id, label, type) {
34
+ this.subdomains.push({ id, label, type });
35
+ }
36
+ getRelationships() {
37
+ return this.relationships;
38
+ }
39
+ addRelationship(source, target, label) {
40
+ this.relationships.push({ source, target, label });
41
+ }
42
+ /** Accepts `LR` / `RL` / `TB` / `TD` / `BT` (case-insensitive); `TD` is stored as `TB`. */
43
+ setDirection(dir) {
44
+ const normalized = normalizeDirection(dir);
45
+ if (normalized) this.direction = normalized;
46
+ }
47
+ getDirection() {
48
+ return this.direction;
49
+ }
50
+ clear() {
51
+ this.domain = void 0;
52
+ this.subdomains = [];
53
+ this.relationships = [];
54
+ this.direction = DEFAULT_DIRECTION;
55
+ this.title = void 0;
56
+ this.accTitle = void 0;
57
+ this.accDescription = void 0;
58
+ }
59
+ setTitle(title) {
60
+ this.title = title;
61
+ }
62
+ getTitle() {
63
+ return this.title;
64
+ }
65
+ // Mermaid DiagramDB interface methods
66
+ setDiagramTitle(title) {
67
+ this.setTitle(title);
68
+ }
69
+ getDiagramTitle() {
70
+ return this.title ?? "";
71
+ }
72
+ setAccTitle(title) {
73
+ this.accTitle = title;
74
+ }
75
+ getAccTitle() {
76
+ return this.accTitle ?? "";
77
+ }
78
+ setAccDescription(desc) {
79
+ this.accDescription = desc;
80
+ }
81
+ getAccDescription() {
82
+ return this.accDescription ?? "";
83
+ }
84
+ };
85
+
86
+ // src/d5-domain/parser.ts
87
+ var TITLE_RE = /^\s*title\s+(.+)$/;
88
+ var DOMAIN_RE = /^\s*Domain\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)\s*\{/;
89
+ var SUBDOMAIN_RE = /^\s*Subdomain\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*(\w+)\s*\)/;
90
+ var REL_RE = /^\s*Rel\(\s*(\w+)\s*,\s*(\w+)\s*(?:,\s*"([^"]+)")?\s*\)/;
91
+ var COMMENT_LINE_RE = /^\s*%%/;
92
+ function stripInlineComment(line) {
93
+ const idx = line.indexOf("%%");
94
+ return idx === -1 ? line : line.slice(0, idx);
95
+ }
96
+ function parse(text, db) {
97
+ const lines = text.split("\n");
98
+ for (const raw of lines) {
99
+ if (COMMENT_LINE_RE.test(raw)) continue;
100
+ const line = stripInlineComment(raw).trim();
101
+ if (line === "" || line === "d5-domain") continue;
102
+ let m;
103
+ if (m = line.match(DIRECTION_RE)) {
104
+ db.setDirection(m[1]);
105
+ } else if (m = line.match(TITLE_RE)) {
106
+ db.setTitle(m[1].trim());
107
+ } else if (m = line.match(DOMAIN_RE)) {
108
+ db.setDomain(m[1], m[2]);
109
+ } else if (m = line.match(SUBDOMAIN_RE)) {
110
+ db.addSubdomain(m[1], m[2], m[3]);
111
+ } else if (m = line.match(REL_RE)) {
112
+ db.addRelationship(m[1], m[2], m[3] ?? "");
113
+ }
114
+ }
115
+ }
116
+
117
+ // node_modules/@dagrejs/dagre/dist/dagre.esm.js
118
+ var ge = Object.defineProperty;
119
+ var hn = (e, n, t) => n in e ? ge(e, n, { enumerable: true, configurable: true, writable: true, value: t }) : e[n] = t;
120
+ var fn = (e, n) => {
121
+ for (var t in n) ge(e, t, { get: n[t], enumerable: true });
122
+ };
123
+ var pe = (e, n, t) => hn(e, n + "" , t);
124
+ var z = {};
125
+ fn(z, { Graph: () => p, alg: () => R, json: () => ye, version: () => pn });
126
+ var bn = Object.defineProperty;
127
+ var Le = (e, n) => {
128
+ for (var t in n) bn(e, t, { get: n[t], enumerable: true });
129
+ };
130
+ var p = class {
131
+ constructor(e) {
132
+ this._isDirected = true, this._isMultigraph = false, this._isCompound = false, this._nodes = {}, this._in = {}, this._preds = {}, this._out = {}, this._sucs = {}, this._edgeObjs = {}, this._edgeLabels = {}, this._nodeCount = 0, this._edgeCount = 0, this._defaultNodeLabelFn = () => {
133
+ }, this._defaultEdgeLabelFn = () => {
134
+ }, e && (this._isDirected = "directed" in e ? e.directed : true, this._isMultigraph = "multigraph" in e ? e.multigraph : false, this._isCompound = "compound" in e ? e.compound : false), this._isCompound && (this._parent = {}, this._children = {}, this._children["\0"] = {});
135
+ }
136
+ isDirected() {
137
+ return this._isDirected;
138
+ }
139
+ isMultigraph() {
140
+ return this._isMultigraph;
141
+ }
142
+ isCompound() {
143
+ return this._isCompound;
144
+ }
145
+ setGraph(e) {
146
+ return this._label = e, this;
147
+ }
148
+ graph() {
149
+ return this._label;
150
+ }
151
+ setDefaultNodeLabel(e) {
152
+ return typeof e != "function" ? this._defaultNodeLabelFn = () => e : this._defaultNodeLabelFn = e, this;
153
+ }
154
+ nodeCount() {
155
+ return this._nodeCount;
156
+ }
157
+ nodes() {
158
+ return Object.keys(this._nodes);
159
+ }
160
+ sources() {
161
+ return this.nodes().filter((e) => Object.keys(this._in[e]).length === 0);
162
+ }
163
+ sinks() {
164
+ return this.nodes().filter((e) => Object.keys(this._out[e]).length === 0);
165
+ }
166
+ setNodes(e, n) {
167
+ return e.forEach((t) => {
168
+ n !== void 0 ? this.setNode(t, n) : this.setNode(t);
169
+ }), this;
170
+ }
171
+ setNode(e, n) {
172
+ return e in this._nodes ? (arguments.length > 1 && (this._nodes[e] = n), this) : (this._nodes[e] = arguments.length > 1 ? n : this._defaultNodeLabelFn(e), this._isCompound && (this._parent[e] = "\0", this._children[e] = {}, this._children["\0"][e] = true), this._in[e] = {}, this._preds[e] = {}, this._out[e] = {}, this._sucs[e] = {}, ++this._nodeCount, this);
173
+ }
174
+ node(e) {
175
+ return this._nodes[e];
176
+ }
177
+ hasNode(e) {
178
+ return e in this._nodes;
179
+ }
180
+ removeNode(e) {
181
+ if (e in this._nodes) {
182
+ let n = (t) => this.removeEdge(this._edgeObjs[t]);
183
+ delete this._nodes[e], this._isCompound && (this._removeFromParentsChildList(e), delete this._parent[e], this.children(e).forEach((t) => {
184
+ this.setParent(t);
185
+ }), delete this._children[e]), Object.keys(this._in[e]).forEach(n), delete this._in[e], delete this._preds[e], Object.keys(this._out[e]).forEach(n), delete this._out[e], delete this._sucs[e], --this._nodeCount;
186
+ }
187
+ return this;
188
+ }
189
+ setParent(e, n) {
190
+ if (!this._isCompound) throw new Error("Cannot set parent in a non-compound graph");
191
+ if (n === void 0) n = "\0";
192
+ else {
193
+ n += "";
194
+ for (let t = n; t !== void 0; t = this.parent(t)) if (t === e) throw new Error("Setting " + n + " as parent of " + e + " would create a cycle");
195
+ this.setNode(n);
196
+ }
197
+ return this.setNode(e), this._removeFromParentsChildList(e), this._parent[e] = n, this._children[n][e] = true, this;
198
+ }
199
+ parent(e) {
200
+ if (this._isCompound) {
201
+ let n = this._parent[e];
202
+ if (n !== "\0") return n;
203
+ }
204
+ }
205
+ children(e = "\0") {
206
+ if (this._isCompound) {
207
+ let n = this._children[e];
208
+ if (n) return Object.keys(n);
209
+ } else {
210
+ if (e === "\0") return this.nodes();
211
+ if (this.hasNode(e)) return [];
212
+ }
213
+ return [];
214
+ }
215
+ predecessors(e) {
216
+ let n = this._preds[e];
217
+ if (n) return Object.keys(n);
218
+ }
219
+ successors(e) {
220
+ let n = this._sucs[e];
221
+ if (n) return Object.keys(n);
222
+ }
223
+ neighbors(e) {
224
+ let n = this.predecessors(e);
225
+ if (n) {
226
+ let t = new Set(n);
227
+ for (let r of this.successors(e)) t.add(r);
228
+ return Array.from(t.values());
229
+ }
230
+ }
231
+ isLeaf(e) {
232
+ let n;
233
+ return this.isDirected() ? n = this.successors(e) : n = this.neighbors(e), n.length === 0;
234
+ }
235
+ filterNodes(e) {
236
+ let n = new this.constructor({ directed: this._isDirected, multigraph: this._isMultigraph, compound: this._isCompound });
237
+ n.setGraph(this.graph()), Object.entries(this._nodes).forEach(([o, i]) => {
238
+ e(o) && n.setNode(o, i);
239
+ }), Object.values(this._edgeObjs).forEach((o) => {
240
+ n.hasNode(o.v) && n.hasNode(o.w) && n.setEdge(o, this.edge(o));
241
+ });
242
+ let t = {}, r = (o) => {
243
+ let i = this.parent(o);
244
+ return !i || n.hasNode(i) ? (t[o] = i != null ? i : void 0, i != null ? i : void 0) : i in t ? t[i] : r(i);
245
+ };
246
+ return this._isCompound && n.nodes().forEach((o) => n.setParent(o, r(o))), n;
247
+ }
248
+ setDefaultEdgeLabel(e) {
249
+ return typeof e != "function" ? this._defaultEdgeLabelFn = () => e : this._defaultEdgeLabelFn = e, this;
250
+ }
251
+ edgeCount() {
252
+ return this._edgeCount;
253
+ }
254
+ edges() {
255
+ return Object.values(this._edgeObjs);
256
+ }
257
+ setPath(e, n) {
258
+ return e.reduce((t, r) => (n !== void 0 ? this.setEdge(t, r, n) : this.setEdge(t, r), r)), this;
259
+ }
260
+ setEdge(e, n, t, r) {
261
+ let o, i, s, a, d = false;
262
+ typeof e == "object" && e !== null && "v" in e ? (o = e.v, i = e.w, s = e.name, arguments.length === 2 && (a = n, d = true)) : (o = e, i = n, s = r, arguments.length > 2 && (a = t, d = true)), o = "" + o, i = "" + i, s !== void 0 && (s = "" + s);
263
+ let l = C(this._isDirected, o, i, s);
264
+ if (l in this._edgeLabels) return d && (this._edgeLabels[l] = a), this;
265
+ if (s !== void 0 && !this._isMultigraph) throw new Error("Cannot set a named edge when isMultigraph = false");
266
+ this.setNode(o), this.setNode(i), this._edgeLabels[l] = d ? a : this._defaultEdgeLabelFn(o, i, s);
267
+ let u = gn(this._isDirected, o, i, s);
268
+ return o = u.v, i = u.w, Object.freeze(u), this._edgeObjs[l] = u, me(this._preds[i], o), me(this._sucs[o], i), this._in[i][l] = u, this._out[o][l] = u, this._edgeCount++, this;
269
+ }
270
+ edge(e, n, t) {
271
+ let r = arguments.length === 1 ? Y(this._isDirected, e) : C(this._isDirected, e, n, t);
272
+ return this._edgeLabels[r];
273
+ }
274
+ edgeAsObj(e, n, t) {
275
+ let r = arguments.length === 1 ? this.edge(e) : this.edge(e, n, t);
276
+ return typeof r != "object" ? { label: r } : r;
277
+ }
278
+ hasEdge(e, n, t) {
279
+ return (arguments.length === 1 ? Y(this._isDirected, e) : C(this._isDirected, e, n, t)) in this._edgeLabels;
280
+ }
281
+ removeEdge(e, n, t) {
282
+ let r = arguments.length === 1 ? Y(this._isDirected, e) : C(this._isDirected, e, n, t), o = this._edgeObjs[r];
283
+ if (o) {
284
+ let i = o.v, s = o.w;
285
+ delete this._edgeLabels[r], delete this._edgeObjs[r], Ee(this._preds[s], i), Ee(this._sucs[i], s), delete this._in[s][r], delete this._out[i][r], this._edgeCount--;
286
+ }
287
+ return this;
288
+ }
289
+ inEdges(e, n) {
290
+ return this.isDirected() ? this.filterEdges(this._in[e], e, n) : this.nodeEdges(e, n);
291
+ }
292
+ outEdges(e, n) {
293
+ return this.isDirected() ? this.filterEdges(this._out[e], e, n) : this.nodeEdges(e, n);
294
+ }
295
+ nodeEdges(e, n) {
296
+ if (e in this._nodes) return this.filterEdges({ ...this._in[e], ...this._out[e] }, e, n);
297
+ }
298
+ _removeFromParentsChildList(e) {
299
+ delete this._children[this._parent[e]][e];
300
+ }
301
+ filterEdges(e, n, t) {
302
+ if (!e) return;
303
+ let r = Object.values(e);
304
+ return t ? r.filter((o) => o.v === n && o.w === t || o.v === t && o.w === n) : r;
305
+ }
306
+ };
307
+ function me(e, n) {
308
+ e[n] ? e[n]++ : e[n] = 1;
309
+ }
310
+ function Ee(e, n) {
311
+ e[n] !== void 0 && !--e[n] && delete e[n];
312
+ }
313
+ function C(e, n, t, r) {
314
+ let o = "" + n, i = "" + t;
315
+ if (!e && o > i) {
316
+ let s = o;
317
+ o = i, i = s;
318
+ }
319
+ return o + "" + i + "" + (r === void 0 ? "\0" : r);
320
+ }
321
+ function gn(e, n, t, r) {
322
+ let o = "" + n, i = "" + t;
323
+ if (!e && o > i) {
324
+ let a = o;
325
+ o = i, i = a;
326
+ }
327
+ let s = { v: o, w: i };
328
+ return r && (s.name = r), s;
329
+ }
330
+ function Y(e, n) {
331
+ return C(e, n.v, n.w, n.name);
332
+ }
333
+ var pn = "4.0.1";
334
+ var ye = {};
335
+ Le(ye, { read: () => yn, write: () => mn });
336
+ function mn(e) {
337
+ let n = { options: { directed: e.isDirected(), multigraph: e.isMultigraph(), compound: e.isCompound() }, nodes: En(e), edges: Ln(e) }, t = e.graph();
338
+ return t !== void 0 && (n.value = structuredClone(t)), n;
339
+ }
340
+ function En(e) {
341
+ return e.nodes().map((n) => {
342
+ let t = e.node(n), r = e.parent(n), o = { v: n };
343
+ return t !== void 0 && (o.value = t), r !== void 0 && (o.parent = r), o;
344
+ });
345
+ }
346
+ function Ln(e) {
347
+ return e.edges().map((n) => {
348
+ let t = e.edge(n), r = { v: n.v, w: n.w };
349
+ return n.name !== void 0 && (r.name = n.name), t !== void 0 && (r.value = t), r;
350
+ });
351
+ }
352
+ function yn(e) {
353
+ let n = new p(e.options);
354
+ return e.value !== void 0 && n.setGraph(e.value), e.nodes.forEach((t) => {
355
+ n.setNode(t.v, t.value), t.parent && n.setParent(t.v, t.parent);
356
+ }), e.edges.forEach((t) => {
357
+ n.setEdge({ v: t.v, w: t.w, name: t.name }, t.value);
358
+ }), n;
359
+ }
360
+ var R = {};
361
+ Le(R, { CycleException: () => D, bellmanFord: () => we, components: () => Gn, dijkstra: () => F, dijkstraAll: () => _n, findCycles: () => xn, floydWarshall: () => On, isAcyclic: () => Cn, postorder: () => Pn, preorder: () => Mn, prim: () => jn, shortestPaths: () => Sn, tarjan: () => Ge, topsort: () => ke });
362
+ var wn = () => 1;
363
+ function we(e, n, t, r) {
364
+ return Nn(e, String(n), t || wn, r || function(o) {
365
+ return e.outEdges(o);
366
+ });
367
+ }
368
+ function Nn(e, n, t, r) {
369
+ let o = {}, i, s = 0, a = e.nodes(), d = function(c) {
370
+ let h = t(c);
371
+ o[c.v].distance + h < o[c.w].distance && (o[c.w] = { distance: o[c.v].distance + h, predecessor: c.v }, i = true);
372
+ }, l = function() {
373
+ a.forEach(function(c) {
374
+ r(c).forEach(function(h) {
375
+ let f = h.v === c ? h.v : h.w, g = f === h.v ? h.w : h.v;
376
+ d({ v: f, w: g });
377
+ });
378
+ });
379
+ };
380
+ a.forEach(function(c) {
381
+ let h = c === n ? 0 : Number.POSITIVE_INFINITY;
382
+ o[c] = { distance: h, predecessor: "" };
383
+ });
384
+ let u = a.length;
385
+ for (let c = 1; c < u && (i = false, s++, l(), !!i); c++) ;
386
+ if (s === u - 1 && (i = false, l(), i)) throw new Error("The graph contains a negative weight cycle");
387
+ return o;
388
+ }
389
+ function Gn(e) {
390
+ let n = {}, t = [], r;
391
+ function o(i) {
392
+ i in n || (n[i] = true, r.push(i), e.successors(i).forEach(o), e.predecessors(i).forEach(o));
393
+ }
394
+ return e.nodes().forEach(function(i) {
395
+ r = [], o(i), r.length && t.push(r);
396
+ }), t;
397
+ }
398
+ var Ne = class {
399
+ constructor() {
400
+ this._arr = [], this._keyIndices = {};
401
+ }
402
+ size() {
403
+ return this._arr.length;
404
+ }
405
+ keys() {
406
+ return this._arr.map((e) => e.key);
407
+ }
408
+ has(e) {
409
+ return e in this._keyIndices;
410
+ }
411
+ priority(e) {
412
+ let n = this._keyIndices[e];
413
+ if (n !== void 0) return this._arr[n].priority;
414
+ }
415
+ min() {
416
+ if (this.size() === 0) throw new Error("Queue underflow");
417
+ return this._arr[0].key;
418
+ }
419
+ add(e, n) {
420
+ let t = this._keyIndices, r = String(e);
421
+ if (!(r in t)) {
422
+ let o = this._arr, i = o.length;
423
+ return t[r] = i, o.push({ key: r, priority: n }), this._decrease(i), true;
424
+ }
425
+ return false;
426
+ }
427
+ removeMin() {
428
+ this._swap(0, this._arr.length - 1);
429
+ let e = this._arr.pop();
430
+ return delete this._keyIndices[e.key], this._heapify(0), e.key;
431
+ }
432
+ decrease(e, n) {
433
+ let t = this._keyIndices[e];
434
+ if (t === void 0) throw new Error(`Key not found: ${e}`);
435
+ let r = this._arr[t].priority;
436
+ if (n > r) throw new Error(`New priority is greater than current priority. Key: ${e} Old: ${r} New: ${n}`);
437
+ this._arr[t].priority = n, this._decrease(t);
438
+ }
439
+ _heapify(e) {
440
+ let n = this._arr, t = 2 * e, r = t + 1, o = e;
441
+ t < n.length && (o = n[t].priority < n[o].priority ? t : o, r < n.length && (o = n[r].priority < n[o].priority ? r : o), o !== e && (this._swap(e, o), this._heapify(o)));
442
+ }
443
+ _decrease(e) {
444
+ let n = this._arr, t = n[e].priority, r;
445
+ for (; e !== 0 && (r = e >> 1, !(n[r].priority < t)); ) this._swap(e, r), e = r;
446
+ }
447
+ _swap(e, n) {
448
+ let t = this._arr, r = this._keyIndices, o = t[e], i = t[n];
449
+ t[e] = i, t[n] = o, r[i.key] = e, r[o.key] = n;
450
+ }
451
+ };
452
+ var kn = () => 1;
453
+ function F(e, n, t, r) {
454
+ let o = function(i) {
455
+ return e.outEdges(i);
456
+ };
457
+ return vn(e, String(n), t || kn, r || o);
458
+ }
459
+ function vn(e, n, t, r) {
460
+ let o = {}, i = new Ne(), s, a, d = function(l) {
461
+ let u = l.v !== s ? l.v : l.w, c = o[u], h = t(l), f = a.distance + h;
462
+ if (h < 0) throw new Error("dijkstra does not allow negative edge weights. Bad edge: " + l + " Weight: " + h);
463
+ f < c.distance && (c.distance = f, c.predecessor = s, i.decrease(u, f));
464
+ };
465
+ for (e.nodes().forEach(function(l) {
466
+ let u = l === n ? 0 : Number.POSITIVE_INFINITY;
467
+ o[l] = { distance: u, predecessor: "" }, i.add(l, u);
468
+ }); i.size() > 0 && (s = i.removeMin(), a = o[s], a.distance !== Number.POSITIVE_INFINITY); ) r(s).forEach(d);
469
+ return o;
470
+ }
471
+ function _n(e, n, t) {
472
+ return e.nodes().reduce(function(r, o) {
473
+ return r[o] = F(e, o, n, t), r;
474
+ }, {});
475
+ }
476
+ function Ge(e) {
477
+ let n = 0, t = [], r = {}, o = [];
478
+ function i(s) {
479
+ let a = r[s] = { onStack: true, lowlink: n, index: n++ };
480
+ if (t.push(s), e.successors(s).forEach(function(d) {
481
+ d in r ? r[d].onStack && (a.lowlink = Math.min(a.lowlink, r[d].index)) : (i(d), a.lowlink = Math.min(a.lowlink, r[d].lowlink));
482
+ }), a.lowlink === a.index) {
483
+ let d = [], l;
484
+ do
485
+ l = t.pop(), r[l].onStack = false, d.push(l);
486
+ while (s !== l);
487
+ o.push(d);
488
+ }
489
+ }
490
+ return e.nodes().forEach(function(s) {
491
+ s in r || i(s);
492
+ }), o;
493
+ }
494
+ function xn(e) {
495
+ return Ge(e).filter(function(n) {
496
+ return n.length > 1 || n.length === 1 && e.hasEdge(n[0], n[0]);
497
+ });
498
+ }
499
+ var Tn = () => 1;
500
+ function On(e, n, t) {
501
+ return In(e, n || Tn, t || function(r) {
502
+ return e.outEdges(r);
503
+ });
504
+ }
505
+ function In(e, n, t) {
506
+ let r = {}, o = e.nodes();
507
+ return o.forEach(function(i) {
508
+ r[i] = {}, r[i][i] = { distance: 0, predecessor: "" }, o.forEach(function(s) {
509
+ i !== s && (r[i][s] = { distance: Number.POSITIVE_INFINITY, predecessor: "" });
510
+ }), t(i).forEach(function(s) {
511
+ let a = s.v === i ? s.w : s.v, d = n(s);
512
+ r[i][a] = { distance: d, predecessor: i };
513
+ });
514
+ }), o.forEach(function(i) {
515
+ let s = r[i];
516
+ o.forEach(function(a) {
517
+ let d = r[a];
518
+ o.forEach(function(l) {
519
+ let u = d[i], c = s[l], h = d[l], f = u.distance + c.distance;
520
+ f < h.distance && (h.distance = f, h.predecessor = c.predecessor);
521
+ });
522
+ });
523
+ }), r;
524
+ }
525
+ var D = class extends Error {
526
+ constructor(...e) {
527
+ super(...e);
528
+ }
529
+ };
530
+ function ke(e) {
531
+ let n = {}, t = {}, r = [];
532
+ function o(i) {
533
+ if (i in t) throw new D();
534
+ i in n || (t[i] = true, n[i] = true, e.predecessors(i).forEach(o), delete t[i], r.push(i));
535
+ }
536
+ if (e.sinks().forEach(o), Object.keys(n).length !== e.nodeCount()) throw new D();
537
+ return r;
538
+ }
539
+ function Cn(e) {
540
+ try {
541
+ ke(e);
542
+ } catch (n) {
543
+ if (n instanceof D) return false;
544
+ throw n;
545
+ }
546
+ return true;
547
+ }
548
+ function Rn(e, n, t, r, o) {
549
+ Array.isArray(n) || (n = [n]);
550
+ let i = ((a) => {
551
+ var d;
552
+ return (d = e.isDirected() ? e.successors(a) : e.neighbors(a)) != null ? d : [];
553
+ }), s = {};
554
+ return n.forEach(function(a) {
555
+ if (!e.hasNode(a)) throw new Error("Graph does not have node: " + a);
556
+ o = ve(e, a, t === "post", s, i, r, o);
557
+ }), o;
558
+ }
559
+ function ve(e, n, t, r, o, i, s) {
560
+ return n in r || (r[n] = true, t || (s = i(s, n)), o(n).forEach(function(a) {
561
+ s = ve(e, a, t, r, o, i, s);
562
+ }), t && (s = i(s, n))), s;
563
+ }
564
+ function _e(e, n, t) {
565
+ return Rn(e, n, t, function(r, o) {
566
+ return r.push(o), r;
567
+ }, []);
568
+ }
569
+ function Pn(e, n) {
570
+ return _e(e, n, "post");
571
+ }
572
+ function Mn(e, n) {
573
+ return _e(e, n, "pre");
574
+ }
575
+ function jn(e, n) {
576
+ let t = new p(), r = {}, o = new Ne(), i;
577
+ function s(d) {
578
+ let l = d.v === i ? d.w : d.v, u = o.priority(l);
579
+ if (u !== void 0) {
580
+ let c = n(d);
581
+ c < u && (r[l] = i, o.decrease(l, c));
582
+ }
583
+ }
584
+ if (e.nodeCount() === 0) return t;
585
+ e.nodes().forEach(function(d) {
586
+ o.add(d, Number.POSITIVE_INFINITY), t.setNode(d);
587
+ }), o.decrease(e.nodes()[0], 0);
588
+ let a = false;
589
+ for (; o.size() > 0; ) {
590
+ if (i = o.removeMin(), i in r) t.setEdge(i, r[i]);
591
+ else {
592
+ if (a) throw new Error("Input graph is not connected: " + e);
593
+ a = true;
594
+ }
595
+ e.nodeEdges(i).forEach(s);
596
+ }
597
+ return t;
598
+ }
599
+ function Sn(e, n, t, r) {
600
+ return Fn(e, n, t, r != null ? r : ((o) => {
601
+ let i = e.outEdges(o);
602
+ return i != null ? i : [];
603
+ }));
604
+ }
605
+ function Fn(e, n, t, r) {
606
+ if (t === void 0) return F(e, n, t, r);
607
+ let o = false, i = e.nodes();
608
+ for (let s = 0; s < i.length; s++) {
609
+ let a = r(i[s]);
610
+ for (let d = 0; d < a.length; d++) {
611
+ let l = a[d], u = l.v === i[s] ? l.v : l.w, c = u === l.v ? l.w : l.v;
612
+ t({ v: u, w: c }) < 0 && (o = true);
613
+ }
614
+ if (o) return we(e, n, t, r);
615
+ }
616
+ return F(e, n, t, r);
617
+ }
618
+ function w(e, n, t, r) {
619
+ let o = r;
620
+ for (; e.hasNode(o); ) o = j(r);
621
+ return t.dummy = n, e.setNode(o, t), o;
622
+ }
623
+ function xe(e) {
624
+ let n = new p().setGraph(e.graph());
625
+ return e.nodes().forEach((t) => n.setNode(t, e.node(t))), e.edges().forEach((t) => {
626
+ let r = n.edge(t.v, t.w) || { weight: 0, minlen: 1 }, o = e.edge(t);
627
+ n.setEdge(t.v, t.w, { weight: r.weight + o.weight, minlen: Math.max(r.minlen, o.minlen) });
628
+ }), n;
629
+ }
630
+ function A(e) {
631
+ let n = new p({ multigraph: e.isMultigraph() }).setGraph(e.graph());
632
+ return e.nodes().forEach((t) => {
633
+ e.children(t).length || n.setNode(t, e.node(t));
634
+ }), e.edges().forEach((t) => {
635
+ n.setEdge(t, e.edge(t));
636
+ }), n;
637
+ }
638
+ function H(e, n) {
639
+ let t = e.x, r = e.y, o = n.x - t, i = n.y - r, s = e.width / 2, a = e.height / 2;
640
+ if (!o && !i) throw new Error("Not possible to find intersection inside of the rectangle");
641
+ let d, l;
642
+ return Math.abs(i) * s > Math.abs(o) * a ? (i < 0 && (a = -a), d = a * o / i, l = a) : (o < 0 && (s = -s), d = s, l = s * i / o), { x: t + d, y: r + l };
643
+ }
644
+ function N(e) {
645
+ let n = k(X(e) + 1).map(() => []);
646
+ return e.nodes().forEach((t) => {
647
+ let r = e.node(t), o = r.rank;
648
+ o !== void 0 && (n[o] || (n[o] = []), n[o][r.order] = t);
649
+ }), n;
650
+ }
651
+ function Te(e) {
652
+ let n = e.nodes().map((r) => {
653
+ let o = e.node(r).rank;
654
+ return o === void 0 ? Number.MAX_VALUE : o;
655
+ }), t = L(Math.min, n);
656
+ e.nodes().forEach((r) => {
657
+ let o = e.node(r);
658
+ Object.hasOwn(o, "rank") && (o.rank -= t);
659
+ });
660
+ }
661
+ function Oe(e) {
662
+ let n = e.nodes().map((s) => e.node(s).rank).filter((s) => s !== void 0), t = L(Math.min, n), r = [];
663
+ e.nodes().forEach((s) => {
664
+ let a = e.node(s).rank - t;
665
+ r[a] || (r[a] = []), r[a].push(s);
666
+ });
667
+ let o = 0, i = e.graph().nodeRankFactor;
668
+ Array.from(r).forEach((s, a) => {
669
+ s === void 0 && a % i !== 0 ? --o : s !== void 0 && o && s.forEach((d) => e.node(d).rank += o);
670
+ });
671
+ }
672
+ function q(e, n, t, r) {
673
+ let o = { width: 0, height: 0 };
674
+ return arguments.length >= 4 && (o.rank = t, o.order = r), w(e, "border", o, n);
675
+ }
676
+ function Dn(e, n = Ie) {
677
+ let t = [];
678
+ for (let r = 0; r < e.length; r += n) {
679
+ let o = e.slice(r, r + n);
680
+ t.push(o);
681
+ }
682
+ return t;
683
+ }
684
+ var Ie = 65535;
685
+ function L(e, n) {
686
+ if (n.length > Ie) {
687
+ let t = Dn(n);
688
+ return e(...t.map((r) => e(...r)));
689
+ } else return e(...n);
690
+ }
691
+ function X(e) {
692
+ let t = e.nodes().map((r) => {
693
+ let o = e.node(r).rank;
694
+ return o === void 0 ? Number.MIN_VALUE : o;
695
+ });
696
+ return L(Math.max, t);
697
+ }
698
+ function Ce(e, n) {
699
+ let t = { lhs: [], rhs: [] };
700
+ return e.forEach((r) => {
701
+ n(r) ? t.lhs.push(r) : t.rhs.push(r);
702
+ }), t;
703
+ }
704
+ function P(e, n) {
705
+ let t = Date.now();
706
+ try {
707
+ return n();
708
+ } finally {
709
+ console.log(e + " time: " + (Date.now() - t) + "ms");
710
+ }
711
+ }
712
+ function M(e, n) {
713
+ return n();
714
+ }
715
+ var An = 0;
716
+ function j(e) {
717
+ let n = ++An;
718
+ return e + ("" + n);
719
+ }
720
+ function k(e, n, t = 1) {
721
+ n == null && (n = e, e = 0);
722
+ let r = (i) => i < n;
723
+ t < 0 && (r = (i) => n < i);
724
+ let o = [];
725
+ for (let i = e; r(i); i += t) o.push(i);
726
+ return o;
727
+ }
728
+ function T(e, n) {
729
+ let t = {};
730
+ for (let r of n) e[r] !== void 0 && (t[r] = e[r]);
731
+ return t;
732
+ }
733
+ function O(e, n) {
734
+ let t;
735
+ return typeof n == "string" ? t = (r) => r[n] : t = n, Object.entries(e).reduce((r, [o, i]) => (r[o] = t(i, o), r), {});
736
+ }
737
+ function Re(e, n) {
738
+ return e.reduce((t, r, o) => (t[r] = n[o], t), {});
739
+ }
740
+ var _ = "\0";
741
+ var U = "3.0.0";
742
+ var K = class {
743
+ constructor() {
744
+ pe(this, "_sentinel");
745
+ let n = {};
746
+ n._next = n._prev = n, this._sentinel = n;
747
+ }
748
+ dequeue() {
749
+ let n = this._sentinel, t = n._prev;
750
+ if (t !== n) return Pe(t), t;
751
+ }
752
+ enqueue(n) {
753
+ let t = this._sentinel;
754
+ n._prev && n._next && Pe(n), n._next = t._next, t._next._prev = n, t._next = n, n._prev = t;
755
+ }
756
+ toString() {
757
+ let n = [], t = this._sentinel, r = t._prev;
758
+ for (; r !== t; ) n.push(JSON.stringify(r, Vn)), r = r._prev;
759
+ return "[" + n.join(", ") + "]";
760
+ }
761
+ };
762
+ function Pe(e) {
763
+ e._prev._next = e._next, e._next._prev = e._prev, delete e._next, delete e._prev;
764
+ }
765
+ function Vn(e, n) {
766
+ if (e !== "_next" && e !== "_prev") return n;
767
+ }
768
+ var Me = K;
769
+ var Wn = () => 1;
770
+ function Q(e, n) {
771
+ if (e.nodeCount() <= 1) return [];
772
+ let t = Yn(e, n || Wn);
773
+ return Bn(t.graph, t.buckets, t.zeroIdx).flatMap((o) => e.outEdges(o.v, o.w) || []);
774
+ }
775
+ function Bn(e, n, t) {
776
+ var a;
777
+ let r = [], o = n[n.length - 1], i = n[0], s;
778
+ for (; e.nodeCount(); ) {
779
+ for (; s = i.dequeue(); ) $(e, n, t, s);
780
+ for (; s = o.dequeue(); ) $(e, n, t, s);
781
+ if (e.nodeCount()) {
782
+ for (let d = n.length - 2; d > 0; --d) if (s = (a = n[d]) == null ? void 0 : a.dequeue(), s) {
783
+ r = r.concat($(e, n, t, s, true) || []);
784
+ break;
785
+ }
786
+ }
787
+ }
788
+ return r;
789
+ }
790
+ function $(e, n, t, r, o) {
791
+ let i = [], s = o ? i : void 0;
792
+ return (e.inEdges(r.v) || []).forEach((a) => {
793
+ let d = e.edge(a), l = e.node(a.v);
794
+ o && i.push({ v: a.v, w: a.w }), l.out -= d, J(n, t, l);
795
+ }), (e.outEdges(r.v) || []).forEach((a) => {
796
+ let d = e.edge(a), l = a.w, u = e.node(l);
797
+ u.in -= d, J(n, t, u);
798
+ }), e.removeNode(r.v), s;
799
+ }
800
+ function Yn(e, n) {
801
+ let t = new p(), r = 0, o = 0;
802
+ e.nodes().forEach((a) => {
803
+ t.setNode(a, { v: a, in: 0, out: 0 });
804
+ }), e.edges().forEach((a) => {
805
+ let d = t.edge(a.v, a.w) || 0, l = n(a), u = d + l;
806
+ t.setEdge(a.v, a.w, u);
807
+ let c = t.node(a.v), h = t.node(a.w);
808
+ o = Math.max(o, c.out += l), r = Math.max(r, h.in += l);
809
+ });
810
+ let i = zn(o + r + 3).map(() => new Me()), s = r + 1;
811
+ return t.nodes().forEach((a) => {
812
+ J(i, s, t.node(a));
813
+ }), { graph: t, buckets: i, zeroIdx: s };
814
+ }
815
+ function J(e, n, t) {
816
+ var r, o, i;
817
+ t.out ? t.in ? (i = e[t.out - t.in + n]) == null || i.enqueue(t) : (o = e[e.length - 1]) == null || o.enqueue(t) : (r = e[0]) == null || r.enqueue(t);
818
+ }
819
+ function zn(e) {
820
+ let n = [];
821
+ for (let t = 0; t < e; t++) n.push(t);
822
+ return n;
823
+ }
824
+ function je(e) {
825
+ (e.graph().acyclicer === "greedy" ? Q(e, t(e)) : Hn(e)).forEach((r) => {
826
+ let o = e.edge(r);
827
+ e.removeEdge(r), o.forwardName = r.name, o.reversed = true, e.setEdge(r.w, r.v, o, j("rev"));
828
+ });
829
+ function t(r) {
830
+ return (o) => r.edge(o).weight;
831
+ }
832
+ }
833
+ function Hn(e) {
834
+ let n = [], t = {}, r = {};
835
+ function o(i) {
836
+ Object.hasOwn(r, i) || (r[i] = true, t[i] = true, e.outEdges(i).forEach((s) => {
837
+ Object.hasOwn(t, s.w) ? n.push(s) : o(s.w);
838
+ }), delete t[i]);
839
+ }
840
+ return e.nodes().forEach(o), n;
841
+ }
842
+ function Se(e) {
843
+ e.edges().forEach((n) => {
844
+ let t = e.edge(n);
845
+ if (t.reversed) {
846
+ e.removeEdge(n);
847
+ let r = t.forwardName;
848
+ delete t.reversed, delete t.forwardName, e.setEdge(n.w, n.v, t, r);
849
+ }
850
+ });
851
+ }
852
+ function Fe(e) {
853
+ e.graph().dummyChains = [], e.edges().forEach((n) => Xn(e, n));
854
+ }
855
+ function Xn(e, n) {
856
+ let t = n.v, r = e.node(t).rank, o = n.w, i = e.node(o).rank, s = n.name, a = e.edge(n), d = a.labelRank;
857
+ if (i === r + 1) return;
858
+ e.removeEdge(n);
859
+ let l, u, c;
860
+ for (c = 0, ++r; r < i; ++c, ++r) a.points = [], u = { width: 0, height: 0, edgeLabel: a, edgeObj: n, rank: r }, l = w(e, "edge", u, "_d"), r === d && (u.width = a.width, u.height = a.height, u.dummy = "edge-label", u.labelpos = a.labelpos), e.setEdge(t, l, { weight: a.weight }, s), c === 0 && e.graph().dummyChains.push(l), t = l;
861
+ e.setEdge(t, o, { weight: a.weight }, s);
862
+ }
863
+ function De(e) {
864
+ e.graph().dummyChains.forEach((n) => {
865
+ let t = e.node(n), r = t.edgeLabel, o;
866
+ for (e.setEdge(t.edgeObj, r); t.dummy; ) o = e.successors(n)[0], e.removeNode(n), r.points.push({ x: t.x, y: t.y }), t.dummy === "edge-label" && (r.x = t.x, r.y = t.y, r.width = t.width, r.height = t.height), n = o, t = e.node(n);
867
+ });
868
+ }
869
+ function S(e) {
870
+ let n = {};
871
+ function t(r) {
872
+ let o = e.node(r);
873
+ if (Object.hasOwn(n, r)) return o.rank;
874
+ n[r] = true;
875
+ let i = e.outEdges(r), s = i ? i.map((d) => d == null ? Number.POSITIVE_INFINITY : t(d.w) - e.edge(d).minlen) : [], a = L(Math.min, s);
876
+ return a === Number.POSITIVE_INFINITY && (a = 0), o.rank = a;
877
+ }
878
+ e.sources().forEach(t);
879
+ }
880
+ function v(e, n) {
881
+ return e.node(n.w).rank - e.node(n.v).rank - e.edge(n).minlen;
882
+ }
883
+ var V = Kn;
884
+ function Kn(e) {
885
+ let n = new p({ directed: false }), t = e.nodes();
886
+ if (t.length === 0) throw new Error("Graph must have at least one node");
887
+ let r = t[0], o = e.nodeCount();
888
+ n.setNode(r, {});
889
+ let i, s;
890
+ for (; $n(n, e) < o && (i = Jn(n, e), !!i); ) s = n.hasNode(i.v) ? v(e, i) : -v(e, i), Qn(n, e, s);
891
+ return n;
892
+ }
893
+ function $n(e, n) {
894
+ function t(r) {
895
+ let o = n.nodeEdges(r);
896
+ o && o.forEach((i) => {
897
+ let s = i.v, a = r === s ? i.w : s;
898
+ !e.hasNode(a) && !v(n, i) && (e.setNode(a, {}), e.setEdge(r, a, {}), t(a));
899
+ });
900
+ }
901
+ return e.nodes().forEach(t), e.nodeCount();
902
+ }
903
+ function Jn(e, n) {
904
+ return n.edges().reduce((r, o) => {
905
+ let i = Number.POSITIVE_INFINITY;
906
+ return e.hasNode(o.v) !== e.hasNode(o.w) && (i = v(n, o)), i < r[0] ? [i, o] : r;
907
+ }, [Number.POSITIVE_INFINITY, null])[1];
908
+ }
909
+ function Qn(e, n, t) {
910
+ e.nodes().forEach((r) => n.node(r).rank += t);
911
+ }
912
+ var { preorder: Zn, postorder: et } = R;
913
+ var Ve = x;
914
+ x.initLowLimValues = ee;
915
+ x.initCutValues = Z;
916
+ x.calcCutValue = We;
917
+ x.leaveEdge = Ye;
918
+ x.enterEdge = ze;
919
+ x.exchangeEdges = He;
920
+ function x(e) {
921
+ e = xe(e), S(e);
922
+ let n = V(e);
923
+ ee(n), Z(n, e);
924
+ let t, r;
925
+ for (; t = Ye(n); ) r = ze(n, e, t), He(n, e, t, r);
926
+ }
927
+ function Z(e, n) {
928
+ let t = et(e, e.nodes());
929
+ t = t.slice(0, t.length - 1), t.forEach((r) => nt(e, n, r));
930
+ }
931
+ function nt(e, n, t) {
932
+ let o = e.node(t).parent, i = e.edge(t, o);
933
+ i.cutvalue = We(e, n, t);
934
+ }
935
+ function We(e, n, t) {
936
+ let o = e.node(t).parent, i = true, s = n.edge(t, o), a = 0;
937
+ s || (i = false, s = n.edge(o, t)), a = s.weight;
938
+ let d = n.nodeEdges(t);
939
+ return d && d.forEach((l) => {
940
+ let u = l.v === t, c = u ? l.w : l.v;
941
+ if (c !== o) {
942
+ let h = u === i, f = n.edge(l).weight;
943
+ if (a += h ? f : -f, rt(e, t, c)) {
944
+ let b = e.edge(t, c).cutvalue;
945
+ a += h ? -b : b;
946
+ }
947
+ }
948
+ }), a;
949
+ }
950
+ function ee(e, n) {
951
+ arguments.length < 2 && (n = e.nodes()[0]), Be(e, {}, 1, n);
952
+ }
953
+ function Be(e, n, t, r, o) {
954
+ let i = t, s = e.node(r);
955
+ n[r] = true;
956
+ let a = e.neighbors(r);
957
+ return a && a.forEach((d) => {
958
+ Object.hasOwn(n, d) || (t = Be(e, n, t, d, r));
959
+ }), s.low = i, s.lim = t++, o ? s.parent = o : delete s.parent, t;
960
+ }
961
+ function Ye(e) {
962
+ return e.edges().find((n) => e.edge(n).cutvalue < 0);
963
+ }
964
+ function ze(e, n, t) {
965
+ let r = t.v, o = t.w;
966
+ n.hasEdge(r, o) || (r = t.w, o = t.v);
967
+ let i = e.node(r), s = e.node(o), a = i, d = false;
968
+ return i.lim > s.lim && (a = s, d = true), n.edges().filter((u) => d === Ae(e, e.node(u.v), a) && d !== Ae(e, e.node(u.w), a)).reduce((u, c) => v(n, c) < v(n, u) ? c : u);
969
+ }
970
+ function He(e, n, t, r) {
971
+ let o = t.v, i = t.w;
972
+ e.removeEdge(o, i), e.setEdge(r.v, r.w, {}), ee(e), Z(e, n), tt(e, n);
973
+ }
974
+ function tt(e, n) {
975
+ let t = e.nodes().find((o) => !e.node(o).parent);
976
+ if (!t) return;
977
+ let r = Zn(e, [t]);
978
+ r = r.slice(1), r.forEach((o) => {
979
+ let s = e.node(o).parent, a = n.edge(o, s), d = false;
980
+ a || (a = n.edge(s, o), d = true), n.node(o).rank = n.node(s).rank + (d ? a.minlen : -a.minlen);
981
+ });
982
+ }
983
+ function rt(e, n, t) {
984
+ return e.hasEdge(n, t);
985
+ }
986
+ function Ae(e, n, t) {
987
+ return t.low <= n.lim && n.lim <= t.lim;
988
+ }
989
+ var Xe = ot;
990
+ function ot(e) {
991
+ let n = e.graph().ranker;
992
+ if (typeof n == "function") return n(e);
993
+ switch (n) {
994
+ case "network-simplex":
995
+ qe(e);
996
+ break;
997
+ case "tight-tree":
998
+ st(e);
999
+ break;
1000
+ case "longest-path":
1001
+ it(e);
1002
+ break;
1003
+ case "none":
1004
+ break;
1005
+ default:
1006
+ qe(e);
1007
+ }
1008
+ }
1009
+ var it = S;
1010
+ function st(e) {
1011
+ S(e), V(e);
1012
+ }
1013
+ function qe(e) {
1014
+ Ve(e);
1015
+ }
1016
+ var Ue = at;
1017
+ function at(e) {
1018
+ let n = lt(e);
1019
+ e.graph().dummyChains.forEach((t) => {
1020
+ let r = e.node(t), o = r.edgeObj, i = dt(e, n, o.v, o.w), s = i.path, a = i.lca, d = 0, l = s[d], u = true;
1021
+ for (; t !== o.w; ) {
1022
+ if (r = e.node(t), u) {
1023
+ for (; (l = s[d]) !== a && e.node(l).maxRank < r.rank; ) d++;
1024
+ l === a && (u = false);
1025
+ }
1026
+ if (!u) {
1027
+ for (; d < s.length - 1 && e.node(s[d + 1]).minRank <= r.rank; ) d++;
1028
+ l = s[d];
1029
+ }
1030
+ l !== void 0 && e.setParent(t, l), t = e.successors(t)[0];
1031
+ }
1032
+ });
1033
+ }
1034
+ function dt(e, n, t, r) {
1035
+ let o = [], i = [], s = Math.min(n[t].low, n[r].low), a = Math.max(n[t].lim, n[r].lim), d;
1036
+ d = t;
1037
+ do
1038
+ d = e.parent(d), o.push(d);
1039
+ while (d && (n[d].low > s || a > n[d].lim));
1040
+ let l = d, u = r;
1041
+ for (; (u = e.parent(u)) !== l; ) i.push(u);
1042
+ return { path: o.concat(i.reverse()), lca: l };
1043
+ }
1044
+ function lt(e) {
1045
+ let n = {}, t = 0;
1046
+ function r(o) {
1047
+ let i = t;
1048
+ e.children(o).forEach(r), n[o] = { low: i, lim: t++ };
1049
+ }
1050
+ return e.children(_).forEach(r), n;
1051
+ }
1052
+ function Ke(e) {
1053
+ let n = w(e, "root", {}, "_root"), t = ut(e), r = Object.values(t), o = L(Math.max, r) - 1, i = 2 * o + 1;
1054
+ e.graph().nestingRoot = n, e.edges().forEach((a) => e.edge(a).minlen *= i);
1055
+ let s = ct(e) + 1;
1056
+ e.children(_).forEach((a) => $e(e, n, i, s, o, t, a)), e.graph().nodeRankFactor = i;
1057
+ }
1058
+ function $e(e, n, t, r, o, i, s) {
1059
+ var c;
1060
+ let a = e.children(s);
1061
+ if (!a.length) {
1062
+ s !== n && e.setEdge(n, s, { weight: 0, minlen: t });
1063
+ return;
1064
+ }
1065
+ let d = q(e, "_bt"), l = q(e, "_bb"), u = e.node(s);
1066
+ e.setParent(d, s), u.borderTop = d, e.setParent(l, s), u.borderBottom = l, a.forEach((h) => {
1067
+ var y;
1068
+ $e(e, n, t, r, o, i, h);
1069
+ let f = e.node(h), g = f.borderTop ? f.borderTop : h, b = f.borderBottom ? f.borderBottom : h, m = f.borderTop ? r : 2 * r, E = g !== b ? 1 : o - ((y = i[s]) != null ? y : 0) + 1;
1070
+ e.setEdge(d, g, { weight: m, minlen: E, nestingEdge: true }), e.setEdge(b, l, { weight: m, minlen: E, nestingEdge: true });
1071
+ }), e.parent(s) || e.setEdge(n, d, { weight: 0, minlen: o + ((c = i[s]) != null ? c : 0) });
1072
+ }
1073
+ function ut(e) {
1074
+ let n = {};
1075
+ function t(r, o) {
1076
+ let i = e.children(r);
1077
+ i && i.length && i.forEach((s) => t(s, o + 1)), n[r] = o;
1078
+ }
1079
+ return e.children(_).forEach((r) => t(r, 1)), n;
1080
+ }
1081
+ function ct(e) {
1082
+ return e.edges().reduce((n, t) => n + e.edge(t).weight, 0);
1083
+ }
1084
+ function Je(e) {
1085
+ let n = e.graph();
1086
+ e.removeNode(n.nestingRoot), delete n.nestingRoot, e.edges().forEach((t) => {
1087
+ e.edge(t).nestingEdge && e.removeEdge(t);
1088
+ });
1089
+ }
1090
+ var Ze = ft;
1091
+ function ft(e) {
1092
+ function n(t) {
1093
+ let r = e.children(t), o = e.node(t);
1094
+ if (r.length && r.forEach(n), Object.hasOwn(o, "minRank")) {
1095
+ o.borderLeft = [], o.borderRight = [];
1096
+ for (let i = o.minRank, s = o.maxRank + 1; i < s; ++i) Qe(e, "borderLeft", "_bl", t, o, i), Qe(e, "borderRight", "_br", t, o, i);
1097
+ }
1098
+ }
1099
+ e.children(_).forEach(n);
1100
+ }
1101
+ function Qe(e, n, t, r, o, i) {
1102
+ let s = { width: 0, height: 0, rank: i, borderType: n }, a = o[n][i - 1], d = w(e, "border", s, t);
1103
+ o[n][i] = d, e.setParent(d, r), a && e.setEdge(a, d, { weight: 1 });
1104
+ }
1105
+ function nn(e) {
1106
+ var t;
1107
+ let n = (t = e.graph().rankdir) == null ? void 0 : t.toLowerCase();
1108
+ (n === "lr" || n === "rl") && rn(e);
1109
+ }
1110
+ function tn(e) {
1111
+ var t;
1112
+ let n = (t = e.graph().rankdir) == null ? void 0 : t.toLowerCase();
1113
+ (n === "bt" || n === "rl") && bt(e), (n === "lr" || n === "rl") && (gt(e), rn(e));
1114
+ }
1115
+ function rn(e) {
1116
+ e.nodes().forEach((n) => en(e.node(n))), e.edges().forEach((n) => en(e.edge(n)));
1117
+ }
1118
+ function en(e) {
1119
+ let n = e.width;
1120
+ e.width = e.height, e.height = n;
1121
+ }
1122
+ function bt(e) {
1123
+ e.nodes().forEach((n) => ne(e.node(n))), e.edges().forEach((n) => {
1124
+ var r;
1125
+ let t = e.edge(n);
1126
+ (r = t.points) == null || r.forEach(ne), Object.hasOwn(t, "y") && ne(t);
1127
+ });
1128
+ }
1129
+ function ne(e) {
1130
+ e.y = -e.y;
1131
+ }
1132
+ function gt(e) {
1133
+ e.nodes().forEach((n) => te(e.node(n))), e.edges().forEach((n) => {
1134
+ var r;
1135
+ let t = e.edge(n);
1136
+ (r = t.points) == null || r.forEach(te), Object.hasOwn(t, "x") && te(t);
1137
+ });
1138
+ }
1139
+ function te(e) {
1140
+ let n = e.x;
1141
+ e.x = e.y, e.y = n;
1142
+ }
1143
+ function re(e) {
1144
+ let n = {}, t = e.nodes().filter((d) => !e.children(d).length), r = t.map((d) => e.node(d).rank), o = L(Math.max, r), i = k(o + 1).map(() => []);
1145
+ function s(d) {
1146
+ if (n[d]) return;
1147
+ n[d] = true;
1148
+ let l = e.node(d);
1149
+ i[l.rank].push(d);
1150
+ let u = e.successors(d);
1151
+ u && u.forEach(s);
1152
+ }
1153
+ return t.sort((d, l) => e.node(d).rank - e.node(l).rank).forEach(s), i;
1154
+ }
1155
+ function oe(e, n) {
1156
+ let t = 0;
1157
+ for (let r = 1; r < n.length; ++r) t += mt(e, n[r - 1], n[r]);
1158
+ return t;
1159
+ }
1160
+ function mt(e, n, t) {
1161
+ let r = Re(t, t.map((l, u) => u)), o = n.flatMap((l) => {
1162
+ let u = e.outEdges(l);
1163
+ return u ? u.map((c) => ({ pos: r[c.w], weight: e.edge(c).weight })).sort((c, h) => c.pos - h.pos) : [];
1164
+ }), i = 1;
1165
+ for (; i < t.length; ) i <<= 1;
1166
+ let s = 2 * i - 1;
1167
+ i -= 1;
1168
+ let a = new Array(s).fill(0), d = 0;
1169
+ return o.forEach((l) => {
1170
+ let u = l.pos + i;
1171
+ a[u] += l.weight;
1172
+ let c = 0;
1173
+ for (; u > 0; ) u % 2 && (c += a[u + 1]), u = u - 1 >> 1, a[u] += l.weight;
1174
+ d += l.weight * c;
1175
+ }), d;
1176
+ }
1177
+ function ie(e, n = []) {
1178
+ return n.map((t) => {
1179
+ let r = e.inEdges(t);
1180
+ if (!r || !r.length) return { v: t };
1181
+ {
1182
+ let o = r.reduce((i, s) => {
1183
+ let a = e.edge(s), d = e.node(s.v);
1184
+ return { sum: i.sum + a.weight * d.order, weight: i.weight + a.weight };
1185
+ }, { sum: 0, weight: 0 });
1186
+ return { v: t, barycenter: o.sum / o.weight, weight: o.weight };
1187
+ }
1188
+ });
1189
+ }
1190
+ function se(e, n) {
1191
+ let t = {};
1192
+ e.forEach((o, i) => {
1193
+ let s = { indegree: 0, in: [], out: [], vs: [o.v], i };
1194
+ o.barycenter !== void 0 && (s.barycenter = o.barycenter, s.weight = o.weight), t[o.v] = s;
1195
+ }), n.edges().forEach((o) => {
1196
+ let i = t[o.v], s = t[o.w];
1197
+ i !== void 0 && s !== void 0 && (s.indegree++, i.out.push(s));
1198
+ });
1199
+ let r = Object.values(t).filter((o) => !o.indegree);
1200
+ return Et(r);
1201
+ }
1202
+ function Et(e) {
1203
+ let n = [];
1204
+ function t(o) {
1205
+ return (i) => {
1206
+ i.merged || (i.barycenter === void 0 || o.barycenter === void 0 || i.barycenter >= o.barycenter) && Lt(o, i);
1207
+ };
1208
+ }
1209
+ function r(o) {
1210
+ return (i) => {
1211
+ i.in.push(o), --i.indegree === 0 && e.push(i);
1212
+ };
1213
+ }
1214
+ for (; e.length; ) {
1215
+ let o = e.pop();
1216
+ n.push(o), o.in.reverse().forEach(t(o)), o.out.forEach(r(o));
1217
+ }
1218
+ return n.filter((o) => !o.merged).map((o) => T(o, ["vs", "i", "barycenter", "weight"]));
1219
+ }
1220
+ function Lt(e, n) {
1221
+ let t = 0, r = 0;
1222
+ e.weight && (t += e.barycenter * e.weight, r += e.weight), n.weight && (t += n.barycenter * n.weight, r += n.weight), e.vs = n.vs.concat(e.vs), e.barycenter = t / r, e.weight = r, e.i = Math.min(n.i, e.i), n.merged = true;
1223
+ }
1224
+ function ae(e, n) {
1225
+ let t = Ce(e, (u) => Object.hasOwn(u, "barycenter")), r = t.lhs, o = t.rhs.sort((u, c) => c.i - u.i), i = [], s = 0, a = 0, d = 0;
1226
+ r.sort(yt(!!n)), d = on(i, o, d), r.forEach((u) => {
1227
+ d += u.vs.length, i.push(u.vs), s += u.barycenter * u.weight, a += u.weight, d = on(i, o, d);
1228
+ });
1229
+ let l = { vs: i.flat(1) };
1230
+ return a && (l.barycenter = s / a, l.weight = a), l;
1231
+ }
1232
+ function on(e, n, t) {
1233
+ let r;
1234
+ for (; n.length && (r = n[n.length - 1]).i <= t; ) n.pop(), e.push(r.vs), t++;
1235
+ return t;
1236
+ }
1237
+ function yt(e) {
1238
+ return (n, t) => n.barycenter < t.barycenter ? -1 : n.barycenter > t.barycenter ? 1 : e ? t.i - n.i : n.i - t.i;
1239
+ }
1240
+ function W(e, n, t, r) {
1241
+ let o = e.children(n), i = e.node(n), s = i ? i.borderLeft : void 0, a = i ? i.borderRight : void 0, d = {};
1242
+ s && (o = o.filter((h) => h !== s && h !== a));
1243
+ let l = ie(e, o);
1244
+ l.forEach((h) => {
1245
+ if (e.children(h.v).length) {
1246
+ let f = W(e, h.v, t, r);
1247
+ d[h.v] = f, Object.hasOwn(f, "barycenter") && Nt(h, f);
1248
+ }
1249
+ });
1250
+ let u = se(l, t);
1251
+ wt(u, d);
1252
+ let c = ae(u, r);
1253
+ if (s && a) {
1254
+ c.vs = [s, c.vs, a].flat(1);
1255
+ let h = e.predecessors(s);
1256
+ if (h && h.length) {
1257
+ let f = e.node(h[0]), g = e.predecessors(a), b = e.node(g[0]);
1258
+ Object.hasOwn(c, "barycenter") || (c.barycenter = 0, c.weight = 0), c.barycenter = (c.barycenter * c.weight + f.order + b.order) / (c.weight + 2), c.weight += 2;
1259
+ }
1260
+ }
1261
+ return c;
1262
+ }
1263
+ function wt(e, n) {
1264
+ e.forEach((t) => {
1265
+ t.vs = t.vs.flatMap((r) => n[r] ? n[r].vs : r);
1266
+ });
1267
+ }
1268
+ function Nt(e, n) {
1269
+ e.barycenter !== void 0 ? (e.barycenter = (e.barycenter * e.weight + n.barycenter * n.weight) / (e.weight + n.weight), e.weight += n.weight) : (e.barycenter = n.barycenter, e.weight = n.weight);
1270
+ }
1271
+ function de(e, n, t, r) {
1272
+ r || (r = e.nodes());
1273
+ let o = Gt(e), i = new p({ compound: true }).setGraph({ root: o }).setDefaultNodeLabel((s) => e.node(s));
1274
+ return r.forEach((s) => {
1275
+ let a = e.node(s), d = e.parent(s);
1276
+ if (a.rank === n || a.minRank <= n && n <= a.maxRank) {
1277
+ i.setNode(s), i.setParent(s, d || o);
1278
+ let l = e[t](s);
1279
+ l && l.forEach((u) => {
1280
+ let c = u.v === s ? u.w : u.v, h = i.edge(c, s), f = h !== void 0 ? h.weight : 0;
1281
+ i.setEdge(c, s, { weight: e.edge(u).weight + f });
1282
+ }), Object.hasOwn(a, "minRank") && i.setNode(s, { borderLeft: a.borderLeft[n], borderRight: a.borderRight[n] });
1283
+ }
1284
+ }), i;
1285
+ }
1286
+ function Gt(e) {
1287
+ let n;
1288
+ for (; e.hasNode(n = j("_root")); ) ;
1289
+ return n;
1290
+ }
1291
+ function le(e, n, t) {
1292
+ let r = {}, o;
1293
+ t.forEach((i) => {
1294
+ let s = e.parent(i), a, d;
1295
+ for (; s; ) {
1296
+ if (a = e.parent(s), a ? (d = r[a], r[a] = s) : (d = o, o = s), d && d !== s) {
1297
+ n.setEdge(d, s);
1298
+ return;
1299
+ }
1300
+ s = a;
1301
+ }
1302
+ });
1303
+ }
1304
+ function B(e, n = {}) {
1305
+ if (typeof n.customOrder == "function") {
1306
+ n.customOrder(e, B);
1307
+ return;
1308
+ }
1309
+ let t = X(e), r = sn(e, k(1, t + 1), "inEdges"), o = sn(e, k(t - 1, -1, -1), "outEdges"), i = re(e);
1310
+ if (an(e, i), n.disableOptimalOrderHeuristic) return;
1311
+ let s = Number.POSITIVE_INFINITY, a, d = n.constraints || [];
1312
+ for (let l = 0, u = 0; u < 4; ++l, ++u) {
1313
+ kt(l % 2 ? r : o, l % 4 >= 2, d), i = N(e);
1314
+ let c = oe(e, i);
1315
+ c < s ? (u = 0, a = Object.assign({}, i), s = c) : c === s && (a = structuredClone(i));
1316
+ }
1317
+ an(e, a);
1318
+ }
1319
+ function sn(e, n, t) {
1320
+ let r = /* @__PURE__ */ new Map(), o = (i, s) => {
1321
+ r.has(i) || r.set(i, []), r.get(i).push(s);
1322
+ };
1323
+ for (let i of e.nodes()) {
1324
+ let s = e.node(i);
1325
+ if (typeof s.rank == "number" && o(s.rank, i), typeof s.minRank == "number" && typeof s.maxRank == "number") for (let a = s.minRank; a <= s.maxRank; a++) a !== s.rank && o(a, i);
1326
+ }
1327
+ return n.map(function(i) {
1328
+ return de(e, i, t, r.get(i) || []);
1329
+ });
1330
+ }
1331
+ function kt(e, n, t) {
1332
+ let r = new p();
1333
+ e.forEach(function(o) {
1334
+ t.forEach((a) => r.setEdge(a.left, a.right));
1335
+ let i = o.graph().root, s = W(o, i, r, n);
1336
+ s.vs.forEach((a, d) => o.node(a).order = d), le(o, r, s.vs);
1337
+ });
1338
+ }
1339
+ function an(e, n) {
1340
+ Object.values(n).forEach((t) => t.forEach((r, o) => e.node(r).order = o));
1341
+ }
1342
+ function vt(e, n) {
1343
+ let t = {};
1344
+ function r(o, i) {
1345
+ let s = 0, a = 0, d = o.length, l = i[i.length - 1];
1346
+ return i.forEach((u, c) => {
1347
+ let h = xt(e, u), f = h ? e.node(h).order : d;
1348
+ (h || u === l) && (i.slice(a, c + 1).forEach((g) => {
1349
+ let b = e.predecessors(g);
1350
+ b && b.forEach((m) => {
1351
+ let E = e.node(m), y = E.order;
1352
+ (y < s || f < y) && !(E.dummy && e.node(g).dummy) && dn(t, m, g);
1353
+ });
1354
+ }), a = c + 1, s = f);
1355
+ }), i;
1356
+ }
1357
+ return n.length && n.reduce(r), t;
1358
+ }
1359
+ function _t(e, n) {
1360
+ let t = {};
1361
+ function r(i, s, a, d, l) {
1362
+ k(s, a).forEach((u) => {
1363
+ let c = i[u];
1364
+ if (c !== void 0 && e.node(c).dummy) {
1365
+ let h = e.predecessors(c);
1366
+ h && h.forEach((f) => {
1367
+ if (f === void 0) return;
1368
+ let g = e.node(f);
1369
+ g.dummy && (g.order < d || g.order > l) && dn(t, f, c);
1370
+ });
1371
+ }
1372
+ });
1373
+ }
1374
+ function o(i, s) {
1375
+ let a = -1, d = -1, l = 0;
1376
+ return s.forEach((u, c) => {
1377
+ if (e.node(u).dummy === "border") {
1378
+ let h = e.predecessors(u);
1379
+ if (h && h.length) {
1380
+ let f = h[0];
1381
+ if (f === void 0) return;
1382
+ d = e.node(f).order, r(s, l, c, a, d), l = c, a = d;
1383
+ }
1384
+ }
1385
+ r(s, l, s.length, d, i.length);
1386
+ }), s;
1387
+ }
1388
+ return n.length && n.reduce(o), t;
1389
+ }
1390
+ function xt(e, n) {
1391
+ if (e.node(n).dummy) {
1392
+ let t = e.predecessors(n);
1393
+ if (t) return t.find((r) => e.node(r).dummy);
1394
+ }
1395
+ }
1396
+ function dn(e, n, t) {
1397
+ if (n > t) {
1398
+ let o = n;
1399
+ n = t, t = o;
1400
+ }
1401
+ let r = e[n];
1402
+ r || (e[n] = r = {}), r[t] = true;
1403
+ }
1404
+ function Tt(e, n, t) {
1405
+ if (n > t) {
1406
+ let o = n;
1407
+ n = t, t = o;
1408
+ }
1409
+ let r = e[n];
1410
+ return r !== void 0 && Object.hasOwn(r, t);
1411
+ }
1412
+ function Ot(e, n, t, r) {
1413
+ let o = {}, i = {}, s = {};
1414
+ return n.forEach((a) => {
1415
+ a.forEach((d, l) => {
1416
+ o[d] = d, i[d] = d, s[d] = l;
1417
+ });
1418
+ }), n.forEach((a) => {
1419
+ let d = -1;
1420
+ a.forEach((l) => {
1421
+ let u = r(l);
1422
+ if (u && u.length) {
1423
+ let c = u.sort((f, g) => {
1424
+ let b = s[f], m = s[g];
1425
+ return (b !== void 0 ? b : 0) - (m !== void 0 ? m : 0);
1426
+ }), h = (c.length - 1) / 2;
1427
+ for (let f = Math.floor(h), g = Math.ceil(h); f <= g; ++f) {
1428
+ let b = c[f];
1429
+ if (b === void 0) continue;
1430
+ let m = s[b];
1431
+ if (m !== void 0 && i[l] === l && d < m && !Tt(t, l, b)) {
1432
+ let E = o[b];
1433
+ E !== void 0 && (i[b] = l, i[l] = o[l] = E, d = m);
1434
+ }
1435
+ }
1436
+ }
1437
+ });
1438
+ }), { root: o, align: i };
1439
+ }
1440
+ function It(e, n, t, r, o = false) {
1441
+ let i = {}, s = Ct(e, n, t, o), a = o ? "borderLeft" : "borderRight";
1442
+ function d(f, g) {
1443
+ let b = s.nodes().slice(), m = {}, E = b.pop();
1444
+ for (; E; ) {
1445
+ if (m[E]) f(E);
1446
+ else {
1447
+ m[E] = true, b.push(E);
1448
+ for (let y of g(E)) b.push(y);
1449
+ }
1450
+ E = b.pop();
1451
+ }
1452
+ }
1453
+ function l(f) {
1454
+ let g = s.inEdges(f);
1455
+ g ? i[f] = g.reduce((b, m) => {
1456
+ var I;
1457
+ let E = (I = i[m.v]) != null ? I : 0, y = s.edge(m);
1458
+ return Math.max(b, E + (y !== void 0 ? y : 0));
1459
+ }, 0) : i[f] = 0;
1460
+ }
1461
+ function u(f) {
1462
+ let g = s.outEdges(f), b = Number.POSITIVE_INFINITY;
1463
+ g && (b = g.reduce((E, y) => {
1464
+ let I = i[y.w], be = s.edge(y);
1465
+ return Math.min(E, (I !== void 0 ? I : 0) - (be !== void 0 ? be : 0));
1466
+ }, Number.POSITIVE_INFINITY));
1467
+ let m = e.node(f);
1468
+ b !== Number.POSITIVE_INFINITY && m.borderType !== a && (i[f] = Math.max(i[f] !== void 0 ? i[f] : 0, b));
1469
+ }
1470
+ function c(f) {
1471
+ return s.predecessors(f) || [];
1472
+ }
1473
+ function h(f) {
1474
+ return s.successors(f) || [];
1475
+ }
1476
+ return d(l, c), d(u, h), Object.keys(r).forEach((f) => {
1477
+ var b;
1478
+ let g = t[f];
1479
+ g !== void 0 && (i[f] = (b = i[g]) != null ? b : 0);
1480
+ }), i;
1481
+ }
1482
+ function Ct(e, n, t, r) {
1483
+ let o = new p(), i = e.graph(), s = jt(i.nodesep, i.edgesep, r);
1484
+ return n.forEach((a) => {
1485
+ let d;
1486
+ a.forEach((l) => {
1487
+ let u = t[l];
1488
+ if (u !== void 0) {
1489
+ if (o.setNode(u), d !== void 0) {
1490
+ let c = t[d];
1491
+ if (c !== void 0) {
1492
+ let h = o.edge(c, u);
1493
+ o.setEdge(c, u, Math.max(s(e, l, d), h || 0));
1494
+ }
1495
+ }
1496
+ d = l;
1497
+ }
1498
+ });
1499
+ }), o;
1500
+ }
1501
+ function Rt(e, n) {
1502
+ return Object.values(n).reduce((t, r) => {
1503
+ let o = Number.NEGATIVE_INFINITY, i = Number.POSITIVE_INFINITY;
1504
+ Object.entries(r).forEach(([a, d]) => {
1505
+ let l = St(e, a) / 2;
1506
+ o = Math.max(d + l, o), i = Math.min(d - l, i);
1507
+ });
1508
+ let s = o - i;
1509
+ return s < t[0] && (t = [s, r]), t;
1510
+ }, [Number.POSITIVE_INFINITY, null])[1];
1511
+ }
1512
+ function Pt(e, n) {
1513
+ let t = Object.values(n), r = L(Math.min, t), o = L(Math.max, t);
1514
+ ["u", "d"].forEach((i) => {
1515
+ ["l", "r"].forEach((s) => {
1516
+ let a = i + s, d = e[a];
1517
+ if (!d || d === n) return;
1518
+ let l = Object.values(d), u = r - L(Math.min, l);
1519
+ s !== "l" && (u = o - L(Math.max, l)), u && (e[a] = O(d, (c) => c + u));
1520
+ });
1521
+ });
1522
+ }
1523
+ function Mt(e, n = void 0) {
1524
+ let t = e.ul;
1525
+ return t ? O(t, (r, o) => {
1526
+ var s, a;
1527
+ if (n) {
1528
+ let d = n.toLowerCase(), l = e[d];
1529
+ if (l && l[o] !== void 0) return l[o];
1530
+ }
1531
+ let i = Object.values(e).map((d) => {
1532
+ let l = d[o];
1533
+ return l !== void 0 ? l : 0;
1534
+ }).sort((d, l) => d - l);
1535
+ return (((s = i[1]) != null ? s : 0) + ((a = i[2]) != null ? a : 0)) / 2;
1536
+ }) : {};
1537
+ }
1538
+ function ln(e) {
1539
+ let n = N(e), t = Object.assign(vt(e, n), _t(e, n)), r = {}, o;
1540
+ ["u", "d"].forEach((s) => {
1541
+ o = s === "u" ? n : Object.values(n).reverse(), ["l", "r"].forEach((a) => {
1542
+ a === "r" && (o = o.map((c) => Object.values(c).reverse()));
1543
+ let l = Ot(e, o, t, (c) => (s === "u" ? e.predecessors(c) : e.successors(c)) || []), u = It(e, o, l.root, l.align, a === "r");
1544
+ a === "r" && (u = O(u, (c) => -c)), r[s + a] = u;
1545
+ });
1546
+ });
1547
+ let i = Rt(e, r);
1548
+ return Pt(r, i), Mt(r, e.graph().align);
1549
+ }
1550
+ function jt(e, n, t) {
1551
+ return (r, o, i) => {
1552
+ let s = r.node(o), a = r.node(i), d = 0, l;
1553
+ if (d += s.width / 2, Object.hasOwn(s, "labelpos")) switch (s.labelpos.toLowerCase()) {
1554
+ case "l":
1555
+ l = -s.width / 2;
1556
+ break;
1557
+ case "r":
1558
+ l = s.width / 2;
1559
+ break;
1560
+ }
1561
+ if (l && (d += t ? l : -l), l = void 0, d += (s.dummy ? n : e) / 2, d += (a.dummy ? n : e) / 2, d += a.width / 2, Object.hasOwn(a, "labelpos")) switch (a.labelpos.toLowerCase()) {
1562
+ case "l":
1563
+ l = a.width / 2;
1564
+ break;
1565
+ case "r":
1566
+ l = -a.width / 2;
1567
+ break;
1568
+ }
1569
+ return l && (d += t ? l : -l), d;
1570
+ };
1571
+ }
1572
+ function St(e, n) {
1573
+ return e.node(n).width;
1574
+ }
1575
+ function un(e) {
1576
+ e = A(e), Ft(e), Object.entries(ln(e)).forEach(([n, t]) => e.node(n).x = t);
1577
+ }
1578
+ function Ft(e) {
1579
+ let n = N(e), t = e.graph(), r = t.ranksep, o = t.rankalign, i = 0;
1580
+ n.forEach((s) => {
1581
+ let a = s.reduce((d, l) => {
1582
+ var c;
1583
+ let u = (c = e.node(l).height) != null ? c : 0;
1584
+ return d > u ? d : u;
1585
+ }, 0);
1586
+ s.forEach((d) => {
1587
+ let l = e.node(d);
1588
+ o === "top" ? l.y = i + l.height / 2 : o === "bottom" ? l.y = i + a - l.height / 2 : l.y = i + a / 2;
1589
+ }), i += a + r;
1590
+ });
1591
+ }
1592
+ function he(e, n = {}) {
1593
+ let t = n.debugTiming ? P : M;
1594
+ return t("layout", () => {
1595
+ let r = t(" buildLayoutGraph", () => Xt(e));
1596
+ return t(" runLayout", () => Dt(r, t, n)), t(" updateInputGraph", () => At(e, r)), r;
1597
+ });
1598
+ }
1599
+ function Dt(e, n, t) {
1600
+ n(" makeSpaceForEdgeLabels", () => Ut(e)), n(" removeSelfEdges", () => rr(e)), n(" acyclic", () => je(e)), n(" nestingGraph.run", () => Ke(e)), n(" rank", () => Xe(A(e))), n(" injectEdgeLabelProxies", () => Kt(e)), n(" removeEmptyRanks", () => Oe(e)), n(" nestingGraph.cleanup", () => Je(e)), n(" normalizeRanks", () => Te(e)), n(" assignRankMinMax", () => $t(e)), n(" removeEdgeLabelProxies", () => Jt(e)), n(" normalize.run", () => Fe(e)), n(" parentDummyChains", () => Ue(e)), n(" addBorderSegments", () => Ze(e)), n(" order", () => B(e, t)), n(" insertSelfEdges", () => or(e)), n(" adjustCoordinateSystem", () => nn(e)), n(" position", () => un(e)), n(" positionSelfEdges", () => ir(e)), n(" removeBorderNodes", () => tr(e)), n(" normalize.undo", () => De(e)), n(" fixupEdgeLabelCoords", () => er(e)), n(" undoCoordinateSystem", () => tn(e)), n(" translateGraph", () => Qt(e)), n(" assignNodeIntersects", () => Zt(e)), n(" reversePoints", () => nr(e)), n(" acyclic.undo", () => Se(e));
1601
+ }
1602
+ function At(e, n) {
1603
+ e.nodes().forEach((t) => {
1604
+ let r = e.node(t), o = n.node(t);
1605
+ r && (r.x = o.x, r.y = o.y, r.order = o.order, r.rank = o.rank, n.children(t).length && (r.width = o.width, r.height = o.height));
1606
+ }), e.edges().forEach((t) => {
1607
+ let r = e.edge(t), o = n.edge(t);
1608
+ r.points = o.points, Object.hasOwn(o, "x") && (r.x = o.x, r.y = o.y);
1609
+ }), e.graph().width = n.graph().width, e.graph().height = n.graph().height;
1610
+ }
1611
+ var Vt = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"];
1612
+ var Wt = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "TB", rankalign: "center" };
1613
+ var Bt = ["acyclicer", "ranker", "rankdir", "align", "rankalign"];
1614
+ var Yt = ["width", "height", "rank"];
1615
+ var cn = { width: 0, height: 0 };
1616
+ var zt = ["minlen", "weight", "width", "height", "labeloffset"];
1617
+ var Ht = { minlen: 1, weight: 1, width: 0, height: 0, labeloffset: 10, labelpos: "r" };
1618
+ var qt = ["labelpos"];
1619
+ function Xt(e) {
1620
+ let n = new p({ multigraph: true, compound: true }), t = ce(e.graph());
1621
+ return n.setGraph(Object.assign({}, Wt, ue(t, Vt), T(t, Bt))), e.nodes().forEach((r) => {
1622
+ let o = ce(e.node(r)), i = ue(o, Yt);
1623
+ Object.keys(cn).forEach((a) => {
1624
+ i[a] === void 0 && (i[a] = cn[a]);
1625
+ }), n.setNode(r, i);
1626
+ let s = e.parent(r);
1627
+ s !== void 0 && n.setParent(r, s);
1628
+ }), e.edges().forEach((r) => {
1629
+ let o = ce(e.edge(r));
1630
+ n.setEdge(r, Object.assign({}, Ht, ue(o, zt), T(o, qt)));
1631
+ }), n;
1632
+ }
1633
+ function Ut(e) {
1634
+ let n = e.graph();
1635
+ n.ranksep /= 2, e.edges().forEach((t) => {
1636
+ let r = e.edge(t);
1637
+ r.minlen *= 2, r.labelpos.toLowerCase() !== "c" && (n.rankdir === "TB" || n.rankdir === "BT" ? r.width += r.labeloffset : r.height += r.labeloffset);
1638
+ });
1639
+ }
1640
+ function Kt(e) {
1641
+ e.edges().forEach((n) => {
1642
+ let t = e.edge(n);
1643
+ if (t.width && t.height) {
1644
+ let r = e.node(n.v), i = { rank: (e.node(n.w).rank - r.rank) / 2 + r.rank, e: n };
1645
+ w(e, "edge-proxy", i, "_ep");
1646
+ }
1647
+ });
1648
+ }
1649
+ function $t(e) {
1650
+ let n = 0;
1651
+ e.nodes().forEach((t) => {
1652
+ let r = e.node(t);
1653
+ r.borderTop && (r.minRank = e.node(r.borderTop).rank, r.maxRank = e.node(r.borderBottom).rank, n = Math.max(n, r.maxRank));
1654
+ }), e.graph().maxRank = n;
1655
+ }
1656
+ function Jt(e) {
1657
+ e.nodes().forEach((n) => {
1658
+ let t = e.node(n);
1659
+ if (t.dummy === "edge-proxy") {
1660
+ let r = t;
1661
+ e.edge(r.e).labelRank = t.rank, e.removeNode(n);
1662
+ }
1663
+ });
1664
+ }
1665
+ function Qt(e) {
1666
+ let n = Number.POSITIVE_INFINITY, t = 0, r = Number.POSITIVE_INFINITY, o = 0, i = e.graph(), s = i.marginx || 0, a = i.marginy || 0;
1667
+ function d(l) {
1668
+ let u = l.x, c = l.y, h = l.width, f = l.height;
1669
+ n = Math.min(n, u - h / 2), t = Math.max(t, u + h / 2), r = Math.min(r, c - f / 2), o = Math.max(o, c + f / 2);
1670
+ }
1671
+ e.nodes().forEach((l) => d(e.node(l))), e.edges().forEach((l) => {
1672
+ let u = e.edge(l);
1673
+ Object.hasOwn(u, "x") && d(u);
1674
+ }), n -= s, r -= a, e.nodes().forEach((l) => {
1675
+ let u = e.node(l);
1676
+ u.x -= n, u.y -= r;
1677
+ }), e.edges().forEach((l) => {
1678
+ let u = e.edge(l);
1679
+ u.points.forEach((c) => {
1680
+ c.x -= n, c.y -= r;
1681
+ }), Object.hasOwn(u, "x") && (u.x -= n), Object.hasOwn(u, "y") && (u.y -= r);
1682
+ }), i.width = t - n + s, i.height = o - r + a;
1683
+ }
1684
+ function Zt(e) {
1685
+ e.edges().forEach((n) => {
1686
+ let t = e.edge(n), r = e.node(n.v), o = e.node(n.w), i, s;
1687
+ t.points ? (i = t.points[0], s = t.points[t.points.length - 1]) : (t.points = [], i = o, s = r), t.points.unshift(H(r, i)), t.points.push(H(o, s));
1688
+ });
1689
+ }
1690
+ function er(e) {
1691
+ e.edges().forEach((n) => {
1692
+ let t = e.edge(n);
1693
+ if (Object.hasOwn(t, "x")) switch ((t.labelpos === "l" || t.labelpos === "r") && (t.width -= t.labeloffset), t.labelpos) {
1694
+ case "l":
1695
+ t.x -= t.width / 2 + t.labeloffset;
1696
+ break;
1697
+ case "r":
1698
+ t.x += t.width / 2 + t.labeloffset;
1699
+ break;
1700
+ }
1701
+ });
1702
+ }
1703
+ function nr(e) {
1704
+ e.edges().forEach((n) => {
1705
+ let t = e.edge(n);
1706
+ t.reversed && t.points.reverse();
1707
+ });
1708
+ }
1709
+ function tr(e) {
1710
+ e.nodes().forEach((n) => {
1711
+ if (e.children(n).length) {
1712
+ let t = e.node(n), r = e.node(t.borderTop), o = e.node(t.borderBottom), i = e.node(t.borderLeft[t.borderLeft.length - 1]), s = e.node(t.borderRight[t.borderRight.length - 1]);
1713
+ t.width = Math.abs(s.x - i.x), t.height = Math.abs(o.y - r.y), t.x = i.x + t.width / 2, t.y = r.y + t.height / 2;
1714
+ }
1715
+ }), e.nodes().forEach((n) => {
1716
+ e.node(n).dummy === "border" && e.removeNode(n);
1717
+ });
1718
+ }
1719
+ function rr(e) {
1720
+ e.edges().forEach((n) => {
1721
+ if (n.v === n.w) {
1722
+ let t = e.node(n.v);
1723
+ t.selfEdges || (t.selfEdges = []), t.selfEdges.push({ e: n, label: e.edge(n) }), e.removeEdge(n);
1724
+ }
1725
+ });
1726
+ }
1727
+ function or(e) {
1728
+ N(e).forEach((t) => {
1729
+ let r = 0;
1730
+ t.forEach((o, i) => {
1731
+ let s = e.node(o);
1732
+ s.order = i + r, (s.selfEdges || []).forEach((a) => {
1733
+ w(e, "selfedge", { width: a.label.width, height: a.label.height, rank: s.rank, order: i + ++r, e: a.e, label: a.label }, "_se");
1734
+ }), delete s.selfEdges;
1735
+ });
1736
+ });
1737
+ }
1738
+ function ir(e) {
1739
+ e.nodes().forEach((n) => {
1740
+ let t = e.node(n);
1741
+ if (t.dummy === "selfedge") {
1742
+ let r = t, o = e.node(r.e.v), i = o.x + o.width / 2, s = o.y, a = t.x - i, d = o.height / 2;
1743
+ e.setEdge(r.e, r.label), e.removeNode(n), r.label.points = [{ x: i + 2 * a / 3, y: s - d }, { x: i + 5 * a / 6, y: s - d }, { x: i + a, y: s }, { x: i + 5 * a / 6, y: s + d }, { x: i + 2 * a / 3, y: s + d }], r.label.x = t.x, r.label.y = t.y;
1744
+ }
1745
+ });
1746
+ }
1747
+ function ue(e, n) {
1748
+ return O(T(e, n), Number);
1749
+ }
1750
+ function ce(e) {
1751
+ let n = {};
1752
+ return e && Object.entries(e).forEach(([t, r]) => {
1753
+ typeof t == "string" && (t = t.toLowerCase()), n[t] = r;
1754
+ }), n;
1755
+ }
1756
+ function fe(e) {
1757
+ let n = N(e), t = new p({ compound: true, multigraph: true }).setGraph({});
1758
+ return e.nodes().forEach((r) => {
1759
+ t.setNode(r, { label: r }), t.setParent(r, "layer" + e.node(r).rank);
1760
+ }), e.edges().forEach((r) => t.setEdge(r.v, r.w, {}, r.name)), n.forEach((r, o) => {
1761
+ let i = "layer" + o;
1762
+ t.setNode(i, { rank: "same" }), r.reduce((s, a) => (t.setEdge(s, a, { style: "invis" }), a));
1763
+ }), t;
1764
+ }
1765
+ var sr = { graphlib: z, version: U, layout: he, debug: fe, util: { time: P, notime: M } };
1766
+ var To = sr;
1767
+
1768
+ // src/shared/text.ts
1769
+ var UI_FONT_FAMILY = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
1770
+ var NARROW = new Set("ijltfI.,:;'|!()[]{}-/\\ ".split(""));
1771
+ var WIDE = new Set("mwMW@%".split(""));
1772
+ var canvasCtx;
1773
+ function getCtx() {
1774
+ if (canvasCtx !== void 0) return canvasCtx;
1775
+ const isJsdom = typeof navigator !== "undefined" && /jsdom/i.test(navigator.userAgent || "");
1776
+ if (isJsdom) {
1777
+ canvasCtx = null;
1778
+ return canvasCtx;
1779
+ }
1780
+ try {
1781
+ const c = typeof document !== "undefined" ? document.createElement("canvas") : null;
1782
+ const ctx = c ? c.getContext("2d") : null;
1783
+ canvasCtx = ctx && typeof ctx.measureText === "function" && ctx.measureText("M").width > 0 ? ctx : null;
1784
+ } catch {
1785
+ canvasCtx = null;
1786
+ }
1787
+ return canvasCtx;
1788
+ }
1789
+ function isBold(weight) {
1790
+ return weight === "bold" || typeof weight === "number" && weight >= 600;
1791
+ }
1792
+ var cache = /* @__PURE__ */ new Map();
1793
+ function measureText(text, font) {
1794
+ if (!text) return 0;
1795
+ const weight = font.weight ?? "normal";
1796
+ const family = font.family ?? UI_FONT_FAMILY;
1797
+ const key = `${font.size}|${weight}|${family}|${text}`;
1798
+ const hit = cache.get(key);
1799
+ if (hit !== void 0) return hit;
1800
+ let width;
1801
+ const ctx = getCtx();
1802
+ if (ctx) {
1803
+ ctx.font = `${isBold(weight) ? "700" : "400"} ${font.size}px ${family}`;
1804
+ width = ctx.measureText(text).width;
1805
+ } else {
1806
+ const boldFactor = isBold(weight) ? 1.05 : 1;
1807
+ let units = 0;
1808
+ for (const ch of text) {
1809
+ if (NARROW.has(ch)) units += 0.32;
1810
+ else if (WIDE.has(ch)) units += 0.87;
1811
+ else if (ch >= "A" && ch <= "Z") units += 0.7;
1812
+ else if (ch >= "0" && ch <= "9") units += 0.56;
1813
+ else units += 0.53;
1814
+ }
1815
+ width = units * font.size * boldFactor;
1816
+ }
1817
+ cache.set(key, width);
1818
+ return width;
1819
+ }
1820
+ function lineHeight(fontSize) {
1821
+ return Math.round(fontSize * 1.3);
1822
+ }
1823
+ function wrapText(text, maxWidth, font) {
1824
+ const words = text.split(/\s+/).filter(Boolean);
1825
+ if (words.length === 0) return [];
1826
+ const lines = [];
1827
+ let line = "";
1828
+ const pushHardBroken = (word) => {
1829
+ const chunks = hardBreak(word, maxWidth, font);
1830
+ for (let i = 0; i < chunks.length - 1; i++) lines.push(chunks[i]);
1831
+ line = chunks[chunks.length - 1] ?? "";
1832
+ };
1833
+ for (const word of words) {
1834
+ if (!line) {
1835
+ if (measureText(word, font) > maxWidth) pushHardBroken(word);
1836
+ else line = word;
1837
+ continue;
1838
+ }
1839
+ const candidate = `${line} ${word}`;
1840
+ if (measureText(candidate, font) <= maxWidth) {
1841
+ line = candidate;
1842
+ } else {
1843
+ lines.push(line);
1844
+ if (measureText(word, font) > maxWidth) pushHardBroken(word);
1845
+ else line = word;
1846
+ }
1847
+ }
1848
+ if (line) lines.push(line);
1849
+ return lines;
1850
+ }
1851
+ function hardBreak(word, maxWidth, font) {
1852
+ const out = [];
1853
+ let cur = "";
1854
+ for (const ch of word) {
1855
+ if (cur && measureText(cur + ch, font) > maxWidth) {
1856
+ out.push(cur);
1857
+ cur = ch;
1858
+ } else {
1859
+ cur += ch;
1860
+ }
1861
+ }
1862
+ if (cur) out.push(cur);
1863
+ return out.length ? out : [word];
1864
+ }
1865
+
1866
+ // src/shared/edge-label.ts
1867
+ var SVG_NS = "http://www.w3.org/2000/svg";
1868
+ var PAD_X = 7;
1869
+ var PAD_Y = 3;
1870
+ var DEFAULT_MAX_WIDTH = 150;
1871
+ var DEFAULT_FONT_SIZE = 10;
1872
+ function layout(text, maxWidth, fontSize) {
1873
+ const font = { size: fontSize, weight: 600 };
1874
+ const lines = wrapText(text, maxWidth, font);
1875
+ const textW = lines.reduce((m, l) => Math.max(m, measureText(l, font)), 1);
1876
+ const lh = lineHeight(fontSize);
1877
+ return {
1878
+ lines,
1879
+ size: { w: Math.ceil(textW + PAD_X * 2), h: Math.ceil(lines.length * lh + PAD_Y * 2) }
1880
+ };
1881
+ }
1882
+ function edgeLabelSize(text, maxWidth = DEFAULT_MAX_WIDTH, fontSize = DEFAULT_FONT_SIZE) {
1883
+ return layout(text, maxWidth, fontSize).size;
1884
+ }
1885
+ function createEdgeLabel(opts) {
1886
+ const fontSize = opts.fontSize ?? DEFAULT_FONT_SIZE;
1887
+ const maxWidth = opts.maxWidth ?? DEFAULT_MAX_WIDTH;
1888
+ const { lines, size } = layout(opts.text, maxWidth, fontSize);
1889
+ const lh = lineHeight(fontSize);
1890
+ const g = document.createElementNS(SVG_NS, "g");
1891
+ g.setAttribute("class", opts.className ?? "d5-edge-label");
1892
+ const rect = document.createElementNS(SVG_NS, "rect");
1893
+ rect.setAttribute("x", String(opts.x - size.w / 2));
1894
+ rect.setAttribute("y", String(opts.y - size.h / 2));
1895
+ rect.setAttribute("width", String(size.w));
1896
+ rect.setAttribute("height", String(size.h));
1897
+ rect.setAttribute("rx", "4");
1898
+ rect.setAttribute("fill", "white");
1899
+ rect.setAttribute("fill-opacity", "0.92");
1900
+ rect.setAttribute("stroke", "#cbd5e1");
1901
+ rect.setAttribute("stroke-width", "1");
1902
+ g.appendChild(rect);
1903
+ const firstBaseline = opts.y - size.h / 2 + PAD_Y + fontSize * 0.82;
1904
+ lines.forEach((ln2, i) => {
1905
+ const t = document.createElementNS(SVG_NS, "text");
1906
+ t.setAttribute("x", String(opts.x));
1907
+ t.setAttribute("y", String(firstBaseline + i * lh));
1908
+ t.setAttribute("text-anchor", "middle");
1909
+ t.setAttribute("font-size", String(fontSize));
1910
+ t.setAttribute("font-weight", "600");
1911
+ t.setAttribute("fill", opts.fill ?? "#475569");
1912
+ t.textContent = ln2;
1913
+ g.appendChild(t);
1914
+ });
1915
+ return g;
1916
+ }
1917
+
1918
+ // src/shared/shape.ts
1919
+ function boxWidth(entries, padX, min, max = Infinity) {
1920
+ const widest = entries.reduce((m, e) => Math.max(m, measureText(e.text, e.font)), 0);
1921
+ return Math.round(Math.max(min, Math.min(max, widest + padX * 2)));
1922
+ }
1923
+ function gridDimensions(count, cellWidth, gap, targetWidth) {
1924
+ if (count <= 0) return { cols: 0, rows: 0 };
1925
+ const fit = Math.max(1, Math.floor((targetWidth + gap) / (cellWidth + gap)));
1926
+ const cols = Math.min(count, fit);
1927
+ const rows = Math.ceil(count / cols);
1928
+ return { cols, rows };
1929
+ }
1930
+
1931
+ // src/d5-domain/renderer.ts
1932
+ var REL_LABEL_MAX_WIDTH = 150;
1933
+ var SVG_NS2 = "http://www.w3.org/2000/svg";
1934
+ var SUBDOMAIN_MIN_WIDTH = 180;
1935
+ var SUBDOMAIN_MAX_WIDTH = 320;
1936
+ var SUBDOMAIN_HEIGHT = 70;
1937
+ var SUBDOMAIN_LABEL_PAD_X = 18;
1938
+ var DOMAIN_PADDING = 30;
1939
+ var TITLE_HEIGHT = 40;
1940
+ var DOMAIN_HEADER = 36;
1941
+ var ARROW_MARKER_SIZE = 8;
1942
+ var SUBDOMAIN_COLORS = {
1943
+ core: { fill: "#dbeafe", stroke: "#3b82f6" },
1944
+ supporting: { fill: "#fef9c3", stroke: "#ca8a04" },
1945
+ generic: { fill: "#e5e7eb", stroke: "#6b7280" }
1946
+ };
1947
+ var LEGEND_ITEMS = [
1948
+ { type: "core", label: "Core" },
1949
+ { type: "supporting", label: "Supporting" },
1950
+ { type: "generic", label: "Generic" }
1951
+ ];
1952
+ function addArrowMarker(svg) {
1953
+ let defs = svg.querySelector("defs");
1954
+ if (!defs) {
1955
+ defs = document.createElementNS(SVG_NS2, "defs");
1956
+ svg.prepend(defs);
1957
+ }
1958
+ const marker = document.createElementNS(SVG_NS2, "marker");
1959
+ marker.setAttribute("id", "d5-arrowhead");
1960
+ marker.setAttribute("markerWidth", String(ARROW_MARKER_SIZE));
1961
+ marker.setAttribute("markerHeight", String(ARROW_MARKER_SIZE));
1962
+ marker.setAttribute("refX", String(ARROW_MARKER_SIZE));
1963
+ marker.setAttribute("refY", String(ARROW_MARKER_SIZE / 2));
1964
+ marker.setAttribute("orient", "auto");
1965
+ const path = document.createElementNS(SVG_NS2, "path");
1966
+ path.setAttribute("d", `M0,0 L${ARROW_MARKER_SIZE},${ARROW_MARKER_SIZE / 2} L0,${ARROW_MARKER_SIZE}`);
1967
+ path.setAttribute("fill", "#64748b");
1968
+ marker.appendChild(path);
1969
+ defs.appendChild(marker);
1970
+ }
1971
+ function generateCurvePath(points) {
1972
+ if (!points || points.length === 0) return "";
1973
+ if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
1974
+ if (points.length === 2) return `M ${points[0].x} ${points[0].y} L ${points[1].x} ${points[1].y}`;
1975
+ let d = `M ${points[0].x} ${points[0].y}`;
1976
+ for (let i = 1; i < points.length - 1; i++) {
1977
+ const p1 = points[i];
1978
+ const p2 = points[i + 1];
1979
+ const midX = (p1.x + p2.x) / 2;
1980
+ const midY = (p1.y + p2.y) / 2;
1981
+ d += ` Q ${p1.x} ${p1.y} ${midX} ${midY}`;
1982
+ }
1983
+ const last = points[points.length - 1];
1984
+ d += ` L ${last.x} ${last.y}`;
1985
+ return d;
1986
+ }
1987
+ function render(db, container) {
1988
+ addArrowMarker(container);
1989
+ const subdomains = db.getSubdomains();
1990
+ const domain = db.getDomain();
1991
+ const relationships = db.getRelationships();
1992
+ const direction = db.getDirection();
1993
+ const horizontal = direction === "LR" || direction === "RL";
1994
+ const g = new To.graphlib.Graph();
1995
+ g.setGraph({
1996
+ rankdir: direction,
1997
+ nodesep: horizontal ? 46 : 74,
1998
+ ranksep: horizontal ? 92 : 58,
1999
+ edgesep: 28,
2000
+ acyclicer: "greedy",
2001
+ ranker: "network-simplex",
2002
+ marginx: 0,
2003
+ marginy: 0
2004
+ });
2005
+ g.setDefaultEdgeLabel(() => ({}));
2006
+ subdomains.forEach((sd) => {
2007
+ const w2 = boxWidth(
2008
+ [{ text: sd.label, font: { size: 13, weight: 600 } }],
2009
+ SUBDOMAIN_LABEL_PAD_X,
2010
+ SUBDOMAIN_MIN_WIDTH,
2011
+ SUBDOMAIN_MAX_WIDTH
2012
+ );
2013
+ g.setNode(sd.id, { width: w2, height: SUBDOMAIN_HEIGHT });
2014
+ });
2015
+ relationships.forEach((rel) => {
2016
+ const edgeCfg = { minlen: 1 };
2017
+ if (rel.label) {
2018
+ const { w: w2, h } = edgeLabelSize(rel.label, REL_LABEL_MAX_WIDTH);
2019
+ edgeCfg.width = w2;
2020
+ edgeCfg.height = h;
2021
+ edgeCfg.labelpos = "c";
2022
+ }
2023
+ g.setEdge(rel.source, rel.target, edgeCfg);
2024
+ });
2025
+ To.layout(g);
2026
+ const graphW = g.graph().width || 0;
2027
+ const graphH = g.graph().height || 0;
2028
+ const domainX = DOMAIN_PADDING;
2029
+ const domainY = (db.getTitle() ? TITLE_HEIGHT : 0) + DOMAIN_PADDING;
2030
+ let domainW = graphW + DOMAIN_PADDING * 2;
2031
+ if (!domain && subdomains.length === 0) domainW = 400;
2032
+ const domainH = (domain ? DOMAIN_HEADER : 0) + graphH + DOMAIN_PADDING * 2;
2033
+ const totalW = domainX + domainW + DOMAIN_PADDING;
2034
+ const legendH = 30;
2035
+ const totalH = domainY + domainH + legendH + DOMAIN_PADDING;
2036
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
2037
+ container.setAttribute("height", String(totalH));
2038
+ container.style.maxWidth = `${totalW}px`;
2039
+ const title = db.getTitle();
2040
+ if (title) {
2041
+ const titleEl = document.createElementNS(SVG_NS2, "text");
2042
+ titleEl.setAttribute("class", "d5-title");
2043
+ titleEl.setAttribute("x", String(totalW / 2));
2044
+ titleEl.setAttribute("y", "24");
2045
+ titleEl.setAttribute("text-anchor", "middle");
2046
+ titleEl.setAttribute("font-size", "18");
2047
+ titleEl.setAttribute("font-weight", "bold");
2048
+ titleEl.setAttribute("fill", "#1e293b");
2049
+ titleEl.textContent = title;
2050
+ container.appendChild(titleEl);
2051
+ }
2052
+ const domainGroup = document.createElementNS(SVG_NS2, "g");
2053
+ domainGroup.setAttribute("class", "d5-domain");
2054
+ const domainRect = document.createElementNS(SVG_NS2, "rect");
2055
+ domainRect.setAttribute("x", String(domainX));
2056
+ domainRect.setAttribute("y", String(domainY));
2057
+ domainRect.setAttribute("width", String(domainW));
2058
+ domainRect.setAttribute("height", String(domainH));
2059
+ domainRect.setAttribute("rx", "12");
2060
+ domainRect.setAttribute("fill", "#f8fafc");
2061
+ domainRect.setAttribute("stroke", "#94a3b8");
2062
+ domainRect.setAttribute("stroke-width", "2");
2063
+ domainRect.setAttribute("stroke-dasharray", "8 4");
2064
+ domainGroup.appendChild(domainRect);
2065
+ if (domain) {
2066
+ const domainLabel = document.createElementNS(SVG_NS2, "text");
2067
+ domainLabel.setAttribute("x", String(domainX + 16));
2068
+ domainLabel.setAttribute("y", String(domainY + 24));
2069
+ domainLabel.setAttribute("font-size", "14");
2070
+ domainLabel.setAttribute("font-weight", "600");
2071
+ domainLabel.setAttribute("fill", "#475569");
2072
+ domainLabel.textContent = domain.label;
2073
+ domainGroup.appendChild(domainLabel);
2074
+ }
2075
+ container.appendChild(domainGroup);
2076
+ const graphStartX = domainX + DOMAIN_PADDING;
2077
+ const graphStartY = domainY + (domain ? DOMAIN_HEADER : 0) + DOMAIN_PADDING;
2078
+ subdomains.forEach((sd) => {
2079
+ const node = g.node(sd.id);
2080
+ if (!node) return;
2081
+ const w2 = node.width;
2082
+ const h = node.height;
2083
+ const x2 = graphStartX + node.x - w2 / 2;
2084
+ const y = graphStartY + node.y - h / 2;
2085
+ const cx = x2 + w2 / 2;
2086
+ const cy = y + h / 2;
2087
+ const colors = SUBDOMAIN_COLORS[sd.type];
2088
+ const group = document.createElementNS(SVG_NS2, "g");
2089
+ group.setAttribute("class", `d5-subdomain d5-subdomain-${sd.type}`);
2090
+ const rect = document.createElementNS(SVG_NS2, "rect");
2091
+ rect.setAttribute("x", String(x2));
2092
+ rect.setAttribute("y", String(y));
2093
+ rect.setAttribute("width", String(w2));
2094
+ rect.setAttribute("height", String(h));
2095
+ rect.setAttribute("rx", "8");
2096
+ rect.setAttribute("fill", colors.fill);
2097
+ rect.setAttribute("stroke", colors.stroke);
2098
+ rect.setAttribute("stroke-width", "2");
2099
+ group.appendChild(rect);
2100
+ const labelText = document.createElementNS(SVG_NS2, "text");
2101
+ labelText.setAttribute("x", String(cx));
2102
+ labelText.setAttribute("y", String(cy - 4));
2103
+ labelText.setAttribute("text-anchor", "middle");
2104
+ labelText.setAttribute("font-size", "13");
2105
+ labelText.setAttribute("font-weight", "600");
2106
+ labelText.setAttribute("fill", "#1e293b");
2107
+ labelText.textContent = sd.label;
2108
+ group.appendChild(labelText);
2109
+ const typeText = document.createElementNS(SVG_NS2, "text");
2110
+ typeText.setAttribute("x", String(cx));
2111
+ typeText.setAttribute("y", String(cy + 16));
2112
+ typeText.setAttribute("text-anchor", "middle");
2113
+ typeText.setAttribute("font-size", "11");
2114
+ typeText.setAttribute("fill", colors.stroke);
2115
+ typeText.textContent = sd.type;
2116
+ group.appendChild(typeText);
2117
+ container.appendChild(group);
2118
+ });
2119
+ relationships.forEach((rel) => {
2120
+ const edge = g.edge(rel.source, rel.target);
2121
+ if (!edge || !edge.points || edge.points.length === 0) return;
2122
+ const shiftedPoints = edge.points.map((p2) => ({
2123
+ x: graphStartX + p2.x,
2124
+ y: graphStartY + p2.y
2125
+ }));
2126
+ const srcNode = g.node(rel.source);
2127
+ const tgtNode = g.node(rel.target);
2128
+ const isBackEdge = !!srcNode && !!tgtNode && tgtNode.y < srcNode.y - 1;
2129
+ const group = document.createElementNS(SVG_NS2, "g");
2130
+ group.setAttribute("class", isBackEdge ? "d5-rel d5-rel-back" : "d5-rel");
2131
+ const pathString = generateCurvePath(shiftedPoints);
2132
+ const path = document.createElementNS(SVG_NS2, "path");
2133
+ path.setAttribute("d", pathString);
2134
+ path.setAttribute("fill", "none");
2135
+ path.setAttribute("stroke", isBackEdge ? "#94a3b8" : "#64748b");
2136
+ path.setAttribute("stroke-width", "1.5");
2137
+ if (isBackEdge) path.setAttribute("stroke-dasharray", "6 4");
2138
+ path.setAttribute("marker-end", "url(#d5-arrowhead)");
2139
+ group.appendChild(path);
2140
+ if (rel.label) {
2141
+ let lx;
2142
+ let ly;
2143
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
2144
+ lx = graphStartX + edge.x;
2145
+ ly = graphStartY + edge.y;
2146
+ } else {
2147
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
2148
+ lx = mid.x;
2149
+ ly = mid.y;
2150
+ }
2151
+ group.appendChild(
2152
+ createEdgeLabel({ x: lx, y: ly, text: rel.label, maxWidth: REL_LABEL_MAX_WIDTH })
2153
+ );
2154
+ }
2155
+ container.appendChild(group);
2156
+ });
2157
+ const legendY = domainY + domainH + 16;
2158
+ const legendGroup = document.createElementNS(SVG_NS2, "g");
2159
+ legendGroup.setAttribute("class", "d5-legend");
2160
+ let legendX = domainX;
2161
+ LEGEND_ITEMS.forEach((item) => {
2162
+ const colors = SUBDOMAIN_COLORS[item.type];
2163
+ const swatch = document.createElementNS(SVG_NS2, "rect");
2164
+ swatch.setAttribute("x", String(legendX));
2165
+ swatch.setAttribute("y", String(legendY));
2166
+ swatch.setAttribute("width", "14");
2167
+ swatch.setAttribute("height", "14");
2168
+ swatch.setAttribute("rx", "3");
2169
+ swatch.setAttribute("fill", colors.fill);
2170
+ swatch.setAttribute("stroke", colors.stroke);
2171
+ swatch.setAttribute("stroke-width", "1.5");
2172
+ legendGroup.appendChild(swatch);
2173
+ const label = document.createElementNS(SVG_NS2, "text");
2174
+ label.setAttribute("x", String(legendX + 20));
2175
+ label.setAttribute("y", String(legendY + 11));
2176
+ label.setAttribute("font-size", "11");
2177
+ label.setAttribute("fill", "#64748b");
2178
+ label.textContent = item.label;
2179
+ legendGroup.appendChild(label);
2180
+ legendX += 90;
2181
+ });
2182
+ container.appendChild(legendGroup);
2183
+ }
2184
+
2185
+ // src/d5-subdomain/detector.ts
2186
+ function detectSubdomain(text) {
2187
+ return text.trimStart().startsWith("d5-subdomain");
2188
+ }
2189
+
2190
+ // src/d5-subdomain/db.ts
2191
+ var DEFAULT_DIRECTION2 = "LR";
2192
+ var D5SubdomainDb = class {
2193
+ subdomains = [];
2194
+ boundedContexts = [];
2195
+ relationships = [];
2196
+ direction = DEFAULT_DIRECTION2;
2197
+ title;
2198
+ accTitle;
2199
+ accDescription;
2200
+ setTitle(title) {
2201
+ this.title = title;
2202
+ }
2203
+ getTitle() {
2204
+ return this.title;
2205
+ }
2206
+ /** Accepts `LR` / `RL` / `TB` / `TD` / `BT` (case-insensitive); `TD` is stored as `TB`. */
2207
+ setDirection(dir) {
2208
+ const normalized = normalizeDirection(dir);
2209
+ if (normalized) this.direction = normalized;
2210
+ }
2211
+ getDirection() {
2212
+ return this.direction;
2213
+ }
2214
+ addSubdomain(id, label, type) {
2215
+ this.subdomains.push({ id, label, type });
2216
+ }
2217
+ getSubdomains() {
2218
+ return this.subdomains;
2219
+ }
2220
+ addBoundedContext(id, label, subdomainId, team) {
2221
+ this.boundedContexts.push({ id, label, subdomainId, team });
2222
+ }
2223
+ getBoundedContexts() {
2224
+ return this.boundedContexts;
2225
+ }
2226
+ addRelationship(source, target, label) {
2227
+ this.relationships.push({ source, target, label });
2228
+ }
2229
+ getRelationships() {
2230
+ return this.relationships;
2231
+ }
2232
+ clear() {
2233
+ this.subdomains = [];
2234
+ this.boundedContexts = [];
2235
+ this.relationships = [];
2236
+ this.direction = DEFAULT_DIRECTION2;
2237
+ this.title = void 0;
2238
+ this.accTitle = void 0;
2239
+ this.accDescription = void 0;
2240
+ }
2241
+ // Mermaid DiagramDB interface methods
2242
+ setDiagramTitle(title) {
2243
+ this.setTitle(title);
2244
+ }
2245
+ getDiagramTitle() {
2246
+ return this.title ?? "";
2247
+ }
2248
+ setAccTitle(title) {
2249
+ this.accTitle = title;
2250
+ }
2251
+ getAccTitle() {
2252
+ return this.accTitle ?? "";
2253
+ }
2254
+ setAccDescription(desc) {
2255
+ this.accDescription = desc;
2256
+ }
2257
+ getAccDescription() {
2258
+ return this.accDescription ?? "";
2259
+ }
2260
+ };
2261
+
2262
+ // src/d5-subdomain/parser.ts
2263
+ var TITLE_RE2 = /^\s*title\s+(.+)$/;
2264
+ var SUBDOMAIN_RE2 = /^\s*Subdomain\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*(\w+)\s*\)\s*\{/;
2265
+ var BC_RE = /^\s*BoundedContext\(\s*(\w+)\s*,\s*"([^"]+)"\s*(?:,\s*team:\s*"([^"]+)")?\s*\)/;
2266
+ var REL_RE2 = /^\s*Rel\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2267
+ var CLOSE_BRACE_RE = /^\s*\}/;
2268
+ var COMMENT_LINE_RE2 = /^\s*%%/;
2269
+ function stripInlineComment2(line) {
2270
+ const idx = line.indexOf("%%");
2271
+ return idx === -1 ? line : line.slice(0, idx);
2272
+ }
2273
+ function parse2(text, db) {
2274
+ const lines = text.split("\n");
2275
+ let currentSubdomainId;
2276
+ for (const raw of lines) {
2277
+ if (COMMENT_LINE_RE2.test(raw)) continue;
2278
+ const line = stripInlineComment2(raw).trim();
2279
+ if (line === "" || line === "d5-subdomain") continue;
2280
+ let m;
2281
+ if (m = line.match(DIRECTION_RE)) {
2282
+ db.setDirection(m[1]);
2283
+ } else if (m = line.match(TITLE_RE2)) {
2284
+ db.setTitle(m[1].trim());
2285
+ } else if (m = line.match(SUBDOMAIN_RE2)) {
2286
+ db.addSubdomain(m[1], m[2], m[3]);
2287
+ currentSubdomainId = m[1];
2288
+ } else if (m = line.match(BC_RE)) {
2289
+ const team = m[3] || void 0;
2290
+ db.addBoundedContext(m[1], m[2], currentSubdomainId, team);
2291
+ } else if (m = line.match(REL_RE2)) {
2292
+ db.addRelationship(m[1], m[2], m[3]);
2293
+ } else if (CLOSE_BRACE_RE.test(line)) {
2294
+ currentSubdomainId = void 0;
2295
+ }
2296
+ }
2297
+ }
2298
+
2299
+ // src/shared/context-relationship.ts
2300
+ var TABLE = [
2301
+ {
2302
+ keys: ["partnership", "p"],
2303
+ pattern: { name: "Partnership", badge: "P", kind: "symmetric", thick: true }
2304
+ },
2305
+ {
2306
+ keys: ["sharedkernel", "sk"],
2307
+ pattern: { name: "Shared Kernel", badge: "SK", kind: "symmetric", doubleStroke: true }
2308
+ },
2309
+ {
2310
+ keys: ["customersupplier", "customersupplier", "cs", "cusup"],
2311
+ pattern: {
2312
+ name: "Customer-Supplier",
2313
+ badge: "C/S",
2314
+ kind: "directional",
2315
+ upstreamRole: "S",
2316
+ downstreamRole: "C"
2317
+ }
2318
+ },
2319
+ {
2320
+ keys: ["conformist", "cf"],
2321
+ pattern: {
2322
+ name: "Conformist",
2323
+ badge: "CF",
2324
+ kind: "directional",
2325
+ upstreamRole: "U",
2326
+ downstreamRole: "D"
2327
+ }
2328
+ },
2329
+ {
2330
+ keys: ["anticorruptionlayer", "anticorruption", "acl"],
2331
+ pattern: {
2332
+ name: "Anti-Corruption Layer",
2333
+ badge: "ACL",
2334
+ kind: "directional",
2335
+ upstreamRole: "U",
2336
+ downstreamRole: "D",
2337
+ aclGate: true
2338
+ }
2339
+ },
2340
+ {
2341
+ keys: ["openhostservice", "ohs"],
2342
+ pattern: {
2343
+ name: "Open Host Service",
2344
+ badge: "OHS",
2345
+ kind: "directional",
2346
+ upstreamRole: "U",
2347
+ downstreamRole: "D",
2348
+ ohsSocket: true
2349
+ }
2350
+ },
2351
+ {
2352
+ keys: ["publishedlanguage", "pl"],
2353
+ pattern: {
2354
+ name: "Published Language",
2355
+ badge: "PL",
2356
+ kind: "directional",
2357
+ upstreamRole: "U",
2358
+ downstreamRole: "D"
2359
+ }
2360
+ },
2361
+ {
2362
+ keys: ["separateways", "sw"],
2363
+ pattern: { name: "Separate Ways", badge: "", kind: "none", dashed: true }
2364
+ },
2365
+ {
2366
+ keys: ["bigballofmud", "bbom", "bbm"],
2367
+ pattern: {
2368
+ name: "Big Ball of Mud",
2369
+ badge: "BBoM",
2370
+ kind: "directional",
2371
+ upstreamRole: "U",
2372
+ downstreamRole: "D"
2373
+ }
2374
+ }
2375
+ ];
2376
+ function normalize(label) {
2377
+ return label.trim().toLowerCase().replace(/[\s_/()-]+/g, "");
2378
+ }
2379
+ function classifyRelationship(label) {
2380
+ const norm = normalize(label);
2381
+ for (const { keys, pattern } of TABLE) {
2382
+ if (keys.includes(norm)) return pattern;
2383
+ }
2384
+ return null;
2385
+ }
2386
+
2387
+ // src/d5-subdomain/renderer.ts
2388
+ var REL_LABEL_MAX_WIDTH2 = 150;
2389
+ var REL_STROKE = "#64748b";
2390
+ var SVG_NS3 = "http://www.w3.org/2000/svg";
2391
+ function pointAlong(from, to, dist) {
2392
+ const dx = to.x - from.x;
2393
+ const dy = to.y - from.y;
2394
+ const len = Math.hypot(dx, dy) || 1;
2395
+ return { x: from.x + dx / len * dist, y: from.y + dy / len * dist };
2396
+ }
2397
+ function angleDeg(from, to) {
2398
+ return Math.atan2(to.y - from.y, to.x - from.x) * 180 / Math.PI;
2399
+ }
2400
+ function roleMarker(cx, cy, text) {
2401
+ const g = document.createElementNS(SVG_NS3, "g");
2402
+ g.setAttribute("class", "d5-rel-role");
2403
+ const c = document.createElementNS(SVG_NS3, "circle");
2404
+ c.setAttribute("cx", String(cx));
2405
+ c.setAttribute("cy", String(cy));
2406
+ c.setAttribute("r", "8");
2407
+ c.setAttribute("fill", "white");
2408
+ c.setAttribute("stroke", REL_STROKE);
2409
+ c.setAttribute("stroke-width", "1.25");
2410
+ g.appendChild(c);
2411
+ const t = document.createElementNS(SVG_NS3, "text");
2412
+ t.setAttribute("x", String(cx));
2413
+ t.setAttribute("y", String(cy + 3));
2414
+ t.setAttribute("text-anchor", "middle");
2415
+ t.setAttribute("font-size", "9");
2416
+ t.setAttribute("font-weight", "bold");
2417
+ t.setAttribute("fill", "#475569");
2418
+ t.textContent = text;
2419
+ g.appendChild(t);
2420
+ return g;
2421
+ }
2422
+ function patternBadge(cx, cy, text) {
2423
+ const g = document.createElementNS(SVG_NS3, "g");
2424
+ g.setAttribute("class", "d5-rel-badge");
2425
+ const w2 = Math.ceil(measureText(text, { size: 10, weight: 700 }) + 12);
2426
+ const h = 18;
2427
+ const rect = document.createElementNS(SVG_NS3, "rect");
2428
+ rect.setAttribute("x", String(cx - w2 / 2));
2429
+ rect.setAttribute("y", String(cy - h / 2));
2430
+ rect.setAttribute("width", String(w2));
2431
+ rect.setAttribute("height", String(h));
2432
+ rect.setAttribute("rx", "4");
2433
+ rect.setAttribute("fill", "#eef2ff");
2434
+ rect.setAttribute("stroke", "#c7d2fe");
2435
+ rect.setAttribute("stroke-width", "1");
2436
+ g.appendChild(rect);
2437
+ const t = document.createElementNS(SVG_NS3, "text");
2438
+ t.setAttribute("x", String(cx));
2439
+ t.setAttribute("y", String(cy + 3.5));
2440
+ t.setAttribute("text-anchor", "middle");
2441
+ t.setAttribute("font-size", "10");
2442
+ t.setAttribute("font-weight", "700");
2443
+ t.setAttribute("fill", "#4338ca");
2444
+ t.textContent = text;
2445
+ g.appendChild(t);
2446
+ return g;
2447
+ }
2448
+ function aclGate(cx, cy, deg) {
2449
+ const g = document.createElementNS(SVG_NS3, "g");
2450
+ g.setAttribute("class", "d5-rel-acl");
2451
+ g.setAttribute("transform", `translate(${cx} ${cy}) rotate(${deg})`);
2452
+ const rect = document.createElementNS(SVG_NS3, "rect");
2453
+ rect.setAttribute("x", "-9");
2454
+ rect.setAttribute("y", "-7");
2455
+ rect.setAttribute("width", "18");
2456
+ rect.setAttribute("height", "14");
2457
+ rect.setAttribute("rx", "2");
2458
+ rect.setAttribute("fill", "white");
2459
+ rect.setAttribute("stroke", REL_STROKE);
2460
+ rect.setAttribute("stroke-width", "1.5");
2461
+ g.appendChild(rect);
2462
+ for (const dx of [-3, 3]) {
2463
+ const tick = document.createElementNS(SVG_NS3, "line");
2464
+ tick.setAttribute("x1", String(dx));
2465
+ tick.setAttribute("y1", "-7");
2466
+ tick.setAttribute("x2", String(dx));
2467
+ tick.setAttribute("y2", "7");
2468
+ tick.setAttribute("stroke", REL_STROKE);
2469
+ tick.setAttribute("stroke-width", "1");
2470
+ g.appendChild(tick);
2471
+ }
2472
+ return g;
2473
+ }
2474
+ function ohsSocket(cx, cy) {
2475
+ const g = document.createElementNS(SVG_NS3, "g");
2476
+ g.setAttribute("class", "d5-rel-ohs");
2477
+ const c = document.createElementNS(SVG_NS3, "circle");
2478
+ c.setAttribute("cx", String(cx));
2479
+ c.setAttribute("cy", String(cy));
2480
+ c.setAttribute("r", "5");
2481
+ c.setAttribute("fill", "white");
2482
+ c.setAttribute("stroke", REL_STROKE);
2483
+ c.setAttribute("stroke-width", "1.5");
2484
+ g.appendChild(c);
2485
+ return g;
2486
+ }
2487
+ var BC_RX = 80;
2488
+ var BC_RY = 28;
2489
+ var BC_LABEL_WRAP_W = 150;
2490
+ var TITLE_HEIGHT2 = 40;
2491
+ var MARGIN = 30;
2492
+ var ARROW_MARKER_SIZE2 = 8;
2493
+ var BC_LABEL_FONT = 12;
2494
+ var BC_TEAM_FONT = 10;
2495
+ var LEGEND_H = 30;
2496
+ var SUBDOMAIN_LABEL_H = 24;
2497
+ var SUBDOMAIN_LABEL_FONT = 13;
2498
+ var CLUSTER_PADDING_X = 20;
2499
+ var CLUSTER_PADDING_Y_TOP = SUBDOMAIN_LABEL_H + 12;
2500
+ var CLUSTER_PADDING_Y_BOTTOM = 16;
2501
+ var NODESEP = 46;
2502
+ var RANKSEP = 96;
2503
+ var SUBDOMAIN_COLORS2 = {
2504
+ core: { fill: "#dbeafe", stroke: "#3b82f6" },
2505
+ supporting: { fill: "#fef9c3", stroke: "#ca8a04" },
2506
+ generic: { fill: "#e5e7eb", stroke: "#6b7280" }
2507
+ };
2508
+ var LEGEND_ITEMS2 = [
2509
+ { type: "core", label: "Core" },
2510
+ { type: "supporting", label: "Supporting" },
2511
+ { type: "generic", label: "Generic" }
2512
+ ];
2513
+ function addArrowMarker2(svg) {
2514
+ let defs = svg.querySelector("defs");
2515
+ if (!defs) {
2516
+ defs = document.createElementNS(SVG_NS3, "defs");
2517
+ svg.prepend(defs);
2518
+ }
2519
+ const marker = document.createElementNS(SVG_NS3, "marker");
2520
+ marker.setAttribute("id", "d5-arrowhead");
2521
+ marker.setAttribute("markerWidth", String(ARROW_MARKER_SIZE2));
2522
+ marker.setAttribute("markerHeight", String(ARROW_MARKER_SIZE2));
2523
+ marker.setAttribute("refX", String(ARROW_MARKER_SIZE2));
2524
+ marker.setAttribute("refY", String(ARROW_MARKER_SIZE2 / 2));
2525
+ marker.setAttribute("orient", "auto");
2526
+ const path = document.createElementNS(SVG_NS3, "path");
2527
+ path.setAttribute(
2528
+ "d",
2529
+ `M0,0 L${ARROW_MARKER_SIZE2},${ARROW_MARKER_SIZE2 / 2} L0,${ARROW_MARKER_SIZE2}`
2530
+ );
2531
+ path.setAttribute("fill", "#64748b");
2532
+ marker.appendChild(path);
2533
+ defs.appendChild(marker);
2534
+ }
2535
+ function generateCurvePath2(points) {
2536
+ if (!points || points.length === 0) return "";
2537
+ if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
2538
+ if (points.length === 2) return `M ${points[0].x} ${points[0].y} L ${points[1].x} ${points[1].y}`;
2539
+ let d = `M ${points[0].x} ${points[0].y}`;
2540
+ for (let i = 1; i < points.length - 1; i++) {
2541
+ const p1 = points[i];
2542
+ const p2 = points[i + 1];
2543
+ const midX = (p1.x + p2.x) / 2;
2544
+ const midY = (p1.y + p2.y) / 2;
2545
+ d += ` Q ${p1.x} ${p1.y} ${midX} ${midY}`;
2546
+ }
2547
+ const last = points[points.length - 1];
2548
+ d += ` L ${last.x} ${last.y}`;
2549
+ return d;
2550
+ }
2551
+ function render2(db, container) {
2552
+ addArrowMarker2(container);
2553
+ const title = db.getTitle();
2554
+ const subdomains = db.getSubdomains();
2555
+ const boundedContexts = db.getBoundedContexts();
2556
+ const relationships = db.getRelationships();
2557
+ const direction = db.getDirection();
2558
+ const horizontal = direction === "LR" || direction === "RL";
2559
+ const g = new To.graphlib.Graph({ compound: true });
2560
+ g.setGraph({
2561
+ rankdir: direction,
2562
+ nodesep: NODESEP,
2563
+ ranksep: horizontal ? RANKSEP : 64,
2564
+ edgesep: 30,
2565
+ acyclicer: "greedy",
2566
+ ranker: "network-simplex",
2567
+ marginx: 0,
2568
+ marginy: 0
2569
+ });
2570
+ g.setDefaultEdgeLabel(() => ({}));
2571
+ const subdomainIds = new Set(subdomains.map((s) => s.id));
2572
+ const bcBoxes = /* @__PURE__ */ new Map();
2573
+ boundedContexts.forEach((bc) => {
2574
+ const labelLines = wrapText(bc.label, BC_LABEL_WRAP_W, {
2575
+ size: BC_LABEL_FONT,
2576
+ weight: 600
2577
+ });
2578
+ const widest = labelLines.reduce(
2579
+ (m, l) => Math.max(m, measureText(l, { size: BC_LABEL_FONT, weight: 600 })),
2580
+ 0
2581
+ );
2582
+ const teamW = bc.team ? measureText(bc.team, { size: BC_TEAM_FONT }) : 0;
2583
+ const contentW = Math.max(widest, teamW);
2584
+ const contentH = labelLines.length * lineHeight(BC_LABEL_FONT) + (bc.team ? lineHeight(BC_TEAM_FONT) : 0);
2585
+ const rx = Math.round(Math.max(BC_RX, contentW / 1.55 + 16));
2586
+ const ry = Math.round(Math.max(BC_RY, contentH / 1.5 + 12));
2587
+ bcBoxes.set(bc.id, { rx, ry, labelLines });
2588
+ });
2589
+ subdomains.forEach((sd) => {
2590
+ const labelW = measureText(sd.label, {
2591
+ size: SUBDOMAIN_LABEL_FONT,
2592
+ weight: 600
2593
+ });
2594
+ const padX = Math.max(CLUSTER_PADDING_X, Math.ceil((labelW + 28 - 2 * BC_RX) / 2));
2595
+ g.setNode(sd.id, {
2596
+ label: sd.label,
2597
+ clusterLabelPos: "top",
2598
+ paddingTop: CLUSTER_PADDING_Y_TOP,
2599
+ paddingBottom: CLUSTER_PADDING_Y_BOTTOM,
2600
+ paddingLeft: padX,
2601
+ paddingRight: padX
2602
+ });
2603
+ });
2604
+ boundedContexts.forEach((bc) => {
2605
+ const box = bcBoxes.get(bc.id);
2606
+ g.setNode(bc.id, { width: box.rx * 2, height: box.ry * 2 });
2607
+ if (bc.subdomainId && subdomainIds.has(bc.subdomainId)) {
2608
+ g.setParent(bc.id, bc.subdomainId);
2609
+ }
2610
+ });
2611
+ relationships.forEach((rel) => {
2612
+ const edgeCfg = { minlen: 1 };
2613
+ if (rel.label) {
2614
+ const pattern = classifyRelationship(rel.label);
2615
+ const { w: w2, h } = pattern ? { w: pattern.badge ? Math.ceil(measureText(pattern.badge, { size: 10, weight: 700 }) + 16) : 8, h: 20 } : edgeLabelSize(rel.label, REL_LABEL_MAX_WIDTH2);
2616
+ edgeCfg.width = w2;
2617
+ edgeCfg.height = h;
2618
+ edgeCfg.labelpos = "c";
2619
+ }
2620
+ g.setEdge(rel.source, rel.target, edgeCfg);
2621
+ });
2622
+ To.layout(g);
2623
+ const graphW = g.graph().width || 0;
2624
+ const graphH = g.graph().height || 0;
2625
+ const usedPatterns = [];
2626
+ for (const rel of relationships) {
2627
+ const p2 = rel.label ? classifyRelationship(rel.label) : null;
2628
+ if (p2 && !usedPatterns.some((u) => u.name === p2.name)) usedPatterns.push(p2);
2629
+ }
2630
+ const gridStartX = MARGIN;
2631
+ const gridStartY = MARGIN + (title ? TITLE_HEIGHT2 : 0);
2632
+ const totalW = gridStartX + graphW + MARGIN;
2633
+ const totalH = gridStartY + graphH + LEGEND_H * (usedPatterns.length > 0 ? 2 : 1) + MARGIN;
2634
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
2635
+ container.setAttribute("height", String(totalH));
2636
+ container.style.maxWidth = `${totalW}px`;
2637
+ if (title) {
2638
+ const titleEl = document.createElementNS(SVG_NS3, "text");
2639
+ titleEl.setAttribute("class", "d5-title");
2640
+ titleEl.setAttribute("x", String(totalW / 2));
2641
+ titleEl.setAttribute("y", "24");
2642
+ titleEl.setAttribute("text-anchor", "middle");
2643
+ titleEl.setAttribute("font-size", "18");
2644
+ titleEl.setAttribute("font-weight", "bold");
2645
+ titleEl.setAttribute("fill", "#1e293b");
2646
+ titleEl.textContent = title;
2647
+ container.appendChild(titleEl);
2648
+ }
2649
+ subdomains.forEach((sd) => {
2650
+ const node = g.node(sd.id);
2651
+ if (!node) return;
2652
+ const colors = SUBDOMAIN_COLORS2[sd.type];
2653
+ const w2 = node.width;
2654
+ const h = node.height;
2655
+ const x2 = gridStartX + node.x - w2 / 2;
2656
+ const y = gridStartY + node.y - h / 2;
2657
+ const group = document.createElementNS(SVG_NS3, "g");
2658
+ group.setAttribute("class", `d5-subdomain d5-subdomain-${sd.type}`);
2659
+ const rect = document.createElementNS(SVG_NS3, "rect");
2660
+ rect.setAttribute("x", String(x2));
2661
+ rect.setAttribute("y", String(y));
2662
+ rect.setAttribute("width", String(w2));
2663
+ rect.setAttribute("height", String(h));
2664
+ rect.setAttribute("rx", "12");
2665
+ rect.setAttribute("fill", colors.fill);
2666
+ rect.setAttribute("stroke", colors.stroke);
2667
+ rect.setAttribute("stroke-width", "2");
2668
+ rect.setAttribute("stroke-dasharray", "8 4");
2669
+ group.appendChild(rect);
2670
+ const labelText = document.createElementNS(SVG_NS3, "text");
2671
+ labelText.setAttribute("x", String(x2 + w2 / 2));
2672
+ labelText.setAttribute("y", String(y + 20));
2673
+ labelText.setAttribute("text-anchor", "middle");
2674
+ labelText.setAttribute("font-size", "13");
2675
+ labelText.setAttribute("font-weight", "600");
2676
+ labelText.setAttribute("fill", "#1e293b");
2677
+ labelText.textContent = sd.label;
2678
+ group.appendChild(labelText);
2679
+ container.appendChild(group);
2680
+ });
2681
+ boundedContexts.forEach((bc) => {
2682
+ const node = g.node(bc.id);
2683
+ if (!node) return;
2684
+ const sd = subdomains.find((s) => s.id === bc.subdomainId);
2685
+ const colors = sd ? SUBDOMAIN_COLORS2[sd.type] : { stroke: "#64748b" };
2686
+ const cx = gridStartX + node.x;
2687
+ const cy = gridStartY + node.y;
2688
+ const box = bcBoxes.get(bc.id);
2689
+ const bcGroup = document.createElementNS(SVG_NS3, "g");
2690
+ bcGroup.setAttribute("class", "d5-bounded-context");
2691
+ const ellipse = document.createElementNS(SVG_NS3, "ellipse");
2692
+ ellipse.setAttribute("cx", String(cx));
2693
+ ellipse.setAttribute("cy", String(cy));
2694
+ ellipse.setAttribute("rx", String(box.rx));
2695
+ ellipse.setAttribute("ry", String(box.ry));
2696
+ ellipse.setAttribute("fill", "white");
2697
+ ellipse.setAttribute("stroke", colors.stroke);
2698
+ ellipse.setAttribute("stroke-width", "2");
2699
+ bcGroup.appendChild(ellipse);
2700
+ const labelLH = lineHeight(BC_LABEL_FONT);
2701
+ const teamLH = bc.team ? lineHeight(BC_TEAM_FONT) : 0;
2702
+ const blockH = box.labelLines.length * labelLH + teamLH;
2703
+ let ty = cy - blockH / 2 + BC_LABEL_FONT * 0.85;
2704
+ box.labelLines.forEach((ln2) => {
2705
+ const bcLabel = document.createElementNS(SVG_NS3, "text");
2706
+ bcLabel.setAttribute("x", String(cx));
2707
+ bcLabel.setAttribute("y", String(ty));
2708
+ bcLabel.setAttribute("text-anchor", "middle");
2709
+ bcLabel.setAttribute("font-size", String(BC_LABEL_FONT));
2710
+ bcLabel.setAttribute("font-weight", "600");
2711
+ bcLabel.setAttribute("fill", "#1e293b");
2712
+ bcLabel.textContent = ln2;
2713
+ bcGroup.appendChild(bcLabel);
2714
+ ty += labelLH;
2715
+ });
2716
+ if (bc.team) {
2717
+ const teamLabel = document.createElementNS(SVG_NS3, "text");
2718
+ teamLabel.setAttribute("x", String(cx));
2719
+ teamLabel.setAttribute("y", String(ty + 2));
2720
+ teamLabel.setAttribute("text-anchor", "middle");
2721
+ teamLabel.setAttribute("font-size", String(BC_TEAM_FONT));
2722
+ teamLabel.setAttribute("font-style", "italic");
2723
+ teamLabel.setAttribute("fill", "#64748b");
2724
+ teamLabel.textContent = bc.team;
2725
+ bcGroup.appendChild(teamLabel);
2726
+ }
2727
+ container.appendChild(bcGroup);
2728
+ });
2729
+ relationships.forEach((rel) => {
2730
+ const edge = g.edge(rel.source, rel.target);
2731
+ if (!edge || !edge.points || edge.points.length === 0) return;
2732
+ const shiftedPoints = edge.points.map((p2) => ({
2733
+ x: gridStartX + p2.x,
2734
+ y: gridStartY + p2.y
2735
+ }));
2736
+ const pattern = rel.label ? classifyRelationship(rel.label) : null;
2737
+ const symmetric = !pattern || pattern.kind !== "directional";
2738
+ const group = document.createElementNS(SVG_NS3, "g");
2739
+ group.setAttribute("class", pattern ? `d5-rel d5-rel-${pattern.badge || "sw"}` : "d5-rel");
2740
+ if (pattern) {
2741
+ const title2 = document.createElementNS(SVG_NS3, "title");
2742
+ title2.textContent = pattern.name;
2743
+ group.appendChild(title2);
2744
+ }
2745
+ const strokeW = pattern?.thick ? 3 : pattern?.doubleStroke ? 4 : 1.5;
2746
+ const path = document.createElementNS(SVG_NS3, "path");
2747
+ path.setAttribute("d", generateCurvePath2(shiftedPoints));
2748
+ path.setAttribute("fill", "none");
2749
+ path.setAttribute("stroke", REL_STROKE);
2750
+ path.setAttribute("stroke-width", String(strokeW));
2751
+ if (pattern?.dashed) path.setAttribute("stroke-dasharray", "7 5");
2752
+ if (!symmetric) path.setAttribute("marker-end", "url(#d5-arrowhead)");
2753
+ group.appendChild(path);
2754
+ if (pattern?.doubleStroke) {
2755
+ const inner = document.createElementNS(SVG_NS3, "path");
2756
+ inner.setAttribute("d", generateCurvePath2(shiftedPoints));
2757
+ inner.setAttribute("fill", "none");
2758
+ inner.setAttribute("stroke", "white");
2759
+ inner.setAttribute("stroke-width", "1.5");
2760
+ group.appendChild(inner);
2761
+ }
2762
+ if (pattern && pattern.kind === "directional" && shiftedPoints.length >= 2) {
2763
+ const p0 = shiftedPoints[0];
2764
+ const p1 = shiftedPoints[1];
2765
+ const pN = shiftedPoints[shiftedPoints.length - 1];
2766
+ const pN1 = shiftedPoints[shiftedPoints.length - 2];
2767
+ if (pattern.upstreamRole) {
2768
+ const m = pointAlong(p0, p1, 16);
2769
+ group.appendChild(roleMarker(m.x, m.y, pattern.upstreamRole));
2770
+ }
2771
+ if (pattern.downstreamRole) {
2772
+ const m = pointAlong(pN, pN1, 20);
2773
+ group.appendChild(roleMarker(m.x, m.y, pattern.downstreamRole));
2774
+ }
2775
+ if (pattern.aclGate) {
2776
+ const m = pointAlong(pN, pN1, 40);
2777
+ group.appendChild(aclGate(m.x, m.y, angleDeg(pN1, pN)));
2778
+ }
2779
+ if (pattern.ohsSocket) {
2780
+ const m = pointAlong(p0, p1, 34);
2781
+ group.appendChild(ohsSocket(m.x, m.y));
2782
+ }
2783
+ }
2784
+ if (rel.label) {
2785
+ let lx;
2786
+ let ly;
2787
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
2788
+ lx = gridStartX + edge.x;
2789
+ ly = gridStartY + edge.y;
2790
+ } else {
2791
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
2792
+ lx = mid.x;
2793
+ ly = mid.y;
2794
+ }
2795
+ if (pattern && pattern.badge) {
2796
+ group.appendChild(patternBadge(lx, ly, pattern.badge));
2797
+ } else if (!pattern) {
2798
+ group.appendChild(
2799
+ createEdgeLabel({ x: lx, y: ly, text: rel.label, maxWidth: REL_LABEL_MAX_WIDTH2 })
2800
+ );
2801
+ }
2802
+ }
2803
+ container.appendChild(group);
2804
+ });
2805
+ const legendGroup = document.createElementNS(SVG_NS3, "g");
2806
+ legendGroup.setAttribute("class", "d5-legend");
2807
+ const legendY = totalH - LEGEND_H * (usedPatterns.length > 0 ? 2 : 1);
2808
+ let legendX = MARGIN;
2809
+ LEGEND_ITEMS2.forEach((item) => {
2810
+ const colors = SUBDOMAIN_COLORS2[item.type];
2811
+ const swatch = document.createElementNS(SVG_NS3, "rect");
2812
+ swatch.setAttribute("x", String(legendX));
2813
+ swatch.setAttribute("y", String(legendY));
2814
+ swatch.setAttribute("width", "14");
2815
+ swatch.setAttribute("height", "14");
2816
+ swatch.setAttribute("rx", "3");
2817
+ swatch.setAttribute("fill", colors.fill);
2818
+ swatch.setAttribute("stroke", colors.stroke);
2819
+ swatch.setAttribute("stroke-width", "1.5");
2820
+ legendGroup.appendChild(swatch);
2821
+ const label = document.createElementNS(SVG_NS3, "text");
2822
+ label.setAttribute("x", String(legendX + 20));
2823
+ label.setAttribute("y", String(legendY + 11));
2824
+ label.setAttribute("font-size", "11");
2825
+ label.setAttribute("fill", "#64748b");
2826
+ label.textContent = item.label;
2827
+ legendGroup.appendChild(label);
2828
+ legendX += 90;
2829
+ });
2830
+ if (usedPatterns.length > 0) {
2831
+ let px = MARGIN;
2832
+ const py = legendY + LEGEND_H;
2833
+ usedPatterns.forEach((p2) => {
2834
+ if (p2.badge) {
2835
+ legendGroup.appendChild(patternBadge(px + 15, py + 7, p2.badge));
2836
+ px += 34;
2837
+ } else {
2838
+ const dash = document.createElementNS(SVG_NS3, "line");
2839
+ dash.setAttribute("x1", String(px));
2840
+ dash.setAttribute("y1", String(py + 7));
2841
+ dash.setAttribute("x2", String(px + 22));
2842
+ dash.setAttribute("y2", String(py + 7));
2843
+ dash.setAttribute("stroke", REL_STROKE);
2844
+ dash.setAttribute("stroke-dasharray", "4 3");
2845
+ legendGroup.appendChild(dash);
2846
+ px += 28;
2847
+ }
2848
+ const label = document.createElementNS(SVG_NS3, "text");
2849
+ label.setAttribute("x", String(px + 4));
2850
+ label.setAttribute("y", String(py + 11));
2851
+ label.setAttribute("font-size", "11");
2852
+ label.setAttribute("fill", "#64748b");
2853
+ label.textContent = p2.name;
2854
+ legendGroup.appendChild(label);
2855
+ px += measureText(p2.name, { size: 11 }) + 22;
2856
+ });
2857
+ }
2858
+ container.appendChild(legendGroup);
2859
+ }
2860
+
2861
+ // src/d5-context/detector.ts
2862
+ function detectContext(text) {
2863
+ return text.trimStart().startsWith("d5-context");
2864
+ }
2865
+
2866
+ // src/d5-context/db.ts
2867
+ var DEFAULT_DIRECTION3 = "TB";
2868
+ var D5ContextDb = class {
2869
+ boundedContext;
2870
+ aggregates = [];
2871
+ readModels = [];
2872
+ terms = [];
2873
+ relationships = [];
2874
+ events = [];
2875
+ policies = [];
2876
+ direction = DEFAULT_DIRECTION3;
2877
+ title;
2878
+ accTitle;
2879
+ accDescription;
2880
+ setTitle(title) {
2881
+ this.title = title;
2882
+ }
2883
+ getTitle() {
2884
+ return this.title;
2885
+ }
2886
+ /** Accepts `LR` / `RL` / `TB` / `TD` / `BT` (case-insensitive); `TD` is stored as `TB`. */
2887
+ setDirection(dir) {
2888
+ const normalized = normalizeDirection(dir);
2889
+ if (normalized) this.direction = normalized;
2890
+ }
2891
+ getDirection() {
2892
+ return this.direction;
2893
+ }
2894
+ setBoundedContext(id, label, team) {
2895
+ this.boundedContext = { id, label, team };
2896
+ }
2897
+ getBoundedContext() {
2898
+ return this.boundedContext;
2899
+ }
2900
+ addAggregate(id, label, root, fields) {
2901
+ this.aggregates.push({ id, label, root, fields });
2902
+ }
2903
+ getAggregates() {
2904
+ return this.aggregates;
2905
+ }
2906
+ addReadModel(id, label) {
2907
+ this.readModels.push({ id, label });
2908
+ }
2909
+ getReadModels() {
2910
+ return this.readModels;
2911
+ }
2912
+ addTerm(term, definition) {
2913
+ this.terms.push({ term, definition });
2914
+ }
2915
+ getTerms() {
2916
+ return this.terms;
2917
+ }
2918
+ addRelationship(source, target, label) {
2919
+ this.relationships.push({ source, target, label });
2920
+ }
2921
+ getRelationships() {
2922
+ return this.relationships;
2923
+ }
2924
+ addEvent(source, target, name) {
2925
+ this.events.push({ source, target, name });
2926
+ }
2927
+ getEvents() {
2928
+ return this.events;
2929
+ }
2930
+ addPolicy(source, target, rule) {
2931
+ this.policies.push({ source, target, rule });
2932
+ }
2933
+ getPolicies() {
2934
+ return this.policies;
2935
+ }
2936
+ clear() {
2937
+ this.boundedContext = void 0;
2938
+ this.aggregates = [];
2939
+ this.readModels = [];
2940
+ this.terms = [];
2941
+ this.relationships = [];
2942
+ this.events = [];
2943
+ this.policies = [];
2944
+ this.direction = DEFAULT_DIRECTION3;
2945
+ this.title = void 0;
2946
+ this.accTitle = void 0;
2947
+ this.accDescription = void 0;
2948
+ }
2949
+ // Mermaid DiagramDB interface methods
2950
+ setDiagramTitle(title) {
2951
+ this.setTitle(title);
2952
+ }
2953
+ getDiagramTitle() {
2954
+ return this.title ?? "";
2955
+ }
2956
+ setAccTitle(title) {
2957
+ this.accTitle = title;
2958
+ }
2959
+ getAccTitle() {
2960
+ return this.accTitle ?? "";
2961
+ }
2962
+ setAccDescription(desc) {
2963
+ this.accDescription = desc;
2964
+ }
2965
+ getAccDescription() {
2966
+ return this.accDescription ?? "";
2967
+ }
2968
+ };
2969
+
2970
+ // src/d5-context/parser.ts
2971
+ var TITLE_RE3 = /^\s*title\s+(.+)$/;
2972
+ var BC_RE2 = /^\s*BoundedContext\(\s*(\w+)\s*,\s*"([^"]+)"\s*(?:,\s*team:\s*"([^"]+)")?\s*\)\s*\{?/;
2973
+ var LANGUAGE_RE = /^\s*Language\s*\{/;
2974
+ var TERM_RE = /^\s*Term\(\s*"([^"]+)"\s*,\s*"([^"]+)"\s*\)/;
2975
+ var AGGREGATE_RE = /^\s*Aggregate\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*root:\s*"([^"]+)"(?:\s*,\s*fields:\s*"([^"]+)")?\s*\)/;
2976
+ var READMODEL_RE = /^\s*ReadModel\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2977
+ var REL_RE3 = /^\s*Rel\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2978
+ var EVENT_RE = /^\s*Event\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2979
+ var POLICY_RE = /^\s*Policy\(\s*(\w+)\s*,\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
2980
+ var COMMENT_LINE_RE3 = /^\s*%%/;
2981
+ function stripInlineComment3(line) {
2982
+ const idx = line.indexOf("%%");
2983
+ return idx === -1 ? line : line.slice(0, idx);
2984
+ }
2985
+ function parse3(text, db) {
2986
+ const lines = text.split("\n");
2987
+ for (const raw of lines) {
2988
+ if (COMMENT_LINE_RE3.test(raw)) continue;
2989
+ const line = stripInlineComment3(raw).trim();
2990
+ if (line === "" || line === "d5-context") continue;
2991
+ let m;
2992
+ if (m = line.match(DIRECTION_RE)) {
2993
+ db.setDirection(m[1]);
2994
+ } else if (m = line.match(TITLE_RE3)) {
2995
+ db.setTitle(m[1].trim());
2996
+ } else if (m = line.match(BC_RE2)) {
2997
+ const team = m[3] || void 0;
2998
+ db.setBoundedContext(m[1], m[2], team);
2999
+ } else if (LANGUAGE_RE.test(line)) ; else if (m = line.match(TERM_RE)) {
3000
+ db.addTerm(m[1], m[2]);
3001
+ } else if (m = line.match(AGGREGATE_RE)) {
3002
+ const fields = m[4] ? m[4].split(",").map((s) => s.trim()) : void 0;
3003
+ db.addAggregate(m[1], m[2], m[3], fields);
3004
+ } else if (m = line.match(READMODEL_RE)) {
3005
+ db.addReadModel(m[1], m[2]);
3006
+ } else if (m = line.match(REL_RE3)) {
3007
+ db.addRelationship(m[1], m[2], m[3]);
3008
+ } else if (m = line.match(EVENT_RE)) {
3009
+ db.addEvent(m[1], m[2], m[3]);
3010
+ } else if (m = line.match(POLICY_RE)) {
3011
+ db.addPolicy(m[1], m[2], m[3]);
3012
+ }
3013
+ }
3014
+ }
3015
+
3016
+ // src/d5-context/renderer.ts
3017
+ var REL_LABEL_MAX_WIDTH3 = 150;
3018
+ var SVG_NS4 = "http://www.w3.org/2000/svg";
3019
+ var AGGREGATE_MIN_WIDTH = 180;
3020
+ var AGGREGATE_MAX_WIDTH = 340;
3021
+ var AGGREGATE_MIN_HEIGHT = 76;
3022
+ var AGGREGATE_FIELD_HEIGHT = 20;
3023
+ var READMODEL_HEIGHT = 74;
3024
+ var GRID_GAP = 60;
3025
+ var MARGIN2 = 30;
3026
+ var TITLE_HEIGHT3 = 34;
3027
+ var BC_PADDING = 30;
3028
+ var BC_HEADER = 36;
3029
+ var ARROW_MARKER_SIZE3 = 8;
3030
+ var NOTE_TEXT_W = 300;
3031
+ var NOTE_PAD = 14;
3032
+ var NOTE_WIDTH = NOTE_TEXT_W + NOTE_PAD * 2;
3033
+ var NOTE_HEADER_H = 30;
3034
+ var NOTE_TERM_GAP = 12;
3035
+ var NOTE_TERM_FONT = { size: 12, weight: 600 };
3036
+ var NOTE_DEF_FONT = { size: 11 };
3037
+ var EVENT_STROKE = "#d97706";
3038
+ var POLICY_STROKE = "#7c3aed";
3039
+ var POLICY_MAX_W = 172;
3040
+ function addArrowMarker3(svg) {
3041
+ let defs = svg.querySelector("defs");
3042
+ if (!defs) {
3043
+ defs = document.createElementNS(SVG_NS4, "defs");
3044
+ svg.prepend(defs);
3045
+ }
3046
+ for (const [id, fill] of [
3047
+ ["d5-arrowhead", "#64748b"],
3048
+ ["d5-event-arrowhead", EVENT_STROKE],
3049
+ ["d5-policy-arrowhead", POLICY_STROKE]
3050
+ ]) {
3051
+ const marker = document.createElementNS(SVG_NS4, "marker");
3052
+ marker.setAttribute("id", id);
3053
+ marker.setAttribute("markerWidth", String(ARROW_MARKER_SIZE3));
3054
+ marker.setAttribute("markerHeight", String(ARROW_MARKER_SIZE3));
3055
+ marker.setAttribute("refX", String(ARROW_MARKER_SIZE3));
3056
+ marker.setAttribute("refY", String(ARROW_MARKER_SIZE3 / 2));
3057
+ marker.setAttribute("orient", "auto");
3058
+ const path = document.createElementNS(SVG_NS4, "path");
3059
+ path.setAttribute(
3060
+ "d",
3061
+ `M0,0 L${ARROW_MARKER_SIZE3},${ARROW_MARKER_SIZE3 / 2} L0,${ARROW_MARKER_SIZE3}`
3062
+ );
3063
+ path.setAttribute("fill", fill);
3064
+ marker.appendChild(path);
3065
+ defs.appendChild(marker);
3066
+ }
3067
+ }
3068
+ function eventRibbon(cx, cy, name) {
3069
+ const g = document.createElementNS(SVG_NS4, "g");
3070
+ g.setAttribute("class", "d5-event-label");
3071
+ const padX = 8;
3072
+ const point = 7;
3073
+ const w2 = Math.ceil(measureText(name, { size: 10, weight: 600 }) + padX * 2 + point);
3074
+ const h = 18;
3075
+ const x2 = cx - w2 / 2;
3076
+ const y = cy - h / 2;
3077
+ const tag = document.createElementNS(SVG_NS4, "path");
3078
+ tag.setAttribute(
3079
+ "d",
3080
+ `M${x2},${y} L${x2 + w2 - point},${y} L${x2 + w2},${cy} L${x2 + w2 - point},${y + h} L${x2},${y + h} Z`
3081
+ );
3082
+ tag.setAttribute("fill", "#fef3c7");
3083
+ tag.setAttribute("stroke", EVENT_STROKE);
3084
+ tag.setAttribute("stroke-width", "1");
3085
+ g.appendChild(tag);
3086
+ const t = document.createElementNS(SVG_NS4, "text");
3087
+ t.setAttribute("x", String(x2 + (w2 - point) / 2));
3088
+ t.setAttribute("y", String(cy + 3.5));
3089
+ t.setAttribute("text-anchor", "middle");
3090
+ t.setAttribute("font-size", "10");
3091
+ t.setAttribute("font-weight", "600");
3092
+ t.setAttribute("fill", "#92400e");
3093
+ t.textContent = name;
3094
+ g.appendChild(t);
3095
+ return g;
3096
+ }
3097
+ function eventRibbonSize(name) {
3098
+ return { w: Math.ceil(measureText(name, { size: 10, weight: 600 }) + 23), h: 20 };
3099
+ }
3100
+ var POLICY_FONT = { size: 10, weight: 600 };
3101
+ var POLICY_PAD_X = 9;
3102
+ var POLICY_PAD_Y = 4;
3103
+ function policyLayout(rule) {
3104
+ const lines = wrapText(rule, POLICY_MAX_W - POLICY_PAD_X * 2, POLICY_FONT);
3105
+ const textW = lines.reduce((m, l) => Math.max(m, measureText(l, POLICY_FONT)), 1);
3106
+ return {
3107
+ lines,
3108
+ w: Math.ceil(textW + POLICY_PAD_X * 2),
3109
+ h: Math.ceil(lines.length * lineHeight(POLICY_FONT.size) + POLICY_PAD_Y * 2)
3110
+ };
3111
+ }
3112
+ function policyTag(cx, cy, rule) {
3113
+ const { lines, w: w2, h } = policyLayout(rule);
3114
+ const g = document.createElementNS(SVG_NS4, "g");
3115
+ g.setAttribute("class", "d5-policy-label");
3116
+ const rect = document.createElementNS(SVG_NS4, "rect");
3117
+ rect.setAttribute("x", String(cx - w2 / 2));
3118
+ rect.setAttribute("y", String(cy - h / 2));
3119
+ rect.setAttribute("width", String(w2));
3120
+ rect.setAttribute("height", String(h));
3121
+ rect.setAttribute("rx", "4");
3122
+ rect.setAttribute("fill", "#ede9fe");
3123
+ rect.setAttribute("stroke", POLICY_STROKE);
3124
+ rect.setAttribute("stroke-width", "1");
3125
+ g.appendChild(rect);
3126
+ const lh = lineHeight(POLICY_FONT.size);
3127
+ let ty = cy - h / 2 + POLICY_PAD_Y + POLICY_FONT.size * 0.82;
3128
+ for (const ln2 of lines) {
3129
+ const t = document.createElementNS(SVG_NS4, "text");
3130
+ t.setAttribute("x", String(cx));
3131
+ t.setAttribute("y", String(ty));
3132
+ t.setAttribute("text-anchor", "middle");
3133
+ t.setAttribute("font-size", String(POLICY_FONT.size));
3134
+ t.setAttribute("font-weight", "600");
3135
+ t.setAttribute("fill", "#5b21b6");
3136
+ t.textContent = ln2;
3137
+ g.appendChild(t);
3138
+ ty += lh;
3139
+ }
3140
+ return g;
3141
+ }
3142
+ function generateCurvePath3(points) {
3143
+ if (!points || points.length === 0) return "";
3144
+ if (points.length === 1) return `M ${points[0].x} ${points[0].y}`;
3145
+ if (points.length === 2) return `M ${points[0].x} ${points[0].y} L ${points[1].x} ${points[1].y}`;
3146
+ let d = `M ${points[0].x} ${points[0].y}`;
3147
+ for (let i = 1; i < points.length - 1; i++) {
3148
+ const p1 = points[i];
3149
+ const p2 = points[i + 1];
3150
+ const midX = (p1.x + p2.x) / 2;
3151
+ const midY = (p1.y + p2.y) / 2;
3152
+ d += ` Q ${p1.x} ${p1.y} ${midX} ${midY}`;
3153
+ }
3154
+ const last = points[points.length - 1];
3155
+ d += ` L ${last.x} ${last.y}`;
3156
+ return d;
3157
+ }
3158
+ function render3(db, container) {
3159
+ addArrowMarker3(container);
3160
+ const title = db.getTitle();
3161
+ const bc = db.getBoundedContext();
3162
+ const aggregates = db.getAggregates();
3163
+ const terms = db.getTerms();
3164
+ const relationships = db.getRelationships();
3165
+ const events = db.getEvents();
3166
+ const policies = db.getPolicies();
3167
+ const readModels = db.getReadModels();
3168
+ const direction = db.getDirection();
3169
+ const g = new To.graphlib.Graph({ multigraph: true });
3170
+ g.setGraph({
3171
+ rankdir: direction,
3172
+ nodesep: GRID_GAP,
3173
+ ranksep: GRID_GAP,
3174
+ edgesep: 20,
3175
+ marginx: 0,
3176
+ marginy: 0
3177
+ });
3178
+ g.setDefaultEdgeLabel(() => ({}));
3179
+ aggregates.forEach((agg) => {
3180
+ const h = AGGREGATE_MIN_HEIGHT + (agg.fields?.length ? agg.fields.length * AGGREGATE_FIELD_HEIGHT + 10 : 0);
3181
+ const candidates = [
3182
+ measureText(agg.label, { size: 13, weight: 600 }) + 28,
3183
+ measureText(`Root: ${agg.root}`, { size: 11 }) + 28,
3184
+ ...(agg.fields ?? []).map((f) => measureText(f, { size: 12 }) + 44)
3185
+ ];
3186
+ const w2 = Math.round(
3187
+ Math.min(AGGREGATE_MAX_WIDTH, Math.max(AGGREGATE_MIN_WIDTH, ...candidates))
3188
+ );
3189
+ g.setNode(agg.id, { width: w2, height: h });
3190
+ });
3191
+ readModels.forEach((rm) => {
3192
+ const w2 = Math.round(
3193
+ Math.min(
3194
+ AGGREGATE_MAX_WIDTH,
3195
+ Math.max(AGGREGATE_MIN_WIDTH, measureText(rm.label, { size: 13, weight: 600 }) + 28)
3196
+ )
3197
+ );
3198
+ g.setNode(rm.id, { width: w2, height: READMODEL_HEIGHT });
3199
+ });
3200
+ relationships.forEach((rel) => {
3201
+ const edgeCfg = { minlen: 1 };
3202
+ if (rel.label) {
3203
+ const { w: w2, h } = edgeLabelSize(rel.label, REL_LABEL_MAX_WIDTH3);
3204
+ edgeCfg.width = w2;
3205
+ edgeCfg.height = h;
3206
+ edgeCfg.labelpos = "c";
3207
+ }
3208
+ g.setEdge(rel.source, rel.target, edgeCfg);
3209
+ });
3210
+ events.forEach((ev, i) => {
3211
+ const { w: w2, h } = eventRibbonSize(ev.name);
3212
+ g.setEdge(ev.source, ev.target, { minlen: 1, width: w2, height: h, labelpos: "c" }, `evt${i}`);
3213
+ });
3214
+ policies.forEach((p2, i) => {
3215
+ const { w: w2, h } = policyLayout(p2.rule);
3216
+ g.setEdge(p2.source, p2.target, { minlen: 1, width: w2, height: h, labelpos: "c" }, `pol${i}`);
3217
+ });
3218
+ To.layout(g);
3219
+ let graphW = g.graph().width || 0;
3220
+ let graphH = g.graph().height || 0;
3221
+ const hasTerms = terms.length > 0;
3222
+ const termBlocks = terms.map((t) => ({
3223
+ nameLines: wrapText(`${t.term}`, NOTE_TEXT_W, NOTE_TERM_FONT),
3224
+ defLines: wrapText(t.definition, NOTE_TEXT_W, NOTE_DEF_FONT)
3225
+ }));
3226
+ let noteH = 0;
3227
+ if (hasTerms) {
3228
+ noteH = NOTE_HEADER_H + NOTE_PAD;
3229
+ for (const b of termBlocks) {
3230
+ noteH += b.nameLines.length * lineHeight(NOTE_TERM_FONT.size) + 2 + b.defLines.length * lineHeight(NOTE_DEF_FONT.size) + NOTE_TERM_GAP;
3231
+ }
3232
+ noteH += NOTE_PAD - NOTE_TERM_GAP;
3233
+ }
3234
+ const innerGridW = graphW + (hasTerms ? (graphW > 0 ? GRID_GAP : 0) + NOTE_WIDTH : 0);
3235
+ const innerGridH = Math.max(graphH, noteH);
3236
+ const gridYOffset = Math.max(0, (innerGridH - graphH) / 2);
3237
+ let bcX = MARGIN2;
3238
+ const bcY = MARGIN2 + (title ? TITLE_HEIGHT3 : 0);
3239
+ let bcW = innerGridW + BC_PADDING * 2;
3240
+ if (!bc && !hasTerms && aggregates.length === 0) bcW = 400;
3241
+ const bcH = (bc ? BC_HEADER : 0) + innerGridH + BC_PADDING * 2;
3242
+ const totalW = bcX + bcW + MARGIN2;
3243
+ const totalH = bcY + bcH + MARGIN2;
3244
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
3245
+ container.setAttribute("height", String(totalH));
3246
+ container.style.maxWidth = `${totalW}px`;
3247
+ if (title) {
3248
+ const titleEl = document.createElementNS(SVG_NS4, "text");
3249
+ titleEl.setAttribute("class", "d5-title");
3250
+ titleEl.setAttribute("x", String(totalW / 2));
3251
+ titleEl.setAttribute("y", "24");
3252
+ titleEl.setAttribute("text-anchor", "middle");
3253
+ titleEl.setAttribute("font-size", "18");
3254
+ titleEl.setAttribute("font-weight", "bold");
3255
+ titleEl.setAttribute("fill", "#1e293b");
3256
+ titleEl.textContent = title;
3257
+ container.appendChild(titleEl);
3258
+ }
3259
+ const bcGroup = document.createElementNS(SVG_NS4, "g");
3260
+ bcGroup.setAttribute("class", "d5-bounded-context");
3261
+ const bcRect = document.createElementNS(SVG_NS4, "rect");
3262
+ bcRect.setAttribute("x", String(bcX));
3263
+ bcRect.setAttribute("y", String(bcY));
3264
+ bcRect.setAttribute("width", String(bcW));
3265
+ bcRect.setAttribute("height", String(bcH));
3266
+ bcRect.setAttribute("rx", "12");
3267
+ bcRect.setAttribute("fill", "#f8fafc");
3268
+ bcRect.setAttribute("stroke", "#94a3b8");
3269
+ bcRect.setAttribute("stroke-width", "2");
3270
+ bcRect.setAttribute("stroke-dasharray", "8 4");
3271
+ bcGroup.appendChild(bcRect);
3272
+ if (bc) {
3273
+ const bcLabel = document.createElementNS(SVG_NS4, "text");
3274
+ bcLabel.setAttribute("x", String(bcX + 16));
3275
+ bcLabel.setAttribute("y", String(bcY + 24));
3276
+ bcLabel.setAttribute("font-size", "14");
3277
+ bcLabel.setAttribute("font-weight", "600");
3278
+ bcLabel.setAttribute("fill", "#475569");
3279
+ bcLabel.textContent = bc.label;
3280
+ bcGroup.appendChild(bcLabel);
3281
+ if (bc.team) {
3282
+ const teamLabel = document.createElementNS(SVG_NS4, "text");
3283
+ teamLabel.setAttribute("x", String(bcX + bcW - 16));
3284
+ teamLabel.setAttribute("y", String(bcY + 24));
3285
+ teamLabel.setAttribute("font-size", "12");
3286
+ teamLabel.setAttribute("font-style", "italic");
3287
+ teamLabel.setAttribute("fill", "#64748b");
3288
+ teamLabel.setAttribute("text-anchor", "end");
3289
+ teamLabel.textContent = `Team: ${bc.team}`;
3290
+ bcGroup.appendChild(teamLabel);
3291
+ }
3292
+ }
3293
+ container.appendChild(bcGroup);
3294
+ const graphStartX = bcX + BC_PADDING;
3295
+ const graphStartY = bcY + (bc ? BC_HEADER : 0) + BC_PADDING;
3296
+ const aggAreaY = graphStartY + gridYOffset;
3297
+ if (hasTerms) {
3298
+ const noteX = graphStartX + (graphW > 0 ? graphW + GRID_GAP : 0);
3299
+ const noteY = graphStartY;
3300
+ const noteGroup = document.createElementNS(SVG_NS4, "g");
3301
+ noteGroup.setAttribute("class", "d5-language-note");
3302
+ const noteRect = document.createElementNS(SVG_NS4, "rect");
3303
+ noteRect.setAttribute("x", String(noteX));
3304
+ noteRect.setAttribute("y", String(noteY));
3305
+ noteRect.setAttribute("width", String(NOTE_WIDTH));
3306
+ noteRect.setAttribute("height", String(noteH));
3307
+ noteRect.setAttribute("rx", "4");
3308
+ noteRect.setAttribute("fill", "#fef9c3");
3309
+ noteRect.setAttribute("stroke", "#ca8a04");
3310
+ noteRect.setAttribute("stroke-width", "1.5");
3311
+ noteGroup.appendChild(noteRect);
3312
+ const noteTop = document.createElementNS(SVG_NS4, "path");
3313
+ noteTop.setAttribute(
3314
+ "d",
3315
+ `M${noteX},${noteY + NOTE_HEADER_H} L${noteX},${noteY + 4} Q${noteX},${noteY} ${noteX + 4},${noteY} L${noteX + NOTE_WIDTH - 4},${noteY} Q${noteX + NOTE_WIDTH},${noteY} ${noteX + NOTE_WIDTH},${noteY + 4} L${noteX + NOTE_WIDTH},${noteY + NOTE_HEADER_H} Z`
3316
+ );
3317
+ noteTop.setAttribute("fill", "#fde047");
3318
+ noteGroup.appendChild(noteTop);
3319
+ const noteTitle = document.createElementNS(SVG_NS4, "text");
3320
+ noteTitle.setAttribute("x", String(noteX + NOTE_WIDTH / 2));
3321
+ noteTitle.setAttribute("y", String(noteY + 20));
3322
+ noteTitle.setAttribute("text-anchor", "middle");
3323
+ noteTitle.setAttribute("font-size", "12");
3324
+ noteTitle.setAttribute("font-weight", "bold");
3325
+ noteTitle.setAttribute("fill", "#854d0e");
3326
+ noteTitle.textContent = "Ubiquitous Language";
3327
+ noteGroup.appendChild(noteTitle);
3328
+ const textX = noteX + NOTE_PAD;
3329
+ let ty = noteY + NOTE_HEADER_H + NOTE_PAD + NOTE_TERM_FONT.size * 0.85;
3330
+ const nameLH = lineHeight(NOTE_TERM_FONT.size);
3331
+ const defLH = lineHeight(NOTE_DEF_FONT.size);
3332
+ termBlocks.forEach((block) => {
3333
+ block.nameLines.forEach((ln2, i) => {
3334
+ const el = document.createElementNS(SVG_NS4, "text");
3335
+ el.setAttribute("x", String(textX));
3336
+ el.setAttribute("y", String(ty));
3337
+ el.setAttribute("font-size", String(NOTE_TERM_FONT.size));
3338
+ el.setAttribute("font-weight", "bold");
3339
+ el.setAttribute("fill", "#713f12");
3340
+ el.textContent = i === block.nameLines.length - 1 ? `${ln2}:` : ln2;
3341
+ noteGroup.appendChild(el);
3342
+ ty += nameLH;
3343
+ });
3344
+ ty += 2;
3345
+ block.defLines.forEach((ln2) => {
3346
+ const el = document.createElementNS(SVG_NS4, "text");
3347
+ el.setAttribute("x", String(textX));
3348
+ el.setAttribute("y", String(ty));
3349
+ el.setAttribute("font-size", String(NOTE_DEF_FONT.size));
3350
+ el.setAttribute("fill", "#854d0e");
3351
+ el.textContent = ln2;
3352
+ noteGroup.appendChild(el);
3353
+ ty += defLH;
3354
+ });
3355
+ ty += NOTE_TERM_GAP;
3356
+ });
3357
+ container.appendChild(noteGroup);
3358
+ }
3359
+ aggregates.forEach((agg) => {
3360
+ const node = g.node(agg.id);
3361
+ if (!node) return;
3362
+ const w2 = node.width;
3363
+ const h = node.height;
3364
+ const x2 = graphStartX + node.x - w2 / 2;
3365
+ const y = aggAreaY + node.y - h / 2;
3366
+ const cx = x2 + w2 / 2;
3367
+ const group = document.createElementNS(SVG_NS4, "g");
3368
+ group.setAttribute("class", "d5-aggregate");
3369
+ const rect = document.createElementNS(SVG_NS4, "rect");
3370
+ rect.setAttribute("x", String(x2));
3371
+ rect.setAttribute("y", String(y));
3372
+ rect.setAttribute("width", String(w2));
3373
+ rect.setAttribute("height", String(h));
3374
+ rect.setAttribute("rx", "8");
3375
+ rect.setAttribute("fill", "white");
3376
+ rect.setAttribute("stroke", "#3b82f6");
3377
+ rect.setAttribute("stroke-width", "2");
3378
+ group.appendChild(rect);
3379
+ const headerH = 34;
3380
+ const header = document.createElementNS(SVG_NS4, "path");
3381
+ header.setAttribute(
3382
+ "d",
3383
+ `M${x2},${y + headerH} L${x2},${y + 8} Q${x2},${y} ${x2 + 8},${y} L${x2 + w2 - 8},${y} Q${x2 + w2},${y} ${x2 + w2},${y + 8} L${x2 + w2},${y + headerH} Z`
3384
+ );
3385
+ header.setAttribute("fill", "#dbeafe");
3386
+ group.appendChild(header);
3387
+ const line = document.createElementNS(SVG_NS4, "line");
3388
+ line.setAttribute("x1", String(x2));
3389
+ line.setAttribute("y1", String(y + headerH));
3390
+ line.setAttribute("x2", String(x2 + w2));
3391
+ line.setAttribute("y2", String(y + headerH));
3392
+ line.setAttribute("stroke", "#3b82f6");
3393
+ line.setAttribute("stroke-width", "1.5");
3394
+ group.appendChild(line);
3395
+ const labelText = document.createElementNS(SVG_NS4, "text");
3396
+ labelText.setAttribute("x", String(cx));
3397
+ labelText.setAttribute("y", String(y + 20));
3398
+ labelText.setAttribute("text-anchor", "middle");
3399
+ labelText.setAttribute("font-size", "13");
3400
+ labelText.setAttribute("font-weight", "bold");
3401
+ labelText.setAttribute("fill", "#1e293b");
3402
+ labelText.textContent = agg.label;
3403
+ group.appendChild(labelText);
3404
+ const rootText = document.createElementNS(SVG_NS4, "text");
3405
+ rootText.setAttribute("x", String(cx));
3406
+ rootText.setAttribute("y", String(y + headerH + 20));
3407
+ rootText.setAttribute("text-anchor", "middle");
3408
+ rootText.setAttribute("font-size", "11");
3409
+ rootText.setAttribute("font-style", "italic");
3410
+ rootText.setAttribute("fill", "#3b82f6");
3411
+ rootText.textContent = `Root: ${agg.root}`;
3412
+ group.appendChild(rootText);
3413
+ if (agg.fields && agg.fields.length > 0) {
3414
+ const sep = document.createElementNS(SVG_NS4, "line");
3415
+ sep.setAttribute("x1", String(x2 + 10));
3416
+ sep.setAttribute("y1", String(y + headerH + 32));
3417
+ sep.setAttribute("x2", String(x2 + w2 - 10));
3418
+ sep.setAttribute("y2", String(y + headerH + 32));
3419
+ sep.setAttribute("stroke", "#cbd5e1");
3420
+ sep.setAttribute("stroke-dasharray", "4 4");
3421
+ group.appendChild(sep);
3422
+ let fy = y + headerH + 50;
3423
+ agg.fields.forEach((field) => {
3424
+ const bullet = document.createElementNS(SVG_NS4, "circle");
3425
+ bullet.setAttribute("cx", String(x2 + 20));
3426
+ bullet.setAttribute("cy", String(fy - 4));
3427
+ bullet.setAttribute("r", "3");
3428
+ bullet.setAttribute("fill", "#94a3b8");
3429
+ group.appendChild(bullet);
3430
+ const fieldText = document.createElementNS(SVG_NS4, "text");
3431
+ fieldText.setAttribute("x", String(x2 + 30));
3432
+ fieldText.setAttribute("y", String(fy));
3433
+ fieldText.setAttribute("font-size", "12");
3434
+ fieldText.setAttribute("fill", "#475569");
3435
+ fieldText.textContent = field;
3436
+ group.appendChild(fieldText);
3437
+ fy += AGGREGATE_FIELD_HEIGHT;
3438
+ });
3439
+ }
3440
+ container.appendChild(group);
3441
+ });
3442
+ readModels.forEach((rm) => {
3443
+ const node = g.node(rm.id);
3444
+ if (!node) return;
3445
+ const w2 = node.width;
3446
+ const h = node.height;
3447
+ const x2 = graphStartX + node.x - w2 / 2;
3448
+ const y = aggAreaY + node.y - h / 2;
3449
+ const cx = x2 + w2 / 2;
3450
+ const group = document.createElementNS(SVG_NS4, "g");
3451
+ group.setAttribute("class", "d5-read-model");
3452
+ const rect = document.createElementNS(SVG_NS4, "rect");
3453
+ rect.setAttribute("x", String(x2));
3454
+ rect.setAttribute("y", String(y));
3455
+ rect.setAttribute("width", String(w2));
3456
+ rect.setAttribute("height", String(h));
3457
+ rect.setAttribute("rx", "6");
3458
+ rect.setAttribute("fill", "white");
3459
+ rect.setAttribute("stroke", "#64748b");
3460
+ rect.setAttribute("stroke-width", "2");
3461
+ group.appendChild(rect);
3462
+ const headerH = 30;
3463
+ const header = document.createElementNS(SVG_NS4, "path");
3464
+ header.setAttribute(
3465
+ "d",
3466
+ `M${x2},${y + headerH} L${x2},${y + 6} Q${x2},${y} ${x2 + 6},${y} L${x2 + w2 - 6},${y} Q${x2 + w2},${y} ${x2 + w2},${y + 6} L${x2 + w2},${y + headerH} Z`
3467
+ );
3468
+ header.setAttribute("fill", "#f1f5f9");
3469
+ group.appendChild(header);
3470
+ const hLine = document.createElementNS(SVG_NS4, "line");
3471
+ hLine.setAttribute("x1", String(x2));
3472
+ hLine.setAttribute("y1", String(y + headerH));
3473
+ hLine.setAttribute("x2", String(x2 + w2));
3474
+ hLine.setAttribute("y2", String(y + headerH));
3475
+ hLine.setAttribute("stroke", "#64748b");
3476
+ hLine.setAttribute("stroke-width", "1");
3477
+ group.appendChild(hLine);
3478
+ const labelText = document.createElementNS(SVG_NS4, "text");
3479
+ labelText.setAttribute("x", String(cx));
3480
+ labelText.setAttribute("y", String(y + 19));
3481
+ labelText.setAttribute("text-anchor", "middle");
3482
+ labelText.setAttribute("font-size", "13");
3483
+ labelText.setAttribute("font-weight", "bold");
3484
+ labelText.setAttribute("fill", "#1e293b");
3485
+ labelText.textContent = rm.label;
3486
+ group.appendChild(labelText);
3487
+ const typeText = document.createElementNS(SVG_NS4, "text");
3488
+ typeText.setAttribute("x", String(cx));
3489
+ typeText.setAttribute("y", String(y + headerH + 16));
3490
+ typeText.setAttribute("text-anchor", "middle");
3491
+ typeText.setAttribute("font-size", "10");
3492
+ typeText.setAttribute("font-style", "italic");
3493
+ typeText.setAttribute("fill", "#64748b");
3494
+ typeText.textContent = "read model";
3495
+ group.appendChild(typeText);
3496
+ for (let r = 0; r < 2; r++) {
3497
+ const row = document.createElementNS(SVG_NS4, "line");
3498
+ row.setAttribute("x1", String(x2 + 12));
3499
+ row.setAttribute("y1", String(y + headerH + 30 + r * 9));
3500
+ row.setAttribute("x2", String(x2 + w2 - 12));
3501
+ row.setAttribute("y2", String(y + headerH + 30 + r * 9));
3502
+ row.setAttribute("stroke", "#e2e8f0");
3503
+ row.setAttribute("stroke-width", "1");
3504
+ group.appendChild(row);
3505
+ }
3506
+ container.appendChild(group);
3507
+ });
3508
+ relationships.forEach((rel) => {
3509
+ const edge = g.edge(rel.source, rel.target);
3510
+ if (!edge || !edge.points || edge.points.length === 0) return;
3511
+ const shiftedPoints = edge.points.map((p2) => ({
3512
+ x: graphStartX + p2.x,
3513
+ y: aggAreaY + p2.y
3514
+ }));
3515
+ const group = document.createElementNS(SVG_NS4, "g");
3516
+ group.setAttribute("class", "d5-rel");
3517
+ const pathString = generateCurvePath3(shiftedPoints);
3518
+ const path = document.createElementNS(SVG_NS4, "path");
3519
+ path.setAttribute("d", pathString);
3520
+ path.setAttribute("fill", "none");
3521
+ path.setAttribute("stroke", "#64748b");
3522
+ path.setAttribute("stroke-width", "1.5");
3523
+ path.setAttribute("marker-end", "url(#d5-arrowhead)");
3524
+ group.appendChild(path);
3525
+ if (rel.label) {
3526
+ let lx;
3527
+ let ly;
3528
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
3529
+ lx = graphStartX + edge.x;
3530
+ ly = aggAreaY + edge.y;
3531
+ } else {
3532
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
3533
+ lx = mid.x;
3534
+ ly = mid.y;
3535
+ }
3536
+ group.appendChild(
3537
+ createEdgeLabel({ x: lx, y: ly, text: rel.label, maxWidth: REL_LABEL_MAX_WIDTH3 })
3538
+ );
3539
+ }
3540
+ container.appendChild(group);
3541
+ });
3542
+ events.forEach((ev, i) => {
3543
+ const edge = g.edge(ev.source, ev.target, `evt${i}`);
3544
+ if (!edge || !edge.points || edge.points.length === 0) return;
3545
+ const shiftedPoints = edge.points.map((p2) => ({
3546
+ x: graphStartX + p2.x,
3547
+ y: aggAreaY + p2.y
3548
+ }));
3549
+ const group = document.createElementNS(SVG_NS4, "g");
3550
+ group.setAttribute("class", "d5-event");
3551
+ const titleEl = document.createElementNS(SVG_NS4, "title");
3552
+ titleEl.textContent = `${ev.name} \u2014 emitted by ${ev.source}, handled by ${ev.target}`;
3553
+ group.appendChild(titleEl);
3554
+ const path = document.createElementNS(SVG_NS4, "path");
3555
+ path.setAttribute("d", generateCurvePath3(shiftedPoints));
3556
+ path.setAttribute("fill", "none");
3557
+ path.setAttribute("stroke", EVENT_STROKE);
3558
+ path.setAttribute("stroke-width", "1.5");
3559
+ path.setAttribute("stroke-dasharray", "6 4");
3560
+ path.setAttribute("marker-end", "url(#d5-event-arrowhead)");
3561
+ group.appendChild(path);
3562
+ let lx;
3563
+ let ly;
3564
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
3565
+ lx = graphStartX + edge.x;
3566
+ ly = aggAreaY + edge.y;
3567
+ } else {
3568
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
3569
+ lx = mid.x;
3570
+ ly = mid.y;
3571
+ }
3572
+ group.appendChild(eventRibbon(lx, ly, ev.name));
3573
+ container.appendChild(group);
3574
+ });
3575
+ policies.forEach((p2, i) => {
3576
+ const edge = g.edge(p2.source, p2.target, `pol${i}`);
3577
+ if (!edge || !edge.points || edge.points.length === 0) return;
3578
+ const shiftedPoints = edge.points.map((pt) => ({
3579
+ x: graphStartX + pt.x,
3580
+ y: aggAreaY + pt.y
3581
+ }));
3582
+ const group = document.createElementNS(SVG_NS4, "g");
3583
+ group.setAttribute("class", "d5-policy");
3584
+ const titleEl = document.createElementNS(SVG_NS4, "title");
3585
+ titleEl.textContent = `Policy \u2014 triggered via ${p2.source}, acts on ${p2.target}`;
3586
+ group.appendChild(titleEl);
3587
+ const path = document.createElementNS(SVG_NS4, "path");
3588
+ path.setAttribute("d", generateCurvePath3(shiftedPoints));
3589
+ path.setAttribute("fill", "none");
3590
+ path.setAttribute("stroke", POLICY_STROKE);
3591
+ path.setAttribute("stroke-width", "1.5");
3592
+ path.setAttribute("stroke-dasharray", "3 3");
3593
+ path.setAttribute("marker-end", "url(#d5-policy-arrowhead)");
3594
+ group.appendChild(path);
3595
+ let lx;
3596
+ let ly;
3597
+ if (typeof edge.x === "number" && typeof edge.y === "number") {
3598
+ lx = graphStartX + edge.x;
3599
+ ly = aggAreaY + edge.y;
3600
+ } else {
3601
+ const mid = shiftedPoints[Math.floor(shiftedPoints.length / 2)];
3602
+ lx = mid.x;
3603
+ ly = mid.y;
3604
+ }
3605
+ group.appendChild(policyTag(lx, ly, p2.rule));
3606
+ container.appendChild(group);
3607
+ });
3608
+ }
3609
+
3610
+ // src/d5-aggregate/detector.ts
3611
+ function detectAggregate(text) {
3612
+ return text.trimStart().startsWith("d5-aggregate");
3613
+ }
3614
+
3615
+ // src/d5-aggregate/db.ts
3616
+ var D5AggregateDb = class {
3617
+ aggregate;
3618
+ entities = [];
3619
+ valueObjects = [];
3620
+ invariants = [];
3621
+ title;
3622
+ accTitle;
3623
+ accDescription;
3624
+ getAggregate() {
3625
+ return this.aggregate;
3626
+ }
3627
+ setAggregate(id, label, root) {
3628
+ this.aggregate = { id, label, root };
3629
+ }
3630
+ getEntities() {
3631
+ return this.entities;
3632
+ }
3633
+ addEntity(id, label) {
3634
+ this.entities.push({ id, label });
3635
+ }
3636
+ getValueObjects() {
3637
+ return this.valueObjects;
3638
+ }
3639
+ addValueObject(id, label) {
3640
+ this.valueObjects.push({ id, label });
3641
+ }
3642
+ getInvariants() {
3643
+ return this.invariants;
3644
+ }
3645
+ addInvariant(text, name) {
3646
+ this.invariants.push({ name: name || void 0, text });
3647
+ }
3648
+ clear() {
3649
+ this.aggregate = void 0;
3650
+ this.entities = [];
3651
+ this.valueObjects = [];
3652
+ this.invariants = [];
3653
+ this.title = void 0;
3654
+ this.accTitle = void 0;
3655
+ this.accDescription = void 0;
3656
+ }
3657
+ setTitle(title) {
3658
+ this.title = title;
3659
+ }
3660
+ getTitle() {
3661
+ return this.title;
3662
+ }
3663
+ // Mermaid DiagramDB interface methods
3664
+ setDiagramTitle(title) {
3665
+ this.setTitle(title);
3666
+ }
3667
+ getDiagramTitle() {
3668
+ return this.title ?? "";
3669
+ }
3670
+ setAccTitle(title) {
3671
+ this.accTitle = title;
3672
+ }
3673
+ getAccTitle() {
3674
+ return this.accTitle ?? "";
3675
+ }
3676
+ setAccDescription(desc) {
3677
+ this.accDescription = desc;
3678
+ }
3679
+ getAccDescription() {
3680
+ return this.accDescription ?? "";
3681
+ }
3682
+ };
3683
+
3684
+ // src/d5-aggregate/parser.ts
3685
+ var TITLE_RE4 = /^\s*title\s+(.+)$/;
3686
+ var AGGREGATE_RE2 = /^\s*Aggregate\(\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*root:\s*"([^"]+)"\s*\)\s*\{/;
3687
+ var ENTITY_RE = /^\s*Entity\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
3688
+ var VALUE_OBJECT_RE = /^\s*ValueObject\(\s*(\w+)\s*,\s*"([^"]+)"\s*\)/;
3689
+ var INVARIANTS_RE = /^\s*Invariants\s*\{/;
3690
+ var INVARIANT_RE = /^\s*Invariant\(\s*"([^"]+)"\s*(?:,\s*"([^"]+)")?\s*\)/;
3691
+ var COMMENT_LINE_RE4 = /^\s*%%/;
3692
+ function stripInlineComment4(line) {
3693
+ const idx = line.indexOf("%%");
3694
+ return idx === -1 ? line : line.slice(0, idx);
3695
+ }
3696
+ function parse4(text, db) {
3697
+ const lines = text.split("\n");
3698
+ for (const raw of lines) {
3699
+ if (COMMENT_LINE_RE4.test(raw)) continue;
3700
+ const line = stripInlineComment4(raw).trim();
3701
+ if (line === "" || line === "d5-aggregate") continue;
3702
+ let m;
3703
+ if (m = line.match(TITLE_RE4)) {
3704
+ db.setTitle(m[1].trim());
3705
+ } else if (m = line.match(AGGREGATE_RE2)) {
3706
+ db.setAggregate(m[1], m[2], m[3]);
3707
+ } else if (INVARIANTS_RE.test(line)) ; else if (m = line.match(INVARIANT_RE)) {
3708
+ if (m[2] !== void 0) db.addInvariant(m[2], m[1]);
3709
+ else db.addInvariant(m[1]);
3710
+ } else if (m = line.match(ENTITY_RE)) {
3711
+ db.addEntity(m[1], m[2]);
3712
+ } else if (m = line.match(VALUE_OBJECT_RE)) {
3713
+ db.addValueObject(m[1], m[2]);
3714
+ }
3715
+ }
3716
+ }
3717
+
3718
+ // src/d5-aggregate/renderer.ts
3719
+ var SVG_NS5 = "http://www.w3.org/2000/svg";
3720
+ var MARGIN3 = 30;
3721
+ var TITLE_HEIGHT4 = 40;
3722
+ var AGG_PADDING = 28;
3723
+ var AGG_HEADER = 36;
3724
+ var ROOT_H = 58;
3725
+ var ROOT_MIN_W = 320;
3726
+ var CELL_MIN_W = 150;
3727
+ var CELL_MAX_W = 240;
3728
+ var CELL_GAP = 18;
3729
+ var CELL_PAD_X = 16;
3730
+ var CELL_PAD_Y = 9;
3731
+ var LABEL_FONT = { size: 13, weight: 600 };
3732
+ var TYPE_FONT_SIZE = 10;
3733
+ var TARGET_GRID_W = 980;
3734
+ var ROOT_TO_GRID_GAP = 24;
3735
+ var INV_FONT = 12;
3736
+ var INV_TITLE_FONT = 12;
3737
+ var INV_PAD = 16;
3738
+ var INV_BULLET_INDENT = 16;
3739
+ var INV_GAP_ABOVE = 22;
3740
+ var INV_TITLE_GAP = 12;
3741
+ var INV_ITEM_GAP = 9;
3742
+ var INV_BAND_BOTTOM_PAD = 12;
3743
+ var INV_MAX_TEXT_COL = 620;
3744
+ function render4(db, container) {
3745
+ const title = db.getTitle();
3746
+ const aggregate = db.getAggregate();
3747
+ const entities = db.getEntities();
3748
+ const valueObjects = db.getValueObjects();
3749
+ const invariants = db.getInvariants();
3750
+ const rootId = aggregate ? entities.find((e) => e.label === aggregate.root)?.id : void 0;
3751
+ const wrapW = CELL_MAX_W - CELL_PAD_X * 2;
3752
+ const members = [
3753
+ ...entities.filter((e) => e.id !== rootId).map((e) => ({ ...e, kind: "entity" })),
3754
+ ...valueObjects.map((v2) => ({ ...v2, kind: "value-object" }))
3755
+ ].map((m) => ({ ...m, lines: wrapText(m.label, wrapW, LABEL_FONT) }));
3756
+ const widestLabel = members.reduce(
3757
+ (max, m) => Math.max(max, ...m.lines.map((l) => measureText(l, LABEL_FONT))),
3758
+ 0
3759
+ );
3760
+ const cellW = Math.round(
3761
+ Math.min(CELL_MAX_W, Math.max(CELL_MIN_W, widestLabel + CELL_PAD_X * 2))
3762
+ );
3763
+ const maxLines = members.reduce((max, m) => Math.max(max, m.lines.length), 1);
3764
+ const cellH = CELL_PAD_Y * 2 + maxLines * lineHeight(LABEL_FONT.size) + 4 + TYPE_FONT_SIZE;
3765
+ const { cols, rows } = gridDimensions(members.length, cellW, CELL_GAP, TARGET_GRID_W);
3766
+ const gridW = cols > 0 ? cols * cellW + (cols - 1) * CELL_GAP : 0;
3767
+ const gridH = rows > 0 ? rows * cellH + (rows - 1) * CELL_GAP : 0;
3768
+ const contentW = Math.max(gridW, rootId ? ROOT_MIN_W : 0, 320);
3769
+ const invWrapW = Math.min(contentW, INV_MAX_TEXT_COL) - INV_PAD * 2 - INV_BULLET_INDENT;
3770
+ const invItemLines = [];
3771
+ let invBandH = 0;
3772
+ if (invariants.length > 0) {
3773
+ invBandH = INV_GAP_ABOVE + lineHeight(INV_TITLE_FONT) + INV_TITLE_GAP;
3774
+ for (const inv of invariants) {
3775
+ const full = inv.name ? `${inv.name} \u2014 ${inv.text}` : inv.text;
3776
+ const lines = wrapText(full, invWrapW, { size: INV_FONT });
3777
+ invItemLines.push(lines);
3778
+ invBandH += lines.length * lineHeight(INV_FONT) + INV_ITEM_GAP;
3779
+ }
3780
+ invBandH += INV_BAND_BOTTOM_PAD - INV_ITEM_GAP;
3781
+ }
3782
+ const aggX = MARGIN3;
3783
+ const aggY = MARGIN3 + (title ? TITLE_HEIGHT4 : 0);
3784
+ const aggW = contentW + AGG_PADDING * 2;
3785
+ const bodyTop = aggY + AGG_HEADER + AGG_PADDING;
3786
+ const rootBottom = rootId ? bodyTop + ROOT_H + ROOT_TO_GRID_GAP : bodyTop;
3787
+ const gridBottom = rootBottom + gridH;
3788
+ const aggH = gridBottom + invBandH + AGG_PADDING - aggY;
3789
+ const legendH = 30;
3790
+ const totalW = aggX + aggW + MARGIN3;
3791
+ const totalH = aggY + aggH + legendH + MARGIN3;
3792
+ container.setAttribute("viewBox", `0 0 ${totalW} ${totalH}`);
3793
+ container.setAttribute("height", String(totalH));
3794
+ container.style.maxWidth = `${totalW}px`;
3795
+ if (title) {
3796
+ const titleEl = document.createElementNS(SVG_NS5, "text");
3797
+ titleEl.setAttribute("class", "d5-title");
3798
+ titleEl.setAttribute("x", String(totalW / 2));
3799
+ titleEl.setAttribute("y", "24");
3800
+ titleEl.setAttribute("text-anchor", "middle");
3801
+ titleEl.setAttribute("font-size", "18");
3802
+ titleEl.setAttribute("font-weight", "bold");
3803
+ titleEl.setAttribute("fill", "#1e293b");
3804
+ titleEl.textContent = title;
3805
+ container.appendChild(titleEl);
3806
+ }
3807
+ if (aggregate) {
3808
+ const aggGroup = document.createElementNS(SVG_NS5, "g");
3809
+ aggGroup.setAttribute("class", "d5-aggregate");
3810
+ const aggRect = document.createElementNS(SVG_NS5, "rect");
3811
+ aggRect.setAttribute("x", String(aggX));
3812
+ aggRect.setAttribute("y", String(aggY));
3813
+ aggRect.setAttribute("width", String(aggW));
3814
+ aggRect.setAttribute("height", String(aggH));
3815
+ aggRect.setAttribute("rx", "12");
3816
+ aggRect.setAttribute("fill", "#f8fafc");
3817
+ aggRect.setAttribute("stroke", "#94a3b8");
3818
+ aggRect.setAttribute("stroke-width", "2");
3819
+ aggRect.setAttribute("stroke-dasharray", "8 4");
3820
+ aggGroup.appendChild(aggRect);
3821
+ const aggLabel = document.createElementNS(SVG_NS5, "text");
3822
+ aggLabel.setAttribute("x", String(aggX + 16));
3823
+ aggLabel.setAttribute("y", String(aggY + 24));
3824
+ aggLabel.setAttribute("font-size", "14");
3825
+ aggLabel.setAttribute("font-weight", "600");
3826
+ aggLabel.setAttribute("fill", "#475569");
3827
+ aggLabel.textContent = `Aggregate: ${aggregate.label}`;
3828
+ aggGroup.appendChild(aggLabel);
3829
+ container.appendChild(aggGroup);
3830
+ }
3831
+ const contentX = aggX + AGG_PADDING;
3832
+ if (rootId && aggregate) {
3833
+ const g = document.createElementNS(SVG_NS5, "g");
3834
+ g.setAttribute("class", "d5-entity d5-aggregate-root");
3835
+ const rect = document.createElementNS(SVG_NS5, "rect");
3836
+ rect.setAttribute("x", String(contentX));
3837
+ rect.setAttribute("y", String(bodyTop));
3838
+ rect.setAttribute("width", String(contentW));
3839
+ rect.setAttribute("height", String(ROOT_H));
3840
+ rect.setAttribute("rx", "8");
3841
+ rect.setAttribute("fill", "#eff6ff");
3842
+ rect.setAttribute("stroke", "#3b82f6");
3843
+ rect.setAttribute("stroke-width", "3");
3844
+ g.appendChild(rect);
3845
+ const cx = contentX + contentW / 2;
3846
+ const label = document.createElementNS(SVG_NS5, "text");
3847
+ label.setAttribute("x", String(cx));
3848
+ label.setAttribute("y", String(bodyTop + 25));
3849
+ label.setAttribute("text-anchor", "middle");
3850
+ label.setAttribute("font-size", "15");
3851
+ label.setAttribute("font-weight", "bold");
3852
+ label.setAttribute("fill", "#1e293b");
3853
+ label.textContent = aggregate.root;
3854
+ g.appendChild(label);
3855
+ const type = document.createElementNS(SVG_NS5, "text");
3856
+ type.setAttribute("x", String(cx));
3857
+ type.setAttribute("y", String(bodyTop + 43));
3858
+ type.setAttribute("text-anchor", "middle");
3859
+ type.setAttribute("font-size", String(TYPE_FONT_SIZE));
3860
+ type.setAttribute("font-style", "italic");
3861
+ type.setAttribute("fill", "#3b82f6");
3862
+ type.textContent = "Aggregate Root";
3863
+ g.appendChild(type);
3864
+ container.appendChild(g);
3865
+ }
3866
+ const gridX = contentX + (contentW - gridW) / 2;
3867
+ members.forEach((m, i) => {
3868
+ const col = i % cols;
3869
+ const row = Math.floor(i / cols);
3870
+ const x2 = gridX + col * (cellW + CELL_GAP);
3871
+ const y = rootBottom + row * (cellH + CELL_GAP);
3872
+ const cx = x2 + cellW / 2;
3873
+ const isEntity = m.kind === "entity";
3874
+ const g = document.createElementNS(SVG_NS5, "g");
3875
+ g.setAttribute("class", isEntity ? "d5-entity" : "d5-value-object");
3876
+ const rect = document.createElementNS(SVG_NS5, "rect");
3877
+ rect.setAttribute("x", String(x2));
3878
+ rect.setAttribute("y", String(y));
3879
+ rect.setAttribute("width", String(cellW));
3880
+ rect.setAttribute("height", String(cellH));
3881
+ rect.setAttribute("rx", String(isEntity ? 6 : cellH / 2));
3882
+ rect.setAttribute("fill", "white");
3883
+ rect.setAttribute("stroke", isEntity ? "#3b82f6" : "#10b981");
3884
+ rect.setAttribute("stroke-width", "2");
3885
+ g.appendChild(rect);
3886
+ const lh = lineHeight(LABEL_FONT.size);
3887
+ const blockH = m.lines.length * lh + 4 + TYPE_FONT_SIZE;
3888
+ let ty = y + (cellH - blockH) / 2 + LABEL_FONT.size * 0.85;
3889
+ m.lines.forEach((ln2) => {
3890
+ const t = document.createElementNS(SVG_NS5, "text");
3891
+ t.setAttribute("x", String(cx));
3892
+ t.setAttribute("y", String(ty));
3893
+ t.setAttribute("text-anchor", "middle");
3894
+ t.setAttribute("font-size", String(LABEL_FONT.size));
3895
+ t.setAttribute("font-weight", "600");
3896
+ t.setAttribute("fill", "#1e293b");
3897
+ t.textContent = ln2;
3898
+ g.appendChild(t);
3899
+ ty += lh;
3900
+ });
3901
+ const type = document.createElementNS(SVG_NS5, "text");
3902
+ type.setAttribute("x", String(cx));
3903
+ type.setAttribute("y", String(ty + 2));
3904
+ type.setAttribute("text-anchor", "middle");
3905
+ type.setAttribute("font-size", String(TYPE_FONT_SIZE));
3906
+ type.setAttribute("font-style", "italic");
3907
+ type.setAttribute("fill", isEntity ? "#3b82f6" : "#10b981");
3908
+ type.textContent = isEntity ? "Entity" : "Value Object";
3909
+ g.appendChild(type);
3910
+ container.appendChild(g);
3911
+ });
3912
+ if (invariants.length > 0) {
3913
+ const bandX = aggX + INV_PAD;
3914
+ const bandRight = aggX + aggW - INV_PAD;
3915
+ let y = gridBottom + INV_GAP_ABOVE;
3916
+ const invGroup = document.createElementNS(SVG_NS5, "g");
3917
+ invGroup.setAttribute("class", "d5-invariants");
3918
+ const backing = document.createElementNS(SVG_NS5, "rect");
3919
+ backing.setAttribute("x", String(bandX - 8));
3920
+ backing.setAttribute("y", String(y - 12));
3921
+ backing.setAttribute("width", String(bandRight - bandX + 16));
3922
+ backing.setAttribute("height", String(invBandH - INV_GAP_ABOVE + 12));
3923
+ backing.setAttribute("rx", "6");
3924
+ backing.setAttribute("fill", "#fffbeb");
3925
+ backing.setAttribute("stroke", "#fcd34d");
3926
+ backing.setAttribute("stroke-width", "1");
3927
+ backing.setAttribute("stroke-dasharray", "4 3");
3928
+ invGroup.appendChild(backing);
3929
+ const heading = document.createElementNS(SVG_NS5, "text");
3930
+ heading.setAttribute("x", String(bandX));
3931
+ heading.setAttribute("y", String(y + INV_TITLE_FONT * 0.9));
3932
+ heading.setAttribute("font-size", String(INV_TITLE_FONT));
3933
+ heading.setAttribute("font-weight", "bold");
3934
+ heading.setAttribute("fill", "#92400e");
3935
+ heading.textContent = aggregate?.root ? `Invariants \u2014 enforced by root: ${aggregate.root}` : "Invariants";
3936
+ invGroup.appendChild(heading);
3937
+ y += lineHeight(INV_TITLE_FONT) + INV_TITLE_GAP;
3938
+ const lh = lineHeight(INV_FONT);
3939
+ invItemLines.forEach((lines) => {
3940
+ const ms = 6;
3941
+ const marker = document.createElementNS(SVG_NS5, "rect");
3942
+ marker.setAttribute("x", String(bandX + 1));
3943
+ marker.setAttribute("y", String(y - ms));
3944
+ marker.setAttribute("width", String(ms));
3945
+ marker.setAttribute("height", String(ms));
3946
+ marker.setAttribute("transform", `rotate(45 ${bandX + 1 + ms / 2} ${y - ms + ms / 2})`);
3947
+ marker.setAttribute("fill", "#f59e0b");
3948
+ invGroup.appendChild(marker);
3949
+ lines.forEach((ln2, i) => {
3950
+ const t = document.createElementNS(SVG_NS5, "text");
3951
+ t.setAttribute("x", String(bandX + INV_BULLET_INDENT));
3952
+ t.setAttribute("y", String(y + i * lh));
3953
+ t.setAttribute("font-size", String(INV_FONT));
3954
+ t.setAttribute("fill", "#78350f");
3955
+ t.textContent = ln2;
3956
+ invGroup.appendChild(t);
3957
+ });
3958
+ y += lines.length * lh + INV_ITEM_GAP;
3959
+ });
3960
+ container.appendChild(invGroup);
3961
+ }
3962
+ const legendY = aggY + aggH + 16;
3963
+ const legendGroup = document.createElementNS(SVG_NS5, "g");
3964
+ legendGroup.setAttribute("class", "d5-legend");
3965
+ const legendItems = [
3966
+ { label: "Aggregate Root", stroke: "#3b82f6", width: 3, rx: 3 },
3967
+ { label: "Entity", stroke: "#3b82f6", width: 2, rx: 3 },
3968
+ { label: "Value Object", stroke: "#10b981", width: 2, rx: 7 }
3969
+ ];
3970
+ let legendX = aggX;
3971
+ legendItems.forEach((item) => {
3972
+ const swatch = document.createElementNS(SVG_NS5, "rect");
3973
+ swatch.setAttribute("x", String(legendX));
3974
+ swatch.setAttribute("y", String(legendY));
3975
+ swatch.setAttribute("width", "14");
3976
+ swatch.setAttribute("height", "14");
3977
+ swatch.setAttribute("rx", String(item.rx));
3978
+ swatch.setAttribute("fill", "white");
3979
+ swatch.setAttribute("stroke", item.stroke);
3980
+ swatch.setAttribute("stroke-width", String(item.width));
3981
+ legendGroup.appendChild(swatch);
3982
+ const label = document.createElementNS(SVG_NS5, "text");
3983
+ label.setAttribute("x", String(legendX + 20));
3984
+ label.setAttribute("y", String(legendY + 11));
3985
+ label.setAttribute("font-size", "11");
3986
+ label.setAttribute("fill", "#64748b");
3987
+ label.textContent = item.label;
3988
+ legendGroup.appendChild(label);
3989
+ legendX += 26 + measureText(item.label, { size: 11 }) + 22;
3990
+ });
3991
+ container.appendChild(legendGroup);
3992
+ }
3993
+
3994
+ // src/index.ts
3995
+ var SVG_NS6 = "http://www.w3.org/2000/svg";
3996
+ function createRenderer(db, renderFn) {
3997
+ return {
3998
+ draw(_text, id, _version, _diagramObject) {
3999
+ let svgElement = document.getElementById(id);
4000
+ if (!svgElement) {
4001
+ svgElement = document.createElementNS(SVG_NS6, "svg");
4002
+ svgElement.setAttribute("id", id);
4003
+ document.body.appendChild(svgElement);
4004
+ }
4005
+ renderFn(db, svgElement);
4006
+ }
4007
+ };
4008
+ }
4009
+ function createParser(db, parseFn) {
4010
+ return {
4011
+ parse(text) {
4012
+ db.clear();
4013
+ parseFn(text, db);
4014
+ }
4015
+ };
4016
+ }
4017
+ var domainDb = new D5DomainDb();
4018
+ var subdomainDb = new D5SubdomainDb();
4019
+ var contextDb = new D5ContextDb();
4020
+ var aggregateDb = new D5AggregateDb();
4021
+ var d5Diagrams = [
4022
+ {
4023
+ id: "d5-domain",
4024
+ detector: (text, _config) => detectDomain(text),
4025
+ loader: async () => ({
4026
+ id: "d5-domain",
4027
+ diagram: {
4028
+ db: domainDb,
4029
+ renderer: createRenderer(domainDb, render),
4030
+ parser: createParser(domainDb, parse)
4031
+ }
4032
+ })
4033
+ },
4034
+ {
4035
+ id: "d5-subdomain",
4036
+ detector: (text, _config) => detectSubdomain(text),
4037
+ loader: async () => ({
4038
+ id: "d5-subdomain",
4039
+ diagram: {
4040
+ db: subdomainDb,
4041
+ renderer: createRenderer(subdomainDb, render2),
4042
+ parser: createParser(subdomainDb, parse2)
4043
+ }
4044
+ })
4045
+ },
4046
+ {
4047
+ id: "d5-context",
4048
+ detector: (text, _config) => detectContext(text),
4049
+ loader: async () => ({
4050
+ id: "d5-context",
4051
+ diagram: {
4052
+ db: contextDb,
4053
+ renderer: createRenderer(contextDb, render3),
4054
+ parser: createParser(contextDb, parse3)
4055
+ }
4056
+ })
4057
+ },
4058
+ {
4059
+ id: "d5-aggregate",
4060
+ detector: (text, _config) => detectAggregate(text),
4061
+ loader: async () => ({
4062
+ id: "d5-aggregate",
4063
+ diagram: {
4064
+ db: aggregateDb,
4065
+ renderer: createRenderer(aggregateDb, render4),
4066
+ parser: createParser(aggregateDb, parse4)
4067
+ }
4068
+ })
4069
+ }
4070
+ ];
4071
+ /*! Bundled license information:
4072
+
4073
+ @dagrejs/dagre/dist/dagre.esm.js:
4074
+ (*! For license information please see dagre.esm.js.LEGAL.txt *)
4075
+ */
4076
+
4077
+ export { d5Diagrams };
4078
+ //# sourceMappingURL=index.js.map
4079
+ //# sourceMappingURL=index.js.map