import*as i18n from"../i18n/i18n.js";import*as SourceFrame from"../source_frame/source_frame.js";import*as TextUtils from"../text_utils/text_utils.js";import*as UI from"../ui/ui.js";import{RequestHTMLView}from"./RequestHTMLView.js";import{RequestResponseView}from"./RequestResponseView.js";import{SignedExchangeInfoView}from"./SignedExchangeInfoView.js";const UIStrings={failedToLoadResponseData:"Failed to load response data",previewNotAvailable:"Preview not available"},str_=i18n.i18n.registerUIStrings("network/RequestPreviewView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class RequestPreviewView extends RequestResponseView{constructor(e){super(e)}async showPreview(){const e=await super.showPreview();if(!(e instanceof UI.View.SimpleView))return e;const t=new UI.Toolbar.Toolbar("network-item-preview-toolbar",this.element);return e.toolbarItems().then((e=>{e.map((e=>t.appendToolbarItem(e)))})),e}async _htmlPreview(){const e=await this.request.contentData();if(e.error)return new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.failedToLoadResponseData));if(!new Set(["text/html","text/plain","application/xhtml+xml"]).has(this.request.mimeType))return null;const t=e.encoded?window.atob(e.content):e.content,i=await SourceFrame.JSONView.JSONView.createView(t);if(i)return i;const r=TextUtils.ContentProvider.contentAsDataURL(e.content,this.request.mimeType,e.encoded,this.request.charset());return r?new RequestHTMLView(r):null}async createPreview(){if(this.request.signedExchangeInfo())return new SignedExchangeInfoView(this.request);const e=await this._htmlPreview();if(e)return e;const t=await SourceFrame.PreviewFactory.PreviewFactory.createPreview(this.request,this.request.mimeType);return t||new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.previewNotAvailable))}}