import{InputView}from"./base.mjs";import{isEmpty}from"../../base/helpers.mjs";class NumberInputView extends InputView{static min=null;static max=null;static step=1;static inputType="number";static bindMouseWheel=!0;static useMouseWheelAltKey=!0;static allowNull=!0;constructor(t,e,s){super(t,e,s),this.minValue=isEmpty(s)||isEmpty(s.min)?this.constructor.min:s.min,this.maxValue=isEmpty(s)||isEmpty(s.max)?this.constructor.max:s.max,this.stepValue=isEmpty(s)||isEmpty(s.step)?this.constructor.step:s.step,this.allowNull=isEmpty(s)||isEmpty(s.allowNull)?this.constructor.allowNull:s.allowNull}get precision(){if(isEmpty(this.stepValue)||this.stepValue>=1||this.stepValue<0)return 0;let t=`${this.stepValue}`.split(".");return t.length<2?0:t[1].length}setMin(t){this.minValue=t,void 0!==this.node&&this.node.attr("min",this.minValue)}setMax(t){this.maxValue=t,void 0!==this.node&&this.node.attr("max",this.maxValue)}setStep(t){this.stepValue=t,void 0!==this.node&&this.node.attr("step",this.stepValue)}checkValue(){let t=this.getValue();this.value;if(isEmpty(t)||isNaN(t)){if(this.allowNull)return;t=isEmpty(this.minValue)?0:this.minValue,this.setValue(t,!1)}if(!isEmpty(this.minValue)&&t<this.minValue)this.setValue(this.minValue,!1);else if(!isEmpty(this.maxValue)&&t>this.maxValue)this.setValue(this.maxValue,!1);else if(!isEmpty(this.stepValue)){let e=Math.round(this.stepValue*Math.pow(10,this.precision)),s=Math.round(t*Math.pow(10,this.precision)),i=s%e;if(0!==i){let t=parseFloat(((s-i)/Math.pow(10,this.precision)).toFixed(this.precision));this.setValue(t,!1)}else{let e=s/Math.pow(10,this.precision);e!=t&&this.setValue(e,!1)}}}changed(){this.checkValue(),super.changed()}getValue(t){let e=super.getValue(t);return isEmpty(this.stepValue)||this.stepValue>=1?parseInt(e):parseFloat(e)}async build(){let t=await super.build();return isEmpty(this.minValue)||t.attr("min",this.minValue),isEmpty(this.maxValue)||t.attr("max",this.maxValue),isEmpty(this.stepValue)||t.attr("step",this.stepValue),this.constructor.bindMouseWheel&&t.on("mousewheel",(t=>{if(this.constructor.useMouseWheelAltKey&&!t.altKey)return;t.preventDefault(),t.stopPropagation();let e=isEmpty(this.stepValue)?1:this.stepValue;t.deltaY>0&&(e*=-1),this.setValue(this.value+e)})),t}}class FloatInputView extends NumberInputView{static step="any"}class SliderInputView extends NumberInputView{static inputType="range"}class SliderPreciseInputView extends NumberInputView{static tagName="enfugue-slider-precise-input-view";static numberInputClass=NumberInputView;static sliderInputClass=SliderInputView;constructor(t,e,s){super(t,e,s);for(let t of["min","max","step"])isEmpty(s[t])&&(s[t]=this.constructor[t]);isEmpty(s.value)&&(s.value=this.constructor.defaultValue),this.sliderInput=new this.constructor.sliderInputClass(t,e,s),this.numberInput=new this.constructor.numberInputClass(t,`${e}Precise`,s),this.sliderInput.onInput((t=>{this.value=t,this.numberInput.setValue(t,!1),this.changed()})),this.numberInput.onInput((t=>{this.value=t,this.sliderInput.setValue(t,!1),this.changed()}))}disable(){super.disable(),this.sliderInput.disable(),this.numberInput.disable()}enable(){super.enable(),this.sliderInput.enable(),this.numberInput.enable()}getValue(){return this.value}setValue(t,e){super.setValue(t,!1);this.sliderInput.setValue(t,!1),this.numberInput.setValue(t,!1),e&&this.changed()}async build(){let t=await super.build();return t.append(await this.sliderInput.getNode(),await this.numberInput.getNode()),t}}class DateTimeInputView extends NumberInputView{static inputType="datetime-local";static step=null;constructor(t,e,s){super(t,e,s),this.value instanceof Date&&(this.value=this.value.toISOString().split(".")[0])}setValue(t){super.setValue(t),this.value instanceof Date&&(this.value=this.value.toISOString().split(".")[0])}getValue(t){return new Date(this.value)}}class DateInputView extends NumberInputView{static inputType="date";static step=null;constructor(t,e,s){super(t,e,s),this.value instanceof Date&&(this.value=this.value.toISOString().split("T")[0])}setValue(t){if(t instanceof Date)try{t=t.toISOString().split("T")[0]}catch(e){t=null}super.setValue(t)}getValue(t){return void 0!==this.node?new Date(this.node.val()):new Date(this.value)}}class TimeInputView extends NumberInputView{static step=null;static inputType="time"}export{NumberInputView,FloatInputView,SliderInputView,SliderPreciseInputView,DateInputView,TimeInputView,DateTimeInputView};
