"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("vue"),P=require("@aplus-frontend/antdv"),q=require("@aplus-frontend/oss");require("../../config-provider/index.js");require("./components/error-result/index.vue.js");const N=require("./style/index.js"),O=require("./utils/default-parser-plugin.js"),v=require("../../config-provider/hooks/use-global-config.js"),S=require("../../config-provider/hooks/use-namespace.js"),U=require("../../config-provider/hooks/use-locale.js"),g=require("./components/error-result/index.vue2.js"),F=e.defineComponent({__name:"ap-file-preview",props:{src:{},fileName:{},format:{},parserPlugin:{},getOssAccess:{type:Function}},setup(d){const t=d,k=v.useGlobalConfig("apUpload"),h=v.useGlobalConfig("apFilePreview"),{b:l}=S.useNamespace("ap-file-preview"),_=N.default("ap-file-preview"),{lang:C}=U.useLocale(),y={"zh-cn":"zh_CN",en:"en_US"},r=e.ref(),o=e.ref(!1),u=e.computed(()=>t.getOssAccess||k.value?.getOssAccess),s=e.computed(()=>t?.format??t?.src?.split(".").pop()),b=e.computed(()=>[...O.defaultParserPlugins,...h.value?.parserPlugin??[],...t?.parserPlugin??[]]),i=e.computed(()=>s.value?b.value?.findLast(a=>s.value&&a.format.includes(s.value)):void 0),w=async()=>{try{if(o.value=!0,r.value=void 0,!t?.src)return;if(t.src?.startsWith("http")||t.src?.startsWith("data:")||t.src?.startsWith("blob:")){r.value=t.src;return}const a=t.src?.match(/^([^:]+):(.*)$/),n=a?.[1],p=a?.[2]??t.src,f=q.createOssInstance();await f.initOssClient({getOssAccess:()=>n?u.value?.(n):u.value?.(),locale:y[C.value]??"zh_CN",onFailure:c=>{throw new Error(typeof c?.message=="string"?c.message:"oss create error")}});const m=await f.getSignatureUrl(p,3600,t?.fileName??p);if(m){const c=await fetch(m).then(B=>B.blob());r.value=URL.createObjectURL(c)}else throw new Error("oss getSignatureUrl error")}finally{o.value=!1}};return e.watch(()=>[t.src],()=>{w()},{immediate:!0}),(a,n)=>(e.openBlock(),e.createElementBlock("div",{class:e.normalizeClass([e.unref(l)(),e.unref(_)])},[o.value?(e.openBlock(),e.createBlock(e.unref(P.Spin),{key:0,class:e.normalizeClass([e.unref(l)("spin")])},null,8,["class"])):!o.value&&r.value&&t.src?(e.openBlock(),e.createElementBlock(e.Fragment,{key:1},[i.value&&s.value?(e.openBlock(),e.createElementBlock(e.Fragment,{key:0},[r.value?(e.openBlock(),e.createBlock(e.resolveDynamicComponent(e.h(e.Fragment,[i.value?.parser?.({src:r.value,originalSrc:t.src,format:s.value,fileName:t.fileName})])),{key:0})):e.createCommentVNode("",!0)],64)):(e.openBlock(),e.createBlock(g.default,{key:1,type:"notPreview"}))],64)):(e.openBlock(),e.createBlock(g.default,{key:2,type:"notExist"}))],2))}});exports.default=F;
