'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = syntheticClick;

var _notifier = require('./notifier');

var _constants = require('./constants');

var blockClick = false;
var blockClickTimerID = null;
var blockClickCounter = 0;

// call node.click() on tap, and block a subsequent click fired by the browser if there is one,
// note that blocking the subsequent click event fired by the browser is required because
// when the tap/node.click() call results in a change to the layout of the DOM,
// e.g. hide something, the subsequent click event fired by the browser will land
// on the DOM in it's new layout, and if where the tap occurred now has something different
// that has a click handler, e.g. a link, then the subsequent click event will land on
// and trigger the click handler, which is very bad, so it needs to be blocked.
function syntheticClick(node) {
  // only block one subsequent click event per node.click() call
  blockClickCounter++;
  if (blockClickTimerID !== null) window.clearTimeout(blockClickTimerID);
  blockClick = false;
  node.click();
  blockClick = true;
  // reset click blocking if subsequent click isn't added to browser's queue w/in queueTime
  blockClickTimerID = window.setTimeout(function () {
    blockClick = false;
    blockClickCounter = 0;
    blockClickTimerID = null;
  }, _constants.queueTime);
}

function handleClick(e) {
  if (!blockClick) return;
  e.stopPropagation();
  e.preventDefault();
  blockClickCounter--;
  // reset click blocking if the number of clicks to block is met
  if (blockClickCounter === 0) {
    blockClick = false;
    if (blockClickTimerID !== null) {
      window.clearTimeout(blockClickTimerID);
      blockClickTimerID = null;
    }
  }
}

// only required for touch devices because called on touchend tap to control tapClick
if (_constants.deviceHasTouch) (0, _notifier.notifyOfAll)(['click'], handleClick);
module.exports = exports['default'];