import{nanoid as w}from"nanoid";import{envConfig as l}from"../../../../config/env-config.js";import{logger as m}from"../../../../tools/notifiers/logger.js";import{sha as I}from"../../../../utils/crypto/sha.js";const u={};function g(r,n="mermaid"){const s=`${n}:${r}`;if(n==="mermaid"&&r.toLowerCase().includes("gantt")&&!r.toLowerCase().includes("todaymarker off")){const t=new Date().toISOString().split("T")[0];return I(s+t)}return I(s)}function y(){return l.REDOCLY_DIAGRAM_MICROSERVICE_URL||"https://tools.redocly.com/diagram-renders"}function R(r,n){return n==="dark"?r.dark??r.light??"":r.light??r.dark??""}async function D(r,n){const s=y(),a=m.startTiming(),e=w(),t=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json","x-request-id":l.REQUEST_ID||""},body:JSON.stringify({type:r,definitions:n,rayId:e,theme:"both",organizationId:l.ORGANIZATION_ID})});if(t.status!==200)throw m.error("Diagram rendering service returned %s (rayId: %s)",t.status,e),new Error(`Something went wrong during rendering of ${r} diagrams`);return m.verboseTime(a,"Rendered %s diagrams (%s)",r,n.length),t.json()}async function T(r){if(r.length===0)return[];const n=new Map;for(let e=0;e<r.length;e++){const{type:t,source:i}=r[e],f=g(i,t);if(!u[f]){n.has(t)||n.set(t,[]);const o=n.get(t);o&&o.push({index:e,source:i})}}const s=[],a=new Map;for(const[e,t]of n){const i=t.map(o=>o.source),f=D(e,i).then(o=>{for(let c=0;c<t.length;c++){const d=o[c],h=g(t[c].source,e);if(d.error)m.error("Failed to render %s diagram: %s",e,d.error),a.set(h,{light:"",dark:"",error:`Failed to render ${e} diagram: ${d.error}`});else{const p={light:d.light??"",dark:d.dark??""};u[h]=p,a.set(h,p)}}}).catch(o=>{m.error("Failed to render %s diagrams: %s",e,o.message);for(const c of t){const d=g(c.source,e);a.set(d,{light:"",dark:"",error:`Failed to render ${e} diagram: ${o.message}`})}});s.push(f)}return await Promise.all(s),r.map(({type:e,source:t})=>{const i=g(t,e);return a.get(i)??u[i]})}async function M(r,n="light"){return(await T(r)).map(a=>R(a,n))}export{u as cache,g as generateDiagramHash,M as renderDiagrams,T as renderDiagramsWithVariants};
