const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./core-k3PRMzIA.js","./Axis-D3q4j9rs.js","./graphic-DeMuFa6Q.js","./createSeriesData-BMcKzd6A.js","./charts-CIooAfJO.js","./customGraphicKeyframeAnimation-C08fYDZH.js","./components-DpplR_Zx.js","./renderers-CAT2F87m.js"])))=>i.map(i=>d[i]); import{i as a,a as g,j as b,H as oe,B as G,r as u,T as ie,b as ae,_ as A}from"./index-C7lE_1mm.js";const le=2,ce=e=>{const t=e.map(s=>s.v===!0),n=t.flatMap((s,o)=>s&&t[o-1]!==!0?[o]:[]),r=t.flatMap((s,o)=>s&&t[o+1]!==!0?[o]:[]);return n.map((s,o)=>({startIdx:s,endIdx:r[o]}))},ue=({points:e,startIdx:t,endIdx:n})=>n-t+1ce(e).map(({startIdx:t,endIdx:n})=>ue({points:e,startIdx:t,endIdx:n})),de=({liveSelected:e,optionSelected:t,legendNames:n})=>{if(a(e))return t;const r=Object.fromEntries(n.flatMap(s=>{const o=e[s];return g(o)?[[s,o]]:[]}));return{...t,...r}},me=(e,t)=>{const n=e?.dataZoom?.[0],r=t.dataZoom;return a(n)||typeof n.start!="number"||a(r)?t:{...t,dataZoom:r.map(s=>({...s,start:n.start,end:n.end}))}},pe=(e,t)=>{const n=t.legend,r=n?.data;return a(n)||a(r)?t:{...t,legend:{...n,selected:de({liveSelected:e?.legend?.[0]?.selected??null,optionSelected:n.selected??{},legendNames:r})}}},fe=({liveOption:e,option:t})=>pe(e,me(e,t)),Z=150,X="h-auto px-2 py-1 text-muted-foreground text-xs hover:text-foreground",ge=({chartInstance:e})=>{const t=s=>{const o=e.current;a(o)||o.dispatchAction({type:s})},n=()=>t("legendAllSelect"),r=()=>{t("legendAllSelect"),t("legendInverseSelect")};return b.jsxs(oe,{gap:1,align:"center",className:"absolute top-0.5 right-2 z-10 justify-end",style:{width:Z},children:[b.jsx(G,{variant:"ghost",size:"sm",onClick:n,className:X,children:"Show all"}),b.jsx(G,{variant:"ghost",size:"sm",onClick:r,className:X,children:"Hide all"})]})},he={"&":"&","<":"<",">":">",'"':""","'":"'"},_e=e=>e.replace(/[&<>"']/g,t=>he[t]??t),B=["#3b82f6","#a855f7","#10b981","#f59e0b","#ef4444","#06b6d4","#ec4899","#84cc16"],$=["rgba(139, 92, 246, 1)","rgba(236, 72, 153, 1)","rgba(6, 182, 212, 1)","rgba(245, 158, 11, 1)","rgba(34, 197, 94, 1)","rgba(239, 68, 68, 1)","rgba(59, 130, 246, 1)","rgba(168, 162, 158, 1)"],xe=18,H=11,j=6,be=1,Ee=36,F=24,ye=48,q=48,Ie=48,U=110,Ae=95,Se=1.5,Le=(e,t)=>g(e)?e:B[t%B.length],Te=(e,t)=>g(e)?e:$[t%$.length],we=e=>Object.keys(e).sort((t,n)=>Number(t)-Number(n)),ve=e=>{const t=e.find(s=>g(s.quantiles)&&Object.keys(s.quantiles).length>=2);if(a(t))return null;const n=t.quantiles;if(a(n))return null;const r=we(n);return r.length<2?null:{lowerKey:r[0],upperKey:r[r.length-1]}},Re=e=>e.map(t=>[Date.parse(t.t),t.v]),z=({points:e,key:t})=>e.map(n=>{const r=n.quantiles?.[t];return[Date.parse(n.t),g(r)?r:null]}),Ne=({upper:e,lower:t})=>e.map(([n,r],s)=>{const o=t[s]?.[1];return a(r)||a(o)?[n,null]:[n,r-o]}),Pe=(e,t)=>{if(!/^#[0-9a-fA-F]{6}$/.test(e))return e;const n=Math.max(0,Math.min(1,t)),r=Math.round(n*255).toString(16).padStart(2,"0");return`${e}${r}`},ke=({series:e,color:t})=>{const n=ve(e.points),r=e.points.length===1,s={option:{id:`series:${e.id}`,name:e.label,type:"line",xAxisIndex:0,yAxisIndex:0,data:Re(e.points),color:t,lineStyle:{width:e.lineWidth??Se},symbol:"circle",symbolSize:4,showSymbol:r,smooth:!1,connectNulls:!1},sourceSeriesId:e.id,isLegendItem:!0};if(a(n))return[s];const o=z({points:e.points,key:n.lowerKey}),d=z({points:e.points,key:n.upperKey}),l=Ne({upper:d,lower:o}),m={option:{id:`band-lower:${e.id}`,name:`${e.label} band lower`,type:"line",xAxisIndex:0,yAxisIndex:0,stack:`band:${e.id}`,data:o,symbol:"none",silent:!0,lineStyle:{opacity:0},areaStyle:{color:"transparent"},tooltip:{show:!1},z:1},sourceSeriesId:null,isLegendItem:!1},c={option:{id:`band-upper:${e.id}`,name:`${e.label} band upper`,type:"line",xAxisIndex:0,yAxisIndex:0,stack:`band:${e.id}`,data:l,symbol:"none",silent:!0,lineStyle:{opacity:0},areaStyle:{color:Pe(t,.18)},tooltip:{show:!1},z:1},sourceSeriesId:null,isLegendItem:!1};return[m,c,s]},Ce=({color:e,bars:t})=>(n,r)=>{const s=t[n.dataIndex];if(a(s))return;const o=r.coord([s.value[0],s.value[2]]),d=r.coord([s.value[1],s.value[2]]),l=s.marker.kind==="point",m=l?o[0]-j/2:o[0],c=l?j:Math.max(1,d[0]-o[0]-be),p=o[1]-H/2;return{type:"rect",shape:{x:m,y:p,width:c,height:H,r:2},style:{fill:e,stroke:e,lineWidth:1},emphasis:{style:{fill:e,stroke:e,lineWidth:2,shadowBlur:4,shadowColor:"rgba(0, 0, 0, 0.4)"}}}},De=({marker:e,laneCategory:t})=>e.kind==="range"?{value:[e.startTimestamp,e.endTimestamp,t],marker:e}:{value:[e.timestamp,e.timestamp,t],marker:e},Oe=({series:e,color:t,laneXAxisIndex:n,laneYAxisIndex:r})=>{const s=W(e.points);if(s.length===0)return null;const o=s.map(l=>De({marker:l,laneCategory:e.label}));return{option:{id:`series:${e.id}`,name:e.label,type:"custom",coordinateSystem:"cartesian2d",xAxisIndex:n,yAxisIndex:r,encode:{x:[0,1],y:2},z:50,itemStyle:{color:t},renderItem:Ce({color:t,bars:o}),data:o.map(l=>({name:`${e.id}:${l.value[0]}`,value:l.value})),tooltip:{show:!0,trigger:"item",formatter:l=>{const m=l.dataIndex,c=o[m];if(a(c))return"";const p=_e(e.label);if(c.marker.kind==="range"){const h=new Date(c.marker.startTimestamp).toUTCString(),i=new Date(c.marker.endTimestamp).toUTCString();return`${p}
${h} → ${i}`}const _=new Date(c.marker.timestamp).toUTCString();return`${p}
${_}`}}},sourceSeriesId:e.id,isLegendItem:!1}},K=e=>[{type:"inside",xAxisIndex:e}],Me=4,Ge=8,Xe=24,Be=4,$e=({legendNames:e,legendSelected:t,showLegendActions:n})=>e.length===0?{show:!1}:{type:"scroll",top:Be,left:Ge,right:n?Z:Xe,selected:t,data:[...e]},He=({entries:e,legend:t})=>({animation:!1,useUTC:!0,grid:{left:Ie,right:24,top:q,bottom:ye},xAxis:{type:"time"},yAxis:{type:"value",scale:!0},tooltip:{trigger:"axis",axisPointer:{type:"line"}},dataZoom:K(0),legend:t,series:e.map(n=>n.option)}),je=({numericEntries:e,binaryEntries:t,laneCategories:n,legend:r})=>{const s=n.length*xe,o=F+s+Ee,d={left:U,right:24,top:q,bottom:o},l={left:U,right:24,bottom:F,height:s,backgroundColor:"rgba(255,255,255,0.03)"},m={type:"time",gridIndex:0},c={type:"value",scale:!0,gridIndex:0},p={color:"rgba(255,255,255,0.05)",type:"solid",width:1};return{animation:!1,useUTC:!0,grid:[d,l],xAxis:[m,{type:"time",gridIndex:1,axisLabel:{show:!1},axisLine:{show:!1,lineStyle:{color:"transparent"}},axisTick:{show:!1},splitLine:{show:!0,lineStyle:p},splitArea:{show:!1}}],yAxis:[c,{type:"category",gridIndex:1,inverse:!0,data:n,axisTick:{show:!1},axisLine:{show:!1,lineStyle:{color:"transparent"}},splitLine:{show:!0,lineStyle:p},splitArea:{show:!1},axisLabel:{fontSize:11,interval:0,hideOverlap:!1,width:Ae,overflow:"truncate",ellipsis:"…"}}],tooltip:{trigger:"axis",axisPointer:{type:"line"}},axisPointer:{link:[{xAxisIndex:[0,1]}]},dataZoom:K([0,1]),legend:r,series:[...e,...t].map(i=>i.option)}},Fe="#64748b",Ue=e=>({silent:!0,symbol:"none",animation:!1,data:e.map(t=>({xAxis:Date.parse(t.t),lineStyle:{color:t.color??Fe,type:"dashed",width:1},label:g(t.label)?{show:!0,formatter:t.label}:{show:!1}}))}),ze=({entries:e,referenceLines:t})=>{if(a(t)||t.length===0)return e;const n=e.findIndex(s=>s.isLegendItem);if(n<0)return e;const r=Ue(t);return e.map((s,o)=>o===n?{...s,option:{...s.option,markLine:r}}:s)},We=({legendEntries:e,hiddenByDefaultSeriesIds:t})=>{if(a(t)||t.length===0)return{};const n=new Set(t);return Object.fromEntries(e.filter(r=>g(r.sourceSeriesId)&&n.has(r.sourceSeriesId)).map(r=>[r.option.name,!1]))},Ze=(e,t=null,n=null)=>{const r=e.filter(i=>i.kind==="numeric"),s=e.filter(i=>i.kind==="binary"),o=ze({entries:r.flatMap((i,E)=>ke({series:i,color:Le(i.color,E)})),referenceLines:t}),d=s.flatMap((i,E)=>{const I=Oe({series:i,color:Te(i.color,E),laneXAxisIndex:1,laneYAxisIndex:1});return g(I)?[{entry:i,laneEntry:I}]:[]}),l=o.filter(i=>i.isLegendItem),m=l.map(i=>i.option.name),c=new Set(m).size>=Me,p=$e({legendNames:m,legendSelected:We({legendEntries:l,hiddenByDefaultSeriesIds:n}),showLegendActions:c});if(d.length===0)return{option:He({entries:o,legend:p}),entries:o,showLegendActions:c};const _=d.map(i=>i.laneEntry),h=d.map(i=>i.entry.label);return{option:je({numericEntries:o,binaryEntries:_,laneCategories:h,legend:p}),entries:[...o,..._],showLegendActions:c}},qe=({series:e,dataIndex:t})=>{const n=W(e.points)[t];if(a(n))return null;const r=n.kind==="range"?n.startIdx:n.idx;return e.points[r]??null},Ke=({series:e,dataIndex:t})=>{const n=e.points[t];return g(n)?n:null},Qe=({series:e,onPointClick:t,onChartReady:n,referenceLines:r,height:s,hiddenByDefaultSeriesIds:o})=>{const d=u.useRef(null),l=u.useRef(null),[m,c]=u.useState(!1),[p,_]=u.useState(!1),h=e.length>0,{option:i,entries:E,showLegendActions:I}=u.useMemo(()=>Ze(e,r,o),[e,r,o]),S=u.useMemo(()=>new Map(e.map(f=>[f.id,f])),[e]),N=u.useRef(t),L=u.useRef(n),P=u.useRef(E),k=u.useRef(S);return u.useEffect(()=>{N.current=t},[t]),u.useEffect(()=>{L.current=n},[n]),u.useEffect(()=>{P.current=E},[E]),u.useEffect(()=>{k.current=S},[S]),u.useEffect(()=>{_(!1)},[e]),u.useEffect(()=>{if(!h||p)return;const f=d.current;if(a(f))return;const x={current:!0};return(async()=>{try{const[T,{CustomChart:V,LineChart:Y},{AxisPointerComponent:Q,DataZoomComponent:J,GridComponent:ee,LegendComponent:te,MarkLineComponent:ne,TooltipComponent:re},{CanvasRenderer:se}]=await Promise.all([A(()=>import("./core-k3PRMzIA.js"),__vite__mapDeps([0,1,2,3]),import.meta.url),A(()=>import("./charts-CIooAfJO.js"),__vite__mapDeps([4,2,1,5,3]),import.meta.url),A(()=>import("./components-DpplR_Zx.js"),__vite__mapDeps([6,2,1,5]),import.meta.url),A(()=>import("./renderers-CAT2F87m.js"),__vite__mapDeps([7,2]),import.meta.url)]);if(!x.current||a(d.current))return;T.use([Y,V,Q,J,ee,te,ne,re,se]);const w=T.init(d.current);l.current=w,w.on("click",C=>{const D=N.current;if(a(D))return;const O=C.seriesIndex;if(typeof O!="number")return;const v=P.current[O];if(a(v)||a(v.sourceSeriesId))return;const y=k.current.get(v.sourceSeriesId);if(a(y))return;const R=C.dataIndex;if(typeof R!="number")return;const M=y.kind==="numeric"?Ke({series:y,dataIndex:R}):qe({series:y,dataIndex:R});a(M)||D({seriesId:y.id,point:M})}),L.current?.(w),c(!0)}catch{x.current&&_(!0)}})(),()=>{x.current=!1,c(!1),g(l.current)&&(L.current?.(null),l.current.dispose(),l.current=null)}},[h,p]),u.useEffect(()=>{if(!m)return;const f=l.current;if(a(f))return;const x=f.getOption();f.setOption(fe({liveOption:x,option:i}),{notMerge:!0})},[i,m]),u.useEffect(()=>{if(!h)return;const f=d.current;if(a(f))return;const x=new ResizeObserver(()=>{l.current?.resize()});return x.observe(f),()=>x.disconnect()},[h]),h?p?b.jsx(ae,{height:s,message:"Failed to load the chart renderer."}):b.jsxs("div",{className:"relative h-full w-full",style:g(s)?{height:s}:void 0,children:[m&&I&&b.jsx(ge,{chartInstance:l}),b.jsx("div",{ref:d,className:"h-full w-full overflow-hidden","data-testid":"time-series-chart-canvas"})]}):b.jsx(ie,{height:s})};export{Qe as TimeSeriesChartCanvas};