import isArray from '../../../util/isArray';
import raf from '../../../util/raf';

const eventHooks = {};

/**
 * Register a wrapper around all events of a certain type
 * example: rafDebounce
 */
export function registerEventHooks(type, hook) {
	if (isArray(type)) {
		for (let i = 0; i < type.length; i++) {
			eventHooks[type[i]] = hook;
		}
	} else {
		eventHooks[type] = hook;
	}
}

function rafDebounce(handler) {
	let free = true;
	return e => {
		if (free) {
			free = false;
			raf(() => {
				handler(e);
				free = true;
			});
		}
	};
}

registerEventHooks(['scroll', 'mousemove', 'drag', 'touchmove'], { setup: rafDebounce });

// 'wheel' is a special case, so let us fix it here
const wheel = ('onwheel' in document || document.documentMode >= 9) ? 'wheel' : 'mousewheel';

function wheelSetup(handler) {
    let free = true;
    return e => {

        handler(e);
    };
}

registerEventHooks(wheel, { setup: wheelSetup });

export default function listenerSetup(type, handler) {
	return event => {
		let wrapper = eventHooks[type];

		if (wrapper.setup) {
			return wrapper.setup(handler)(event);
		}

		return handler(event);
	};
}
@@ -851,17 +851,16 @@
  e =%3E %7B%0A
-%0A
         
@@ -991,28 +991,8 @@
 ) %7B%0A
-%09return event =%3E %7B%0A%09
 %09let
@@ -1020,18 +1020,16 @@
 %5Btype%5D;%0A
-%0A%09
 %09if (wra
@@ -1028,24 +1028,35 @@
 %09if (wrapper
+ && wrapper
 .setup) %7B%0A%09%09
@@ -1053,17 +1053,16 @@
 etup) %7B%0A
-%09
 %09%09return
@@ -1088,23 +1088,14 @@
 ler)
-(event)
 ;%0A
-%09
 %09%7D%0A%0A
-%09
 %09ret
@@ -1109,18 +1109,7 @@
 dler
-(event);%0A%09%7D
 ;%0A%7D

Update listenerSetup, should only call wrapper on creation, not each time the handler is called