(function($){'use strict';try{new CustomEvent('IE has CustomEvent, but doesn\'t support constructor')}catch(e){window.CustomEvent=function(event,params){params=params||{bubbles:!1,cancelable:!1,detail:undefined,};const evt=document.createEvent('CustomEvent');evt.initCustomEvent(event,params.bubbles,params.cancelable,params.detail);return evt};CustomEvent.prototype=Object.create(window.Event.prototype)}
function CustomNumber(input,sub,add){const self=this;this.input=input;this.sub=sub;this.add=add;this._subHandler=function(){self._change(-1);self._changeByTimer(-1)};this._addHandler=function(){self._change(1);self._changeByTimer(1)};this.sub.addEventListener('mousedown',this._subHandler,!1);this.add.addEventListener('mousedown',this._addHandler,!1)}
CustomNumber.prototype={destroy:function(){this.sub.removeEventListener('mousedown',this._subHandler,!1);this.add.removeEventListener('mousedown',this._addHandler,!1)},_change:function(direction){const step=this._step();const min=this._min();const max=this._max();let value=this._value()+(step*direction);if(max!==null){value=Math.min(max,value)}
if(min!==null){value=Math.max(min,value)}
const triggerChange=this.input.value!==value.toString();this.input.value=value.toString();if(triggerChange){this.input.dispatchEvent(new CustomEvent('change',{bubbles:!0}))}},_changeByTimer:function(direction){const self=this;let interval;const timer=setTimeout(function(){interval=setInterval(function(){self._change(direction)},50)},300);const documentMouseUp=function(){clearTimeout(timer);clearInterval(interval);document.removeEventListener('mouseup',documentMouseUp,!1)};document.addEventListener('mouseup',documentMouseUp,!1)},_step:function(){let step=1;if(this.input.hasAttribute('step')){step=parseFloat(this.input.getAttribute('step'));step=isNaN(step)?1:step}
return step},_min:function(){let min=null;if(this.input.hasAttribute('min')){min=parseFloat(this.input.getAttribute('min'));min=isNaN(min)?null:min}
return min},_max:function(){let max=null;if(this.input.hasAttribute('max')){max=parseFloat(this.input.getAttribute('max'));max=isNaN(max)?null:max}
return max},_value:function(){const value=parseFloat(this.input.value);return isNaN(value)?0:value},};function init(options){options=$.extend({destroy:!1},options);return this.each(function(){if(!$(this).is('.th-input-number')){return}
let instance=$(this).data('customNumber');if(instance&&options.destroy){instance.destroy();$(this).removeData('customNumber')}else if(!instance&&!options.destroy){instance=new CustomNumber(this.querySelector('.th-input-number__input'),this.querySelector('.th-input-number__sub'),this.querySelector('.th-input-number__add'));$(this).data('customNumber',instance)}})}
$.fn.customNumber=init}(jQuery))