import{r as e}from"./p-DKrliuRO.js";import{d as t}from"./p-CTyRvZ3_.js";import{h as i}from"./p-Doda7dHc.js";import{k as r}from"./p-CNNbCnBf.js";import"./p-CRukm3e1.js";const a=async({file:r,filename:a="untitled",usageType:o="chat"})=>{try{const i=await n(r),l={fileName:a,usageType:o},{id:c,url:d,key:u}=await s(l,i);return o&&await(async i=>{const r=e.get("businessId"),a=e.get("shopId"),{data:s,status:n}=await t().post(`api/${r}/${a}/file/${i}/confirm`);if(200===n)return s;throw new Error("Failed to confirm file upload")})(c),{id:c,url:d,key:u}}catch(e){i(e)}},s=async(r,a)=>{let s={id:"",url:"",key:""};try{const i=await(async i=>{const r=e.get("businessId"),a=e.get("shopId"),{fileName:s,usageType:n}=i,{data:o,status:l}=await t().post(`api/${r}/${a}/file/sign`,{file_name:s,usage_type:n});if(200===l)return o;throw new Error("Failed to get file sign")})(r);if(i){const{id:e,url:t,fields:r}=i,n={url:t,signedRequest:{fields:r,url:t}},o=await(async({signedRequest:e,url:t},i,r=!0)=>new Promise(((a,s)=>{const n=new FormData;r&&n.append("acl","public-read"),Object.keys(e.fields).forEach((t=>{n.append(t,e.fields[t])})),n.append("file",i);const o=new XMLHttpRequest;o.open("POST",e.url,!0),o.send(n),o.onload=function(){4===o.readyState&&(200===o.status||204===o.status?a(`${t}`):s("Could not upload file."))}})))(n,a,!1);s={id:e,url:encodeURI(o||""),key:r.key}}}catch(e){i(e)}return s},n=async t=>{if(t.size>r)throw new Error("File size exceeds maximum limit of 25MB");if(!e.get("isProduction")&&(e=>e.type.startsWith("image/"))(t))try{return await((e,t=500,i=500)=>new Promise(((r,a)=>{const s=new FileReader;s.onload=s=>{const n=new Image;n.src=s.target?s.target.result:"",n.onload=()=>{const{width:s,height:o}=n;let l=s,c=o;s>t&&(l=t,c=o/s*t),c>i&&(c=i,l=s/o*i);const d=document.createElement("canvas");d.width=l,d.height=c;const u=d.getContext("2d");u&&u.drawImage(n,0,0,l,c),d.toBlob((t=>{if(t){const i=new File([t],e.name,{type:e.type,lastModified:e.lastModified});r(i)}else a(new Error("Failed to resize the image."))}),e.type)}},s.onerror=e=>{console.log("error",e),a(new Error("Failed to read the image."))},s.readAsDataURL(e)})))(t)}catch{return t}return t},o=e=>`https://d16rfd3rgmqmmh.cloudfront.net/${e}`;export{o as h,a as u}