import{k as A,j as D,t as H,g as I,m as E,n as B,p as _,f as z,i as N,v as P,A as R,w as c,x as X,s as Y,F as O,q as F}from"./web.Os7kOAdY.js";import{g as U,t as G}from"./i18n-runtime._BAkvU05.js";var W=R('<svg><g><polygon fill="var(--bg-2, #2a2a3e)"stroke="var(--accent, #6c8)"stroke-width=2></polygon><text text-anchor=middle fill="var(--text, #eee)"font-size=12></svg>',!1,!0,!1),V=R('<svg><g><rect fill="var(--bg-2, #2a2a3e)"stroke="var(--accent, #6c8)"stroke-width=2></rect><text text-anchor=middle fill="var(--text, #eee)"font-size=12></svg>',!1,!0,!1),J=R('<svg><g><line stroke="var(--text, #aaa)"marker-end=url(#arrowhead)></line><!$><!/></svg>',!1,!0,!1),K=R('<svg><text fill="var(--text, #ccc)"font-size=11></svg>',!1,!0,!1),Q=R('<div class=mobile-flex-workbench style=display:flex;gap:16px;flex-wrap:wrap><div style=flex:1;min-width:280px><label style=font-weight:600;display:block;margin-bottom:8px></label><textarea rows=12 style="width:100%;font-family:monospace;font-size:13px;padding:10px;border-radius:8px;border:1px solid var(--border,#444);background:var(--bg,#1a1a2e);color:var(--text,#eee);resize:vertical"></textarea><button style="margin-top:8px;padding:6px 16px;border-radius:6px;border:none;background:var(--accent,#6c8);color:#000;cursor:pointer;font-weight:600"></button></div><div style="flex:1.5;min-width:320px;overflow:auto;border:1px solid var(--border,#444);border-radius:8px;background:var(--bg,#1a1a2e);padding:8px"><svg class=mermaid-svg xmlns=http://www.w3.org/2000/svg><defs><marker id=arrowhead markerWidth=10 markerHeight=7 refX=9 refY=3.5 orient=auto><polygon points="0 0, 10 3.5, 0 7"fill="var(--text, #aaa)"></polygon></marker></defs><!$><!/><!$><!/>');function j(S){const $=S.split(`
`).map(v=>v.trim()).filter(v=>v.length>0);let m="TD";const f=new Map,y=[];for(const v of $){const x=v.match(/^graph\s+(TD|TB|LR|RL|BT)$/i);if(x){const e=x[1].toUpperCase();m=e==="LR"||e==="RL"?"LR":"TD";continue}const w=/^(\w+)(?:\[([^\]]*)\]|\(([^)]*)\)|\{([^}]*)\})?\s*(-->|-.->|==>|--\s*[^-].*?-->)\s*(?:\|([^|]*)\|)?\s*(\w+)(?:\[([^\]]*)\]|\(([^)]*)\)|\{([^}]*)\})?$/,i=v.match(w);if(i){const e=i[1],d=i[2]||i[3]||i[4]||e,o=i[5].trim(),n=i[6]||void 0,s=i[7],a=i[8]||i[9]||i[10]||s;let t="rect";i[3]!==void 0&&(t="round"),i[4]!==void 0&&(t="diamond");let r="rect";i[9]!==void 0&&(r="round"),i[10]!==void 0&&(r="diamond"),f.has(e)?d!==e&&(f.get(e).label=d,f.get(e).shape=t):f.set(e,{id:e,label:d,shape:t}),f.has(s)?a!==s&&(f.get(s).label=a,f.get(s).shape=r):f.set(s,{id:s,label:a,shape:r});let l="solid";o.includes("-.->")?l="dashed":o.includes("==>")&&(l="thick"),y.push({from:e,to:s,style:l,label:n});continue}const M=/^(\w+)(?:\[([^\]]*)\]|\(([^)]*)\)|\{([^}]*)\})$/,b=v.match(M);if(b){const e=b[1],d=b[2]||b[3]||b[4]||e;let o="rect";b[3]!==void 0&&(o="round"),b[4]!==void 0&&(o="diamond"),f.has(e)?(f.get(e).label=d,f.get(e).shape=o):f.set(e,{id:e,label:d,shape:o})}}return{direction:m,nodes:Array.from(f.values()),edges:y}}function q(S){const{direction:$,nodes:m,edges:f}=S;if(m.length===0)return{nodes:[],edges:[],width:200,height:100};const y=new Map,v=new Map;for(const t of m)y.set(t.id,[]),v.set(t.id,0);for(const t of f)y.get(t.from)?.push(t.to),v.set(t.to,(v.get(t.to)||0)+1);const x=[],w=new Map,i=[];for(const t of m)(v.get(t.id)||0)===0&&i.push(t.id);for(i.length===0&&m.length>0&&i.push(m[0].id);i.length>0;){const t=i.shift();if(w.has(t))continue;const r=f.filter(h=>h.to===t&&w.has(h.from)).map(h=>w.get(h.from)),l=r.length>0?Math.max(...r)+1:0;for(w.set(t,l);x.length<=l;)x.push([]);x[l].push(t);for(const h of y.get(t)||[]){const g=(v.get(h)||1)-1;v.set(h,g),g<=0&&i.push(h)}}for(const t of m)if(!w.has(t.id)){const r=x.length;x.push([t.id]),w.set(t.id,r)}const M=120,b=44,e=$==="LR"?180:160,d=$==="LR"?70:90,o=[],n=new Map;for(let t=0;t<x.length;t++){const r=x[t];for(let l=0;l<r.length;l++){const h=r[l],g=m.find(C=>C.id===h);let u,k;$==="TD"?(u=l*e-(r.length-1)*e/2+300,k=t*d+40):(u=t*e+40,k=l*d-(r.length-1)*d/2+200),n.set(h,{x:u,y:k}),o.push({id:h,label:g.label,shape:g.shape,x:u,y:k,w:M,h:b})}}let s=0,a=0;for(const t of o)s=Math.max(s,t.x+t.w+40),a=Math.max(a,t.y+t.h+40);return{nodes:o,edges:f,width:Math.max(s,400),height:Math.max(a,200)}}function te(){const[S,$]=D(U(),{equals:!1}),m=e=>G(e,S());H(()=>{const e=d=>{const o=d.detail?.locale;o&&$(o)};window.addEventListener("taitan:locale-change",e),$(U()),Y(()=>window.removeEventListener("taitan:locale-change",e))});const f=`graph TD
A[Start] --> B{Decision}
B --> C[Option 1]
B --> D[Option 2]
C --> E[End]
D --> E`,[y,v]=D(f),[x,w]=D(q(j(f)));I(()=>{try{const e=j(y());w(q(e))}catch{}});const i=e=>{if(e.shape==="diamond"){const o=e.x+e.w/2,n=e.y+e.h/2,s=e.w/2,a=e.h/2;return(()=>{var t=E(W),r=t.firstChild,l=r.nextSibling;return c(r,"points",`${o},${n-a} ${o+s},${n} ${o},${n+a} ${o-s},${n}`),c(l,"x",o),c(l,"y",n+4),_(l,()=>e.label),t})()}const d=e.shape==="round"?16:4;return(()=>{var o=E(V),n=o.firstChild,s=n.nextSibling;return c(n,"rx",d),c(n,"ry",d),_(s,()=>e.label),N(a=>{var t=e.x,r=e.y,l=e.w,h=e.h,g=e.x+e.w/2,u=e.y+e.h/2+4;return t!==a.e&&c(n,"x",a.e=t),r!==a.t&&c(n,"y",a.t=r),l!==a.a&&c(n,"width",a.a=l),h!==a.o&&c(n,"height",a.o=h),g!==a.i&&c(s,"x",a.i=g),u!==a.n&&c(s,"y",a.n=u),a},{e:void 0,t:void 0,a:void 0,o:void 0,i:void 0,n:void 0}),o})()},M=e=>{const d=x(),o=d.nodes.find(g=>g.id===e.from),n=d.nodes.find(g=>g.id===e.to);if(!o||!n)return null;const s=o.x+o.w/2,a=o.y+o.h,t=n.x+n.w/2,r=n.y,l=e.style==="thick"?3:2,h=e.style==="dashed"?"5,3":void 0;return(()=>{var g=E(J),u=g.firstChild,k=u.nextSibling,[C,p]=B(k.nextSibling);return c(u,"x1",s),c(u,"y1",a),c(u,"x2",t),c(u,"y2",r),c(u,"stroke-width",l),c(u,"stroke-dasharray",h),_(g,(()=>{var T=F(()=>!!e.label);return()=>T()&&(()=>{var L=E(K);return c(L,"x",(s+t)/2+5),c(L,"y",(a+r)/2-5),_(L,()=>e.label),L})()})(),C,p),g})()},b=()=>{const e=document.querySelector(".mermaid-svg");if(!e)return;const d=new Blob([e.outerHTML],{type:"image/svg+xml"}),o=URL.createObjectURL(d),n=document.createElement("a");n.href=o,n.download="mermaid-diagram.svg",n.click(),URL.revokeObjectURL(o)};return(()=>{var e=E(Q),d=e.firstChild,o=d.firstChild,n=o.nextSibling,s=n.nextSibling,a=d.nextSibling,t=a.firstChild,r=t.firstChild,l=r.nextSibling,[h,g]=B(l.nextSibling),u=h.nextSibling,[k,C]=B(u.nextSibling);return _(o,()=>m("Mermaid 代码")),n.$$input=p=>v(p.currentTarget.value),s.$$click=b,_(s,()=>m("下载 SVG")),_(t,z(O,{get each(){return x().edges},children:p=>M(p)}),h,g),_(t,z(O,{get each(){return x().nodes},children:p=>i(p)}),k,C),N(p=>{var T=x().width,L=x().height;return T!==p.e&&c(t,"width",p.e=T),L!==p.t&&c(t,"height",p.t=L),p},{e:void 0,t:void 0}),N(()=>X(n,"value",y())),P(),e})()}A(["input","click"]);export{te as default};
