import*as Common from"../common/common.js";import*as Components from"../components/components.js";import*as Host from"../host/host.js";import*as i18n from"../i18n/i18n.js";import*as Platform from"../platform/platform.js";import*as Root from"../root/root.js";import*as SDK from"../sdk/sdk.js";import*as ThemeSupport from"../theme_support/theme_support.js";import*as Timeline from"../timeline/timeline.js";import*as UI from"../ui/ui.js";import*as Workspace from"../workspace/workspace.js";const UIStrings={viewOriginalTrace:"View Original Trace",viewTrace:"View Trace",thePerformanceMetricsAboveAre:'The performance metrics above are simulated and won\'t match the timings found in this trace. Disable simulated throttling in "Lighthouse Settings" if you want the timings to match.'},str_=i18n.i18n.registerUIStrings("lighthouse/LighthouseReportRenderer.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_),MaxLengthForLinks=40;export class LighthouseReportRenderer extends self.ReportRenderer{constructor(e){super(e)}static addViewTraceButton(e,t){if(!t||!t.traces||!t.traces.defaultPass)return;const i="simulate"===t.settings.throttlingMethod,o=e.querySelector(".lh-audit-group");if(!o)return;const r=o.querySelector(".lh-metrics__disclaimer");if(!r)return;const n=t.traces.defaultPass,s=i18nString(i?UIStrings.viewOriginalTrace:UIStrings.viewTrace),a=UI.UIUtils.createTextButton(s,(async function(){Host.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace),await UI.InspectorView.InspectorView.instance().showPanel("timeline"),Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(n.traceEvents)}),"view-trace");i&&UI.Tooltip.Tooltip.install(a,i18nString(UIStrings.thePerformanceMetricsAboveAre)),o.insertBefore(a,r.nextSibling)}static async linkifyNodeDetails(e){const t=SDK.SDKModel.TargetManager.instance().mainTarget();if(!t)return;const i=t.model(SDK.DOMModel.DOMModel);if(i)for(const o of e.getElementsByClassName("lh-node")){const e=o,t=e.dataset;if(!t.path)continue;const r=await i.pushNodeByPathToFrontend(t.path);if(!r)continue;const n=i.nodeForId(r);if(!n)continue;const s=await Common.Linkifier.Linkifier.linkify(n,{tooltip:t.snippet,preventKeyboardFocus:void 0});UI.Tooltip.Tooltip.install(e,"");const a=e.querySelector(".lh-element-screenshot");e.textContent="",a&&e.append(a),e.appendChild(s)}}static async linkifySourceLocationDetails(e){for(const t of e.getElementsByClassName("lh-source-location")){const e=t,i=e.dataset;if(!i.sourceUrl||!i.sourceLine||!i.sourceColumn)continue;const o=i.sourceUrl,r=Number(i.sourceLine),n=Number(i.sourceColumn),s=await Components.Linkifier.Linkifier.linkifyURL(o,{lineNumber:r,columnNumber:n,maxLength:40,bypassURLTrimming:void 0,className:void 0,preventClick:void 0,tabStop:void 0,text:void 0});UI.Tooltip.Tooltip.install(e,""),e.textContent="",e.appendChild(s)}}static handleDarkMode(e){"dark"===ThemeSupport.ThemeSupport.instance().themeName()&&e.classList.add("dark")}}export class LighthouseReportUIFeatures extends self.ReportUIFeatures{constructor(e){super(e),this._beforePrint=null,this._afterPrint=null}setBeforePrint(e){this._beforePrint=e}setAfterPrint(e){this._afterPrint=e}getReportHtml(){return this.resetUIState(),Lighthouse.ReportGenerator.generateReportHtml(this.json)}async _saveFile(e){const t=`${new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain().replace(/[^a-z0-9.-]+/gi,"_")}-${Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime))}${e.type.match("json")?".json":".html"}`,i=await e.text();Workspace.FileManager.FileManager.instance().save(t,i,!0)}async _print(){const e=this.getDocument().querySelector(".lh-root").cloneNode(!0),t=window.open("","_blank","channelmode=1,status=1,resizable=1");if(!t)return;const i=t.document.createElement("style");i.textContent=Root.Runtime.cachedResources.get("third_party/lighthouse/report-assets/report.css")||"",t.document.head.appendChild(i),t.document.body.replaceWith(e),await LighthouseReportRenderer.linkifyNodeDetails(e),this._beforePrint&&this._beforePrint(),t.focus(),t.print(),t.close(),this._afterPrint&&this._afterPrint()}getDocument(){return this._document}resetUIState(){this._resetUIState()}}