import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as UI from"../ui/ui.js";import{FontView}from"./FontView.js";import{ImageView}from"./ImageView.js";import{JSONView}from"./JSONView.js";import{ResourceSourceFrame}from"./ResourceSourceFrame.js";import{XMLView}from"./XMLView.js";const UIStrings={nothingToPreview:"Nothing to preview"},str_=i18n.i18n.registerUIStrings("source_frame/PreviewFactory.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class PreviewFactory{static async createPreview(e,r){let t=Common.ResourceType.ResourceType.fromMimeType(r);switch(t===Common.ResourceType.resourceTypes.Other&&(t=e.contentType()),t){case Common.ResourceType.resourceTypes.Image:return new ImageView(r,e);case Common.ResourceType.resourceTypes.Font:return new FontView(r,e)}const o=await e.requestContent();if(null===o.content)return new UI.EmptyWidget.EmptyWidget(o.error);if(!o.content)return new UI.EmptyWidget.EmptyWidget(i18nString(UIStrings.nothingToPreview));let i=o.content;await e.contentEncoded()&&(i=window.atob(i));const n=XMLView.parseXML(i,r);if(n)return XMLView.createSearchableView(n);const s=await JSONView.createView(i);if(s)return s;if(t.isTextType()){const t=r.replace(/;.*/,"")||e.contentType().canonicalMimeType();return ResourceSourceFrame.createSearchableView(e,t,!0)}return null}}