import{isEmpty}from"../../../base/helpers.mjs";import{ElementBuilder}from"../../../base/builder.mjs";import{View}from"../../base.mjs";import{ImageAdjustmentFilter}from"../../../graphics/image-adjust.mjs";import{ImagePixelizeFilter}from"../../../graphics/image-pixelize.mjs";import{ImageBoxBlurFilter,ImageGaussianBlurFilter}from"../../../graphics/image-blur.mjs";import{ImageSharpenFilter}from"../../../graphics/image-sharpen.mjs";import{FormView}from"../../forms/base.mjs";import{SliderPreciseInputView,SelectInputView}from"../../forms/input.mjs";const E=new ElementBuilder;class FilterSelectInputView extends SelectInputView{static defaultOptions={pixelize:"Pixelize",box:"Box Blur",gaussian:"Gaussian Blur",sharpen:"Sharpen"}}class ImageFilterFormView extends FormView{static autoSubmit=!0;static disableOnSubmit=!1;static fieldSets={Filter:{filter:{class:FilterSelectInputView,config:{required:!0}}},Size:{size:{class:SliderPreciseInputView,config:{min:1,max:64,step:1,value:1}}},Radius:{radius:{class:SliderPreciseInputView,config:{min:1,max:64,step:1,value:1}}},Weight:{weight:{class:SliderPreciseInputView,config:{min:0,max:100,step:1,value:0}}}};static defaultValues={size:16,radius:2,weight:0};static fieldSetConditions={Size:e=>-1!==["pixelize"].indexOf(e.filter),Radius:e=>-1!==["gaussian","box","sharpen"].indexOf(e.filter),Weight:e=>-1!==["sharpen"].indexOf(e.filter)}}class ImageAdjustmentFormView extends ImageFilterFormView{static fieldSets={"Color Channel Adjustments":{red:{label:"Red Amount",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}},green:{label:"Green Amount",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}},blue:{label:"Blue Amount",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}}},"Brightness and Contrast":{brightness:{label:"Brightness Adjustment",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}},contrast:{label:"Contrast Adjustment",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}}},"Hue, Saturation and Lightness":{hue:{label:"Hue Shift",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}},saturation:{label:"Saturation Adjustment",class:SliderPreciseInputView,config:{min:-100,max:100,value:0}},lightness:{label:"Lightness Enhancement",class:SliderPreciseInputView,config:{min:0,max:100,value:0}}},Noise:{hueNoise:{label:"Hue Noise",class:SliderPreciseInputView,config:{min:0,max:100,value:0}},saturationNoise:{label:"Saturation Noise",class:SliderPreciseInputView,config:{min:0,max:100,value:0}},lightnessNoise:{label:"Lightness Noise",class:SliderPreciseInputView,config:{min:0,max:100,value:0}}}};static defaultValues={red:0,green:0,blue:0,brightness:0,contrast:0,hue:0,saturation:0,lightness:0,hueNoise:0,saturationNoise:0,lightnessNoise:0}}class ImageFilterView extends View{static filterFormView=ImageFilterFormView;constructor(e,i,t){super(e),this.image=i,this.container=t,this.cancelCallbacks=[],this.saveCallbacks=[],this.formView=new this.constructor.filterFormView(e),this.formView.onSubmit((e=>{this.setFilter(e)}))}createFilter(e,i=!0){switch(e){case"box":return new ImageBoxBlurFilter(this.image,i);case"gaussian":return new ImageGaussianBlurFilter(this.image,i);case"sharpen":return new ImageSharpenFilter(this.image,i);case"pixelize":return new ImagePixelizeFilter(this.image,i);case"adjust":return new ImageAdjustmentFilter(this.image,i);default:this.editor.application.notifications.push("error",`Unknown filter ${e}`)}}getImageSource(){return isEmpty(this.filter)?this.image:this.filter.imageSource}setFilter(e){isEmpty(e.filter)||this.filterType!==e.filter&&(this.removeCanvas(),this.filter=this.createFilter(e.filter,!1),this.filterType=e.filter,this.filter.getCanvas().then((i=>{this.filter.setConstants(e),this.canvas=i,this.container.appendChild(this.canvas)}))),isEmpty(this.filter)||this.filter.setConstants(e)}removeCanvas(){if(!isEmpty(this.canvas)){try{this.container.removeChild(this.canvas)}catch(e){}this.canvas=null}}onCancel(e){this.cancelCallbacks.push(e)}onSave(e){this.saveCallbacks.push(e)}async saved(){for(let e of this.saveCallbacks)await e()}async canceled(){for(let e of this.cancelCallbacks)await e()}async build(){let e=await super.build(),i=E.button().class("column").content("Reset"),t=E.button().class("column").content("Save"),s=E.button().class("column").content("Cancel"),a=E.div().class("flex-columns half-spaced margin-top padded-horizontal").content(i,t,s);return i.on("click",(()=>{this.formView.setValues(this.constructor.filterFormView.defaultValues),setTimeout((()=>{this.formView.submit()}),100)})),t.on("click",(()=>this.saved())),s.on("click",(()=>this.canceled())),e.content(await this.formView.getNode(),a),e}}class ImageAdjustmentView extends ImageFilterView{static filterFormView=ImageAdjustmentFormView;constructor(e,i,t){super(e,i,t),this.setFilter({filter:"adjust"})}}export{ImageFilterFormView,ImageAdjustmentFormView,ImageFilterView,ImageAdjustmentView};
