"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.releasePointer = exports.lockPointer = void 0;

var _handle = require("@enact/core/handle");

var active;
var capturedDown = false; // if there is a focused input (there should be) and the target is not the input

var shouldCapture = function shouldCapture(_ref) {
  var target = _ref.target;
  return active && target !== active;
};

var isCapturing = function isCapturing() {
  return capturedDown && active;
};

var setCapturing = function setCapturing(capturing) {
  return (0, _handle.returnsTrue)(function () {
    return capturedDown = capturing;
  });
};

var handlePointerDown = (0, _handle.handle)(shouldCapture, // If we should capture the click
_handle.preventDefault, // prevent the down event bubbling
_handle.stop, // (and stop propagation to support touch)
setCapturing(true) // and flag that we've started capturing a down event
);
var handlePointerUp = (0, _handle.handle)(isCapturing, // if a down event was captured
_handle.preventDefault, // prevent the up event bubbling
_handle.stop // (and stop propagation to support touch)
);
var handlePointerClick = (0, _handle.handle)(isCapturing, // if a down event was captured
_handle.stop, // prevent the click event from propagating
setCapturing(false), // clear the capturing flag
function () {
  return active.blur();
} // and blur the active node
); // Lock the pointer from emitting click events until released

var lockPointer = function lockPointer(target) {
  active = target;
  document.addEventListener('mousedown', handlePointerDown, {
    capture: true
  });
  document.addEventListener('mouseup', handlePointerUp, {
    capture: true
  });
  document.addEventListener('touchstart', handlePointerDown, {
    capture: true
  });
  document.addEventListener('touchend', handlePointerUp, {
    capture: true
  });
  document.addEventListener('click', handlePointerClick, {
    capture: true
  });
}; // Release the pointer and allow subsequent click events


exports.lockPointer = lockPointer;

var releasePointer = function releasePointer(target) {
  if (target === active) {
    active = null;
    document.removeEventListener('mousedown', handlePointerDown, {
      capture: true
    });
    document.removeEventListener('mouseup', handlePointerUp, {
      capture: true
    });
    document.removeEventListener('touchstart', handlePointerDown, {
      capture: true
    });
    document.removeEventListener('touchend', handlePointerUp, {
      capture: true
    });
    document.removeEventListener('click', handlePointerClick, {
      capture: true
    });
  }
};

exports.releasePointer = releasePointer;