'use strict';

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2013, 2017
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

/**
 * Helper class to provide various event functionality.
 *
 * This class provide an 'on' method to attach a handler. The return is object with an off() method that will disconnect the event handler.
 *
 * This class provide some gesture support like tap and hold
 *
 * The class will create an event wrapper that will be provide the same interface to get pointer location whether it is a touch or mouse event
 *
 */

define(['jquery', 'hammerjs', 'jquery.hammer', '../../../lib/@waca/core-client/js/core-client/utils/EventHelper'], function ($, hammer) {

	var isTouch = 'ontouchstart' in window;
	var hammerGestures = {};
	for (var name in hammer.gestures) {
		if (hammer.gestures.hasOwnProperty(name)) {
			hammerGestures[hammer.gestures[name].name] = 1;
		}
	}

	var getTouches = function getTouches(e) {
		var touches;
		if (e.targetTouches) {
			touches = e.targetTouches;
		} else if (e.originalEvent && e.originalEvent.targetTouches) {
			touches = e.originalEvent.targetTouches;
		} else if (e.gesture) {
			touches = e.gesture.touches;
		} else {
			touches = null;
		}

		return touches;
	};

	return {
		// Used to track event handlers that don't get cleaned up.
		currentHandlerCount: 0,

		/**
   *  Attach an event handler to a given node.
   *  This is is a helper method that can be used if we want to attach hammer gestures as well as native dom events
   *
   *
   * @param node
   * @param eventName - name of the event e.g. 'mousedown', 'click', etc..
   * @param handler - function called when the event is triggered.
   * @param options - hammer options.
   * @returns - an object with 'off' method that can be used to remove the event handler.
   */
		on: function on(node, eventName, handler) {
			if (!isTouch && this.isTouch(eventName)) {
				// When we have a touch event and no touch support, we simply don't register the event
				return {
					off: function off() {}
				};
			}
			// Make sure to fix the event so that location info for touch and mouse are in the same location in the event
			var f = function (evt) {
				return handler(this.fixEvent(evt));
			}.bind(this);
			this._addListener(node, eventName, f);
			return {
				off: function () {
					this._removeListener(node, eventName, f);
				}.bind(this)
			};
		},

		fixEvent: function fixEvent(e) {
			var event = e;
			// get the information form the right place if we have a touch event (native, jquery or hammer)
			var touches = getTouches(e);
			event.isTouch = touches !== null ? true : false;
			var coords = touches && touches.length ? touches[0] : null;
			// touchend does not have coordinates
			if (coords && coords.clientX !== undefined) {
				event.clientX = coords.clientX;
				event.clientY = coords.clientY;
				event.pageX = coords.pageX;
				event.pageY = coords.pageY;
			}
			return event;
		},
		_addListener: function _addListener(node, eventName, handler) {
			if (handler) {
				this.currentHandlerCount++;
				$(node).on(eventName, handler);
			}
		},

		_removeListener: function _removeListener(node, eventName, handler) {
			if (handler) {
				this.currentHandlerCount--;
				$(node).off(eventName, handler);
			}
		},
		isTouch: function isTouch(name) {
			return name.match(/touch/) || hammerGestures[name];
		},

		setInlineEdit: function setInlineEdit(sId, fCallback, options) {
			var $node = $(sId);
			$node.inlineEditor(fCallback, options);
			return {
				'off': function off() {
					$node.inlineEditor('remove');
				},
				'blur': function blur() {
					$node.inlineEditor('blur');
				},
				'isEditing': function isEditing() {
					return $node.inlineEditor('isEditing');
				}
			};
		}
	};
});
//# sourceMappingURL=EventHelper.js.map