import*as Host from"../host/host.js";import*as ThemeSupport from"../theme_support/theme_support.js";import*as UI from"../ui/ui.js";const labelMap=new Map;export class TimelineGrid{constructor(){this.element=document.createElement("div"),UI.Utils.appendStyle(this.element,"perf_ui/timelineGrid.css",{enableLegacyPatching:!0}),this._dividersElement=this.element.createChild("div","resources-dividers"),this._gridHeaderElement=document.createElement("div"),this._gridHeaderElement.classList.add("timeline-grid-header"),this._eventDividersElement=this._gridHeaderElement.createChild("div","resources-event-dividers"),this._dividersLabelBarElement=this._gridHeaderElement.createChild("div","resources-dividers-label-bar"),this.element.appendChild(this._gridHeaderElement)}static calculateGridOffsets(e,i){const t=e.computePosition(e.maximumBoundary());let s=t/64,r=e.boundarySpan()/s;const d=t/e.boundarySpan(),n=Math.ceil(Math.log(r)/Math.LN10);r=Math.pow(10,n),r*d>=320&&(r/=5),r*d>=128&&(r/=2);const o=Math.ceil((e.minimumBoundary()-e.zeroTime())/r)*r+e.zeroTime();let l=e.maximumBoundary();l+=64/d,s=Math.ceil((l-o)/r),r||(s=0);const a=[];for(let m=0;m<s;++m){const t=o+r*m;e.computePosition(t)<(i||0)||a.push({position:Math.floor(e.computePosition(t)),time:t})}return{offsets:a,precision:Math.max(0,-Math.floor(Math.log(1.01*r)/Math.LN10))}}static drawCanvasGrid(e,i){e.save(),e.scale(window.devicePixelRatio,window.devicePixelRatio);const t=Math.floor(e.canvas.height/window.devicePixelRatio);e.strokeStyle=ThemeSupport.ThemeSupport.instance().patchColorText("rgba(0, 0, 0, 0.1)",ThemeSupport.ThemeSupport.ColorUsage.Foreground),e.lineWidth=1,e.translate(.5,.5),e.beginPath();for(const s of i.offsets)e.moveTo(s.position,0),e.lineTo(s.position,t);e.stroke(),e.restore()}static drawCanvasHeaders(e,i,t,s,r,d){e.save(),e.scale(window.devicePixelRatio,window.devicePixelRatio);const n=Math.ceil(e.canvas.width/window.devicePixelRatio);e.beginPath(),e.fillStyle=ThemeSupport.ThemeSupport.instance().patchColorText("rgba(255, 255, 255, 0.5)",ThemeSupport.ThemeSupport.ColorUsage.Background),e.fillRect(0,0,n,r),e.fillStyle=ThemeSupport.ThemeSupport.instance().patchColorText("#333",ThemeSupport.ThemeSupport.ColorUsage.Foreground),e.textBaseline="hanging",e.font="11px "+Host.Platform.fontFamily();for(const o of i.offsets){const i=t(o.time),r=e.measureText(i).width,n=o.position-r-4;(!d||d<n)&&e.fillText(i,n,s)}e.restore()}get dividersElement(){return this._dividersElement}get dividersLabelBarElement(){return this._dividersLabelBarElement}removeDividers(){this._dividersElement.removeChildren(),this._dividersLabelBarElement.removeChildren()}updateDividers(e,i){const t=TimelineGrid.calculateGridOffsets(e,i),s=t.offsets,r=t.precision,d=this._dividersElement.clientWidth;let n=this._dividersElement.firstChild,o=this._dividersLabelBarElement.firstChild;for(let l=0;l<s.length;++l){if(!n){n=document.createElement("div"),n.className="resources-divider",this._dividersElement.appendChild(n),o=document.createElement("div"),o.className="resources-divider";const e=document.createElement("div");e.className="resources-divider-label",labelMap.set(o,e),o.appendChild(e),this._dividersLabelBarElement.appendChild(o)}const i=s[l].time,t=s[l].position;if(o){const t=labelMap.get(o);t&&(t.textContent=e.formatValue(i,r))}const a=100*t/d;n.style.left=a+"%",o&&(o.style.left=a+"%"),n=n.nextSibling,o&&(o=o.nextSibling)}for(;n;){const e=n.nextSibling;if(this._dividersElement.removeChild(n),!e)break;n=e}for(;o;){const e=o.nextSibling;if(this._dividersLabelBarElement.removeChild(o),!e)break;o=e}return!0}addEventDivider(e){this._eventDividersElement.appendChild(e)}addEventDividers(e){this._gridHeaderElement.removeChild(this._eventDividersElement);for(const i of e)this._eventDividersElement.appendChild(i);this._gridHeaderElement.appendChild(this._eventDividersElement)}removeEventDividers(){this._eventDividersElement.removeChildren()}hideEventDividers(){this._eventDividersElement.classList.add("hidden")}showEventDividers(){this._eventDividersElement.classList.remove("hidden")}hideDividers(){this._dividersElement.classList.add("hidden")}showDividers(){this._dividersElement.classList.remove("hidden")}setScrollTop(e){this._dividersLabelBarElement.style.top=e+"px",this._eventDividersElement.style.top=e+"px"}}