import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={version:"Version",updateActivity:"Update Activity",timeline:"Timeline",startTimeS:"Start time: {PH1}",endTimeS:"End time: {PH1}"},str_=i18n.i18n.registerUIStrings("resources/ServiceWorkerUpdateCycleView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ServiceWorkerUpdateCycleView{static calculateServiceWorkerUpdateRanges(e){function t(e,t){t.start<Number.MAX_VALUE&&t.start<=t.end&&e.push(t)}function r(e){let r=e.currentState,i=0,s=0,a=0,n=0;const o=r.status;if(o===Protocol.ServiceWorker.ServiceWorkerVersionStatus.New)return[];for(;r;)r.status===Protocol.ServiceWorker.ServiceWorkerVersionStatus.Activated?i=r.last_updated_timestamp:r.status===Protocol.ServiceWorker.ServiceWorkerVersionStatus.Activating?(0===i&&(i=r.last_updated_timestamp),s=r.last_updated_timestamp):r.status===Protocol.ServiceWorker.ServiceWorkerVersionStatus.Installed?a=r.last_updated_timestamp:r.status===Protocol.ServiceWorker.ServiceWorkerVersionStatus.Installing&&(0===a&&(a=r.last_updated_timestamp),n=r.last_updated_timestamp),r=r.previousState;const c=[];return function(e,r,i,s,a,n,o){t(e,{id:r,phase:"Install",start:i,end:s}),o!==Protocol.ServiceWorker.ServiceWorkerVersionStatus.Activating&&o!==Protocol.ServiceWorker.ServiceWorkerVersionStatus.Activated&&o!==Protocol.ServiceWorker.ServiceWorkerVersionStatus.Redundant||(t(e,{id:r,phase:"Wait",start:s,end:a}),t(e,{id:r,phase:"Activate",start:a,end:n}))}(c,e.id,n,a,s,i,o),c}const i=e.versionsByMode(),s=[SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Active,SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Waiting,SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Installing,SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Redundant];for(const a of s){const e=i.get(a);if(e){return r(e)}}return[]}static createTimingTable(e){const t=document.createElement("table");t.classList.add("service-worker-update-timing-table"),UI.Utils.appendStyle(t,"resources/serviceWorkerUpdateCycleView.css");const r=this.calculateServiceWorkerUpdateRanges(e);return this.updateTimingTable(t,r),t}static createTimingTableHead(e){const t=e.createChild("tr","service-worker-update-timing-table-header");UI.UIUtils.createTextChild(t.createChild("td"),i18nString(UIStrings.version)),UI.UIUtils.createTextChild(t.createChild("td"),i18nString(UIStrings.updateActivity)),UI.UIUtils.createTextChild(t.createChild("td"),i18nString(UIStrings.timeline))}static removeRows(e){const t=e.getElementsByTagName("tr");for(;t[0];)t[0].parentNode&&t[0].parentNode.removeChild(t[0])}static updateTimingTable(e,t){this.removeRows(e),this.createTimingTableHead(e);const r=t;if(0===r.length)return;const i=r.map((e=>e.start)),s=r.map((e=>e.end)),a=i.reduce(((e,t)=>Math.min(e,t))),n=s.reduce(((e,t)=>Math.max(e,t))),o=100/(n-a);for(const c of r){const t=c.phase,r=o*(c.start-a),i=o*(n-c.end),s=e.createChild("tr"),d=s.createChild("td");UI.UIUtils.createTextChild(d,"#"+c.id),d.classList.add("service-worker-update-timing-bar-clickable"),d.setAttribute("tabindex","0"),d.setAttribute("role","switch"),UI.ARIAUtils.setChecked(d,!1);const l=s.createChild("td");UI.UIUtils.createTextChild(l,t),this.constructUpdateDetails(e,s,c);const u=s.createChild("td").createChild("div","service-worker-update-timing-row").createChild("span","service-worker-update-timing-bar "+t.toLowerCase());u.style.left=r+"%",u.style.right=i+"%",u.textContent="\u200b"}}static constructUpdateDetails(e,t,r){const i=e.createChild("tr","service-worker-update-timing-bar-details");i.classList.add("service-worker-update-timing-bar-details-collapsed"),self.onInvokeElement(t,(e=>this.onToggleUpdateDetails(i,e)));const s=new UI.TreeOutline.TreeOutlineInShadow;i.appendChild(s.element);const a=document.createElementWithClass("div","service-worker-update-details-treeitem"),n=new Date(r.start).toISOString();a.textContent=i18nString(UIStrings.startTimeS,{PH1:n});const o=new UI.TreeOutline.TreeElement(a);s.appendChild(o);const c=document.createElementWithClass("div","service-worker-update-details-treeitem"),d=new Date(r.end).toISOString();c.textContent=i18nString(UIStrings.endTimeS,{PH1:d});const l=new UI.TreeOutline.TreeElement(c);s.appendChild(l)}static onToggleUpdateDetails(e,t){if(!t.target)return;const r=t.target;if(r.classList.contains("service-worker-update-timing-bar-clickable")){e.classList.toggle("service-worker-update-timing-bar-details-collapsed"),e.classList.toggle("service-worker-update-timing-bar-details-expanded");const t="true"===r.getAttribute("aria-checked");UI.ARIAUtils.setChecked(r,!t)}}static refresh(e,t){const r=this.calculateServiceWorkerUpdateRanges(t);this.updateTimingTable(e,r)}}