import{m1 as T,ek as k,gR as A,l$ as g,bK as h,h2 as R,gA as _,fN as x,Q as O,k6 as $,lT as D,ku as z,n0 as C}from"./BQCsJfSh.js";import{m as y,c as H,f as N,u as P}from"./r_eeToVa.js";import{as as X}from"./DURqPrI0.js";const i=e=>$(e.replace(/^ws/,"http")),f=async e=>{var n;const r=A(e);if((n=r==null?void 0:r.supported_nips)!=null&&n.map(String).includes("BUD-02"))return!0;const t=i(e),a=g.get()||h.ephemeral(),l={"X-Content-Type":"text/plain","X-Content-Length":"1","X-SHA-256":"73cb3858a687a8494ca3323053016282f3dad39d42cf62ca4e79dda2aac7d9ac"};try{const s=await a.sign(y({action:"upload",server:t}));return(await H(t,{authEvent:s,headers:l})).status===200}catch(s){String(s).match(/Failed to fetch|NetworkError/)||console.error(s)}return!1},w=T(([e])=>f(e)),S=async(e={})=>{if(e.url&&await w(e.url))return i(e.url);const r=R("server",_(X(C)));for(const t of r)return i(t);return x(O)},j=async(e,r={})=>e.type.match("image/(webp|gif|svg)")?e:k(e,r),F=async(e,r={})=>{try{const{name:t,type:a}=e,l=[];if(r.encrypt){const{ciphertext:m,key:b,nonce:E,algorithm:L}=await N(e);l.push(["decryption-key",b],["decryption-nonce",E],["encryption-algorithm",L]),e=new File([new Uint8Array(m)],t,{type:"application/octet-stream"})}const n="."+a.split("/")[1],s=await S(r),p=[await D(await e.arrayBuffer())],v=g.get()||h.ephemeral(),B=y({action:"upload",server:s,hashes:p}),U=await v.sign(B),d=await P(s,e,{authEvent:U}),u=await d.text();let o;try{o=z(u)}catch{return{error:u}}if(!(o!=null&&o.uploaded))return{error:u||`Failed to upload file (HTTP ${d.status})`};let c=o.url;return r.encrypt?c=c.replace(/\.\w+$/,"")+n:new URL(c).pathname.split(".").length===1&&(c+=n),{result:{...o,tags:l,url:c}}}catch(t){return console.error("Error caught when uploading file:",t),{error:t.toString()}}},M=e=>new Promise((r,t)=>{const a=new FileReader;a.addEventListener("load",()=>r(a.result)),a.addEventListener("error",()=>t(a.error)),a.readAsDataURL(e)}),V=async(e,r={})=>{const{result:t}=await F(e,r);return t!=null&&t.url?t:{url:await M(e),tags:[]}},G=Object.freeze(Object.defineProperty({__proto__:null,compressFileForUpload:j,fetchHasBlossomSupport:f,getBlossomServer:S,hasBlossomSupport:w,normalizeBlossomUrl:i,uploadFile:F,uploadFileOrFallback:V},Symbol.toStringTag,{value:"Module"}));export{V as a,G as b,j as c,w as h,F as u};
//# sourceMappingURL=D5IUcPgW.js.map
