import{r as i,e as t,h as s,H as o}from"./p-a89095f5.js";import{a as e}from"./p-8aa359c2.js";import"./p-bf71b10e.js";const l=class{constructor(s){i(this,s),this.fireenjinUpload=t(this,"fireenjinUpload",7),this.fireenjinSubmit=t(this,"fireenjinSubmit",7),this.ionChange=t(this,"ionChange",7),this.path=void 0,this.fileName=void 0,this.color="primary",this.placeholder=void 0,this.iconEnd="cloud-upload",this.iconStart=void 0,this.fill="outline",this.expand="block",this.shape="round",this.multiple=!1,this.endpoint="upload",this.name="file",this.disabled=!1,this.value=void 0,this.label=void 0,this.showLink=!1,this.showClear=!1,this.accept=void 0,this.required=!1,this.autoSubmit=!1,this.submitEndpoint=void 0,this.documentId=void 0,this.uploading=!1,this.complete=!1}async onSuccess(i){var t,s,o,l,n,h,r,a;(null===(o=null===(s=null===(t=null==i?void 0:i.detail)||void 0===t?void 0:t.name)||void 0===s?void 0:s.includes)||void 0===o?void 0:o.call(s,"autosubmit-"))||(this.uploading=!1,this.clearProgressBar(),this.value=await e(null===(n=null===(l=null==i?void 0:i.detail)||void 0===l?void 0:l.data)||void 0===n?void 0:n.ref),this.label=null===(a=null===(r=null===(h=null==i?void 0:i.detail)||void 0===h?void 0:h.data)||void 0===r?void 0:r.metadata)||void 0===a?void 0:a.name,this.ionChange.emit({name:this.name,value:this.value,event:i}),this.autoSubmit&&this.fireenjinSubmit.emit({event:i,endpoint:this.submitEndpoint||this.endpoint,name:`autosubmit-${this.name}`,id:this.documentId,data:{[this.name]:this.value}}))}onError(i){console.log(i),this.uploading=!1,this.clearProgressBar()}onProgress({detail:{progress:i}}){i&&this.setProgressBarColor(i)}async clear(){this.clearProgressBar(),this.value=null,this.ionChange.emit({name:this.name,value:this.value})}clearProgressBar(){this.label=null,this.buttonEl.style.background="transparent"}setProgressBarColor(i){this.buttonEl.style.background=`linear-gradient(to right, rgba(var(--ion-color-${this.color||"primary"}-rgb), 0.1) 0%, rgba(var(--ion-color-${this.color||"primary"}-rgb), 0.3) ${100*i}%, rgba(var(--ion-color-${this.color||"primary"}-rgb), 0) ${100*i}%)`}async uploadFiles(i){const t=await async function({multiple:i,required:t,accept:s}){return new Promise(((o,e)=>{try{const e=document.createElement("input");e.type="file",e.required=!!t,s&&(e.accept=s),i&&(e.multiple=!0),e.onchange=i=>{o(Array.from(i.target.files))},e.click()}catch(i){e(i)}}))}({multiple:this.multiple,accept:this.accept,required:this.required});if(null==t?void 0:t.length){this.uploading=!0;for(const s of t||[])this.fireenjinUpload.emit({event:i,endpoint:this.endpoint,name:this.name,data:{file:s,path:this.path,fileName:this.fileName}})}}render(){return s(o,{class:{"upload-complete":this.complete,"upload-running":this.uploading}},s("ion-button",{class:"upload-button",ref:i=>this.buttonEl=i,fill:this.fill,expand:this.expand,shape:this.shape,color:this.color,onClick:i=>this.uploadFiles(i),style:{"border-radius":"var(--border-radius)",overflow:"hidden",transition:"all ease 0.1s"},disabled:this.disabled},this.iconStart&&s("ion-icon",{slot:"start",name:this.iconStart}),this.uploading?s("ion-font",null,"Uploading"):s("ion-label",{style:{maxWidth:"90%",overflow:"hidden",textOverflow:"ellipsis"}},this.label||this.placeholder||"",s("slot",null)),this.iconEnd&&!this.uploading&&s("ion-icon",{slot:"end",name:this.iconEnd}),this.uploading&&s("ion-spinner",{slot:"end",name:"crescent",style:{height:"20px",width:"20px",marginLeft:"0.5rem"}})),this.value&&(this.showLink||this.showClear)&&s("ion-buttons",{style:{display:"flex",justifyContent:"center",alignItems:"center",gap:"1rem"}},this.showLink&&s("ion-button",{class:"open-file",target:"_blank",href:this.value,size:"small",fill:"clear",expand:"full",color:"medium"},"Open File",s("ion-icon",{name:"open",slot:"end"})),this.showClear&&s("ion-button",{onClick:()=>this.clear(),href:"#",class:"clear-file",size:"small",fill:"clear",expand:"full",color:"medium"},"Clear",s("ion-icon",{name:"close-circle",slot:"end"}))))}};l.style="";export{l as fireenjin_upload_button}