import*as Common from"../common/common.js";import*as SDK from"../sdk/sdk.js";export class Importer{static requestsFromHARLog(e){const t=new Map;for(const r of e.pages)t.set(r.id,r);e.entries.sort(((e,t)=>e.startedDateTime.valueOf()-t.startedDateTime.valueOf()));const s=new Map,o=[];for(const r of e.entries){const e=r.pageref;let n=e?s.get(e):void 0;const c=n?n.mainRequest.url():r.request.url;let i=null;const u=r.customInitiator();u&&(i={type:u.type,url:u.url,lineNumber:u.lineNumber});const m=new SDK.NetworkRequest.NetworkRequest("har-"+o.length,r.request.url,c,"","",i),a=e?t.get(e):void 0;!n&&e&&a&&(n=Importer._buildPageLoad(a,m),s.set(e,n)),Importer._fillRequestFromHAREntry(m,r,n),n&&n.bindRequest(m),o.push(m)}return o}static _buildPageLoad(e,t){const s=new SDK.NetworkLog.PageLoad(t);return s.startTime=e.startedDateTime.valueOf(),s.contentLoadTime=1e3*Number(e.pageTimings.onContentLoad),s.loadTime=1e3*Number(e.pageTimings.onLoad),s}static _fillRequestFromHAREntry(e,t,s){t.request.postData?e.setRequestFormData(!0,t.request.postData.text):e.setRequestFormData(!1,null),e.connectionId=t.connection||"",e.requestMethod=t.request.method,e.setRequestHeaders(t.request.headers),t.response.content.mimeType&&"x-unknown"!==t.response.content.mimeType&&(e.mimeType=t.response.content.mimeType),e.responseHeaders=t.response.headers,e.statusCode=t.response.status,e.statusText=t.response.statusText;let o=t.response.httpVersion.toLowerCase();"http/2.0"===o&&(o="h2"),e.protocol=o.replace(/^http\/2\.0?\+quic/,"http/2+quic");const r=t.startedDateTime.getTime()/1e3;e.setIssueTime(r,r);const n=t.response.content.size>0?t.response.content.size:0,c=t.response.headersSize>0?t.response.headersSize:0,i=t.response.bodySize>0?t.response.bodySize:0;e.resourceSize=n||c+i;let u=t.response.customAsNumber("transferSize");void 0===u&&(u=t.response.headersSize+t.response.bodySize),e.setTransferSize(u>=0?u:0);const m=t.customAsString("fromCache");"memory"===m?e.setFromMemoryCache():"disk"===m&&e.setFromDiskCache();const a=t.response.content.text,p={error:null,content:a||null,encoded:"base64"===t.response.content.encoding};e.setContentDataProvider((async()=>p)),Importer._setupTiming(e,r,t.time,t.timings),e.setRemoteAddress(t.serverIPAddress||"",80),e.setResourceType(Importer._getResourceType(e,t,s));const d=t.customAsString("priority");d&&Protocol.Network.ResourcePriority.hasOwnProperty(d)&&e.setPriority(d);const l=t.customAsArray("webSocketMessages");if(l)for(const y of l){if(void 0===y.time)continue;if(!Object.values(SDK.NetworkRequest.WebSocketFrameType).includes(y.type))continue;if(void 0===y.opcode)continue;if(void 0===y.data)continue;const t=y.type===SDK.NetworkRequest.WebSocketFrameType.Send;e.addFrame({time:y.time,text:y.data,opCode:y.opcode,mask:t,type:y.type})}e.finished=!0}static _getResourceType(e,t,s){const o=t.customAsString("resourceType");if(o){const e=Common.ResourceType.ResourceType.fromName(o);if(e)return e}if(s&&s.mainRequest===e)return Common.ResourceType.resourceTypes.Document;const r=Common.ResourceType.ResourceType.fromMimeType(t.response.content.mimeType);if(r!==Common.ResourceType.resourceTypes.Other)return r;const n=Common.ResourceType.ResourceType.fromURL(t.request.url);return n||Common.ResourceType.resourceTypes.Other}static _setupTiming(e,t,s,o){function r(e){return void 0===e||e<0?-1:(n+=e,n)}let n=o.blocked&&o.blocked>=0?o.blocked:0;const c=o.customAsNumber("blocked_proxy")||-1,i=o.customAsNumber("blocked_queueing")||-1,u=o.ssl&&o.ssl>=0?o.ssl:0;o.connect&&o.connect>0&&(o.connect-=u);const m={proxyStart:c>0?n-c:-1,proxyEnd:c>0?n:-1,requestTime:t+(i>0?i:0)/1e3,dnsStart:o.dns&&o.dns>=0?n:-1,dnsEnd:r(o.dns),connectStart:o.connect&&o.connect>=0?n:-1,connectEnd:r(o.connect)+u,sslStart:o.ssl&&o.ssl>=0?n:-1,sslEnd:r(o.ssl),workerStart:-1,workerReady:-1,workerFetchStart:-1,workerRespondWithSettled:-1,sendStart:o.send>=0?n:-1,sendEnd:r(o.send),pushStart:0,pushEnd:0,receiveHeadersEnd:r(o.wait)};r(o.receive),e.timing=m,e.endTime=t+Math.max(s,n)/1e3}}