let modal,target,cropper,img_input,status,size,name="",passurl="";var Filter={};function clear_canvas(t){var e=t.getContext("2d");e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,t.width,t.height)}function draw_canvas(t,e){t.getContext("2d").putImageData(e,0,0)}function getPixels(t){return t.getContext("2d").getImageData(0,0,t.width,t.height)}function apply_filter(){var t=$(modal).find(".filter"),e=$(modal).find("canvas")[0];if(null!=t&&null!=e)for(var a=0;a<t.length;a++)"range"===t[a].getAttribute("type")?t[a].onchange=function(t){null==Filter.original&&(Filter.original=getPixels(e),Filter.previous=Filter.original),clear_canvas(e),draw_canvas(e,Filter.previous),pixels=getPixels(e),result=Filter[$(this).attr("filter")](pixels,Number($(this).val())),clear_canvas(e),draw_canvas(e,result)}:t[a].onclick=function(t){null==Filter.original&&(Filter.original=getPixels(e),Filter.previous=Filter.original),clear_canvas(e),draw_canvas(e,Filter.previous),pixels=getPixels(e),result=Filter[$(this).attr("filter")](pixels),clear_canvas(e),draw_canvas(e,result)}}Filter.tmpCanvas=document.createElement("canvas"),Filter.tmpCtx=Filter.tmpCanvas.getContext("2d"),$(".img-upload-input").on("change",function(t){let e=null!=$(this).attr("pshape")?$(this).attr("pshape"):"square";modal=$(this).attr("editor"),target=$(this).attr("target"),status=$(this).attr("status"),passurl=$(this).attr("passurl"),size=$(this).attr("size");let a=null!=$(this).attr("w")?$(this).attr("w"):"300",r=null!=$(this).attr("h")?$(this).attr("h"):"300",i=new FileReader,l=this.files[0];if(null!=l){name=l.name,i.readAsDataURL(l);let t=name.replace(/^.*\./,""),n=["jpg","jpeg","png","svg"];try{img_input=$(this),n.includes(t)?(cropper=$(modal).find(".img-edit-container").croppie({enableExif:!0,viewport:{width:Number(a),height:Number(r),type:e},boundary:{width:Number(a)+100,height:Number(r)+100},enableOrientation:!0}),i.onload=function(t){cropper.croppie("bind",{url:t.target.result}),apply_filter()}):null!=status&&(console.log("not valid extension"),$(status).html('<div class="alert alert-danger">Please Select Valid File Image File. <small>Supported Extensions are (.png .svg .jpg .jpeg) </small></div>'))}catch(t){null==modal&&console.log("No Editor Modal Found. Create a Editor modal. Refer to documentation for help."),console.log(t)}}}),$(".img-upload-btn").on("click",function(t){Filter.original=void 0,Filter.previous=void 0,cropper.croppie("result",{type:"canvas",size:size}).then(function(t){""==passurl&&null==passurl||$.ajax({data:{image:t,name:name},url:passurl,type:"POST"}).done(function(e){null!=target&&e.response&&(console.log(e.response),$(target).attr("src",t)),null!=e.status?null!=status?null!=e.category?$(status).html('<div class="alert alert-'+e.category+'">'+e.status+"</div>"):$(status).html('<div class="alert alert-info">'+e.status+"</div>"):console.log(e.status):console.log("No Status is send from server")})}),null!=cropper&&cropper.croppie("destroy"),null!=img_input&&img_input.val(""),t.preventDefault()}),$(".img-remove-btn").on("click",function(t){Filter.original=void 0,Filter.previous=void 0,null!=img_input&&img_input.val(""),null!=cropper&&cropper.croppie("destroy"),t.preventDefault()}),$(".img-rotate-right").on("click",function(t){cropper.croppie("rotate",-90)}),$(".img-rotate-left").on("click",function(t){cropper.croppie("rotate",90)}),$(".img-clear-filter").on("click",function(t){null!=Filter.original&&(canvas=$(modal).find("canvas")[0],clear_canvas(canvas),draw_canvas(canvas,Filter.original),Filter.previous=Filter.original)}),Filter.grayscale=function(t){for(var e=t.data,a=0;a<e.length;a+=4){var r=.2126*e[a]+.7152*e[a+1]+.0722*e[a+2];e[a]=e[a+1]=e[a+2]=r}return t},Filter.brightness=function(t,e){for(var a=t.data,r=0;r<a.length;r+=4)a[r]+=e,a[r+1]+=e,a[r+2]+=e;return t},Filter.threshold=function(t,e){for(var a=t.data,r=0;r<a.length;r+=4){var i=.2126*a[r]+.7152*a[r+1]+.0722*a[r+2]>=e?255:0;a[r]=a[r+1]=a[r+2]=i}return t},Filter.blur=function(t){return Filter.convolution(t,[1/9,1/9,1/9,1/9,1/9,1/9,1/9,1/9,1/9])},Filter.sharpen=function(t){return Filter.convolution(t,[0,-1,0,-1,5,-1,0,-1,0])},Filter.createImageData=function(t,e){return this.tmpCtx.createImageData(t,e)},Filter.convolution=function(t,e,a){for(var r=Math.round(Math.sqrt(e.length)),i=Math.floor(r/2),l=t.data,n=t.width,o=t.height,s=n,u=o,c=Filter.createImageData(s,u),p=c.data,g=a?1:0,d=0;d<u;d++)for(var v=0;v<s;v++){for(var h=d,m=v,f=4*(d*s+v),F=0,$=0,x=0,_=0,w=0;w<r;w++)for(var b=0;b<r;b++){var y=4*(Math.min(o-1,Math.max(0,h+w-i))*n+Math.min(n-1,Math.max(0,m+b-i))),C=e[w*r+b];F+=l[y]*C,$+=l[y+1]*C,x+=l[y+2]*C,_+=l[y+3]*C}p[f]=F,p[f+1]=$,p[f+2]=x,p[f+3]=_+g*(255-_)}return c};