const MouseWheel = (($)=>{
    const DefaultConfig = {
        keys:[37, 38, 39, 40],
        target:window
    }
    class MouseWheel{
        constructor(config) {
            this._config = this._getConfig(config);
            this.target = this._config.target || window;
            this.targetOnmousewheel = -1;
            this.documentOnmousewheel = -1;
            this.documentOnkeydown = -1;
            this.isDisable = false;
        }
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config);
        }
        preventDefault(e) {
            e = e || this.target.event;
            if (e.preventDefault)
                e.preventDefault();
            e.returnValue = false;
        }
        keydown = (e)=> {
            const {keys} = this._config;
            for (let i = keys.length; i--;) {
                if (e.keyCode === keys[i]) {
                    this.preventDefault(e);
                    return;
                }
            }
        }
        wheel = (e)=> {
            this.preventDefault(e);
        }
        disableScroll() {
            if(!this.isDisable){
                this.isDisable = true;
                if (this.target.addEventListener) {
                    this.target.addEventListener('DOMMouseScroll', this.wheel, false);
                }
                if(this.targetOnmousewheel == -1){
                    this.targetOnmousewheel = this.target.onmousewheel;
                }
                if(this.documentOnmousewheel == -1){
                    this.documentOnmousewheel = document.onmousewheel;
                }
                if(this.documentOnkeydown == -1){
                    this.documentOnkeydown = document.onkeydown;
                }
                this.target.onmousewheel = document.onmousewheel = this.wheel;
                document.onkeydown = this.keydown;
            }
        }
        enableScroll() {
            if(this.isDisable){
                if (this.target.removeEventListener) {
                    this.target.removeEventListener('DOMMouseScroll', this.wheel, false);
                }
                if(this.targetOnmousewheel!= -1){
                    this.target.onmousewheel = this.targetOnmousewheel;
                }
                if(this.documentOnmousewheel != -1){
                    document.onmousewheel = this.documentOnmousewheel;
                }
                if(this.documentOnkeydown != -1){
                    document.onkeydown = this.documentOnkeydown;
                }
            }
        }
        dispose(){
            this.enableScroll();
            this.target = null;
            this.targetOnmousewheel = -1;
            this.documentOnmousewheel = -1;
            this.documentOnkeydown = -1;
        }
    }
    return MouseWheel;
})($);
export default MouseWheel;