1import{a5 as f}from"./qyuwrbik.js";var u=Object.defineProperty,p=(c,e,t)=>e in c?u(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t,m=(c,e,t)=>p(c,typeof e!="symbol"?e+"":e,t);class w{constructor(e={}){m(this,"mermaidInstance",null),m(this,"isLoading",!1),m(this,"lastValidResult",""),m(this,"viewStateMap",new WeakMap),m(this,"containerHeight",400),this.config=e}async loadMermaid(){if(this.mermaidInstance)return this.mermaidInstance;if(this.isLoading)return new Promise(e=>{const t=()=>{this.mermaidInstance?e(this.mermaidInstance):setTimeout(t,50)};t()});this.isLoading=!0;try{const{default:e}=await f(async()=>{const{default:t}=await import("mermaid");return{default:t}},[],import.meta.url);return e.initialize({theme:this.config.theme||"default",startOnLoad:!1,suppressErrorRendering:!0,...this.config}),this.mermaidInstance=e,e}catch(e){throw console.error("Failed to load mermaid:",e),new Error("Failed to load mermaid library")}finally{this.isLoading=!1}}async renderToContainer(e,t,a="light"){const i=await this.renderMermaid(t,a);e.innerHTML=i;const s=e.querySelector("svg");s&&(this.initViewState(e,s),this.applyTransform(e,s),s.addEventListener("mousedown",n=>this.onSvgMouseDown(n,e,s)))}initViewState(e,t){let a=t.getAttribute("viewBox"),i=0,s=0;if(a){const l=a.split(/\s+/);i=parseFloat(l[2]),s=parseFloat(l[3])}else i=t.width.baseVal.value||t.getBoundingClientRect().width,s=t.height.baseVal.value||t.getBoundingClientRect().height;const n=e.clientWidth||0,r=this.containerHeight;let o=1;i&&s&&n&&r&&(o=Math.min(n/i,r/s,1)),this.viewStateMap.set(e,{scale:o,offsetX:0,offsetY:0,dragging:!1,dragStart:{x:0,y:0},lastOffset:{x:0,y:0}})}applyTransform(e,t){const a=this.viewStateMap.get(e);a&&(t.style.maxWidth="90%",t.style.maxHeight="90%",t.style.position="absolute",t.style.left="50%",t.style.top="50%",t.style.transform=`translate(-50%, -50%) translate(${a.offsetX}px, ${a.offsetY}px) scale(${a.scale})`,t.style.transformOrigin="center center",t.style.cursor=a.dragging?"grabbing":"grab")}zoomIn(e){const t=e.querySelector("svg"),a=this.viewStateMap.get(e);t&&a&&(a.scale=Math.min(a.scale+.2,3),this.applyTransform(e,t))}zoomOut(e){const t=e.querySelector("svg"),a=this.viewStateMap.get(e);t&&a&&(a.scale=Math.max(a.scale-.2,.2),this.applyTransform(e,t))}reset(e){const t=e.querySelector("svg");t&&(this.initViewState(e,t),this.applyTransform(e,t))}async download(e,t="diagram.png"){const a=e.querySelector("svg");if(a)try{const i=a.cloneNode(!0),s=new XMLSerializer().serializeToString(i),n=`data:image/svg+xml;charset=utf-8,${encodeURIComponent(s)}`,r=new Image;await new Promise((h,g)=>{r.onload=()=>h(),r.onerror=d=>g(new Error("Image loading failed")),r.src=n});const o=document.createElement("canvas"),l=o.getContext("2d");if(!l)throw new Error("Canvas context not available");o.width=r.width*2,o.height=r.height*2,l.fillStyle="white",l.fillRect(0,0,o.width,o.height),l.drawImage(r,0,0),o.toBlob(h=>{if(!h){console.error("Failed to create blob from canvas");return}const g=URL.createObjectURL(h),d=document.createElement("a");d.href=g,d.download=t,document.body.appendChild(d),d.click(),setTimeout(()=>{document.body.removeChild(d),URL.revokeObjectURL(g)},100)},"image/png")}catch(i){console.error("Failed to download diagram:",i)}}onSvgMouseDown(e,t,a){const i=this.viewStateMap.get(t);if(!i)return;i.dragging=!0,i.dragStart={x:e.clientX,y:e.clientY},i.lastOffset={x:i.offsetX,y:i.offsetY};const s=r=>this.onSvgMouseMove(r,t,a),n=()=>this.onSvgMouseUp(t,a,s,n);document.addEventListener("mousemove",s),document.addEventListener("mouseup",n),this.applyTransform(t,a)}onSvgMouseMove(e,t,a){const i=this.viewStateMap.get(t);!i||!i.dragging||(i.offsetX=i.lastOffset.x+(e.clientX-i.dragStart.x),i.offsetY=i.lastOffset.y+(e.clientY-i.dragStart.y),this.applyTransform(t,a))}onSvgMouseUp(e,t,a,i){const s=this.viewStateMap.get(e);s&&(s.dragging=!1,document.removeEventListener("mousemove",a),document.removeEventListener("mouseup",i),this.applyTransform(e,t))}async renderMermaid(e,t="light"){try{const a=await this.loadMermaid();this.config.theme!==t&&a.initialize({startOnLoad:!1,suppressErrorRendering:!0,theme:t==="dark"?"dark":"default",...this.config});const i=`mc_mermaid_${Date.now()}_${Math.random().toString(36).substr(2,9)}`,{svg:s}=await a.render(i,e);return this.lastValidResult=s,s}catch{return this.lastValidResult}}setConfig(e={}){this.config={theme:"default",...e}}}
1export{w as MermaidService};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.