System.register(["src/client/graphics.js"], function (_export, _context) {
  "use strict";

  var pt;
  return {
    setters: [function (_srcClientGraphicsJs) {
      pt = _srcClientGraphicsJs.pt;
    }],
    execute: function () {
      _recorder_._src_client_morphic_dragbehavior_js = _recorder_._src_client_morphic_dragbehavior_js || {};
      _recorder_._src_client_morphic_dragbehavior_js.pt = pt;
      class DragBehavior {
        static on(target) {
          return new _recorder_._src_client_morphic_dragbehavior_js.DragBehavior(target);
        }

        constructor(target) {
          this.target = target;
          this.target.draggable = true;
          lively.addEventListener("DragBehavior", this.target, "dragstart", evt => this.onDragStart(evt));
        }

        onDragStart(evt) {
          this.originalPosition = lively.getPosition(this.target);
          this.offset = this.originalPosition.subPt(_recorder_._src_client_morphic_dragbehavior_js.pt(evt.clientX, evt.clientY));
          evt.dataTransfer.setDragImage(document.createElement("div"), 0, 0);

          lively.addEventListener("DragBehavior", this.target, "drag", evt => this.onDrag(evt));
          lively.addEventListener("DragBehavior", this.target, "dragend", evt => this.onDragEnd(evt));

          if (this.target.dragBehaviorStart) {
            this.target.dragBehaviorStart(evt, this.originalPosition);
          }
        }

        onDrag(evt) {
          // console.log("pos " + evt.clientX, evt.offsetX, evt.clientX - evt.offsetX, evt )
          if (evt.clientX == 0 && evt.clientY == 0) return; // #Garbage event
          if (evt.offsetX < 0) return; // another #Garbage event

          var pos = _recorder_._src_client_morphic_dragbehavior_js.pt(evt.clientX, evt.clientY).addPt(this.offset);
          if (this.target.dragBehaviorMove) {
            this.target.dragBehaviorMove(evt, pos, this.originalPosition);
          } else {
            lively.setPosition(this.target, pos);
          }
        }

        onDragEnd(evt) {
          lively.removeEventListener("DragBehavior", this.target, "drag");
          lively.removeEventListener("DragBehavior", this.target, "dragend");

          if (this.target.dragBehaviorEnd) {
            this.target.dragBehaviorEnd(evt, this.originalPosition);
          }
        }
      }

      _export("default", DragBehavior);

      _recorder_._src_client_morphic_dragbehavior_js.DragBehavior = DragBehavior;
    }
  };
});