import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";const UIStrings={url:"URL"},str_=i18n.i18n.registerUIStrings("network/NetworkSearchScope.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class NetworkSearchScope{performIndexing(e){queueMicrotask((()=>{e.done()}))}async performSearch(e,t,r,s){const n=[],o=SDK.NetworkLog.NetworkLog.instance().requests().filter((t=>e.filePathMatchesFileQuery(t.url())));t.setTotalWork(o.length);for(const i of o){const r=this._searchRequest(e,i,t);n.push(r)}const a=(await Promise.all(n)).filter((e=>null!==e));if(t.isCanceled())s(!1);else{for(const e of a.sort(((e,t)=>e.label().localeCompare(t.label()))))e.matchesCount()>0&&r(e);t.done(),s(!0)}}async _searchRequest(e,t,r){let s=[];if(t.contentType().isTextType()&&(s=await t.searchInContent(e.query(),!e.ignoreCase(),e.isRegex())),r.isCanceled())return null;const n=[];a(t.url())&&n.push(UIRequestLocation.urlMatch(t));for(const i of t.requestHeaders())o(i)&&n.push(UIRequestLocation.requestHeaderMatch(t,i));for(const i of t.responseHeaders)o(i)&&n.push(UIRequestLocation.responseHeaderMatch(t,i));for(const i of s)n.push(UIRequestLocation.bodyMatch(t,i));return r.worked(),new NetworkSearchResult(t,n);function o(e){return a(`${e.name}: ${e.value}`)}function a(t){const r=e.ignoreCase()?"i":"",s=e.queries().map((e=>new RegExp(e,r)));let n=0;for(const e of s){const r=t.substr(n).match(e);if(!r||!r.index)return!1;n+=r.index+r[0].length}return!0}}stopSearch(){}}export class UIRequestLocation{constructor(e,t,r,s,n){this.request=e,this.requestHeader=t,this.responseHeader=r,this.searchMatch=s,this.isUrlMatch=n}static requestHeaderMatch(e,t){return new UIRequestLocation(e,t,null,null,!1)}static responseHeaderMatch(e,t){return new UIRequestLocation(e,null,t,null,!1)}static bodyMatch(e,t){return new UIRequestLocation(e,null,null,t,!1)}static urlMatch(e){return new UIRequestLocation(e,null,null,null,!0)}}export class NetworkSearchResult{constructor(e,t){this._request=e,this._locations=t}matchesCount(){return this._locations.length}label(){return this._request.displayName}description(){const e=this._request.parsedURL;return e?e.urlWithoutScheme():this._request.url()}matchLineContent(e){const t=this._locations[e];if(t.isUrlMatch)return this._request.url();const r=t.requestHeader||t.responseHeader;return r?r.value:t.searchMatch.lineContent}matchRevealable(e){return this._locations[e]}matchLabel(e){const t=this._locations[e];if(t.isUrlMatch)return i18nString(UIStrings.url);const r=t.requestHeader||t.responseHeader;return r?`${r.name}:`:t.searchMatch.lineNumber+1}}