import{r as t,d as i,B as e,h as o,e as n}from"./p-03169580.js";async function r(t,i=1080,e=1080){return new Promise(((o,n)=>{try{let n=new Image;n.src=t;n.onload=()=>{let r;if(n.height<=e&&n.width<=i){r=t}else{if(n.height>n.width){i=Math.floor(e*(n.width/n.height))}else{e=Math.floor(i*(n.height/n.width))}let t=document.createElement("canvas");let o=t.getContext("2d");t.width=n.width;t.height=n.height;o.drawImage(n,0,0,t.width,t.height);let s={width:Math.floor(n.width),height:Math.floor(n.height)};let a={width:null,height:null};while(s.width*.5>i){a.width=Math.floor(s.width*.5);a.height=Math.floor(s.height*.5);o.drawImage(t,0,0,s.width,s.height,0,0,a.width,a.height);s.width=a.width;s.height=a.height}let h=document.createElement("canvas");let l=h.getContext("2d");h.width=i;h.height=e;l.drawImage(t,0,0,s.width,s.height,0,0,i,e);r=h.toDataURL("image/jpeg",1)}o(r)}}catch(t){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;border-radius:4px;border:2px solid var(--ion-color-light);height:150px;width:150px;margin:0 auto;display:block;background-color:var(--fireenjin-photo-background, transparent);background-repeat:no-repeat;background-size:cover;background-position:center;font-size:75px;line-height:150px;text-align:center}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){t(this,e);this.fireenjinUpload=i(this,"fireenjinUpload",7);this.ionInput=i(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(t){if(t.detail.endpoint!=="upload"||t.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(t){if(this.disabled){return false}const i=this.photoUploaderEl.querySelector('input[type="file"]');i.click()}selectFile(t){var i;for(const e of((i=t===null||t===void 0?void 0:t.target)===null||i===void 0?void 0:i.files)||[]){this.uploadPhoto(e)}}uploadPhoto(t){this.loading=true;if(!window.FileReader)return;const i=new FileReader;i.onload=async i=>{var e,o;if(i.target.readyState!=2)return;if(i.target.error){alert("Error while reading file");return}this.fireenjinUpload.emit({event:i,endpoint:this.endpoint,name:this.name,data:{id:this.documentId,type:this.type,path:this.path,file:t,fileName:this.fileName,encodedContent:this.resize?r((e=i===null||i===void 0?void 0:i.target)===null||e===void 0?void 0:e.result):(o=i===null||i===void 0?void 0:i.target)===null||o===void 0?void 0:o.result}})};i.readAsDataURL(t)}onDrop(t){t.preventDefault();this.uploadPhoto(t.dataTransfer.files[0])}onDrag(t){t.preventDefault()}onDragEnter(){this.showButton=true}onDragLeave(){this.showButton=false}getCssVarColor(t){if(!(e===null||e===void 0?void 0:e.isBrowser))return t;return getComputedStyle(document.documentElement).getPropertyValue(`--ion-color-${t}`).replace(" ","")}render(){return o("div",{style:{"--fireenjin-photo-background":this.color&&(this.color.includes("#")||this.color.includes("rgb(")||this.color.includes("rgba(")||this.color.includes("hsl(")||this.color.includes("hsla("))?this.color:this.color?this.getCssVarColor(this.color):"transparent"}},o("div",{class:"upload-wrapper"},o("div",{class:this.loading?"photo is-loading":"photo",style:{backgroundImage:this.photoUrl?`url('${this.photoUrl}')`:null},onClick:t=>this.triggerFileInput(t),onDrop:t=>this.onDrop(t),onDragOver:t=>this.onDrag(t),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:t=>this.triggerFileInput(t)},this.buttonText,o("ion-icon",{name:"image",slot:"end"})):null),o("slot",null),o("input",{type:"file",onChange:t=>this.selectFile(t),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-051b8b13.entry.js.map