@shibayama/pdgkit 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -297,6 +297,8 @@ var THICK_ARROW_TERMINAL_CLEARANCE = 5.4;
297
297
  var VERTICAL_PORT_RATIO = 0.25;
298
298
  var PORT_STUB = 6;
299
299
  var MAX_ROUTE_LANES = 18;
300
+ var LOOP_LANE_GAP = 10;
301
+ var LOOP_LANE_STEP = 7;
300
302
  var EPS = 1e-3;
301
303
  function layout(doc) {
302
304
  switch (doc.kind) {
@@ -476,7 +478,6 @@ function layoutFlow(doc) {
476
478
  const positions = /* @__PURE__ */ new Map();
477
479
  const sortedRanks = [...byRank.keys()].sort((a, b) => a - b);
478
480
  let y = MARGIN;
479
- let maxX = 0;
480
481
  for (const r of sortedRanks) {
481
482
  const lane = byRank.get(r);
482
483
  const widths = lane.map((id) => shapeOf(id) === "diamond" ? NODE_W * 1.2 : NODE_W);
@@ -488,7 +489,6 @@ function layoutFlow(doc) {
488
489
  positions.set(lane[i], { x, y, w: widths[i], h: NODE_H });
489
490
  x += widths[i] + H_GAP;
490
491
  }
491
- if (x > maxX) maxX = x;
492
492
  y += NODE_H + V_GAP;
493
493
  }
494
494
  for (const id of ids) {
@@ -508,7 +508,8 @@ function layoutFlow(doc) {
508
508
  });
509
509
  }
510
510
  const edges = makeEdges(doc.edges, positions);
511
- return { nodes: placed, edges, width: maxX + MARGIN, height: y + MARGIN, kind: "flow" };
511
+ const { width, height } = flowExtent(placed, edges);
512
+ return { nodes: placed, edges, width, height, kind: "flow" };
512
513
  }
513
514
  function layoutState(doc) {
514
515
  const { byRank } = computeRanks(doc);
@@ -520,7 +521,6 @@ function layoutState(doc) {
520
521
  const positions = /* @__PURE__ */ new Map();
521
522
  const sortedRanks = [...byRank.keys()].sort((a, b) => a - b);
522
523
  let y = MARGIN;
523
- let maxX = 0;
524
524
  for (const r of sortedRanks) {
525
525
  const lane = byRank.get(r);
526
526
  const widths = lane.map((id) => shapeOf(id) === "circle" ? 6 : NODE_W);
@@ -532,7 +532,6 @@ function layoutState(doc) {
532
532
  positions.set(lane[i], { x, y: y + (NODE_H - heights[i]) / 2, w: widths[i], h: heights[i] });
533
533
  x += widths[i] + H_GAP;
534
534
  }
535
- if (x > maxX) maxX = x;
536
535
  y += NODE_H + V_GAP;
537
536
  }
538
537
  for (const id of doc.nodes.keys()) {
@@ -552,7 +551,8 @@ function layoutState(doc) {
552
551
  });
553
552
  }
554
553
  const edges = makeEdges(doc.edges, positions);
555
- return { nodes: placed, edges, width: maxX + MARGIN, height: y + MARGIN, kind: "state" };
554
+ const { width, height } = flowExtent(placed, edges);
555
+ return { nodes: placed, edges, width, height, kind: "state" };
556
556
  }
557
557
  function layoutSeq(doc) {
558
558
  const seen = /* @__PURE__ */ new Set();
@@ -671,21 +671,50 @@ function computeRanks(doc) {
671
671
  return { byRank };
672
672
  }
673
673
  function makeEdges(srcEdges, positions) {
674
- return srcEdges.map((e) => {
674
+ const boxes = [...positions.values()];
675
+ const rightLimit = boxes.length ? Math.max(...boxes.map((b) => b.x + b.w)) : MARGIN;
676
+ const feedback = srcEdges.map((e, index) => ({ index, a: positions.get(e.from), b: positions.get(e.to) })).filter((r) => !!r.a && !!r.b && r.b.y + r.b.h <= r.a.y + EPS).sort((p, q) => feedbackSpan(p.a, p.b) - feedbackSpan(q.a, q.b));
677
+ const laneOf = /* @__PURE__ */ new Map();
678
+ feedback.forEach((r, nest) => laneOf.set(r.index, nest));
679
+ return srcEdges.map((e, index) => {
675
680
  const a = positions.get(e.from);
676
681
  const b = positions.get(e.to);
677
682
  if (!a || !b) {
678
683
  return { from: e.from, to: e.to, points: [], label: e.label, op: e.op };
679
684
  }
680
- return {
681
- from: e.from,
682
- to: e.to,
683
- points: orthogonalRoute(a, b),
684
- label: e.label,
685
- op: e.op
686
- };
685
+ const nest = laneOf.get(index);
686
+ const points = nest === void 0 ? orthogonalRoute(a, b) : feedbackRoute(a, b, rightLimit + LOOP_LANE_GAP + nest * LOOP_LANE_STEP);
687
+ return { from: e.from, to: e.to, points, label: e.label, op: e.op };
687
688
  });
688
689
  }
690
+ function feedbackSpan(a, b) {
691
+ return a.y + a.h / 2 - (b.y + b.h / 2);
692
+ }
693
+ function feedbackRoute(a, b, laneX) {
694
+ const ay = a.y + a.h / 2;
695
+ const by = b.y + b.h / 2;
696
+ return [
697
+ [a.x + a.w, ay],
698
+ [laneX, ay],
699
+ [laneX, by],
700
+ [b.x + b.w, by]
701
+ ];
702
+ }
703
+ function flowExtent(placed, edges) {
704
+ let maxX = MARGIN;
705
+ let maxY = MARGIN;
706
+ for (const n of placed) {
707
+ if (n.x + n.w > maxX) maxX = n.x + n.w;
708
+ if (n.y + n.h > maxY) maxY = n.y + n.h;
709
+ }
710
+ for (const e of edges) {
711
+ for (const [x, y] of e.points) {
712
+ if (x > maxX) maxX = x;
713
+ if (y > maxY) maxY = y;
714
+ }
715
+ }
716
+ return { width: maxX + MARGIN, height: maxY + MARGIN };
717
+ }
689
718
  function hasLinearChildFlow(children, edges, childMap) {
690
719
  const childSet = new Set(children);
691
720
  const pairs = /* @__PURE__ */ new Set();
@@ -3107,7 +3136,7 @@ var langSchema = z.enum(["ja", "en", "both"]).default("ja");
3107
3136
  function textResult(text) {
3108
3137
  return { content: [{ type: "text", text }] };
3109
3138
  }
3110
- function buildServer(version = "0.1.0") {
3139
+ function buildServer(version = "0.1.2") {
3111
3140
  const server = new McpServer({ name: "pdgkit", version });
3112
3141
  server.registerTool(
3113
3142
  "pdg_validate",
@@ -3181,7 +3210,7 @@ function buildServer(version = "0.1.0") {
3181
3210
 
3182
3211
  // src/node/index.ts
3183
3212
  import { readFileSync as readFileSync2 } from "fs";
3184
- var VERSION = "0.1.0";
3213
+ var VERSION = "0.1.2";
3185
3214
 
3186
3215
  // bin/pdgkit-mcp.ts
3187
3216
  async function main() {
package/dist/pdgkit.cjs CHANGED
@@ -217,6 +217,8 @@ var THICK_ARROW_TERMINAL_CLEARANCE = 5.4;
217
217
  var VERTICAL_PORT_RATIO = 0.25;
218
218
  var PORT_STUB = 6;
219
219
  var MAX_ROUTE_LANES = 18;
220
+ var LOOP_LANE_GAP = 10;
221
+ var LOOP_LANE_STEP = 7;
220
222
  var EPS = 1e-3;
221
223
  function layout(doc) {
222
224
  switch (doc.kind) {
@@ -396,7 +398,6 @@ function layoutFlow(doc) {
396
398
  const positions = /* @__PURE__ */ new Map();
397
399
  const sortedRanks = [...byRank.keys()].sort((a, b) => a - b);
398
400
  let y = MARGIN;
399
- let maxX = 0;
400
401
  for (const r of sortedRanks) {
401
402
  const lane = byRank.get(r);
402
403
  const widths = lane.map((id) => shapeOf(id) === "diamond" ? NODE_W * 1.2 : NODE_W);
@@ -408,7 +409,6 @@ function layoutFlow(doc) {
408
409
  positions.set(lane[i], { x, y, w: widths[i], h: NODE_H });
409
410
  x += widths[i] + H_GAP;
410
411
  }
411
- if (x > maxX) maxX = x;
412
412
  y += NODE_H + V_GAP;
413
413
  }
414
414
  for (const id of ids) {
@@ -428,7 +428,8 @@ function layoutFlow(doc) {
428
428
  });
429
429
  }
430
430
  const edges = makeEdges(doc.edges, positions);
431
- return { nodes: placed, edges, width: maxX + MARGIN, height: y + MARGIN, kind: "flow" };
431
+ const { width, height } = flowExtent(placed, edges);
432
+ return { nodes: placed, edges, width, height, kind: "flow" };
432
433
  }
433
434
  function layoutState(doc) {
434
435
  const { byRank } = computeRanks(doc);
@@ -440,7 +441,6 @@ function layoutState(doc) {
440
441
  const positions = /* @__PURE__ */ new Map();
441
442
  const sortedRanks = [...byRank.keys()].sort((a, b) => a - b);
442
443
  let y = MARGIN;
443
- let maxX = 0;
444
444
  for (const r of sortedRanks) {
445
445
  const lane = byRank.get(r);
446
446
  const widths = lane.map((id) => shapeOf(id) === "circle" ? 6 : NODE_W);
@@ -452,7 +452,6 @@ function layoutState(doc) {
452
452
  positions.set(lane[i], { x, y: y + (NODE_H - heights[i]) / 2, w: widths[i], h: heights[i] });
453
453
  x += widths[i] + H_GAP;
454
454
  }
455
- if (x > maxX) maxX = x;
456
455
  y += NODE_H + V_GAP;
457
456
  }
458
457
  for (const id of doc.nodes.keys()) {
@@ -472,7 +471,8 @@ function layoutState(doc) {
472
471
  });
473
472
  }
474
473
  const edges = makeEdges(doc.edges, positions);
475
- return { nodes: placed, edges, width: maxX + MARGIN, height: y + MARGIN, kind: "state" };
474
+ const { width, height } = flowExtent(placed, edges);
475
+ return { nodes: placed, edges, width, height, kind: "state" };
476
476
  }
477
477
  function layoutSeq(doc) {
478
478
  const seen = /* @__PURE__ */ new Set();
@@ -591,21 +591,50 @@ function computeRanks(doc) {
591
591
  return { byRank };
592
592
  }
593
593
  function makeEdges(srcEdges, positions) {
594
- return srcEdges.map((e) => {
594
+ const boxes = [...positions.values()];
595
+ const rightLimit = boxes.length ? Math.max(...boxes.map((b) => b.x + b.w)) : MARGIN;
596
+ const feedback = srcEdges.map((e, index) => ({ index, a: positions.get(e.from), b: positions.get(e.to) })).filter((r) => !!r.a && !!r.b && r.b.y + r.b.h <= r.a.y + EPS).sort((p, q) => feedbackSpan(p.a, p.b) - feedbackSpan(q.a, q.b));
597
+ const laneOf = /* @__PURE__ */ new Map();
598
+ feedback.forEach((r, nest) => laneOf.set(r.index, nest));
599
+ return srcEdges.map((e, index) => {
595
600
  const a = positions.get(e.from);
596
601
  const b = positions.get(e.to);
597
602
  if (!a || !b) {
598
603
  return { from: e.from, to: e.to, points: [], label: e.label, op: e.op };
599
604
  }
600
- return {
601
- from: e.from,
602
- to: e.to,
603
- points: orthogonalRoute(a, b),
604
- label: e.label,
605
- op: e.op
606
- };
605
+ const nest = laneOf.get(index);
606
+ const points = nest === void 0 ? orthogonalRoute(a, b) : feedbackRoute(a, b, rightLimit + LOOP_LANE_GAP + nest * LOOP_LANE_STEP);
607
+ return { from: e.from, to: e.to, points, label: e.label, op: e.op };
607
608
  });
608
609
  }
610
+ function feedbackSpan(a, b) {
611
+ return a.y + a.h / 2 - (b.y + b.h / 2);
612
+ }
613
+ function feedbackRoute(a, b, laneX) {
614
+ const ay = a.y + a.h / 2;
615
+ const by = b.y + b.h / 2;
616
+ return [
617
+ [a.x + a.w, ay],
618
+ [laneX, ay],
619
+ [laneX, by],
620
+ [b.x + b.w, by]
621
+ ];
622
+ }
623
+ function flowExtent(placed, edges) {
624
+ let maxX = MARGIN;
625
+ let maxY = MARGIN;
626
+ for (const n of placed) {
627
+ if (n.x + n.w > maxX) maxX = n.x + n.w;
628
+ if (n.y + n.h > maxY) maxY = n.y + n.h;
629
+ }
630
+ for (const e of edges) {
631
+ for (const [x, y] of e.points) {
632
+ if (x > maxX) maxX = x;
633
+ if (y > maxY) maxY = y;
634
+ }
635
+ }
636
+ return { width: maxX + MARGIN, height: maxY + MARGIN };
637
+ }
609
638
  function hasLinearChildFlow(children, edges, childMap) {
610
639
  const childSet = new Set(children);
611
640
  const pairs = /* @__PURE__ */ new Set();
@@ -3336,7 +3365,7 @@ async function renderToPptx(source, opts = {}) {
3336
3365
  }
3337
3366
 
3338
3367
  // src/node/index.ts
3339
- var VERSION = "0.1.0";
3368
+ var VERSION = "0.1.2";
3340
3369
  function loadAuthoringGuide() {
3341
3370
  return (0, import_node_fs2.readFileSync)(resolvePackageFile("docs", "ai-authoring-guide.md"), "utf8");
3342
3371
  }
@@ -1,7 +1,7 @@
1
- "use strict";var pdgkit=(()=>{var lt=Object.defineProperty;var xe=Object.getOwnPropertyDescriptor;var be=Object.getOwnPropertyNames;var ye=Object.prototype.hasOwnProperty;var Se=(t,e)=>{for(var n in e)lt(t,n,{get:e[n],enumerable:!0})},we=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of be(e))!ye.call(t,o)&&o!==n&&lt(t,o,{get:()=>e[o],enumerable:!(r=xe(e,o))||r.enumerable});return t};var Ee=t=>we(lt({},"__esModule",{value:!0}),t);var Kn={};Se(Kn,{PATTERN_SOURCE:()=>Lt,SAMPLES:()=>Bt,SAMPLE_ORDER:()=>Pt,VERSION:()=>Hn,buildSvgModel:()=>Et,computeContentBox:()=>at,layout:()=>z,parse:()=>F,refsToCsv:()=>Ot,refsToMarkdown:()=>Mt,render:()=>q,renderToSvg:()=>ut,serializeSvg:()=>J,svgDisplayDimensions:()=>ct,toSvgString:()=>Wn,validate:()=>me});var K=/[A-Za-z0-9_*]+/.source,vt=[{lit:"<->",kind:"bidir"},{lit:"=>",kind:"thick"},{lit:"->",kind:"arrow"},{lit:"<-",kind:"arrow",reverse:!0},{lit:".>",kind:"dashed-arrow"},{lit:"..",kind:"dashed"},{lit:"-",kind:"line"}],Ae=new RegExp(`^(${K})\\s*=(?!>)\\s*(.*)$`),Me=new RegExp(`^(${K})\\s+(<->|=>|->|<-|\\.>|\\.\\.|-)\\s+(${K})\\s*(?::\\s*(.*))?$`),Oe=new RegExp(`^(${K})\\s*:\\s*(.+)$`);function F(t){let e={nodes:new Map,containments:[],edges:[],diagnostics:[],kind:"block"},n=t.split(/\r?\n/);for(let r=0;r<n.length;r++){let s=Tt(n[r]).trim();s&&(Pe(s,r+1,e)||Be(s,r+1,e)||Le(s,r+1,e)||e.diagnostics.push({severity:"error",line:r+1,col:1,message:`\u69CB\u6587\u4E0D\u660E: "${s}"`}))}return e.kind=_e(e),e}function Pe(t,e,n){let r=t.match(Ae);if(!r)return!1;let o=r[1],s=r[2],i=ht(s);return n.nodes.get(o)&&n.diagnostics.push({severity:"warning",line:e,col:1,message:`\u7B26\u53F7 "${o}" \u306F\u518D\u5B9A\u7FA9\u3055\u308C\u307E\u3057\u305F`}),n.nodes.set(o,{id:o,label:i,implicit:!1}),!0}function Be(t,e,n){let r=t.match(Me);if(!r)return!1;let o=r[2],s=vt.find(u=>u.lit===o),i=s.reverse?r[3]:r[1],a=s.reverse?r[1]:r[3],c=r[4]??"",l={from:i,to:a,op:s.kind,label:c.trim()?ht(c):void 0,line:e};return n.edges.push(l),Q(n,i),Q(n,a),!0}function Le(t,e,n){let r=t.match(Oe);if(!r)return!1;let o=r[1],i=r[2].trim().split(/\s+/).filter(Boolean),a=new RegExp(`^${K}$`);for(let c of i)if(!a.test(c))return n.diagnostics.push({severity:"error",line:e,col:1,message:`\u5305\u542B\u306E\u5B50\u3068\u3057\u3066\u4E0D\u6B63\u306A\u30C8\u30FC\u30AF\u30F3: "${c}"`}),!0;n.containments.push({parent:o,children:i,line:e}),Q(n,o);for(let c of i)Q(n,c);return!0}function Q(t,e){let n=t.nodes.get(e);return n||(n={id:e,label:{},implicit:!0},t.nodes.set(e,n),n)}function Tt(t){let e=!1;for(let n=0;n<t.length;n++){let r=t[n];if(r==='"')e=!e;else if(!e&&r==="#")return t.slice(0,n)}return t}function ht(t){let e=t.trim();if(!e)return{};let n=Re(e);return n===-1?{ja:ft(e)}:{ja:ft(e.slice(0,n).trim()),en:ft(e.slice(n+1).trim())}}function Re(t){let e=!1;for(let n=0;n<t.length;n++){if(t[n]==='"'){e=!e;continue}if(!e&&t[n]==="/"&&Nt(t[n-1])&&Nt(t[n+1]))return n}return-1}function Nt(t){return t===" "||t===" "}function ft(t){return t.length>=2&&t.startsWith('"')&&t.endsWith('"')?t.slice(1,-1):t}function _e(t){if(t.containments.length>0)return"block";for(let n of t.nodes.values()){let r=n.label.ja??"",o=n.label.en??"";if(r.endsWith("?")||o.endsWith("?"))return"flow"}if(t.nodes.has("*"))return"state";let e=new Set;for(let n of t.edges){if(n.op==="bidir")return"seq";let r=`${n.from}|${n.to}`,o=`${n.to}|${n.from}`;if(e.has(o))return"seq";e.add(r)}return"flow"}function z(t){switch(t.kind){case"block":return Ne(t);case"flow":return ve(t);case"state":return Te(t);case"seq":return ke(t)}}function Ne(t){let e=new Map;for(let h of t.containments)e.set(h.parent,h.children);let n=new Map;for(let h of t.containments)for(let g of h.children)n.set(g,h.parent);let r=new Set;for(let h of e.values())for(let g of h)r.add(g);let s=[...t.nodes.keys()].filter(h=>!r.has(h)),i=[],a=new Map;function c(h){let g=e.get(h);if(!g||g.length===0)return{w:36,h:14};let E=g.map(c);if(l(h)==="grid"){let w=u(h,g),B=Math.max(...E.map(M=>M.w)),P=m(E,w);return{w:w*B+16+(w-1)*24,h:f(h)+P.reduce((M,R)=>M+R,0)+16+Math.max(0,P.length-1)*24}}let O=Math.max(...E.map(w=>w.w)),A=E.reduce((w,B)=>w+B.h,0);return{w:O+16,h:f(h)+A+16+(g.length-1)*24}}function l(h){let g=e.get(h);return!g||g.length<=2?"stack":d(h,g)?"grid":De(g,t.edges,e)?"stack":"grid"}function u(h,g){return d(h,g)?Math.min(g.length,4):g.length>=7?4:g.length>=5?3:2}function m(h,g){let E=Math.ceil(h.length/g),O=[];for(let A=0;A<E;A++){let w=h.slice(A*g,A*g+g);O.push(Math.max(...w.map(B=>B.h)))}return O}function f(h){let g=e.get(h);return g&&d(h,g)?19:5}function d(h,g){return t.edges.some(E=>E.from===h&&g.some(O=>O===E.to||gt(O,E.to,e)))}function y(h,g,E){let O=c(h);a.set(h,{x:g,y:E,w:O.w,h:O.h});let A=t.nodes.get(h),w=e.get(h);if(!w||w.length===0){i.push({id:h,x:g,y:E,w:O.w,h:O.h,label:A?.label??{},shape:"box",isContainer:!1});return}let B=w.map(c);if(l(h)==="grid"){let P=u(h,w),M=Math.max(...B.map(v=>v.w)),R=m(B,P),W=[],Rt=E+f(h)+8;for(let v=0;v<R.length;v++)W[v]=Rt,Rt+=R[v]+24;for(let v=0;v<w.length;v++){let _t=Math.floor(v/P),de=v%P,ge=g+8+de*(M+24)+(M-B[v].w)/2,pe=W[_t]+(R[_t]-B[v].h)/2;y(w[v],ge,pe)}Ce(w,B,R,W,P,e,t.edges,a,i)}else{let P=Math.max(...B.map(R=>R.w)),M=E+f(h)+8;for(let R=0;R<w.length;R++){let W=g+8+(P-B[R].w)/2;y(w[R],W,M),M+=B[R].h+24}}i.push({id:h,x:g,y:E,w:O.w,h:O.h,label:A?.label??{},shape:"box",isContainer:!0})}let S=8;for(let h of s){let g=new Set(a.keys());y(h,S,8);let E=tt(h,e),O=Ie(h,E,g,t.edges,a);Math.abs(O)>=.001&&qt(E,O,a,i);let A=c(h);S+=A.w+32}let b=Ge(t.edges,a,i,n);i.sort((h,g)=>{if(h.isContainer!==g.isContainer)return h.isContainer?-1:1;if(h.isContainer&&g.isContainer){let E=Gt(h.id,n)-Gt(g.id,n);return E!==0?E:g.w*g.h-h.w*h.h}return h.y-g.y||h.x-g.x});let x=[...a.values()],p=b.flatMap(h=>h.points),L=x.length?Math.max(...x.map(h=>h.x+h.w)):8,k=x.length?Math.max(...x.map(h=>h.y+h.h)):8,_=p.length?Math.max(...p.map(([h])=>h)):8,D=p.length?Math.max(...p.map(([,h])=>h)):8,V=Math.max(L,_)+8,C=Math.max(k,D)+8;return{nodes:i,edges:b,width:V,height:C,kind:"block"}}function ve(t){let e=[...t.nodes.keys()],{byRank:n}=Ut(t);function r(f){let d=t.nodes.get(f),y=d?.label.ja??"",S=d?.label.en??"";if(y.endsWith("?")||S.endsWith("?"))return"diamond";let b=t.edges.filter(p=>p.to===f).length,x=t.edges.filter(p=>p.from===f).length;return b===0||x===0?"round":"box"}let o=14,s=10,i=new Map,a=[...n.keys()].sort((f,d)=>f-d),c=8,l=0;for(let f of a){let d=n.get(f),y=d.map(p=>r(p)==="diamond"?36*1.2:36),S=y.reduce((p,L)=>p+L,0)+(d.length-1)*s,b=8;b=(Math.max(S+16,200)-S)/2;for(let p=0;p<d.length;p++)i.set(d[p],{x:b,y:c,w:y[p],h:14}),b+=y[p]+s;b>l&&(l=b),c+=14+o}for(let f of e)i.has(f)||(i.set(f,{x:8,y:c,w:36,h:14}),c+=14+o);let u=[];for(let[f,d]of i)u.push({id:f,...d,label:t.nodes.get(f)?.label??{},shape:r(f),isContainer:!1});let m=zt(t.edges,i);return{nodes:u,edges:m,width:l+8,height:c+8,kind:"flow"}}function Te(t){let{byRank:e}=Ut(t);function n(m){return m==="*"?"circle":"round"}let r=14,o=10,s=new Map,i=[...e.keys()].sort((m,f)=>m-f),a=8,c=0;for(let m of i){let f=e.get(m),d=f.map(p=>n(p)==="circle"?6:36),y=f.map(p=>n(p)==="circle"?6:14),S=d.reduce((p,L)=>p+L,0)+(f.length-1)*o,x=(Math.max(S+16,200)-S)/2;for(let p=0;p<f.length;p++)s.set(f[p],{x,y:a+(14-y[p])/2,w:d[p],h:y[p]}),x+=d[p]+o;x>c&&(c=x),a+=14+r}for(let m of t.nodes.keys())s.has(m)||(s.set(m,{x:8,y:a,w:36,h:14}),a+=14+r);let l=[];for(let[m,f]of s)l.push({id:m,...f,label:t.nodes.get(m)?.label??{},shape:n(m),isContainer:!1});let u=zt(t.edges,s);return{nodes:l,edges:u,width:c+8,height:a+8,kind:"state"}}function ke(t){let e=new Set,n=[];for(let d of t.edges)for(let y of[d.from,d.to])e.has(y)||(e.add(y),n.push(y));for(let d of t.nodes.keys())e.has(d)||(e.add(d),n.push(d));let r=40,o=12,s=28,i=12,a=new Map,c=[],l=8;for(let d of n)a.set(d,l+r/2),c.push({id:d,x:l,y:8,w:r,h:o,label:t.nodes.get(d)?.label??{},shape:"actor",isContainer:!1}),l+=r+s;let u=8+o+i,m=[];for(let d of t.edges){let y=a.get(d.from),S=a.get(d.to);y===void 0||S===void 0||(m.push({from:d.from,to:d.to,points:[[y,u],[S,u]],label:d.label,op:d.op}),u+=i)}let f=n.map(d=>({from:d,to:d,points:[[a.get(d),8+o],[a.get(d),u+i]],op:"dashed",isLifeline:!0}));return{nodes:c,edges:[...f,...m],width:l,height:u+16,kind:"seq"}}function Ut(t){let e=[...t.nodes.keys()],n=new Map;for(let u of e)n.set(u,0);for(let u of t.edges)n.set(u.to,(n.get(u.to)??0)+1);let r=new Map;for(let u of t.edges)r.has(u.from)||r.set(u.from,[]),r.get(u.from).push(u.to);let o=e.filter(u=>n.get(u)===0);o.length===0&&(o=e.includes("*")?["*"]:e.length?[e[0]]:[]);let s=new Map,i=new Set;for(let u of o)s.set(u,0),i.add(u);let a=[...o];for(;a.length;){let u=[];for(let m of a){let f=s.get(m);for(let d of r.get(m)??[])i.has(d)||(s.set(d,f+1),i.add(d),u.push(d))}a=u}let c=0;for(let u of s.values())u>c&&(c=u);for(let u of e)i.has(u)||(c++,s.set(u,c),i.add(u));let l=new Map;for(let[u,m]of s)l.has(m)||l.set(m,[]),l.get(m).push(u);return{byRank:l}}function zt(t,e){return t.map(n=>{let r=e.get(n.from),o=e.get(n.to);return!r||!o?{from:n.from,to:n.to,points:[],label:n.label,op:n.op}:{from:n.from,to:n.to,points:dt(r,o),label:n.label,op:n.op}})}function De(t,e,n){let r=new Set(t),o=new Set,s=new Map,i=new Map;for(let l of t)s.set(l,0),i.set(l,0);for(let l of e){let u=kt(l.from,r,n),m=kt(l.to,r,n);if(!u||!m||u===m)continue;let f=`${u}|${m}`;o.has(f)||(o.add(f),i.set(u,(i.get(u)??0)+1),s.set(m,(s.get(m)??0)+1))}if(o.size<t.length-1)return!1;let a=0,c=0;for(let l of t){let u=s.get(l)??0,m=i.get(l)??0;if(u>1||m>1)return!1;u===0&&m===1&&a++,u===1&&m===0&&c++}return a===1&&c===1}function kt(t,e,n){if(e.has(t))return t;for(let r of e)if(gt(r,t,n))return r}function gt(t,e,n){let r=n.get(t);if(!r)return!1;for(let o of r)if(o===e||gt(o,e,n))return!0;return!1}function tt(t,e){let n=new Set([t]);for(let r of e.get(t)??[])for(let o of tt(r,e))n.add(o);return n}function Ce(t,e,n,r,o,s,i,a,c){let l=Math.ceil(t.length/o);for(let u=0;u<l;u++){let m=t.slice(u*o,u*o+o),f=r[u],d=n[u];if(!(f===void 0||d===void 0))for(let y of m){let S=t.indexOf(y),b=e[S];if(!b||b.h>=d-.001)continue;let x=tt(y,s),p=new Set;for(let h of m)if(h!==y)for(let g of tt(h,s))p.add(g);let L=[];for(let h of i){let g=x.has(h.from),E=x.has(h.to);g&&p.has(h.to)?et(L,G(a.get(h.to))-G(a.get(h.from)),nt(h,!1)):E&&p.has(h.from)&&et(L,G(a.get(h.from))-G(a.get(h.to)),nt(h,!0))}if(L.length===0)continue;L.sort((h,g)=>h-g);let k=L[Math.floor(L.length/2)],_=a.get(y);if(!_)continue;let D=f-_.y,V=f+d-b.h-_.y,C=Math.min(V,Math.max(D,k));Math.abs(C)>=.001&&qt(x,C,a,c)}}}function Ie(t,e,n,r,o){let s=[];for(let c of r){let l=e.has(c.from),u=e.has(c.to),m=n.has(c.from),f=n.has(c.to);l&&f?et(s,G(o.get(c.to))-G(o.get(c.from)),nt(c,!1)):u&&m&&et(s,G(o.get(c.from))-G(o.get(c.to)),nt(c,!0))}if(s.length===0)return 0;s.sort((c,l)=>c-l);let i=s[Math.floor(s.length/2)],a=o.get(t);return a?Math.max(8-a.y,i):i}function et(t,e,n){if(Number.isFinite(e))for(let r=0;r<n;r++)t.push(e)}function nt(t,e){return(t.op==="dashed"||t.op==="dashed-arrow"?1:3)+(e?1:0)}function G(t){return t?t.y+t.h/2:Number.NaN}function qt(t,e,n,r){for(let o of t){let s=n.get(o);s&&(s.y+=e)}for(let o of r)t.has(o.id)&&(o.y+=e)}function Ge(t,e,n,r){let o=new Set(n.filter(i=>i.isContainer).map(i=>i.id)),s=t.map((i,a)=>{let c=e.get(i.from),l=e.get(i.to);if(!c||!l)return{edge:i,index:a,endpointBoundaries:[],endpointInteriorBarriers:[]};let u=Ct(i.from,i.to,e,r,o)??c,m=Ct(i.to,i.from,e,r,o)??l,f=[...u===c?[]:[c],...m===l?[]:[l]],d=[...It(i.from,i.to,r,o)?[c]:[],...It(i.to,i.from,r,o)?[l]:[]];return{edge:i,index:a,routeA:u,routeB:m,endpointBoundaries:f,endpointInteriorBarriers:d,bounds:en(i.from,i.to,r,e)}});return $e(s,n,r)}function $e(t,e,n){let r=[];for(let o of t){let s=We(o),i=r.flatMap((u,m)=>Ye(u,m)),a=r.flatMap((u,m)=>Ve(u,m)),c=i.filter(u=>u.edgeIndex!==o.index&&!je(u,o.edge)&&(!s||Jt(s,u))),l=a.filter(u=>He(u,o,s));r.push(Fe(o,[...e,...c,...l],n))}return r}function Fe(t,e,n){let{edge:r,routeA:o,routeB:s}=t;return!o||!s?{from:r.from,to:r.to,points:[],label:r.label,op:r.op}:{from:r.from,to:r.to,points:Qt(o,s,r.from,r.to,e,n,t.bounds,t.endpointBoundaries,t.endpointInteriorBarriers,r.op),label:r.label,op:r.op}}function Ye(t,e){if(t.isLifeline||t.points.length<2)return[];let n=[];return Xe(t.op)&&n.push(Dt(t,e,t.points[t.points.length-1],"end")),t.op==="bidir"&&n.push(Dt(t,e,t.points[0],"start")),n}function Xe(t){return t!=="line"&&t!=="dashed"}function Dt(t,e,n,r){let o=t.op==="thick"?5:4.2;return{id:`__arrow_guard_${e}_${r}`,x:n[0]-o,y:n[1]-o,w:o*2,h:o*2,isContainer:!1,edgeIndex:e,edgeFrom:t.from,edgeTo:t.to}}function je(t,e){return t.edgeFrom===e.from||t.edgeFrom===e.to||t.edgeTo===e.from||t.edgeTo===e.to}function Ve(t,e){if(t.points.length<2)return[];let n=[],r=2.2,o=1.2;for(let s=0;s<t.points.length-1;s++){let i=t.points[s],a=t.points[s+1];if(!(pt(i,a)<=o*2)){if(Math.abs(i[0]-a[0])<.001){let l=Math.min(i[1],a[1])+o,u=Math.max(i[1],a[1])-o;n.push({id:`__edge_guard_${e}_${s}`,x:i[0]-r,y:l,w:r*2,h:u-l,isContainer:!1,edgeIndex:e,edgeOp:t.op,orientation:"vertical"})}else if(Math.abs(i[1]-a[1])<.001){let l=Math.min(i[0],a[0])+o,u=Math.max(i[0],a[0])-o;n.push({id:`__edge_guard_${e}_${s}`,x:l,y:i[1]-r,w:u-l,h:r*2,isContainer:!1,edgeIndex:e,edgeOp:t.op,orientation:"horizontal"})}}}return n}function He(t,e,n){return t.edgeIndex===e.index?!1:!n||Jt(n,t)}function We(t){if(!t.routeA||!t.routeB)return;let e=Math.min(t.routeA.x,t.routeB.x),n=Math.min(t.routeA.y,t.routeB.y),r=Math.max(t.routeA.x+t.routeA.w,t.routeB.x+t.routeB.w),o=Math.max(t.routeA.y+t.routeA.h,t.routeB.y+t.routeB.h),s=48,i={x:e-s,y:n-s,w:r-e+s*2,h:o-n+s*2};return t.bounds?{x:Math.max(i.x,t.bounds.x),y:Math.max(i.y,t.bounds.y),w:Math.min(i.x+i.w,t.bounds.x+t.bounds.w)-Math.max(i.x,t.bounds.x),h:Math.min(i.y+i.h,t.bounds.y+t.bounds.h)-Math.max(i.y,t.bounds.y)}:i}function Jt(t,e){return t.x<e.x+e.w&&t.x+t.w>e.x&&t.y<e.y+e.h&&t.y+t.h>e.y}function Ct(t,e,n,r,o){let s=n.get(t),i=n.get(e);if(!s||!i)return;if(!rt(t,e,r))return o.has(t)?s:void 0;let a=s.x+8,c=s.x+s.w-8,l=s.y+5+8/2;return{x:Math.min(c,Math.max(a,i.x+i.w/2))-.1,y:l-.1,w:.2,h:.2}}function It(t,e,n,r){return r.has(t)&&!rt(t,e,n)}function rt(t,e,n){let r=e,o=new Set;for(;n.has(r)&&!o.has(r);)if(o.add(r),r=n.get(r),r===t)return!0;return!1}function Gt(t,e){let n=0,r=t,o=new Set;for(;e.has(r)&&!o.has(r);)o.add(r),r=e.get(r),n++;return n}function Qt(t,e,n,r,o,s,i,a=[],c=[],l="line",u=!0){let m=o.filter(h=>h.id!==n&&h.id!==r),f=m.filter(h=>!h.isContainer),d=f.filter(h=>h.id.startsWith("__arrow_guard_")),y=ze(n,r,s),S=m.filter(h=>h.isContainer&&y.has(h.id)),b=m.filter(h=>h.isContainer&&!y.has(h.id)),x=S.filter(h=>rt(h.id,n,s)!==rt(h.id,r,s)),p=a.map((h,g)=>({...h,id:`__endpoint_boundary_${g}`,isContainer:!0})),L=f.filter(h=>!h.id.startsWith("__")),k=f.filter(Zt),_=Ke(t,e);if(_&&$t(_,l)&&(!i||Wt(_,i))&&!I(_,L,.8)&&!I(_,d,.8)&&(!u||!Xt(_,k))&&!I(_,b,.8)&&!I(_,c,0)&&!mt(_,[...a,...S,...b],4))return _;let D=tn(t,e,[...m,...p],i),V=dt(t,e),C=Number.POSITIVE_INFINITY;for(let h of jt(t))for(let g of jt(e)){let E=h.point,O=g.point,A=Vt(h),w=Vt(g),B=[[E,A,[w[0],A[1]],w,O],[E,A,[A[0],w[1]],w,O]];(Math.abs(A[0]-w[0])<.001||Math.abs(A[1]-w[1])<.001)&&B.push([E,A,w,O]);for(let P of D.xs)B.push([E,A,[P,A[1]],[P,w[1]],w,O]);for(let P of D.ys)B.push([E,A,[A[0],P],[w[0],P],w,O]);for(let P of D.xs)for(let M of D.ys)B.push([E,A,[P,A[1]],[P,M],[w[0],M],w,O]),B.push([E,A,[A[0],M],[P,M],[P,w[1]],w,O]);for(let P of B){let M=rn(P);if(!on(M)||!$t(M,l)||i&&!Wt(M,i)||mt(M,[t,e],0)||I(M,c,0)||I(M,L,.8)||I(M,d,.8)||u&&Xt(M,k)||I(M,b,.8)||mt(M,[...a,...S,...b],4))continue;let R=sn(M,t,e,f,b,S,x,a)+qe(t,e,h.side,g.side,l)+h.offsetPenalty+g.offsetPenalty;R<C&&(V=M,C=R)}}return C===Number.POSITIVE_INFINITY?u&&k.length>0?Qt(t,e,n,r,o,s,i,a,c,l,!1):dt(t,e):V}function Ke(t,e){let n=t.x+t.w/2,r=t.y+t.h/2,o=e.x+e.w/2,s=e.y+e.h/2;if(Math.abs(r-s)<.001){if(t.x+t.w<=e.x)return[[t.x+t.w,r],[e.x,s]];if(e.x+e.w<=t.x)return[[t.x,r],[e.x+e.w,s]]}if(Math.abs(n-o)<.001){if(t.y+t.h<=e.y)return[[n,t.y+t.h],[o,e.y]];if(e.y+e.h<=t.y)return[[n,t.y],[o,e.y+e.h]]}}function $t(t,e){return!(t.length<2||e!=="line"&&e!=="dashed"&&Ft(t,!1)<Yt(e)||e==="bidir"&&Ft(t,!0)<Yt(e))}function Ft(t,e){if(t.length<2)return 0;let n=e?t[0]:t[t.length-1],r=e?t[1]:t[t.length-2];return pt(n,r)}function Yt(t){return t==="thick"?5.4:4.6}function I(t,e,n){for(let r=0;r<t.length-1;r++)for(let o of e)if(Y(t[r],t[r+1],o,n)>.001)return!0;return!1}function mt(t,e,n){for(let r=0;r<t.length-1;r++)for(let o of e)if(te(t[r],t[r+1],o,n)>.001)return!0;return!1}function Xt(t,e){for(let n=0;n<t.length-1;n++)for(let r of e)if(Ue(t[n],t[n+1],r))return!0;return!1}function Ue(t,e,n){let r=Math.abs(t[0]-e[0])<.001,o=Math.abs(t[1]-e[1])<.001;if(r&&n.orientation==="vertical"){let s=n.x+n.w/2;return Math.abs(t[0]-s)>n.w/2+.001?!1:H(t[1],e[1],n.y,n.y+n.h)>.001}if(o&&n.orientation==="horizontal"){let s=n.y+n.h/2;return Math.abs(t[1]-s)>n.h/2+.001?!1:H(t[0],e[0],n.x,n.x+n.w)>.001}return!1}function ze(t,e,n){return new Set([t,e,...ot(t,n),...ot(e,n)])}function jt(t){let e=t.x+t.w/2,n=t.y+t.h/2,r=Math.min(t.w*.25,Math.max(3,t.w/2-4)),o=Math.min(t.h*.25,Math.max(2,t.h/2-3)),s=9;return[{point:[t.x+t.w,n],side:"right",offsetPenalty:0},{point:[t.x,n],side:"left",offsetPenalty:0},{point:[e,t.y+t.h],side:"bottom",offsetPenalty:0},{point:[e,t.y],side:"top",offsetPenalty:0},{point:[t.x+t.w,n-o],side:"right",offsetPenalty:s},{point:[t.x+t.w,n+o],side:"right",offsetPenalty:s},{point:[t.x,n-o],side:"left",offsetPenalty:s},{point:[t.x,n+o],side:"left",offsetPenalty:s},{point:[e-r,t.y+t.h],side:"bottom",offsetPenalty:s},{point:[e+r,t.y+t.h],side:"bottom",offsetPenalty:s},{point:[e-r,t.y],side:"top",offsetPenalty:s},{point:[e+r,t.y],side:"top",offsetPenalty:s}]}function Vt(t){let[e,n]=t.point;switch(t.side){case"right":return[e+6,n];case"left":return[e-6,n];case"bottom":return[e,n+6];case"top":return[e,n-6]}}function qe(t,e,n,r,o){let s=t.x+t.w/2,i=t.y+t.h/2,a=e.x+e.w/2,c=e.y+e.h/2,l=a-s,u=c-i;return o==="dashed"?Je(n,r,l,u,t,e):Qe(n,l,u,t)+Ze(r,l,u,e)}function Je(t,e,n,r,o,s){if(Math.abs(r)>Math.min(o.h,s.h)*.8){let i=r>0?"bottom":"top",a=r>0?"top":"bottom";return Z(t,i)+Z(e,a)}if(Math.abs(n)>Math.min(o.w,s.w)*.8){let i=n>0?"right":"left",a=n>0?"left":"right";return Z(t,i)+Z(e,a)}return 0}function Z(t,e){return t===e?0:t===X(e)?1400:450}function Qe(t,e,n,r){if(Math.abs(n)>r.h*.8&&Math.abs(n)>=Math.abs(e)*.25){let o=n>0?"bottom":"top";return t===o?0:t===X(o)?800:180}if(Math.abs(e)>r.w*.5){let o=e>0?"right":"left";return t===o?0:t===X(o)?900:90}if(Math.abs(n)>r.h*.8){let o=n>0?"bottom":"top";return t===o?0:t===X(o)?600:120}return 0}function Ze(t,e,n,r){if(Math.abs(n)>r.h*.8&&Math.abs(n)>=Math.abs(e)*.25){let o=n>0?"top":"bottom";return t===o?0:t===X(o)?1400:360}if(Math.abs(e)>r.w*.5&&Math.abs(e)>=Math.abs(n)*1.2){let o=e>0?"left":"right";return t===o?0:t===X(o)?1200:220}if(Math.abs(n)>r.h*.35){let o=n>0?"top":"bottom";return t===o?0:t==="left"||t==="right"?9e3:1200}if(Math.abs(e)>r.w*.5){let o=e>0?"left":"right";return t===o?0:t===X(o)?1e3:160}return 0}function X(t){switch(t){case"right":return"left";case"left":return"right";case"bottom":return"top";case"top":return"bottom"}}function tn(t,e,n,r){let o=[t,e,...n],s=24,i=r?r.x:Math.max(8,Math.min(...o.map(f=>f.x))-s),a=r?r.x+r.w:Math.max(...o.map(f=>f.x+f.w))+s,c=r?r.y:Math.max(8,Math.min(...o.map(f=>f.y))-s),l=r?r.y+r.h:Math.max(...o.map(f=>f.y+f.h))+s,u=[(t.x+t.w/2+e.x+e.w/2)/2],m=[(t.y+t.h/2+e.y+e.h/2)/2];for(let f of o)for(let d of[12,12*1.5,24])u.push(f.x-d,f.x+f.w+d),m.push(f.y-d,f.y+f.h+d);return r&&(u.push(r.x,r.x+r.w),m.push(r.y,r.y+r.h)),{xs:Ht(Kt(u.filter(f=>f>=i&&f<=a)),(t.x+t.w/2+e.x+e.w/2)/2),ys:Ht(Kt(m.filter(f=>f>=c&&f<=l)),(t.y+t.h/2+e.y+e.h/2)/2)}}function Ht(t,e){if(t.length<=18)return t;let n=new Set([t[0],t[t.length-1]]);for(let r of[...t].sort((o,s)=>Math.abs(o-e)-Math.abs(s-e)))if(n.add(r),n.size>=18)break;return[...n].sort((r,o)=>r-o)}function en(t,e,n,r){let o=nn(t,e,n);if(!o)return;let s=r.get(o);if(!s)return;let i=12/2,a=s.y+5+8/2,c=s.y+s.h-i;return{x:s.x+i,y:a,w:Math.max(0,s.w-i*2),h:Math.max(0,c-a)}}function nn(t,e,n){let r=new Set(ot(e,n));return ot(t,n).find(o=>r.has(o))}function ot(t,e){let n=[],r=t,o=new Set;for(;e.has(r)&&!o.has(r);)o.add(r),r=e.get(r),n.push(r);return n}function Wt(t,e){let n=e.x+e.w,r=e.y+e.h;return t.every(([o,s])=>o>=e.x-.001&&o<=n+.001&&s>=e.y-.001&&s<=r+.001)}function Kt(t){let e=new Set,n=[];for(let r of t){let o=Math.round(r*1e3)/1e3,s=o.toFixed(3);e.has(s)||(e.add(s),n.push(o))}return n.sort((r,o)=>r-o)}function rn(t){let e=[];for(let r of t){let o=e[e.length-1];(!o||Math.abs(o[0]-r[0])>=.001||Math.abs(o[1]-r[1])>=.001)&&e.push(r)}let n=[];for(let r of e)for(n.push(r);n.length>=3;){let o=n[n.length-3],s=n[n.length-2],i=n[n.length-1],a=Math.abs(o[0]-s[0])<.001&&Math.abs(s[0]-i[0])<.001,c=Math.abs(o[1]-s[1])<.001&&Math.abs(s[1]-i[1])<.001;if(!a&&!c)break;n.splice(n.length-2,1)}return n}function on(t){if(t.length<2)return!1;for(let e=0;e<t.length-1;e++){let n=t[e],r=t[e+1];if(Math.abs(n[0]-r[0])>=.001&&Math.abs(n[1]-r[1])>=.001)return!1}return!0}function sn(t,e,n,r,o,s,i,a){let c=0;for(let l=0;l<t.length-1;l++){let u=t[l],m=t[l+1];c+=pt(u,m);let f=Y(u,m,e,0);f>0&&(c+=22e4+f*1500);let d=Y(u,m,n,0);d>0&&(c+=22e4+d*1500);let y=U(u,m,e);y>0&&(c+=8e3+y*120);let S=U(u,m,n);S>0&&(c+=8e3+S*120);for(let b of a){let x=U(u,m,b);x>0&&(c+=12e3+x*160)}for(let b of r){let x=Y(u,m,b,.8);x>0&&(c+=cn(b,u,m,x))}for(let b of o){let x=Y(u,m,b,.8);x>0&&(c+=18e4+x*1200);let p=U(u,m,b);p>0&&(c+=18e3+p*300)}for(let b of s){let x=Y(u,m,ln(b),.8);x>0&&(c+=14e3+x*120);let p=U(u,m,b);p>0&&(c+=6e3+p*120)}for(let b of i){let x=Y(u,m,b,0);x>0&&(c+=x*40)}}return c+=Math.max(0,t.length-2)*3,c+=an(t,e,n),c}function an(t,e,n){let o=Math.min(e.x,n.x)-60,s=Math.min(e.y,n.y)-60,i=Math.max(e.x+e.w,n.x+n.w)+60,a=Math.max(e.y+e.h,n.y+n.h)+60,c=0;for(let[l,u]of t)l<o&&(c+=(o-l)*90),l>i&&(c+=(l-i)*90),u<s&&(c+=(s-u)*90),u>a&&(c+=(u-a)*90);return c}function cn(t,e,n,r){return t.id.startsWith("__arrow_guard_")?7e4+r*700:Zt(t)?un(t,e,n,r):16e4+r*1200}function un(t,e,n,r){let o=Math.abs(e[0]-n[0])<.001,s=t.orientation==="vertical";return o===s?85e3+r*1200:500+r*160}function Zt(t){return t.id.startsWith("__edge_guard_")&&"orientation"in t&&(t.orientation==="vertical"||t.orientation==="horizontal")}function ln(t){return{x:t.x,y:t.y,w:t.w,h:13}}function pt(t,e){return Math.abs(t[0]-e[0])+Math.abs(t[1]-e[1])}function Y(t,e,n,r){let o=n.x-r,s=n.x+n.w+r,i=n.y-r,a=n.y+n.h+r;if(Math.abs(t[0]-e[0])<.001){let c=t[0];return c<=o||c>=s?0:H(t[1],e[1],i,a)}if(Math.abs(t[1]-e[1])<.001){let c=t[1];return c<=i||c>=a?0:H(t[0],e[0],o,s)}return 0}function U(t,e,n){return te(t,e,n,0)}function te(t,e,n,r){if(Math.abs(t[0]-e[0])<.001){let o=t[0];return Math.abs(o-n.x)>r+.001&&Math.abs(o-(n.x+n.w))>r+.001?0:H(t[1],e[1],n.y,n.y+n.h)}if(Math.abs(t[1]-e[1])<.001){let o=t[1];return Math.abs(o-n.y)>r+.001&&Math.abs(o-(n.y+n.h))>r+.001?0:H(t[0],e[0],n.x,n.x+n.w)}return 0}function H(t,e,n,r){let o=Math.min(t,e),s=Math.max(t,e),i=Math.min(n,r),a=Math.max(n,r);return Math.max(0,Math.min(s,a)-Math.max(o,i))}function dt(t,e){let n={x:t.x+t.w/2,y:t.y+t.h/2},r={x:e.x+e.w/2,y:e.y+e.h/2},o=r.x-n.x,s=r.y-n.y,i,a,c=t.x+t.w<=e.x||e.x+e.w<=t.x,l=t.y+t.h<=e.y||e.y+e.h<=t.y;if(c?!0:l?!1:Math.abs(o)>Math.abs(s)){if(i=[o>0?t.x+t.w:t.x,n.y],a=[o>0?e.x:e.x+e.w,r.y],Math.abs(i[1]-a[1])<.5)return[i,a];let m=(i[0]+a[0])/2;return[i,[m,i[1]],[m,a[1]],a]}else{if(i=[n.x,s>0?t.y+t.h:t.y],a=[r.x,s>0?e.y:e.y+e.h],Math.abs(i[0]-a[0])<.5)return[i,a];let m=(i[1]+a[1])/2;return[i,[i[0],m],[a[0],m],a]}}var se="http://www.w3.org/2000/svg",fn='"Hiragino Sans", "Yu Gothic", "Noto Sans CJK JP", sans-serif';var hn=[0,3.2,6.4],j=.001;function q(t,e={}){let n=e.lang??"ja",r=N("svg",{xmlns:se,viewBox:`0 0 ${t.width} ${t.height}`,"font-family":fn,"shape-rendering":"geometricPrecision"}),o=t.nodes.filter(a=>a.isContainer),s=t.nodes.filter(a=>!a.isContainer),i=[];for(let a of o)r.appendChild(mn(a,n));for(let a of t.edges)r.appendChild(pn(a,n,t,i));for(let a of t.edges)r.appendChild(xn(a));for(let a of s)r.appendChild(dn(a,n));return r}function mn(t,e){let n=N("g");n.appendChild(N("rect",{x:t.x,y:t.y,width:t.w,height:t.h,fill:"white",stroke:"#000","stroke-width":.3}));let r=St(t.label,e);if(r.length||t.id){let o=N("text",{x:t.x+2,y:t.y+3.5,"font-size":2.6,fill:"#000"});o.textContent=(t.id?t.id+" ":"")+(r[0]??""),n.appendChild(o)}return n}function dn(t,e){let n=N("g");n.appendChild(gn(t));let r=[];t.id&&t.id!=="*"&&r.push(t.id);let o=St(t.label,e);for(let l of o)r.push(l);if(r.length===0)return n;let s=2.8,i=s*1.2,a=r.length*i,c=t.y+t.h/2-a/2+i*.8;for(let l=0;l<r.length;l++){let u=N("text",{x:t.x+t.w/2,y:c+l*i,"font-size":s,fill:"#000","text-anchor":"middle"});u.textContent=r[l],n.appendChild(u)}return n}function gn(t){let e="#000",n="white";switch(t.shape){case"round":return N("rect",{x:t.x,y:t.y,width:t.w,height:t.h,rx:Math.min(t.w,t.h)/2,ry:Math.min(t.w,t.h)/2,fill:n,stroke:e,"stroke-width":.4});case"circle":{let o=Math.min(t.w,t.h)/2;return N("circle",{cx:t.x+t.w/2,cy:t.y+t.h/2,r:o,fill:"#000",stroke:e})}case"diamond":{let o=t.x+t.w/2,s=t.y+t.h/2,i=[[o,t.y],[t.x+t.w,s],[o,t.y+t.h],[t.x,s]].map(a=>a.join(",")).join(" ");return N("polygon",{points:i,fill:n,stroke:e,"stroke-width":.4})}default:return N("rect",{x:t.x,y:t.y,width:t.w,height:t.h,fill:n,stroke:e,"stroke-width":.4})}}function pn(t,e,n,r){let o=N("g");if(t.points.length<2)return o;let s=Sn(t);if(s.length<2)return o;let i=s.map((u,m)=>m===0?`M ${u[0]} ${u[1]}`:`L ${u[0]} ${u[1]}`).join(" "),a=N("path",{d:i,fill:"none",stroke:"#000","stroke-width":bn(t.op),"stroke-linecap":"butt","stroke-linejoin":"miter"}),c=yn(t.op);c&&a.setAttribute("stroke-dasharray",c),o.appendChild(a);let l=St(t.label,e);if(l.length){let u=l[0],m=l[1],f=ie(t,l,n,r);r.push(yt(f.box,.8));let d=(y,S,b,x,p,L)=>{let k=N("text",{x:S,y:b,"font-size":x,fill:"#000","text-anchor":p,"dominant-baseline":L});k.textContent=y,o.appendChild(k)};d(u,f.ja.x,f.ja.y,2.4,f.anchor,f.ja.baseline),m&&f.en&&d(m,f.en.x,f.en.y,2.1,f.anchor,f.en.baseline)}return o}function xn(t){let e=N("g");if(t.isLifeline)return e;let n=re(t,!1),r=re(t,!0);return n&&e.appendChild(n),r&&e.appendChild(r),e}function bn(t){return t==="thick"?.7:.4}function yn(t){return t==="dashed"||t==="dashed-arrow"?"1.4 1.2":null}function Sn(t){let e=t.points.map(n=>[n[0],n[1]]);return t.op!=="line"&&t.op!=="dashed"&&(e=ee(e,!1,ne(t.op))),t.op==="bidir"&&(e=ee(e,!0,ne(t.op))),e}function ee(t,e,n){if(t.length<2||n<=0)return t;let r=e?[...t].reverse():[...t],o=n;for(;r.length>=2&&o>0;){let s=r[r.length-1],i=r[r.length-2],a=i[0]-s[0],c=i[1]-s[1],l=Math.hypot(a,c);if(l<j){r.pop();continue}l>o?(r[r.length-1]=[s[0]+a/l*o,s[1]+c/l*o],o=0):(r.pop(),o-=l)}return e?r.reverse():r}function ne(t){return t==="thick"?3.4:2.8}function re(t,e){if(t.points.length<2||e&&t.op!=="bidir"||!e&&(t.op==="line"||t.op==="dashed"))return null;let n=e?t.points:[...t.points].reverse();return wn(n[0],n[1],t.op==="thick")}function wn(t,e,n){let r=e[0]-t[0],o=e[1]-t[1],s=Math.hypot(r,o);if(s<.001)return null;let i=r/s,a=o/s,c=n?3.4:2.8,l=n?1.6:1.25,u=t[0]+i*c,m=t[1]+a*c,f=-a,d=i,y=[t,[u+f*l,m+d*l],[u-f*l,m-d*l]].map(S=>S.join(",")).join(" ");return N("polygon",{points:y,fill:"#000",stroke:"#000","stroke-width":0})}function N(t,e={}){let n=document.createElementNS(se,t);for(let[r,o]of Object.entries(e))n.setAttribute(r,String(o));return n}function St(t,e){if(!t)return[];if(e==="ja")return t.ja?[t.ja]:t.en?[t.en]:[];if(e==="en")return t.en?[t.en]:t.ja?[t.ja]:[];let n=[];return t.ja&&n.push(t.ja),t.en&&n.push(t.en),n}function ie(t,e,n,r=[]){let o=[],s=Math.max(...e.map((a,c)=>st(a,c===0?2.4:2.1))),i=e.length>1?2.4+2.1+.6:2.4;for(let a=0;a<t.points.length-1;a++){let c=t.points[a],l=t.points[a+1],u=l[0]-c[0],m=l[1]-c[1];if(!(Math.abs(u)+Math.abs(m)<8))for(let d of[.5,.35,.65,.22,.78]){let y=c[0]+u*d,S=c[1]+m*d;for(let b of hn){let x=4+b,p=b*9;Math.abs(m)<j?(o.push(xt(t,n,r,e,s,i,c,l,y,S,"above",x,p)),o.push(xt(t,n,r,e,s,i,c,l,y,S,"below",x,p))):Math.abs(u)<j&&(o.push(bt(t,n,r,e,s,i,c,l,y,S,"right",x,p)),o.push(bt(t,n,r,e,s,i,c,l,y,S,"left",x,p)))}}}if(o.length===0){let[a,c]=En(t.points),l=(a[0]+c[0])/2,u=(a[1]+c[1])/2;return Math.abs(c[1]-a[1])>=Math.abs(c[0]-a[0])?bt(t,n,r,e,s,i,a,c,l,u,"right",4,0):xt(t,n,r,e,s,i,a,c,l,u,"above",4,0)}return o.sort((a,c)=>a.score-c.score),o[0]}function xt(t,e,n,r,o,s,i,a,c,l,u,m,f){let d=u==="above"?l-m-s:l+m,y={x:c-o/2,y:d,w:o,h:s},S,b;r.length>1?(S=d+2.4,b=S+.6+2.1):S=d+2.4;let x={box:y,lineA:i,lineB:a,score:0,vertical:!1,anchor:"middle",ja:{x:c,y:S,baseline:"alphabetic"},en:b===void 0?void 0:{x:c,y:b,baseline:"alphabetic"}};return x.score=ae(x,t,e,n)+(u==="above"?0:8)+f,x}function bt(t,e,n,r,o,s,i,a,c,l,u,m,f){let d=u==="right"?c+m:c-m,y={x:u==="right"?d:d-o,y:l-s/2,w:o,h:s},S=u==="right"?"start":"end",b=r.length>1?l-2.4/2-.6/2:l,x=r.length>1?l+2.1/2+.6/2:void 0,p={box:y,lineA:i,lineB:a,score:0,vertical:!0,anchor:S,ja:{x:d,y:b,baseline:"middle"},en:x===void 0?void 0:{x:d,y:x,baseline:"middle"}};return p.score=ae(p,t,e,n)+(u==="right"?0:8)+f,p}function ae(t,e,n,r){let o=t.vertical?5:0,s=t.box;s.x<0&&(o+=Math.abs(s.x)*300),s.y<0&&(o+=Math.abs(s.y)*300),s.x+s.w>n.width&&(o+=(s.x+s.w-n.width)*300),s.y+s.h>n.height&&(o+=(s.y+s.h-n.height)*300);for(let f of n.nodes)if(f.isContainer){let d={x:f.x,y:f.y,w:f.w,h:6};o+=$(s,d)*900,o+=An(s,f)*1800,o+=Mn(s,f,1.8)*9e3}else{let d=$(s,f);d>0&&(o+=1e6+d*5e4),o+=$(s,yt(f,1.8))*5200}for(let f of n.edges)for(let d=0;d<f.points.length-1;d++){let y=f.points[d],S=f.points[d+1];On(y,S,yt(s,.8))&&(o+=f===e?35:380)}for(let f of r)o+=$(s,f)*18e3;let i=Math.abs(t.lineA[0]-t.lineB[0])+Math.abs(t.lineA[1]-t.lineB[1]),a=t.vertical?s.h:s.w;o+=Math.max(0,Math.max(36,a*2.4)-i)*80;let c=t.vertical?s.y+s.h/2:s.x+s.w/2,l=t.vertical?t.lineA[1]:t.lineA[0],u=t.vertical?t.lineB[1]:t.lineB[0],m=Math.min(Math.abs(c-l),Math.abs(c-u));return o+=Math.max(0,12-m)*80,o}function st(t,e){let n=0;for(let r of t)r===" "?n+=e*.35:r.charCodeAt(0)<=127?n+=e*.58:n+=e;return Math.max(e*2,n)}function En(t){let e=0,n=-1;for(let s=0;s<t.length-1;s++){let i=t[s+1][0]-t[s][0],a=t[s+1][1]-t[s][1],c=i*i+a*a;c>n&&(n=c,e=s)}let r=t[e],o=t[e+1];return[r,o]}function yt(t,e){return{x:t.x-e,y:t.y-e,w:t.w+e*2,h:t.h+e*2}}function $(t,e){let n=Math.max(0,Math.min(t.x+t.w,e.x+e.w)-Math.max(t.x,e.x)),r=Math.max(0,Math.min(t.y+t.h,e.y+e.h)-Math.max(t.y,e.y));return n*r}function An(t,e){let n=Math.abs(t.x-e.x)<1||Math.abs(t.x+t.w-e.x)<1,r=Math.abs(t.x-(e.x+e.w))<1||Math.abs(t.x+t.w-(e.x+e.w))<1,o=Math.abs(t.y-e.y)<1||Math.abs(t.y+t.h-e.y)<1,s=Math.abs(t.y-(e.y+e.h))<1||Math.abs(t.y+t.h-(e.y+e.h))<1;return Number(n||r||o||s)}function Mn(t,e,n){return $(t,{x:e.x-n,y:e.y-n,w:n*2,h:e.h+n*2})+$(t,{x:e.x+e.w-n,y:e.y-n,w:n*2,h:e.h+n*2})+$(t,{x:e.x-n,y:e.y-n,w:e.w+n*2,h:n*2})+$(t,{x:e.x-n,y:e.y+e.h-n,w:e.w+n*2,h:n*2})}function On(t,e,n){let r=n.x,o=n.x+n.w,s=n.y,i=n.y+n.h;if(Math.abs(t[0]-e[0])<j){let a=t[0];return a<=r||a>=o?!1:oe(t[1],e[1],s,i)>j}if(Math.abs(t[1]-e[1])<j){let a=t[1];return a<=s||a>=i?!1:oe(t[0],e[0],r,o)>j}return!1}function oe(t,e,n,r){let o=Math.min(t,e),s=Math.max(t,e),i=Math.min(n,r),a=Math.max(n,r);return Math.max(0,Math.min(s,a)-Math.max(o,i))}var Pn="http://www.w3.org/2000/svg",it=class{constructor(e,n){this.namespaceURI=e;this.tagName=n}namespaceURI;tagName;nodeType="element";attrs=new Map;children=[];text=null;setAttribute(e,n){this.attrs.set(e,String(n))}getAttribute(e){return this.attrs.has(e)?this.attrs.get(e):null}removeAttribute(e){this.attrs.delete(e)}appendChild(e){return this.text=null,this.children.push(e),e}set textContent(e){this.text=e==null?"":String(e),this.children=[]}get textContent(){return this.text!=null?this.text:this.children.map(e=>e.textContent).join("")}},Bn={createElementNS(t,e){return new it(t,e)},createElement(t){return new it(Pn,t)}};function ce(t){let e=globalThis,n=e.document;e.document=Bn;try{return t()}finally{e.document=n}}function Ln(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function Rn(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function J(t){let e=[...t.attrs].map(([r,o])=>` ${r}="${Ln(o)}"`).join("");if(t.text!=null)return`<${t.tagName}${e}>${Rn(t.text)}</${t.tagName}>`;if(t.children.length===0)return`<${t.tagName}${e}/>`;let n=t.children.map(J).join("");return`<${t.tagName}${e}>${n}</${t.tagName}>`}var _n=3;function T(t,e,n=0){let r=t.getAttribute(e);if(r==null)return n;let o=Number.parseFloat(r);return Number.isFinite(o)?o:n}function wt(t){let e=[],n=/-?\d*\.?\d+(?:e[-+]?\d+)?/gi,r;for(;(r=n.exec(t))!==null;)e.push(Number.parseFloat(r[0]));return e}function Nn(t){switch(t.tagName){case"rect":{let e=T(t,"x"),n=T(t,"y"),r=T(t,"width"),o=T(t,"height");return r<=0&&o<=0?null:{minX:e,minY:n,maxX:e+r,maxY:n+o}}case"circle":{let e=T(t,"cx"),n=T(t,"cy"),r=T(t,"r");return r<=0?null:{minX:e-r,minY:n-r,maxX:e+r,maxY:n+r}}case"line":{let e=T(t,"x1"),n=T(t,"y1"),r=T(t,"x2"),o=T(t,"y2");return{minX:Math.min(e,r),minY:Math.min(n,o),maxX:Math.max(e,r),maxY:Math.max(n,o)}}case"polygon":case"polyline":{let e=wt(t.getAttribute("points")??"");return ue(e)}case"path":{let e=wt(t.getAttribute("d")??"");return ue(e)}case"text":return vn(t);default:return null}}function ue(t){if(t.length<2)return null;let e=1/0,n=1/0,r=-1/0,o=-1/0;for(let s=0;s+1<t.length;s+=2){let i=t[s],a=t[s+1];i<e&&(e=i),a<n&&(n=a),i>r&&(r=i),a>o&&(o=a)}return Number.isFinite(e)?{minX:e,minY:n,maxX:r,maxY:o}:null}function vn(t){let e=t.textContent;if(!e)return null;let n=T(t,"x"),r=T(t,"y"),o=T(t,"font-size",2.8),s=st(e,o),i=t.getAttribute("text-anchor")??"start",a;i==="middle"?a=n-s/2:i==="end"?a=n-s:a=n;let c=a+s,l=t.getAttribute("dominant-baseline")??"alphabetic",u,m;return l==="middle"||l==="central"?(u=r-o*.6,m=r+o*.6):(u=r-o*.8,m=r+o*.25),{minX:a,minY:u,maxX:c,maxY:m}}function le(t,e){let n=Nn(t);n&&e.push(n);for(let r of t.children)le(r,e)}function Tn(t){if(t.length===0)return null;let e=1/0,n=1/0,r=-1/0,o=-1/0;for(let s of t)s.minX<e&&(e=s.minX),s.minY<n&&(n=s.minY),s.maxX>r&&(r=s.maxX),s.maxY>o&&(o=s.maxY);return{minX:e,minY:n,maxX:r,maxY:o}}function kn(t){let e=wt(t.getAttribute("viewBox")??"");return e.length===4&&e[2]>0&&e[3]>0?{minX:e[0],minY:e[1],width:e[2],height:e[3]}:{minX:0,minY:0,width:210,height:297}}function at(t,e=_n){let n=[];le(t,n);let r=Tn(n);if(!r)return kn(t);let o=Math.max(0,e);return{minX:r.minX-o,minY:r.minY-o,width:Math.max(1,r.maxX-r.minX+o*2),height:Math.max(1,r.maxY-r.minY+o*2)}}function ct(t,e=1600){let n=Math.max(t.width,t.height,1),r=Math.max(1,e/n);return{width:Math.max(1,Math.ceil(t.width*r)),height:Math.max(1,Math.ceil(t.height*r)),scale:r}}var Dn="http://www.w3.org/2000/svg",Cn=3,In=1600;function Et(t,e={}){let{lang:n="ja",crop:r=!0,bleed:o=Cn,targetSide:s=In}=e,i=F(t),a=z(i),c=ce(()=>q(a,{lang:n})),l=r?at(c,o):{minX:0,minY:0,width:a.width,height:a.height},u=ct(l,s);return c.setAttribute("xmlns",Dn),c.setAttribute("version","1.1"),c.setAttribute("viewBox",`${l.minX} ${l.minY} ${l.width} ${l.height}`),c.setAttribute("width",String(u.width)),c.setAttribute("height",String(u.height)),c.setAttribute("preserveAspectRatio","xMidYMid meet"),{el:c,kind:i.kind,viewBox:l,width:u.width,height:u.height}}function ut(t,e={}){let n=Et(t,e),r=J(n.el);return{svg:e.xmlDeclaration??!0?`<?xml version="1.0" encoding="UTF-8"?>
2
- ${r}`:r,kind:n.kind,viewBox:n.viewBox,width:n.width,height:n.height}}function Mt(t){let e=fe(t),n=[];n.push("## \u7B26\u53F7\u306E\u8AAC\u660E / Reference Signs"),n.push(""),n.push("| \u7B26\u53F7 | \u540D\u79F0(\u65E5\u672C\u8A9E) | Name (English) |"),n.push("|------|---------------|----------------|");for(let r of e){let o=t.nodes.get(r);n.push(`| ${r} | ${o.label.ja??""} | ${o.label.en??""} |`)}return n.join(`
3
- `)}function Ot(t){let e=fe(t),n=["id,ja,en"];for(let r of e){let o=t.nodes.get(r);n.push(`${At(r)},${At(o.label.ja??"")},${At(o.label.en??"")}`)}return n.join(`
4
- `)}function At(t){return/[",\n]/.test(t)?'"'+t.replace(/"/g,'""')+'"':t}function fe(t){return[...t.nodes.keys()].filter(n=>n!=="*").sort((n,r)=>{let o=parseInt(n,10),s=parseInt(r,10);return!isNaN(o)&&!isNaN(s)?o-s:n.localeCompare(r)})}var Pt=["block","system","iot","imagePipeline","controlLoop","flow","state","seq","handshake"],Bt={block:{label:"\u30D6\u30ED\u30C3\u30AF\u56F3",hint:"\u300C:\u300D\u3092\u4F7F\u3046\u3068\u30D6\u30ED\u30C3\u30AF\u56F3\u306B\u306A\u308B",source:`# \u30D6\u30ED\u30C3\u30AF\u56F3(\u88C5\u7F6E\u30AF\u30EC\u30FC\u30E0\u7528)
1
+ "use strict";var pdgkit=(()=>{var lt=Object.defineProperty;var ye=Object.getOwnPropertyDescriptor;var Se=Object.getOwnPropertyNames;var we=Object.prototype.hasOwnProperty;var Ee=(t,e)=>{for(var n in e)lt(t,n,{get:e[n],enumerable:!0})},Ae=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Se(e))!we.call(t,o)&&o!==n&&lt(t,o,{get:()=>e[o],enumerable:!(r=ye(e,o))||r.enumerable});return t};var Me=t=>Ae(lt({},"__esModule",{value:!0}),t);var qn={};Ee(qn,{PATTERN_SOURCE:()=>Bt,SAMPLES:()=>Lt,SAMPLE_ORDER:()=>Pt,VERSION:()=>Un,buildSvgModel:()=>Et,computeContentBox:()=>at,layout:()=>z,parse:()=>Y,refsToCsv:()=>Ot,refsToMarkdown:()=>Mt,render:()=>q,renderToSvg:()=>ut,serializeSvg:()=>J,svgDisplayDimensions:()=>ct,toSvgString:()=>zn,validate:()=>ge});var K=/[A-Za-z0-9_*]+/.source,vt=[{lit:"<->",kind:"bidir"},{lit:"=>",kind:"thick"},{lit:"->",kind:"arrow"},{lit:"<-",kind:"arrow",reverse:!0},{lit:".>",kind:"dashed-arrow"},{lit:"..",kind:"dashed"},{lit:"-",kind:"line"}],Oe=new RegExp(`^(${K})\\s*=(?!>)\\s*(.*)$`),Pe=new RegExp(`^(${K})\\s+(<->|=>|->|<-|\\.>|\\.\\.|-)\\s+(${K})\\s*(?::\\s*(.*))?$`),Le=new RegExp(`^(${K})\\s*:\\s*(.+)$`);function Y(t){let e={nodes:new Map,containments:[],edges:[],diagnostics:[],kind:"block"},n=t.split(/\r?\n/);for(let r=0;r<n.length;r++){let s=Tt(n[r]).trim();s&&(Be(s,r+1,e)||_e(s,r+1,e)||Re(s,r+1,e)||e.diagnostics.push({severity:"error",line:r+1,col:1,message:`\u69CB\u6587\u4E0D\u660E: "${s}"`}))}return e.kind=ve(e),e}function Be(t,e,n){let r=t.match(Oe);if(!r)return!1;let o=r[1],s=r[2],i=ht(s);return n.nodes.get(o)&&n.diagnostics.push({severity:"warning",line:e,col:1,message:`\u7B26\u53F7 "${o}" \u306F\u518D\u5B9A\u7FA9\u3055\u308C\u307E\u3057\u305F`}),n.nodes.set(o,{id:o,label:i,implicit:!1}),!0}function _e(t,e,n){let r=t.match(Pe);if(!r)return!1;let o=r[2],s=vt.find(u=>u.lit===o),i=s.reverse?r[3]:r[1],a=s.reverse?r[1]:r[3],c=r[4]??"",l={from:i,to:a,op:s.kind,label:c.trim()?ht(c):void 0,line:e};return n.edges.push(l),Q(n,i),Q(n,a),!0}function Re(t,e,n){let r=t.match(Le);if(!r)return!1;let o=r[1],i=r[2].trim().split(/\s+/).filter(Boolean),a=new RegExp(`^${K}$`);for(let c of i)if(!a.test(c))return n.diagnostics.push({severity:"error",line:e,col:1,message:`\u5305\u542B\u306E\u5B50\u3068\u3057\u3066\u4E0D\u6B63\u306A\u30C8\u30FC\u30AF\u30F3: "${c}"`}),!0;n.containments.push({parent:o,children:i,line:e}),Q(n,o);for(let c of i)Q(n,c);return!0}function Q(t,e){let n=t.nodes.get(e);return n||(n={id:e,label:{},implicit:!0},t.nodes.set(e,n),n)}function Tt(t){let e=!1;for(let n=0;n<t.length;n++){let r=t[n];if(r==='"')e=!e;else if(!e&&r==="#")return t.slice(0,n)}return t}function ht(t){let e=t.trim();if(!e)return{};let n=Ne(e);return n===-1?{ja:ft(e)}:{ja:ft(e.slice(0,n).trim()),en:ft(e.slice(n+1).trim())}}function Ne(t){let e=!1;for(let n=0;n<t.length;n++){if(t[n]==='"'){e=!e;continue}if(!e&&t[n]==="/"&&Nt(t[n-1])&&Nt(t[n+1]))return n}return-1}function Nt(t){return t===" "||t===" "}function ft(t){return t.length>=2&&t.startsWith('"')&&t.endsWith('"')?t.slice(1,-1):t}function ve(t){if(t.containments.length>0)return"block";for(let n of t.nodes.values()){let r=n.label.ja??"",o=n.label.en??"";if(r.endsWith("?")||o.endsWith("?"))return"flow"}if(t.nodes.has("*"))return"state";let e=new Set;for(let n of t.edges){if(n.op==="bidir")return"seq";let r=`${n.from}|${n.to}`,o=`${n.to}|${n.from}`;if(e.has(o))return"seq";e.add(r)}return"flow"}function z(t){switch(t.kind){case"block":return Te(t);case"flow":return ke(t);case"state":return De(t);case"seq":return Ce(t)}}function Te(t){let e=new Map;for(let f of t.containments)e.set(f.parent,f.children);let n=new Map;for(let f of t.containments)for(let g of f.children)n.set(g,f.parent);let r=new Set;for(let f of e.values())for(let g of f)r.add(g);let s=[...t.nodes.keys()].filter(f=>!r.has(f)),i=[],a=new Map;function c(f){let g=e.get(f);if(!g||g.length===0)return{w:36,h:14};let A=g.map(c);if(l(f)==="grid"){let w=u(f,g),B=Math.max(...A.map(O=>O.w)),L=d(A,w);return{w:w*B+16+(w-1)*24,h:m(f)+L.reduce((O,_)=>O+_,0)+16+Math.max(0,L.length-1)*24}}let P=Math.max(...A.map(w=>w.w)),M=A.reduce((w,B)=>w+B.h,0);return{w:P+16,h:m(f)+M+16+(g.length-1)*24}}function l(f){let g=e.get(f);return!g||g.length<=2?"stack":h(f,g)?"grid":Ge(g,t.edges,e)?"stack":"grid"}function u(f,g){return h(f,g)?Math.min(g.length,4):g.length>=7?4:g.length>=5?3:2}function d(f,g){let A=Math.ceil(f.length/g),P=[];for(let M=0;M<A;M++){let w=f.slice(M*g,M*g+g);P.push(Math.max(...w.map(B=>B.h)))}return P}function m(f){let g=e.get(f);return g&&h(f,g)?19:5}function h(f,g){return t.edges.some(A=>A.from===f&&g.some(P=>P===A.to||gt(P,A.to,e)))}function x(f,g,A){let P=c(f);a.set(f,{x:g,y:A,w:P.w,h:P.h});let M=t.nodes.get(f),w=e.get(f);if(!w||w.length===0){i.push({id:f,x:g,y:A,w:P.w,h:P.h,label:M?.label??{},shape:"box",isContainer:!1});return}let B=w.map(c);if(l(f)==="grid"){let L=u(f,w),O=Math.max(...B.map(T=>T.w)),_=d(B,L),W=[],_t=A+m(f)+8;for(let T=0;T<_.length;T++)W[T]=_t,_t+=_[T]+24;for(let T=0;T<w.length;T++){let Rt=Math.floor(T/L),xe=T%L,pe=g+8+xe*(O+24)+(O-B[T].w)/2,be=W[Rt]+(_[Rt]-B[T].h)/2;x(w[T],pe,be)}$e(w,B,_,W,L,e,t.edges,a,i)}else{let L=Math.max(...B.map(_=>_.w)),O=A+m(f)+8;for(let _=0;_<w.length;_++){let W=g+8+(L-B[_].w)/2;x(w[_],W,O),O+=B[_].h+24}}i.push({id:f,x:g,y:A,w:P.w,h:P.h,label:M?.label??{},shape:"box",isContainer:!0})}let y=8;for(let f of s){let g=new Set(a.keys());x(f,y,8);let A=tt(f,e),P=Ye(f,A,g,t.edges,a);Math.abs(P)>=.001&&Qt(A,P,a,i);let M=c(f);y+=M.w+32}let b=Fe(t.edges,a,i,n);i.sort((f,g)=>{if(f.isContainer!==g.isContainer)return f.isContainer?-1:1;if(f.isContainer&&g.isContainer){let A=$t(f.id,n)-$t(g.id,n);return A!==0?A:g.w*g.h-f.w*f.h}return f.y-g.y||f.x-g.x});let p=[...a.values()],E=b.flatMap(f=>f.points),S=p.length?Math.max(...p.map(f=>f.x+f.w)):8,v=p.length?Math.max(...p.map(f=>f.y+f.h)):8,R=E.length?Math.max(...E.map(([f])=>f)):8,D=E.length?Math.max(...E.map(([,f])=>f)):8,X=Math.max(S,R)+8,C=Math.max(v,D)+8;return{nodes:i,edges:b,width:X,height:C,kind:"block"}}function ke(t){let e=[...t.nodes.keys()],{byRank:n}=zt(t);function r(h){let x=t.nodes.get(h),y=x?.label.ja??"",b=x?.label.en??"";if(y.endsWith("?")||b.endsWith("?"))return"diamond";let p=t.edges.filter(S=>S.to===h).length,E=t.edges.filter(S=>S.from===h).length;return p===0||E===0?"round":"box"}let o=14,s=10,i=new Map,a=[...n.keys()].sort((h,x)=>h-x),c=8;for(let h of a){let x=n.get(h),y=x.map(S=>r(S)==="diamond"?36*1.2:36),b=y.reduce((S,v)=>S+v,0)+(x.length-1)*s,p=8;p=(Math.max(b+16,200)-b)/2;for(let S=0;S<x.length;S++)i.set(x[S],{x:p,y:c,w:y[S],h:14}),p+=y[S]+s;c+=14+o}for(let h of e)i.has(h)||(i.set(h,{x:8,y:c,w:36,h:14}),c+=14+o);let l=[];for(let[h,x]of i)l.push({id:h,...x,label:t.nodes.get(h)?.label??{},shape:r(h),isContainer:!1});let u=qt(t.edges,i),{width:d,height:m}=Jt(l,u);return{nodes:l,edges:u,width:d,height:m,kind:"flow"}}function De(t){let{byRank:e}=zt(t);function n(m){return m==="*"?"circle":"round"}let r=14,o=10,s=new Map,i=[...e.keys()].sort((m,h)=>m-h),a=8;for(let m of i){let h=e.get(m),x=h.map(S=>n(S)==="circle"?6:36),y=h.map(S=>n(S)==="circle"?6:14),b=x.reduce((S,v)=>S+v,0)+(h.length-1)*o,E=(Math.max(b+16,200)-b)/2;for(let S=0;S<h.length;S++)s.set(h[S],{x:E,y:a+(14-y[S])/2,w:x[S],h:y[S]}),E+=x[S]+o;a+=14+r}for(let m of t.nodes.keys())s.has(m)||(s.set(m,{x:8,y:a,w:36,h:14}),a+=14+r);let c=[];for(let[m,h]of s)c.push({id:m,...h,label:t.nodes.get(m)?.label??{},shape:n(m),isContainer:!1});let l=qt(t.edges,s),{width:u,height:d}=Jt(c,l);return{nodes:c,edges:l,width:u,height:d,kind:"state"}}function Ce(t){let e=new Set,n=[];for(let h of t.edges)for(let x of[h.from,h.to])e.has(x)||(e.add(x),n.push(x));for(let h of t.nodes.keys())e.has(h)||(e.add(h),n.push(h));let r=40,o=12,s=28,i=12,a=new Map,c=[],l=8;for(let h of n)a.set(h,l+r/2),c.push({id:h,x:l,y:8,w:r,h:o,label:t.nodes.get(h)?.label??{},shape:"actor",isContainer:!1}),l+=r+s;let u=8+o+i,d=[];for(let h of t.edges){let x=a.get(h.from),y=a.get(h.to);x===void 0||y===void 0||(d.push({from:h.from,to:h.to,points:[[x,u],[y,u]],label:h.label,op:h.op}),u+=i)}let m=n.map(h=>({from:h,to:h,points:[[a.get(h),8+o],[a.get(h),u+i]],op:"dashed",isLifeline:!0}));return{nodes:c,edges:[...m,...d],width:l,height:u+16,kind:"seq"}}function zt(t){let e=[...t.nodes.keys()],n=new Map;for(let u of e)n.set(u,0);for(let u of t.edges)n.set(u.to,(n.get(u.to)??0)+1);let r=new Map;for(let u of t.edges)r.has(u.from)||r.set(u.from,[]),r.get(u.from).push(u.to);let o=e.filter(u=>n.get(u)===0);o.length===0&&(o=e.includes("*")?["*"]:e.length?[e[0]]:[]);let s=new Map,i=new Set;for(let u of o)s.set(u,0),i.add(u);let a=[...o];for(;a.length;){let u=[];for(let d of a){let m=s.get(d);for(let h of r.get(d)??[])i.has(h)||(s.set(h,m+1),i.add(h),u.push(h))}a=u}let c=0;for(let u of s.values())u>c&&(c=u);for(let u of e)i.has(u)||(c++,s.set(u,c),i.add(u));let l=new Map;for(let[u,d]of s)l.has(d)||l.set(d,[]),l.get(d).push(u);return{byRank:l}}function qt(t,e){let n=[...e.values()],r=n.length?Math.max(...n.map(i=>i.x+i.w)):8,o=t.map((i,a)=>({index:a,a:e.get(i.from),b:e.get(i.to)})).filter(i=>!!i.a&&!!i.b&&i.b.y+i.b.h<=i.a.y+.001).sort((i,a)=>kt(i.a,i.b)-kt(a.a,a.b)),s=new Map;return o.forEach((i,a)=>s.set(i.index,a)),t.map((i,a)=>{let c=e.get(i.from),l=e.get(i.to);if(!c||!l)return{from:i.from,to:i.to,points:[],label:i.label,op:i.op};let u=s.get(a),d=u===void 0?dt(c,l):Ie(c,l,r+10+u*7);return{from:i.from,to:i.to,points:d,label:i.label,op:i.op}})}function kt(t,e){return t.y+t.h/2-(e.y+e.h/2)}function Ie(t,e,n){let r=t.y+t.h/2,o=e.y+e.h/2;return[[t.x+t.w,r],[n,r],[n,o],[e.x+e.w,o]]}function Jt(t,e){let n=8,r=8;for(let o of t)o.x+o.w>n&&(n=o.x+o.w),o.y+o.h>r&&(r=o.y+o.h);for(let o of e)for(let[s,i]of o.points)s>n&&(n=s),i>r&&(r=i);return{width:n+8,height:r+8}}function Ge(t,e,n){let r=new Set(t),o=new Set,s=new Map,i=new Map;for(let l of t)s.set(l,0),i.set(l,0);for(let l of e){let u=Dt(l.from,r,n),d=Dt(l.to,r,n);if(!u||!d||u===d)continue;let m=`${u}|${d}`;o.has(m)||(o.add(m),i.set(u,(i.get(u)??0)+1),s.set(d,(s.get(d)??0)+1))}if(o.size<t.length-1)return!1;let a=0,c=0;for(let l of t){let u=s.get(l)??0,d=i.get(l)??0;if(u>1||d>1)return!1;u===0&&d===1&&a++,u===1&&d===0&&c++}return a===1&&c===1}function Dt(t,e,n){if(e.has(t))return t;for(let r of e)if(gt(r,t,n))return r}function gt(t,e,n){let r=n.get(t);if(!r)return!1;for(let o of r)if(o===e||gt(o,e,n))return!0;return!1}function tt(t,e){let n=new Set([t]);for(let r of e.get(t)??[])for(let o of tt(r,e))n.add(o);return n}function $e(t,e,n,r,o,s,i,a,c){let l=Math.ceil(t.length/o);for(let u=0;u<l;u++){let d=t.slice(u*o,u*o+o),m=r[u],h=n[u];if(!(m===void 0||h===void 0))for(let x of d){let y=t.indexOf(x),b=e[y];if(!b||b.h>=h-.001)continue;let p=tt(x,s),E=new Set;for(let f of d)if(f!==x)for(let g of tt(f,s))E.add(g);let S=[];for(let f of i){let g=p.has(f.from),A=p.has(f.to);g&&E.has(f.to)?et(S,G(a.get(f.to))-G(a.get(f.from)),nt(f,!1)):A&&E.has(f.from)&&et(S,G(a.get(f.from))-G(a.get(f.to)),nt(f,!0))}if(S.length===0)continue;S.sort((f,g)=>f-g);let v=S[Math.floor(S.length/2)],R=a.get(x);if(!R)continue;let D=m-R.y,X=m+h-b.h-R.y,C=Math.min(X,Math.max(D,v));Math.abs(C)>=.001&&Qt(p,C,a,c)}}}function Ye(t,e,n,r,o){let s=[];for(let c of r){let l=e.has(c.from),u=e.has(c.to),d=n.has(c.from),m=n.has(c.to);l&&m?et(s,G(o.get(c.to))-G(o.get(c.from)),nt(c,!1)):u&&d&&et(s,G(o.get(c.from))-G(o.get(c.to)),nt(c,!0))}if(s.length===0)return 0;s.sort((c,l)=>c-l);let i=s[Math.floor(s.length/2)],a=o.get(t);return a?Math.max(8-a.y,i):i}function et(t,e,n){if(Number.isFinite(e))for(let r=0;r<n;r++)t.push(e)}function nt(t,e){return(t.op==="dashed"||t.op==="dashed-arrow"?1:3)+(e?1:0)}function G(t){return t?t.y+t.h/2:Number.NaN}function Qt(t,e,n,r){for(let o of t){let s=n.get(o);s&&(s.y+=e)}for(let o of r)t.has(o.id)&&(o.y+=e)}function Fe(t,e,n,r){let o=new Set(n.filter(i=>i.isContainer).map(i=>i.id)),s=t.map((i,a)=>{let c=e.get(i.from),l=e.get(i.to);if(!c||!l)return{edge:i,index:a,endpointBoundaries:[],endpointInteriorBarriers:[]};let u=It(i.from,i.to,e,r,o)??c,d=It(i.to,i.from,e,r,o)??l,m=[...u===c?[]:[c],...d===l?[]:[l]],h=[...Gt(i.from,i.to,r,o)?[c]:[],...Gt(i.to,i.from,r,o)?[l]:[]];return{edge:i,index:a,routeA:u,routeB:d,endpointBoundaries:m,endpointInteriorBarriers:h,bounds:on(i.from,i.to,r,e)}});return je(s,n,r)}function je(t,e,n){let r=[];for(let o of t){let s=ze(o),i=r.flatMap((u,d)=>Xe(u,d)),a=r.flatMap((u,d)=>Ke(u,d)),c=i.filter(u=>u.edgeIndex!==o.index&&!We(u,o.edge)&&(!s||Zt(s,u))),l=a.filter(u=>Ue(u,o,s));r.push(Ve(o,[...e,...c,...l],n))}return r}function Ve(t,e,n){let{edge:r,routeA:o,routeB:s}=t;return!o||!s?{from:r.from,to:r.to,points:[],label:r.label,op:r.op}:{from:r.from,to:r.to,points:te(o,s,r.from,r.to,e,n,t.bounds,t.endpointBoundaries,t.endpointInteriorBarriers,r.op),label:r.label,op:r.op}}function Xe(t,e){if(t.isLifeline||t.points.length<2)return[];let n=[];return He(t.op)&&n.push(Ct(t,e,t.points[t.points.length-1],"end")),t.op==="bidir"&&n.push(Ct(t,e,t.points[0],"start")),n}function He(t){return t!=="line"&&t!=="dashed"}function Ct(t,e,n,r){let o=t.op==="thick"?5:4.2;return{id:`__arrow_guard_${e}_${r}`,x:n[0]-o,y:n[1]-o,w:o*2,h:o*2,isContainer:!1,edgeIndex:e,edgeFrom:t.from,edgeTo:t.to}}function We(t,e){return t.edgeFrom===e.from||t.edgeFrom===e.to||t.edgeTo===e.from||t.edgeTo===e.to}function Ke(t,e){if(t.points.length<2)return[];let n=[],r=2.2,o=1.2;for(let s=0;s<t.points.length-1;s++){let i=t.points[s],a=t.points[s+1];if(!(xt(i,a)<=o*2)){if(Math.abs(i[0]-a[0])<.001){let l=Math.min(i[1],a[1])+o,u=Math.max(i[1],a[1])-o;n.push({id:`__edge_guard_${e}_${s}`,x:i[0]-r,y:l,w:r*2,h:u-l,isContainer:!1,edgeIndex:e,edgeOp:t.op,orientation:"vertical"})}else if(Math.abs(i[1]-a[1])<.001){let l=Math.min(i[0],a[0])+o,u=Math.max(i[0],a[0])-o;n.push({id:`__edge_guard_${e}_${s}`,x:l,y:i[1]-r,w:u-l,h:r*2,isContainer:!1,edgeIndex:e,edgeOp:t.op,orientation:"horizontal"})}}}return n}function Ue(t,e,n){return t.edgeIndex===e.index?!1:!n||Zt(n,t)}function ze(t){if(!t.routeA||!t.routeB)return;let e=Math.min(t.routeA.x,t.routeB.x),n=Math.min(t.routeA.y,t.routeB.y),r=Math.max(t.routeA.x+t.routeA.w,t.routeB.x+t.routeB.w),o=Math.max(t.routeA.y+t.routeA.h,t.routeB.y+t.routeB.h),s=48,i={x:e-s,y:n-s,w:r-e+s*2,h:o-n+s*2};return t.bounds?{x:Math.max(i.x,t.bounds.x),y:Math.max(i.y,t.bounds.y),w:Math.min(i.x+i.w,t.bounds.x+t.bounds.w)-Math.max(i.x,t.bounds.x),h:Math.min(i.y+i.h,t.bounds.y+t.bounds.h)-Math.max(i.y,t.bounds.y)}:i}function Zt(t,e){return t.x<e.x+e.w&&t.x+t.w>e.x&&t.y<e.y+e.h&&t.y+t.h>e.y}function It(t,e,n,r,o){let s=n.get(t),i=n.get(e);if(!s||!i)return;if(!rt(t,e,r))return o.has(t)?s:void 0;let a=s.x+8,c=s.x+s.w-8,l=s.y+5+8/2;return{x:Math.min(c,Math.max(a,i.x+i.w/2))-.1,y:l-.1,w:.2,h:.2}}function Gt(t,e,n,r){return r.has(t)&&!rt(t,e,n)}function rt(t,e,n){let r=e,o=new Set;for(;n.has(r)&&!o.has(r);)if(o.add(r),r=n.get(r),r===t)return!0;return!1}function $t(t,e){let n=0,r=t,o=new Set;for(;e.has(r)&&!o.has(r);)o.add(r),r=e.get(r),n++;return n}function te(t,e,n,r,o,s,i,a=[],c=[],l="line",u=!0){let d=o.filter(f=>f.id!==n&&f.id!==r),m=d.filter(f=>!f.isContainer),h=m.filter(f=>f.id.startsWith("__arrow_guard_")),x=Qe(n,r,s),y=d.filter(f=>f.isContainer&&x.has(f.id)),b=d.filter(f=>f.isContainer&&!x.has(f.id)),p=y.filter(f=>rt(f.id,n,s)!==rt(f.id,r,s)),E=a.map((f,g)=>({...f,id:`__endpoint_boundary_${g}`,isContainer:!0})),S=m.filter(f=>!f.id.startsWith("__")),v=m.filter(ee),R=qe(t,e);if(R&&Yt(R,l)&&(!i||Kt(R,i))&&!I(R,S,.8)&&!I(R,h,.8)&&(!u||!Vt(R,v))&&!I(R,b,.8)&&!I(R,c,0)&&!mt(R,[...a,...y,...b],4))return R;let D=rn(t,e,[...d,...E],i),X=dt(t,e),C=Number.POSITIVE_INFINITY;for(let f of Xt(t))for(let g of Xt(e)){let A=f.point,P=g.point,M=Ht(f),w=Ht(g),B=[[A,M,[w[0],M[1]],w,P],[A,M,[M[0],w[1]],w,P]];(Math.abs(M[0]-w[0])<.001||Math.abs(M[1]-w[1])<.001)&&B.push([A,M,w,P]);for(let L of D.xs)B.push([A,M,[L,M[1]],[L,w[1]],w,P]);for(let L of D.ys)B.push([A,M,[M[0],L],[w[0],L],w,P]);for(let L of D.xs)for(let O of D.ys)B.push([A,M,[L,M[1]],[L,O],[w[0],O],w,P]),B.push([A,M,[M[0],O],[L,O],[L,w[1]],w,P]);for(let L of B){let O=an(L);if(!cn(O)||!Yt(O,l)||i&&!Kt(O,i)||mt(O,[t,e],0)||I(O,c,0)||I(O,S,.8)||I(O,h,.8)||u&&Vt(O,v)||I(O,b,.8)||mt(O,[...a,...y,...b],4))continue;let _=un(O,t,e,m,b,y,p,a)+Ze(t,e,f.side,g.side,l)+f.offsetPenalty+g.offsetPenalty;_<C&&(X=O,C=_)}}return C===Number.POSITIVE_INFINITY?u&&v.length>0?te(t,e,n,r,o,s,i,a,c,l,!1):dt(t,e):X}function qe(t,e){let n=t.x+t.w/2,r=t.y+t.h/2,o=e.x+e.w/2,s=e.y+e.h/2;if(Math.abs(r-s)<.001){if(t.x+t.w<=e.x)return[[t.x+t.w,r],[e.x,s]];if(e.x+e.w<=t.x)return[[t.x,r],[e.x+e.w,s]]}if(Math.abs(n-o)<.001){if(t.y+t.h<=e.y)return[[n,t.y+t.h],[o,e.y]];if(e.y+e.h<=t.y)return[[n,t.y],[o,e.y+e.h]]}}function Yt(t,e){return!(t.length<2||e!=="line"&&e!=="dashed"&&Ft(t,!1)<jt(e)||e==="bidir"&&Ft(t,!0)<jt(e))}function Ft(t,e){if(t.length<2)return 0;let n=e?t[0]:t[t.length-1],r=e?t[1]:t[t.length-2];return xt(n,r)}function jt(t){return t==="thick"?5.4:4.6}function I(t,e,n){for(let r=0;r<t.length-1;r++)for(let o of e)if(F(t[r],t[r+1],o,n)>.001)return!0;return!1}function mt(t,e,n){for(let r=0;r<t.length-1;r++)for(let o of e)if(ne(t[r],t[r+1],o,n)>.001)return!0;return!1}function Vt(t,e){for(let n=0;n<t.length-1;n++)for(let r of e)if(Je(t[n],t[n+1],r))return!0;return!1}function Je(t,e,n){let r=Math.abs(t[0]-e[0])<.001,o=Math.abs(t[1]-e[1])<.001;if(r&&n.orientation==="vertical"){let s=n.x+n.w/2;return Math.abs(t[0]-s)>n.w/2+.001?!1:H(t[1],e[1],n.y,n.y+n.h)>.001}if(o&&n.orientation==="horizontal"){let s=n.y+n.h/2;return Math.abs(t[1]-s)>n.h/2+.001?!1:H(t[0],e[0],n.x,n.x+n.w)>.001}return!1}function Qe(t,e,n){return new Set([t,e,...ot(t,n),...ot(e,n)])}function Xt(t){let e=t.x+t.w/2,n=t.y+t.h/2,r=Math.min(t.w*.25,Math.max(3,t.w/2-4)),o=Math.min(t.h*.25,Math.max(2,t.h/2-3)),s=9;return[{point:[t.x+t.w,n],side:"right",offsetPenalty:0},{point:[t.x,n],side:"left",offsetPenalty:0},{point:[e,t.y+t.h],side:"bottom",offsetPenalty:0},{point:[e,t.y],side:"top",offsetPenalty:0},{point:[t.x+t.w,n-o],side:"right",offsetPenalty:s},{point:[t.x+t.w,n+o],side:"right",offsetPenalty:s},{point:[t.x,n-o],side:"left",offsetPenalty:s},{point:[t.x,n+o],side:"left",offsetPenalty:s},{point:[e-r,t.y+t.h],side:"bottom",offsetPenalty:s},{point:[e+r,t.y+t.h],side:"bottom",offsetPenalty:s},{point:[e-r,t.y],side:"top",offsetPenalty:s},{point:[e+r,t.y],side:"top",offsetPenalty:s}]}function Ht(t){let[e,n]=t.point;switch(t.side){case"right":return[e+6,n];case"left":return[e-6,n];case"bottom":return[e,n+6];case"top":return[e,n-6]}}function Ze(t,e,n,r,o){let s=t.x+t.w/2,i=t.y+t.h/2,a=e.x+e.w/2,c=e.y+e.h/2,l=a-s,u=c-i;return o==="dashed"?tn(n,r,l,u,t,e):en(n,l,u,t)+nn(r,l,u,e)}function tn(t,e,n,r,o,s){if(Math.abs(r)>Math.min(o.h,s.h)*.8){let i=r>0?"bottom":"top",a=r>0?"top":"bottom";return Z(t,i)+Z(e,a)}if(Math.abs(n)>Math.min(o.w,s.w)*.8){let i=n>0?"right":"left",a=n>0?"left":"right";return Z(t,i)+Z(e,a)}return 0}function Z(t,e){return t===e?0:t===j(e)?1400:450}function en(t,e,n,r){if(Math.abs(n)>r.h*.8&&Math.abs(n)>=Math.abs(e)*.25){let o=n>0?"bottom":"top";return t===o?0:t===j(o)?800:180}if(Math.abs(e)>r.w*.5){let o=e>0?"right":"left";return t===o?0:t===j(o)?900:90}if(Math.abs(n)>r.h*.8){let o=n>0?"bottom":"top";return t===o?0:t===j(o)?600:120}return 0}function nn(t,e,n,r){if(Math.abs(n)>r.h*.8&&Math.abs(n)>=Math.abs(e)*.25){let o=n>0?"top":"bottom";return t===o?0:t===j(o)?1400:360}if(Math.abs(e)>r.w*.5&&Math.abs(e)>=Math.abs(n)*1.2){let o=e>0?"left":"right";return t===o?0:t===j(o)?1200:220}if(Math.abs(n)>r.h*.35){let o=n>0?"top":"bottom";return t===o?0:t==="left"||t==="right"?9e3:1200}if(Math.abs(e)>r.w*.5){let o=e>0?"left":"right";return t===o?0:t===j(o)?1e3:160}return 0}function j(t){switch(t){case"right":return"left";case"left":return"right";case"bottom":return"top";case"top":return"bottom"}}function rn(t,e,n,r){let o=[t,e,...n],s=24,i=r?r.x:Math.max(8,Math.min(...o.map(m=>m.x))-s),a=r?r.x+r.w:Math.max(...o.map(m=>m.x+m.w))+s,c=r?r.y:Math.max(8,Math.min(...o.map(m=>m.y))-s),l=r?r.y+r.h:Math.max(...o.map(m=>m.y+m.h))+s,u=[(t.x+t.w/2+e.x+e.w/2)/2],d=[(t.y+t.h/2+e.y+e.h/2)/2];for(let m of o)for(let h of[12,12*1.5,24])u.push(m.x-h,m.x+m.w+h),d.push(m.y-h,m.y+m.h+h);return r&&(u.push(r.x,r.x+r.w),d.push(r.y,r.y+r.h)),{xs:Wt(Ut(u.filter(m=>m>=i&&m<=a)),(t.x+t.w/2+e.x+e.w/2)/2),ys:Wt(Ut(d.filter(m=>m>=c&&m<=l)),(t.y+t.h/2+e.y+e.h/2)/2)}}function Wt(t,e){if(t.length<=18)return t;let n=new Set([t[0],t[t.length-1]]);for(let r of[...t].sort((o,s)=>Math.abs(o-e)-Math.abs(s-e)))if(n.add(r),n.size>=18)break;return[...n].sort((r,o)=>r-o)}function on(t,e,n,r){let o=sn(t,e,n);if(!o)return;let s=r.get(o);if(!s)return;let i=12/2,a=s.y+5+8/2,c=s.y+s.h-i;return{x:s.x+i,y:a,w:Math.max(0,s.w-i*2),h:Math.max(0,c-a)}}function sn(t,e,n){let r=new Set(ot(e,n));return ot(t,n).find(o=>r.has(o))}function ot(t,e){let n=[],r=t,o=new Set;for(;e.has(r)&&!o.has(r);)o.add(r),r=e.get(r),n.push(r);return n}function Kt(t,e){let n=e.x+e.w,r=e.y+e.h;return t.every(([o,s])=>o>=e.x-.001&&o<=n+.001&&s>=e.y-.001&&s<=r+.001)}function Ut(t){let e=new Set,n=[];for(let r of t){let o=Math.round(r*1e3)/1e3,s=o.toFixed(3);e.has(s)||(e.add(s),n.push(o))}return n.sort((r,o)=>r-o)}function an(t){let e=[];for(let r of t){let o=e[e.length-1];(!o||Math.abs(o[0]-r[0])>=.001||Math.abs(o[1]-r[1])>=.001)&&e.push(r)}let n=[];for(let r of e)for(n.push(r);n.length>=3;){let o=n[n.length-3],s=n[n.length-2],i=n[n.length-1],a=Math.abs(o[0]-s[0])<.001&&Math.abs(s[0]-i[0])<.001,c=Math.abs(o[1]-s[1])<.001&&Math.abs(s[1]-i[1])<.001;if(!a&&!c)break;n.splice(n.length-2,1)}return n}function cn(t){if(t.length<2)return!1;for(let e=0;e<t.length-1;e++){let n=t[e],r=t[e+1];if(Math.abs(n[0]-r[0])>=.001&&Math.abs(n[1]-r[1])>=.001)return!1}return!0}function un(t,e,n,r,o,s,i,a){let c=0;for(let l=0;l<t.length-1;l++){let u=t[l],d=t[l+1];c+=xt(u,d);let m=F(u,d,e,0);m>0&&(c+=22e4+m*1500);let h=F(u,d,n,0);h>0&&(c+=22e4+h*1500);let x=U(u,d,e);x>0&&(c+=8e3+x*120);let y=U(u,d,n);y>0&&(c+=8e3+y*120);for(let b of a){let p=U(u,d,b);p>0&&(c+=12e3+p*160)}for(let b of r){let p=F(u,d,b,.8);p>0&&(c+=fn(b,u,d,p))}for(let b of o){let p=F(u,d,b,.8);p>0&&(c+=18e4+p*1200);let E=U(u,d,b);E>0&&(c+=18e3+E*300)}for(let b of s){let p=F(u,d,mn(b),.8);p>0&&(c+=14e3+p*120);let E=U(u,d,b);E>0&&(c+=6e3+E*120)}for(let b of i){let p=F(u,d,b,0);p>0&&(c+=p*40)}}return c+=Math.max(0,t.length-2)*3,c+=ln(t,e,n),c}function ln(t,e,n){let o=Math.min(e.x,n.x)-60,s=Math.min(e.y,n.y)-60,i=Math.max(e.x+e.w,n.x+n.w)+60,a=Math.max(e.y+e.h,n.y+n.h)+60,c=0;for(let[l,u]of t)l<o&&(c+=(o-l)*90),l>i&&(c+=(l-i)*90),u<s&&(c+=(s-u)*90),u>a&&(c+=(u-a)*90);return c}function fn(t,e,n,r){return t.id.startsWith("__arrow_guard_")?7e4+r*700:ee(t)?hn(t,e,n,r):16e4+r*1200}function hn(t,e,n,r){let o=Math.abs(e[0]-n[0])<.001,s=t.orientation==="vertical";return o===s?85e3+r*1200:500+r*160}function ee(t){return t.id.startsWith("__edge_guard_")&&"orientation"in t&&(t.orientation==="vertical"||t.orientation==="horizontal")}function mn(t){return{x:t.x,y:t.y,w:t.w,h:13}}function xt(t,e){return Math.abs(t[0]-e[0])+Math.abs(t[1]-e[1])}function F(t,e,n,r){let o=n.x-r,s=n.x+n.w+r,i=n.y-r,a=n.y+n.h+r;if(Math.abs(t[0]-e[0])<.001){let c=t[0];return c<=o||c>=s?0:H(t[1],e[1],i,a)}if(Math.abs(t[1]-e[1])<.001){let c=t[1];return c<=i||c>=a?0:H(t[0],e[0],o,s)}return 0}function U(t,e,n){return ne(t,e,n,0)}function ne(t,e,n,r){if(Math.abs(t[0]-e[0])<.001){let o=t[0];return Math.abs(o-n.x)>r+.001&&Math.abs(o-(n.x+n.w))>r+.001?0:H(t[1],e[1],n.y,n.y+n.h)}if(Math.abs(t[1]-e[1])<.001){let o=t[1];return Math.abs(o-n.y)>r+.001&&Math.abs(o-(n.y+n.h))>r+.001?0:H(t[0],e[0],n.x,n.x+n.w)}return 0}function H(t,e,n,r){let o=Math.min(t,e),s=Math.max(t,e),i=Math.min(n,r),a=Math.max(n,r);return Math.max(0,Math.min(s,a)-Math.max(o,i))}function dt(t,e){let n={x:t.x+t.w/2,y:t.y+t.h/2},r={x:e.x+e.w/2,y:e.y+e.h/2},o=r.x-n.x,s=r.y-n.y,i,a,c=t.x+t.w<=e.x||e.x+e.w<=t.x,l=t.y+t.h<=e.y||e.y+e.h<=t.y;if(c?!0:l?!1:Math.abs(o)>Math.abs(s)){if(i=[o>0?t.x+t.w:t.x,n.y],a=[o>0?e.x:e.x+e.w,r.y],Math.abs(i[1]-a[1])<.5)return[i,a];let d=(i[0]+a[0])/2;return[i,[d,i[1]],[d,a[1]],a]}else{if(i=[n.x,s>0?t.y+t.h:t.y],a=[r.x,s>0?e.y:e.y+e.h],Math.abs(i[0]-a[0])<.5)return[i,a];let d=(i[1]+a[1])/2;return[i,[i[0],d],[a[0],d],a]}}var ae="http://www.w3.org/2000/svg",dn='"Hiragino Sans", "Yu Gothic", "Noto Sans CJK JP", sans-serif';var gn=[0,3.2,6.4],V=.001;function q(t,e={}){let n=e.lang??"ja",r=N("svg",{xmlns:ae,viewBox:`0 0 ${t.width} ${t.height}`,"font-family":dn,"shape-rendering":"geometricPrecision"}),o=t.nodes.filter(a=>a.isContainer),s=t.nodes.filter(a=>!a.isContainer),i=[];for(let a of o)r.appendChild(xn(a,n));for(let a of t.edges)r.appendChild(yn(a,n,t,i));for(let a of t.edges)r.appendChild(Sn(a));for(let a of s)r.appendChild(pn(a,n));return r}function xn(t,e){let n=N("g");n.appendChild(N("rect",{x:t.x,y:t.y,width:t.w,height:t.h,fill:"white",stroke:"#000","stroke-width":.3}));let r=St(t.label,e);if(r.length||t.id){let o=N("text",{x:t.x+2,y:t.y+3.5,"font-size":2.6,fill:"#000"});o.textContent=(t.id?t.id+" ":"")+(r[0]??""),n.appendChild(o)}return n}function pn(t,e){let n=N("g");n.appendChild(bn(t));let r=[];t.id&&t.id!=="*"&&r.push(t.id);let o=St(t.label,e);for(let l of o)r.push(l);if(r.length===0)return n;let s=2.8,i=s*1.2,a=r.length*i,c=t.y+t.h/2-a/2+i*.8;for(let l=0;l<r.length;l++){let u=N("text",{x:t.x+t.w/2,y:c+l*i,"font-size":s,fill:"#000","text-anchor":"middle"});u.textContent=r[l],n.appendChild(u)}return n}function bn(t){let e="#000",n="white";switch(t.shape){case"round":return N("rect",{x:t.x,y:t.y,width:t.w,height:t.h,rx:Math.min(t.w,t.h)/2,ry:Math.min(t.w,t.h)/2,fill:n,stroke:e,"stroke-width":.4});case"circle":{let o=Math.min(t.w,t.h)/2;return N("circle",{cx:t.x+t.w/2,cy:t.y+t.h/2,r:o,fill:"#000",stroke:e})}case"diamond":{let o=t.x+t.w/2,s=t.y+t.h/2,i=[[o,t.y],[t.x+t.w,s],[o,t.y+t.h],[t.x,s]].map(a=>a.join(",")).join(" ");return N("polygon",{points:i,fill:n,stroke:e,"stroke-width":.4})}default:return N("rect",{x:t.x,y:t.y,width:t.w,height:t.h,fill:n,stroke:e,"stroke-width":.4})}}function yn(t,e,n,r){let o=N("g");if(t.points.length<2)return o;let s=An(t);if(s.length<2)return o;let i=s.map((u,d)=>d===0?`M ${u[0]} ${u[1]}`:`L ${u[0]} ${u[1]}`).join(" "),a=N("path",{d:i,fill:"none",stroke:"#000","stroke-width":wn(t.op),"stroke-linecap":"butt","stroke-linejoin":"miter"}),c=En(t.op);c&&a.setAttribute("stroke-dasharray",c),o.appendChild(a);let l=St(t.label,e);if(l.length){let u=l[0],d=l[1],m=ce(t,l,n,r);r.push(yt(m.box,.8));let h=(x,y,b,p,E,S)=>{let v=N("text",{x:y,y:b,"font-size":p,fill:"#000","text-anchor":E,"dominant-baseline":S});v.textContent=x,o.appendChild(v)};h(u,m.ja.x,m.ja.y,2.4,m.anchor,m.ja.baseline),d&&m.en&&h(d,m.en.x,m.en.y,2.1,m.anchor,m.en.baseline)}return o}function Sn(t){let e=N("g");if(t.isLifeline)return e;let n=se(t,!1),r=se(t,!0);return n&&e.appendChild(n),r&&e.appendChild(r),e}function wn(t){return t==="thick"?.7:.4}function En(t){return t==="dashed"||t==="dashed-arrow"?"1.4 1.2":null}function An(t){let e=t.points.map(n=>[n[0],n[1]]);return t.op!=="line"&&t.op!=="dashed"&&(e=re(e,!1,oe(t.op))),t.op==="bidir"&&(e=re(e,!0,oe(t.op))),e}function re(t,e,n){if(t.length<2||n<=0)return t;let r=e?[...t].reverse():[...t],o=n;for(;r.length>=2&&o>0;){let s=r[r.length-1],i=r[r.length-2],a=i[0]-s[0],c=i[1]-s[1],l=Math.hypot(a,c);if(l<V){r.pop();continue}l>o?(r[r.length-1]=[s[0]+a/l*o,s[1]+c/l*o],o=0):(r.pop(),o-=l)}return e?r.reverse():r}function oe(t){return t==="thick"?3.4:2.8}function se(t,e){if(t.points.length<2||e&&t.op!=="bidir"||!e&&(t.op==="line"||t.op==="dashed"))return null;let n=e?t.points:[...t.points].reverse();return Mn(n[0],n[1],t.op==="thick")}function Mn(t,e,n){let r=e[0]-t[0],o=e[1]-t[1],s=Math.hypot(r,o);if(s<.001)return null;let i=r/s,a=o/s,c=n?3.4:2.8,l=n?1.6:1.25,u=t[0]+i*c,d=t[1]+a*c,m=-a,h=i,x=[t,[u+m*l,d+h*l],[u-m*l,d-h*l]].map(y=>y.join(",")).join(" ");return N("polygon",{points:x,fill:"#000",stroke:"#000","stroke-width":0})}function N(t,e={}){let n=document.createElementNS(ae,t);for(let[r,o]of Object.entries(e))n.setAttribute(r,String(o));return n}function St(t,e){if(!t)return[];if(e==="ja")return t.ja?[t.ja]:t.en?[t.en]:[];if(e==="en")return t.en?[t.en]:t.ja?[t.ja]:[];let n=[];return t.ja&&n.push(t.ja),t.en&&n.push(t.en),n}function ce(t,e,n,r=[]){let o=[],s=Math.max(...e.map((a,c)=>st(a,c===0?2.4:2.1))),i=e.length>1?2.4+2.1+.6:2.4;for(let a=0;a<t.points.length-1;a++){let c=t.points[a],l=t.points[a+1],u=l[0]-c[0],d=l[1]-c[1];if(!(Math.abs(u)+Math.abs(d)<8))for(let h of[.5,.35,.65,.22,.78]){let x=c[0]+u*h,y=c[1]+d*h;for(let b of gn){let p=4+b,E=b*9;Math.abs(d)<V?(o.push(pt(t,n,r,e,s,i,c,l,x,y,"above",p,E)),o.push(pt(t,n,r,e,s,i,c,l,x,y,"below",p,E))):Math.abs(u)<V&&(o.push(bt(t,n,r,e,s,i,c,l,x,y,"right",p,E)),o.push(bt(t,n,r,e,s,i,c,l,x,y,"left",p,E)))}}}if(o.length===0){let[a,c]=On(t.points),l=(a[0]+c[0])/2,u=(a[1]+c[1])/2;return Math.abs(c[1]-a[1])>=Math.abs(c[0]-a[0])?bt(t,n,r,e,s,i,a,c,l,u,"right",4,0):pt(t,n,r,e,s,i,a,c,l,u,"above",4,0)}return o.sort((a,c)=>a.score-c.score),o[0]}function pt(t,e,n,r,o,s,i,a,c,l,u,d,m){let h=u==="above"?l-d-s:l+d,x={x:c-o/2,y:h,w:o,h:s},y,b;r.length>1?(y=h+2.4,b=y+.6+2.1):y=h+2.4;let p={box:x,lineA:i,lineB:a,score:0,vertical:!1,anchor:"middle",ja:{x:c,y,baseline:"alphabetic"},en:b===void 0?void 0:{x:c,y:b,baseline:"alphabetic"}};return p.score=ue(p,t,e,n)+(u==="above"?0:8)+m,p}function bt(t,e,n,r,o,s,i,a,c,l,u,d,m){let h=u==="right"?c+d:c-d,x={x:u==="right"?h:h-o,y:l-s/2,w:o,h:s},y=u==="right"?"start":"end",b=r.length>1?l-2.4/2-.6/2:l,p=r.length>1?l+2.1/2+.6/2:void 0,E={box:x,lineA:i,lineB:a,score:0,vertical:!0,anchor:y,ja:{x:h,y:b,baseline:"middle"},en:p===void 0?void 0:{x:h,y:p,baseline:"middle"}};return E.score=ue(E,t,e,n)+(u==="right"?0:8)+m,E}function ue(t,e,n,r){let o=t.vertical?5:0,s=t.box;s.x<0&&(o+=Math.abs(s.x)*300),s.y<0&&(o+=Math.abs(s.y)*300),s.x+s.w>n.width&&(o+=(s.x+s.w-n.width)*300),s.y+s.h>n.height&&(o+=(s.y+s.h-n.height)*300);for(let m of n.nodes)if(m.isContainer){let h={x:m.x,y:m.y,w:m.w,h:6};o+=$(s,h)*900,o+=Pn(s,m)*1800,o+=Ln(s,m,1.8)*9e3}else{let h=$(s,m);h>0&&(o+=1e6+h*5e4),o+=$(s,yt(m,1.8))*5200}for(let m of n.edges)for(let h=0;h<m.points.length-1;h++){let x=m.points[h],y=m.points[h+1];Bn(x,y,yt(s,.8))&&(o+=m===e?35:380)}for(let m of r)o+=$(s,m)*18e3;let i=Math.abs(t.lineA[0]-t.lineB[0])+Math.abs(t.lineA[1]-t.lineB[1]),a=t.vertical?s.h:s.w;o+=Math.max(0,Math.max(36,a*2.4)-i)*80;let c=t.vertical?s.y+s.h/2:s.x+s.w/2,l=t.vertical?t.lineA[1]:t.lineA[0],u=t.vertical?t.lineB[1]:t.lineB[0],d=Math.min(Math.abs(c-l),Math.abs(c-u));return o+=Math.max(0,12-d)*80,o}function st(t,e){let n=0;for(let r of t)r===" "?n+=e*.35:r.charCodeAt(0)<=127?n+=e*.58:n+=e;return Math.max(e*2,n)}function On(t){let e=0,n=-1;for(let s=0;s<t.length-1;s++){let i=t[s+1][0]-t[s][0],a=t[s+1][1]-t[s][1],c=i*i+a*a;c>n&&(n=c,e=s)}let r=t[e],o=t[e+1];return[r,o]}function yt(t,e){return{x:t.x-e,y:t.y-e,w:t.w+e*2,h:t.h+e*2}}function $(t,e){let n=Math.max(0,Math.min(t.x+t.w,e.x+e.w)-Math.max(t.x,e.x)),r=Math.max(0,Math.min(t.y+t.h,e.y+e.h)-Math.max(t.y,e.y));return n*r}function Pn(t,e){let n=Math.abs(t.x-e.x)<1||Math.abs(t.x+t.w-e.x)<1,r=Math.abs(t.x-(e.x+e.w))<1||Math.abs(t.x+t.w-(e.x+e.w))<1,o=Math.abs(t.y-e.y)<1||Math.abs(t.y+t.h-e.y)<1,s=Math.abs(t.y-(e.y+e.h))<1||Math.abs(t.y+t.h-(e.y+e.h))<1;return Number(n||r||o||s)}function Ln(t,e,n){return $(t,{x:e.x-n,y:e.y-n,w:n*2,h:e.h+n*2})+$(t,{x:e.x+e.w-n,y:e.y-n,w:n*2,h:e.h+n*2})+$(t,{x:e.x-n,y:e.y-n,w:e.w+n*2,h:n*2})+$(t,{x:e.x-n,y:e.y+e.h-n,w:e.w+n*2,h:n*2})}function Bn(t,e,n){let r=n.x,o=n.x+n.w,s=n.y,i=n.y+n.h;if(Math.abs(t[0]-e[0])<V){let a=t[0];return a<=r||a>=o?!1:ie(t[1],e[1],s,i)>V}if(Math.abs(t[1]-e[1])<V){let a=t[1];return a<=s||a>=i?!1:ie(t[0],e[0],r,o)>V}return!1}function ie(t,e,n,r){let o=Math.min(t,e),s=Math.max(t,e),i=Math.min(n,r),a=Math.max(n,r);return Math.max(0,Math.min(s,a)-Math.max(o,i))}var _n="http://www.w3.org/2000/svg",it=class{constructor(e,n){this.namespaceURI=e;this.tagName=n}namespaceURI;tagName;nodeType="element";attrs=new Map;children=[];text=null;setAttribute(e,n){this.attrs.set(e,String(n))}getAttribute(e){return this.attrs.has(e)?this.attrs.get(e):null}removeAttribute(e){this.attrs.delete(e)}appendChild(e){return this.text=null,this.children.push(e),e}set textContent(e){this.text=e==null?"":String(e),this.children=[]}get textContent(){return this.text!=null?this.text:this.children.map(e=>e.textContent).join("")}},Rn={createElementNS(t,e){return new it(t,e)},createElement(t){return new it(_n,t)}};function le(t){let e=globalThis,n=e.document;e.document=Rn;try{return t()}finally{e.document=n}}function Nn(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function vn(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function J(t){let e=[...t.attrs].map(([r,o])=>` ${r}="${Nn(o)}"`).join("");if(t.text!=null)return`<${t.tagName}${e}>${vn(t.text)}</${t.tagName}>`;if(t.children.length===0)return`<${t.tagName}${e}/>`;let n=t.children.map(J).join("");return`<${t.tagName}${e}>${n}</${t.tagName}>`}var Tn=3;function k(t,e,n=0){let r=t.getAttribute(e);if(r==null)return n;let o=Number.parseFloat(r);return Number.isFinite(o)?o:n}function wt(t){let e=[],n=/-?\d*\.?\d+(?:e[-+]?\d+)?/gi,r;for(;(r=n.exec(t))!==null;)e.push(Number.parseFloat(r[0]));return e}function kn(t){switch(t.tagName){case"rect":{let e=k(t,"x"),n=k(t,"y"),r=k(t,"width"),o=k(t,"height");return r<=0&&o<=0?null:{minX:e,minY:n,maxX:e+r,maxY:n+o}}case"circle":{let e=k(t,"cx"),n=k(t,"cy"),r=k(t,"r");return r<=0?null:{minX:e-r,minY:n-r,maxX:e+r,maxY:n+r}}case"line":{let e=k(t,"x1"),n=k(t,"y1"),r=k(t,"x2"),o=k(t,"y2");return{minX:Math.min(e,r),minY:Math.min(n,o),maxX:Math.max(e,r),maxY:Math.max(n,o)}}case"polygon":case"polyline":{let e=wt(t.getAttribute("points")??"");return fe(e)}case"path":{let e=wt(t.getAttribute("d")??"");return fe(e)}case"text":return Dn(t);default:return null}}function fe(t){if(t.length<2)return null;let e=1/0,n=1/0,r=-1/0,o=-1/0;for(let s=0;s+1<t.length;s+=2){let i=t[s],a=t[s+1];i<e&&(e=i),a<n&&(n=a),i>r&&(r=i),a>o&&(o=a)}return Number.isFinite(e)?{minX:e,minY:n,maxX:r,maxY:o}:null}function Dn(t){let e=t.textContent;if(!e)return null;let n=k(t,"x"),r=k(t,"y"),o=k(t,"font-size",2.8),s=st(e,o),i=t.getAttribute("text-anchor")??"start",a;i==="middle"?a=n-s/2:i==="end"?a=n-s:a=n;let c=a+s,l=t.getAttribute("dominant-baseline")??"alphabetic",u,d;return l==="middle"||l==="central"?(u=r-o*.6,d=r+o*.6):(u=r-o*.8,d=r+o*.25),{minX:a,minY:u,maxX:c,maxY:d}}function he(t,e){let n=kn(t);n&&e.push(n);for(let r of t.children)he(r,e)}function Cn(t){if(t.length===0)return null;let e=1/0,n=1/0,r=-1/0,o=-1/0;for(let s of t)s.minX<e&&(e=s.minX),s.minY<n&&(n=s.minY),s.maxX>r&&(r=s.maxX),s.maxY>o&&(o=s.maxY);return{minX:e,minY:n,maxX:r,maxY:o}}function In(t){let e=wt(t.getAttribute("viewBox")??"");return e.length===4&&e[2]>0&&e[3]>0?{minX:e[0],minY:e[1],width:e[2],height:e[3]}:{minX:0,minY:0,width:210,height:297}}function at(t,e=Tn){let n=[];he(t,n);let r=Cn(n);if(!r)return In(t);let o=Math.max(0,e);return{minX:r.minX-o,minY:r.minY-o,width:Math.max(1,r.maxX-r.minX+o*2),height:Math.max(1,r.maxY-r.minY+o*2)}}function ct(t,e=1600){let n=Math.max(t.width,t.height,1),r=Math.max(1,e/n);return{width:Math.max(1,Math.ceil(t.width*r)),height:Math.max(1,Math.ceil(t.height*r)),scale:r}}var Gn="http://www.w3.org/2000/svg",$n=3,Yn=1600;function Et(t,e={}){let{lang:n="ja",crop:r=!0,bleed:o=$n,targetSide:s=Yn}=e,i=Y(t),a=z(i),c=le(()=>q(a,{lang:n})),l=r?at(c,o):{minX:0,minY:0,width:a.width,height:a.height},u=ct(l,s);return c.setAttribute("xmlns",Gn),c.setAttribute("version","1.1"),c.setAttribute("viewBox",`${l.minX} ${l.minY} ${l.width} ${l.height}`),c.setAttribute("width",String(u.width)),c.setAttribute("height",String(u.height)),c.setAttribute("preserveAspectRatio","xMidYMid meet"),{el:c,kind:i.kind,viewBox:l,width:u.width,height:u.height}}function ut(t,e={}){let n=Et(t,e),r=J(n.el);return{svg:e.xmlDeclaration??!0?`<?xml version="1.0" encoding="UTF-8"?>
2
+ ${r}`:r,kind:n.kind,viewBox:n.viewBox,width:n.width,height:n.height}}function Mt(t){let e=me(t),n=[];n.push("## \u7B26\u53F7\u306E\u8AAC\u660E / Reference Signs"),n.push(""),n.push("| \u7B26\u53F7 | \u540D\u79F0(\u65E5\u672C\u8A9E) | Name (English) |"),n.push("|------|---------------|----------------|");for(let r of e){let o=t.nodes.get(r);n.push(`| ${r} | ${o.label.ja??""} | ${o.label.en??""} |`)}return n.join(`
3
+ `)}function Ot(t){let e=me(t),n=["id,ja,en"];for(let r of e){let o=t.nodes.get(r);n.push(`${At(r)},${At(o.label.ja??"")},${At(o.label.en??"")}`)}return n.join(`
4
+ `)}function At(t){return/[",\n]/.test(t)?'"'+t.replace(/"/g,'""')+'"':t}function me(t){return[...t.nodes.keys()].filter(n=>n!=="*").sort((n,r)=>{let o=parseInt(n,10),s=parseInt(r,10);return!isNaN(o)&&!isNaN(s)?o-s:n.localeCompare(r)})}var Pt=["block","system","iot","imagePipeline","controlLoop","flow","state","seq","handshake"],Lt={block:{label:"\u30D6\u30ED\u30C3\u30AF\u56F3",hint:"\u300C:\u300D\u3092\u4F7F\u3046\u3068\u30D6\u30ED\u30C3\u30AF\u56F3\u306B\u306A\u308B",source:`# \u30D6\u30ED\u30C3\u30AF\u56F3(\u88C5\u7F6E\u30AF\u30EC\u30FC\u30E0\u7528)
5
5
  # \u30D2\u30F3\u30C8:\u300C:\u300D\u3067\u5305\u542B\u95A2\u4FC2\u3092\u66F8\u304F \u2192 \u30D6\u30ED\u30C3\u30AF\u56F3\u3068\u5224\u5B9A\u3055\u308C\u308B
6
6
 
7
7
  10 = \u5236\u5FA1\u88C5\u7F6E / control device
@@ -153,7 +153,7 @@ S3 -> *
153
153
  100 <-> 200 : \u30C7\u30FC\u30BF\u901A\u4FE1 / data exchange
154
154
  100 -> 200 : \u5207\u65AD\u8981\u6C42 / disconnect
155
155
  200 -> 100 : \u5207\u65AD\u5B8C\u4E86 / disconnected
156
- `}};var Lt={cond:`S100 = \u958B\u59CB / Start
156
+ `}};var Bt={cond:`S100 = \u958B\u59CB / Start
157
157
  S110 = \u5224\u5B9A? / Decision?
158
158
  S120 = Yes\u51E6\u7406 / Yes Process
159
159
  S130 = No\u51E6\u7406 / No Process
@@ -244,4 +244,4 @@ S150 -> S100`,system:`100 = \u30B7\u30B9\u30C6\u30E0\u672C\u4F53 / Main system
244
244
  20 : 21 22
245
245
  21 .> 40 : \u7121\u7DDA / wireless
246
246
  22 -> 30 : \u6709\u7DDA / wired
247
- 30 <-> 40 : \u901A\u4FE1 / comm`};var he={block:"\u30D6\u30ED\u30C3\u30AF\u56F3",flow:"\u30D5\u30ED\u30FC\u30C1\u30E3\u30FC\u30C8",state:"\u72B6\u614B\u9077\u79FB\u56F3",seq:"\u30B7\u30FC\u30B1\u30F3\u30B9\u56F3"};function Gn(t){let e=!1;for(let n=0;n<t.length;n++){let r=t[n];if(r==='"')e=!e;else if(!e&&r==="#")return t.slice(n+1)}return null}function $n(t){let e=!1;for(let n=0;n<t.length;n++){let r=t[n];if(r==='"')e=!e;else if(!e&&r==="#")return t.slice(0,n)}return t}var Fn=/^\s*!?\s*(?:pdgkit\s*:)?\s*kind\s*[:=]?\s*(block|flow|state|seq)\b/i;function Yn(t){for(let e=0;e<t.length;e++){let n=Gn(t[e]);if(n==null)continue;let r=n.match(Fn);if(r)return{kind:r[1].toLowerCase(),line:e+1}}return null}var Xn=/(?:^|\s)(?:<->|=>|->|<-|\.>|\.\.|-)(?:\s|$)/g,jn=/[A-Za-z0-9_*](?:<->|<-|->|=>|\.>|\.\.|-)[A-Za-z0-9_*]/;function Vn(t){let e=t.match(Xn);return e?e.length:0}function me(t){let e=F(t),n=[...e.diagnostics],r=t.split(/\r?\n/),o=Yn(r),s=o?o.kind:null,i=null;o&&(i=o.kind===e.kind,i||n.push({severity:"error",line:o.line,col:1,message:`\u56F3\u7A2E\u30A2\u30B5\u30FC\u30B7\u30E7\u30F3\u4E0D\u4E00\u81F4: \u5BA3\u8A00=${he[o.kind]}(${o.kind}) \u3060\u304C\u63A8\u8AD6=${he[e.kind]}(${e.kind})\u3002\u69CB\u9020(\u5305\u542B\u300C:\u300D/ \u672B\u5C3E\u300C?\u300D/ \u7B26\u53F7\u300C*\u300D/ \u5F80\u5FA9\u30FB\u300C<->\u300D)\u3092\u898B\u76F4\u3059\u304B\u3001\u5BA3\u8A00\u3092\u4FEE\u6B63\u3057\u3066\u304F\u3060\u3055\u3044\u3002`}));let a=new Set(e.diagnostics.filter(l=>l.severity==="error"&&l.message.startsWith("\u69CB\u6587\u4E0D\u660E")).map(l=>l.line));for(let l of a){let u=$n(r[l-1]??"").trim();u&&(Vn(u)>=2?n.push({severity:"info",line:l,col:1,message:"\u9023\u9396\u8A18\u6CD5(A -> B -> C)\u306F\u672A\u5BFE\u5FDC\u3067\u3059\u30021\u884C\u306B1\u63A5\u7D9A\u3067\u5206\u5272\u3057\u3066\u304F\u3060\u3055\u3044(\u4F8B: A -> B / B -> C)\u3002"}):jn.test(u)&&n.push({severity:"info",line:l,col:1,message:'\u63A5\u7D9A\u6F14\u7B97\u5B50\u306E\u524D\u5F8C\u306B\u306F\u534A\u89D2\u30B9\u30DA\u30FC\u30B9\u304C\u5FC5\u8981\u3067\u3059(\u4F8B: "11-12" \u3067\u306F\u306A\u304F "11 - 12")\u3002'}))}n.sort((l,u)=>l.line-u.line||l.col-u.col);let c={errors:n.filter(l=>l.severity==="error").length,warnings:n.filter(l=>l.severity==="warning").length,infos:n.filter(l=>l.severity==="info").length};return{ok:c.errors===0,kind:e.kind,declaredKind:s,kindMatches:i,diagnostics:n,counts:c}}var Hn="0.1.0";function Wn(t,e="ja"){return ut(t,{lang:e}).svg}return Ee(Kn);})();
247
+ 30 <-> 40 : \u901A\u4FE1 / comm`};var de={block:"\u30D6\u30ED\u30C3\u30AF\u56F3",flow:"\u30D5\u30ED\u30FC\u30C1\u30E3\u30FC\u30C8",state:"\u72B6\u614B\u9077\u79FB\u56F3",seq:"\u30B7\u30FC\u30B1\u30F3\u30B9\u56F3"};function Fn(t){let e=!1;for(let n=0;n<t.length;n++){let r=t[n];if(r==='"')e=!e;else if(!e&&r==="#")return t.slice(n+1)}return null}function jn(t){let e=!1;for(let n=0;n<t.length;n++){let r=t[n];if(r==='"')e=!e;else if(!e&&r==="#")return t.slice(0,n)}return t}var Vn=/^\s*!?\s*(?:pdgkit\s*:)?\s*kind\s*[:=]?\s*(block|flow|state|seq)\b/i;function Xn(t){for(let e=0;e<t.length;e++){let n=Fn(t[e]);if(n==null)continue;let r=n.match(Vn);if(r)return{kind:r[1].toLowerCase(),line:e+1}}return null}var Hn=/(?:^|\s)(?:<->|=>|->|<-|\.>|\.\.|-)(?:\s|$)/g,Wn=/[A-Za-z0-9_*](?:<->|<-|->|=>|\.>|\.\.|-)[A-Za-z0-9_*]/;function Kn(t){let e=t.match(Hn);return e?e.length:0}function ge(t){let e=Y(t),n=[...e.diagnostics],r=t.split(/\r?\n/),o=Xn(r),s=o?o.kind:null,i=null;o&&(i=o.kind===e.kind,i||n.push({severity:"error",line:o.line,col:1,message:`\u56F3\u7A2E\u30A2\u30B5\u30FC\u30B7\u30E7\u30F3\u4E0D\u4E00\u81F4: \u5BA3\u8A00=${de[o.kind]}(${o.kind}) \u3060\u304C\u63A8\u8AD6=${de[e.kind]}(${e.kind})\u3002\u69CB\u9020(\u5305\u542B\u300C:\u300D/ \u672B\u5C3E\u300C?\u300D/ \u7B26\u53F7\u300C*\u300D/ \u5F80\u5FA9\u30FB\u300C<->\u300D)\u3092\u898B\u76F4\u3059\u304B\u3001\u5BA3\u8A00\u3092\u4FEE\u6B63\u3057\u3066\u304F\u3060\u3055\u3044\u3002`}));let a=new Set(e.diagnostics.filter(l=>l.severity==="error"&&l.message.startsWith("\u69CB\u6587\u4E0D\u660E")).map(l=>l.line));for(let l of a){let u=jn(r[l-1]??"").trim();u&&(Kn(u)>=2?n.push({severity:"info",line:l,col:1,message:"\u9023\u9396\u8A18\u6CD5(A -> B -> C)\u306F\u672A\u5BFE\u5FDC\u3067\u3059\u30021\u884C\u306B1\u63A5\u7D9A\u3067\u5206\u5272\u3057\u3066\u304F\u3060\u3055\u3044(\u4F8B: A -> B / B -> C)\u3002"}):Wn.test(u)&&n.push({severity:"info",line:l,col:1,message:'\u63A5\u7D9A\u6F14\u7B97\u5B50\u306E\u524D\u5F8C\u306B\u306F\u534A\u89D2\u30B9\u30DA\u30FC\u30B9\u304C\u5FC5\u8981\u3067\u3059(\u4F8B: "11-12" \u3067\u306F\u306A\u304F "11 - 12")\u3002'}))}n.sort((l,u)=>l.line-u.line||l.col-u.col);let c={errors:n.filter(l=>l.severity==="error").length,warnings:n.filter(l=>l.severity==="warning").length,infos:n.filter(l=>l.severity==="info").length};return{ok:c.errors===0,kind:e.kind,declaredKind:s,kindMatches:i,diagnostics:n,counts:c}}var Un="0.1.2";function zn(t,e="ja"){return ut(t,{lang:e}).svg}return Me(qn);})();
package/dist/pdgkit.js CHANGED
@@ -194,6 +194,8 @@ var THICK_ARROW_TERMINAL_CLEARANCE = 5.4;
194
194
  var VERTICAL_PORT_RATIO = 0.25;
195
195
  var PORT_STUB = 6;
196
196
  var MAX_ROUTE_LANES = 18;
197
+ var LOOP_LANE_GAP = 10;
198
+ var LOOP_LANE_STEP = 7;
197
199
  var EPS = 1e-3;
198
200
  function layout(doc) {
199
201
  switch (doc.kind) {
@@ -373,7 +375,6 @@ function layoutFlow(doc) {
373
375
  const positions = /* @__PURE__ */ new Map();
374
376
  const sortedRanks = [...byRank.keys()].sort((a, b) => a - b);
375
377
  let y = MARGIN;
376
- let maxX = 0;
377
378
  for (const r of sortedRanks) {
378
379
  const lane = byRank.get(r);
379
380
  const widths = lane.map((id) => shapeOf(id) === "diamond" ? NODE_W * 1.2 : NODE_W);
@@ -385,7 +386,6 @@ function layoutFlow(doc) {
385
386
  positions.set(lane[i], { x, y, w: widths[i], h: NODE_H });
386
387
  x += widths[i] + H_GAP;
387
388
  }
388
- if (x > maxX) maxX = x;
389
389
  y += NODE_H + V_GAP;
390
390
  }
391
391
  for (const id of ids) {
@@ -405,7 +405,8 @@ function layoutFlow(doc) {
405
405
  });
406
406
  }
407
407
  const edges = makeEdges(doc.edges, positions);
408
- return { nodes: placed, edges, width: maxX + MARGIN, height: y + MARGIN, kind: "flow" };
408
+ const { width, height } = flowExtent(placed, edges);
409
+ return { nodes: placed, edges, width, height, kind: "flow" };
409
410
  }
410
411
  function layoutState(doc) {
411
412
  const { byRank } = computeRanks(doc);
@@ -417,7 +418,6 @@ function layoutState(doc) {
417
418
  const positions = /* @__PURE__ */ new Map();
418
419
  const sortedRanks = [...byRank.keys()].sort((a, b) => a - b);
419
420
  let y = MARGIN;
420
- let maxX = 0;
421
421
  for (const r of sortedRanks) {
422
422
  const lane = byRank.get(r);
423
423
  const widths = lane.map((id) => shapeOf(id) === "circle" ? 6 : NODE_W);
@@ -429,7 +429,6 @@ function layoutState(doc) {
429
429
  positions.set(lane[i], { x, y: y + (NODE_H - heights[i]) / 2, w: widths[i], h: heights[i] });
430
430
  x += widths[i] + H_GAP;
431
431
  }
432
- if (x > maxX) maxX = x;
433
432
  y += NODE_H + V_GAP;
434
433
  }
435
434
  for (const id of doc.nodes.keys()) {
@@ -449,7 +448,8 @@ function layoutState(doc) {
449
448
  });
450
449
  }
451
450
  const edges = makeEdges(doc.edges, positions);
452
- return { nodes: placed, edges, width: maxX + MARGIN, height: y + MARGIN, kind: "state" };
451
+ const { width, height } = flowExtent(placed, edges);
452
+ return { nodes: placed, edges, width, height, kind: "state" };
453
453
  }
454
454
  function layoutSeq(doc) {
455
455
  const seen = /* @__PURE__ */ new Set();
@@ -568,21 +568,50 @@ function computeRanks(doc) {
568
568
  return { byRank };
569
569
  }
570
570
  function makeEdges(srcEdges, positions) {
571
- return srcEdges.map((e) => {
571
+ const boxes = [...positions.values()];
572
+ const rightLimit = boxes.length ? Math.max(...boxes.map((b) => b.x + b.w)) : MARGIN;
573
+ const feedback = srcEdges.map((e, index) => ({ index, a: positions.get(e.from), b: positions.get(e.to) })).filter((r) => !!r.a && !!r.b && r.b.y + r.b.h <= r.a.y + EPS).sort((p, q) => feedbackSpan(p.a, p.b) - feedbackSpan(q.a, q.b));
574
+ const laneOf = /* @__PURE__ */ new Map();
575
+ feedback.forEach((r, nest) => laneOf.set(r.index, nest));
576
+ return srcEdges.map((e, index) => {
572
577
  const a = positions.get(e.from);
573
578
  const b = positions.get(e.to);
574
579
  if (!a || !b) {
575
580
  return { from: e.from, to: e.to, points: [], label: e.label, op: e.op };
576
581
  }
577
- return {
578
- from: e.from,
579
- to: e.to,
580
- points: orthogonalRoute(a, b),
581
- label: e.label,
582
- op: e.op
583
- };
582
+ const nest = laneOf.get(index);
583
+ const points = nest === void 0 ? orthogonalRoute(a, b) : feedbackRoute(a, b, rightLimit + LOOP_LANE_GAP + nest * LOOP_LANE_STEP);
584
+ return { from: e.from, to: e.to, points, label: e.label, op: e.op };
584
585
  });
585
586
  }
587
+ function feedbackSpan(a, b) {
588
+ return a.y + a.h / 2 - (b.y + b.h / 2);
589
+ }
590
+ function feedbackRoute(a, b, laneX) {
591
+ const ay = a.y + a.h / 2;
592
+ const by = b.y + b.h / 2;
593
+ return [
594
+ [a.x + a.w, ay],
595
+ [laneX, ay],
596
+ [laneX, by],
597
+ [b.x + b.w, by]
598
+ ];
599
+ }
600
+ function flowExtent(placed, edges) {
601
+ let maxX = MARGIN;
602
+ let maxY = MARGIN;
603
+ for (const n of placed) {
604
+ if (n.x + n.w > maxX) maxX = n.x + n.w;
605
+ if (n.y + n.h > maxY) maxY = n.y + n.h;
606
+ }
607
+ for (const e of edges) {
608
+ for (const [x, y] of e.points) {
609
+ if (x > maxX) maxX = x;
610
+ if (y > maxY) maxY = y;
611
+ }
612
+ }
613
+ return { width: maxX + MARGIN, height: maxY + MARGIN };
614
+ }
586
615
  function hasLinearChildFlow(children, edges, childMap) {
587
616
  const childSet = new Set(children);
588
617
  const pairs = /* @__PURE__ */ new Set();
@@ -3312,7 +3341,7 @@ async function renderToPptx(source, opts = {}) {
3312
3341
  }
3313
3342
 
3314
3343
  // src/node/index.ts
3315
- var VERSION = "0.1.0";
3344
+ var VERSION = "0.1.2";
3316
3345
  function loadAuthoringGuide() {
3317
3346
  return readFileSync2(resolvePackageFile("docs", "ai-authoring-guide.md"), "utf8");
3318
3347
  }