System.register(['./node-helpers.js', '../graphics.js'], function (_export, _context) {
  "use strict";

  var nodes, pt;
  return {
    setters: [function (_nodeHelpersJs) {
      nodes = _nodeHelpersJs;
    }, function (_graphicsJs) {
      pt = _graphicsJs.pt;
    }],
    execute: function () {
      const __SystemJSRewritingHack = {};
      _recorder_._src_client_morphic_event_helpers_js = _recorder_._src_client_morphic_event_helpers_js || {};
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'noticableDistanceTo', {
        get() {
          return noticableDistanceTo;
        },

        set(thisIsVererySecretVariableName) {
          _export('noticableDistanceTo', noticableDistanceTo = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'getTargetNode', {
        get() {
          return getTargetNode;
        },

        set(thisIsVererySecretVariableName) {
          _export('getTargetNode', getTargetNode = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'distanceTo', {
        get() {
          return distanceTo;
        },

        set(thisIsVererySecretVariableName) {
          _export('distanceTo', distanceTo = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'elementsUnder', {
        get() {
          return elementsUnder;
        },

        set(thisIsVererySecretVariableName) {
          _export('elementsUnder', elementsUnder = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'globalPosition', {
        get() {
          return globalPosition;
        },

        set(thisIsVererySecretVariableName) {
          _export('globalPosition', globalPosition = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'nodes', {
        get() {
          return nodes;
        },

        set(thisIsVererySecretVariableName) {
          nodes = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'pt', {
        get() {
          return pt;
        },

        set(thisIsVererySecretVariableName) {
          pt = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });


      // todo: export class wrappers

      /**
      Event(e).globalPosition();
      */

      var offset = 30;

      Object.defineProperty(_recorder_._src_client_morphic_event_helpers_js, 'offset', {
        get() {
          return offset;
        },

        set(thisIsVererySecretVariableName) {
          offset = thisIsVererySecretVariableName;
          return true;
        },

        enumerable: true,
        configurable: true
      });
      function globalPosition(e) {
        return pt(e.clientX, e.clientY);
      }

      _export('globalPosition', globalPosition);

      function elementsUnder(e) {

        var pos = globalPosition(e);
        var elements = Array.from(document.elementsFromPoint(e.clientX, e.clientY));

        var result = Array.from(elements).filter(ea => !ea.isMetaNode
        // go deep one level #ContinueHere
        );for (var ea of elements.reverse()) {
          if (ea.shadowRoot && ea.shadowRoot.elementsFromPoint) {
            result.unshift(...Array.from(ea.shadowRoot.elementsFromPoint(e.clientX, e.clientY)).filter(ea => !ea.isMetaNode));
          }
        }
        if (result.find(ea => ea.tagName && ea.tagName.match("HALO"))) {
          debugger;
        }

        return result;
      }

      _export('elementsUnder', elementsUnder);

      function distanceTo(e, pos) {
        var yDist = Math.abs(pos.y - globalPosition(e).y);
        var xDist = Math.abs(pos.x - globalPosition(e).x);
        return Math.sqrt(xDist * xDist + yDist * yDist);
      }

      _export('distanceTo', distanceTo);

      function getTargetNode(e) {
        var target = elementsUnder(e)[0];
        return document.body === target ? null : target;
      }

      _export('getTargetNode', getTargetNode);

      function noticableDistanceTo(e, pos) {
        return distanceTo(e, pos) > offset;
      }

      _export('noticableDistanceTo', noticableDistanceTo);
    }
  };
});