import{r as i,d as t,B as e,h as o,e as n}from"./p-03169580.js";async function r(i,t=1080,e=1080){return new Promise(((o,n)=>{try{let n=new Image;n.src=i;n.onload=()=>{let r;if(n.height<=e&&n.width<=t){r=i}else{if(n.height>n.width){t=Math.floor(e*(n.width/n.height))}else{e=Math.floor(t*(n.height/n.width))}let i=document.createElement("canvas");let o=i.getContext("2d");i.width=n.width;i.height=n.height;o.drawImage(n,0,0,i.width,i.height);let s={width:Math.floor(n.width),height:Math.floor(n.height)};let a={width:null,height:null};while(s.width*.5>t){a.width=Math.floor(s.width*.5);a.height=Math.floor(s.height*.5);o.drawImage(i,0,0,s.width,s.height,0,0,a.width,a.height);s.width=a.width;s.height=a.height}let l=document.createElement("canvas");let h=l.getContext("2d");l.width=t;l.height=e;h.drawImage(i,0,0,s.width,s.height,0,0,t,e);r=l.toDataURL("image/jpeg",1)}o(r)}}catch(i){n("Error resizing image!")}}))}const s='fireenjin-input-photo .upload-wrapper{display:block;margin:0 auto;height:150px;width:150px;position:relative}fireenjin-input-photo .upload-wrapper .photo{position:relative;background:var(--ion-color-medium);border-radius:4px;border:2px solid var(--ion-color-light);height:150px;width:150px;margin:0 auto;display:block;background-repeat:no-repeat;background-size:cover;background-position:center;color:var(--ion-color-medium);font-size:75px;line-height:150px;text-align:center;font-weight:bolder}fireenjin-input-photo .upload-wrapper .photo.is-loading:before{border-radius:4px}fireenjin-input-photo .upload-wrapper .photo.is-loading:after{border-radius:4px}fireenjin-input-photo .upload-wrapper .photo:hover{cursor:pointer;border-color:var(--ion-color-primary)}fireenjin-input-photo input[type="file"]{height:0;width:0;visibility:hidden;opacity:0;pointer-events:none;float:left}';let a=class{constructor(e){i(this,e);this.fireenjinUpload=t(this,"fireenjinUpload",7);this.ionInput=t(this,"ionInput",7);this.disabled=false;this.showButton=false;this.buttonText="Edit Image";this.type="photo";this.endpoint="upload";this.multiple=false;this.resize=false}onSuccess(i){if(i.detail.endpoint!=="upload"||i.detail.name!==this.name)return false;this.loading=false}onPhotoChange(){this.updatePhoto()}componentDidLoad(){if(!(e===null||e===void 0?void 0:e.isBrowser))return;this.updatePhoto()}updatePhoto(){this.photoUrl=this.value?this.value:this.fallback?this.fallback:null;if(this.value){this.ionInput.emit({name:this.name,value:this.value})}}async triggerFileInput(i){if(this.disabled){return false}const t=this.photoUploaderEl.querySelector('input[type="file"]');t.click()}selectFile(i){var t;for(const e of((t=i===null||i===void 0?void 0:i.target)===null||t===void 0?void 0:t.files)||[]){this.uploadPhoto(e)}}uploadPhoto(i){this.loading=true;if(!window.FileReader)return;const t=new FileReader;t.onload=async t=>{var e,o;if(t.target.readyState!=2)return;if(t.target.error){alert("Error while reading file");return}this.fireenjinUpload.emit({event:t,endpoint:this.endpoint,name:this.name,data:{id:this.documentId,type:this.type,path:this.path,file:i,fileName:this.fileName,encodedContent:this.resize?r((e=t===null||t===void 0?void 0:t.target)===null||e===void 0?void 0:e.result):(o=t===null||t===void 0?void 0:t.target)===null||o===void 0?void 0:o.result}})};t.readAsDataURL(i)}onDrop(i){i.preventDefault();this.uploadPhoto(i.dataTransfer.files[0])}onDrag(i){i.preventDefault()}onDragEnter(){this.showButton=true}onDragLeave(){this.showButton=false}render(){return o("div",null,o("div",{class:"upload-wrapper"},o("div",{class:this.loading?"photo is-loading":"photo",style:{backgroundImage:this.photoUrl?`url('${this.photoUrl}')`:null},onClick:i=>this.triggerFileInput(i),onDrop:i=>this.onDrop(i),onDragOver:i=>this.onDrag(i),onDragEnter:()=>this.onDragEnter(),onDragLeave:()=>this.onDragLeave()},!this.photoUrl&&this.initials?this.initials:null),this.showButton?o("ion-button",{fill:"clear",expand:"block",size:"small",onClick:i=>this.triggerFileInput(i)},this.buttonText,o("ion-icon",{name:"image",slot:"end"})):null),o("slot",null),o("input",{type:"file",onChange:i=>this.selectFile(i),accept:"image/*",multiple:this.multiple}))}get photoUploaderEl(){return n(this)}static get watchers(){return{value:["onPhotoChange"]}}};a.style=s;export{a as fireenjin_input_photo};
//# sourceMappingURL=p-deb2d8a7.entry.js.map