import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as SourceFrame from"../source_frame/source_frame.js";import*as UI from"../ui/ui.js";const UIStrings={thisRequestHasNoResponseData:"This request has no response data available.",failedToLoadResponseData:"Failed to load response data"},str_=i18n.i18n.registerUIStrings("network/RequestResponseView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class RequestResponseView extends UI.Widget.VBox{constructor(e){super(),this.element.classList.add("request-view"),this.request=e,this._contentViewPromise=null}static _hasTextContent(e,t){const s=e.mimeType||"";let o=Common.ResourceType.ResourceType.fromMimeType(s);return o===Common.ResourceType.resourceTypes.Other&&(o=e.contentType()),o===Common.ResourceType.resourceTypes.Image?s.startsWith("image/svg"):!!o.isTextType()||!t.error&&(o===Common.ResourceType.resourceTypes.Other&&(Boolean(t.content)&&!t.encoded))}static async sourceViewForRequest(e){let t=requestToSourceView.get(e);if(void 0!==t)return t;const s=await e.contentData();if(!RequestResponseView._hasTextContent(e,s))return requestToSourceView.delete(e),null;const o=e.resourceType().canonicalMimeType()||e.mimeType;return t=SourceFrame.ResourceSourceFrame.ResourceSourceFrame.createSearchableView(e,o),requestToSourceView.set(e,t),t}wasShown(){this._doShowPreview()}_doShowPreview(){return this._contentViewPromise||(this._contentViewPromise=this.showPreview()),this._contentViewPromise}async showPreview(){const e=await this.createPreview();return e.show(this.element),e}async createPreview(){const e=await this.request.contentData(),t=await RequestResponseView.sourceViewForRequest(this.request);return e.content&&t||e.error?e.content&&t?t:new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.failedToLoadResponseData)):new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.thisRequestHasNoResponseData))}async revealLine(e){const t=await this._doShowPreview();t instanceof SourceFrame.ResourceSourceFrame.SearchableContainer&&t.revealPosition(e)}}const requestToSourceView=new WeakMap;