import*as Components from"../components/components.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const UIStrings={thisRequestHasNoInitiatorData:"This request has no initiator data.",requestCallStack:"Request call stack",requestInitiatorChain:"Request initiator chain"},str_=i18n.i18n.registerUIStrings("network/RequestInitiatorView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class RequestInitiatorView extends UI.Widget.VBox{constructor(e){super(),this.registerRequiredCSS("network/requestInitiatorView.css",{enableLegacyPatching:!1}),this.element.classList.add("request-initiator-view"),this._linkifier=new Components.Linkifier.Linkifier,this._request=e,this._emptyWidget=new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.thisRequestHasNoInitiatorData)),this._emptyWidget.show(this.element),this._hasShown=!1}static createStackTracePreview(e,t,i,n){const s=e.initiator();if(!s||!s.stack)return null;const r=SDK.NetworkManager.NetworkManager.forRequest(e),a=r?r.target():null;return Components.JSPresentationUtils.buildStackTracePreviewContents(a,t,{stackTrace:s.stack,contentUpdated:n,tabStops:i})}_createTree(){const e=new UI.TreeOutline.TreeOutlineInShadow;return e.registerRequiredCSS("network/requestInitiatorViewTree.css",{enableLegacyPatching:!1}),e.contentElement.classList.add("request-initiator-view-tree"),e}_buildRequestChainTree(e,t,i){const n=new UI.TreeOutline.TreeElement(t);i.appendChild(n),n.titleElement instanceof HTMLElement&&n.titleElement.classList.add("request-initiator-view-section-title");const s=e.initiators;let r=n;for(const l of Array.from(s).reverse()){const e=new UI.TreeOutline.TreeElement(l.url());r.appendChild(e),r.expand(),r=e}n.expand(),r.select();const a=r.titleElement;a instanceof HTMLElement&&(a.style.fontWeight="bold");const o=e.initiated;return this._depthFirstSearchTreeBuilder(o,r,this._request),n}_depthFirstSearchTreeBuilder(e,t,i){const n=new Set;n.add(this._request);for(const s of e.keys())if(e.get(s)===i){const i=new UI.TreeOutline.TreeElement(s.url());t.appendChild(i),t.expand(),n.has(s)||(n.add(s),this._depthFirstSearchTreeBuilder(e,i,s))}}_buildStackTraceSection(e,t,i){const n=new UI.TreeOutline.TreeElement(t);i.appendChild(n),n.titleElement instanceof HTMLElement&&n.titleElement.classList.add("request-initiator-view-section-title");const s=new UI.TreeOutline.TreeElement(e,!1);s.selectable=!1,n.appendChild(s),n.expand()}wasShown(){if(this._hasShown)return;let e=!1;const t=this._createTree(),i=RequestInitiatorView.createStackTracePreview(this._request,this._linkifier,!0);i&&(e=!0,this._buildStackTraceSection(i.element,i18nString(UIStrings.requestCallStack),t));const n=SDK.NetworkLog.NetworkLog.instance().initiatorGraphForRequest(this._request);(n.initiators.size>1||n.initiated.size>1)&&(e=!0,this._buildRequestChainTree(n,i18nString(UIStrings.requestInitiatorChain),t));const s=t.firstChild();s&&s.select(!0),e&&(this.element.appendChild(t.element),this._emptyWidget.hideWidget()),this._hasShown=!0}}