System.register(['src/components/halo/lively-halo-item.js', 'src/client/morphic/node-helpers.js', 'src/client/morphic/event-helpers.js', 'src/client/graphics.js', 'src/client/preferences.js', 'src/client/morphic/snapping.js', 'src/client/strings.js'], function (_export, _context) {
  "use strict";

  var HaloItem, nodes, events, pt, Preferences, Snapping, Grid, Strings;
  return {
    setters: [function (_srcComponentsHaloLivelyHaloItemJs) {
      HaloItem = _srcComponentsHaloLivelyHaloItemJs.default;
    }, function (_srcClientMorphicNodeHelpersJs) {
      nodes = _srcClientMorphicNodeHelpersJs;
    }, function (_srcClientMorphicEventHelpersJs) {
      events = _srcClientMorphicEventHelpersJs;
    }, function (_srcClientGraphicsJs) {
      pt = _srcClientGraphicsJs.pt;
    }, function (_srcClientPreferencesJs) {
      Preferences = _srcClientPreferencesJs.default;
    }, function (_srcClientMorphicSnappingJs) {
      Snapping = _srcClientMorphicSnappingJs.default;
      Grid = _srcClientMorphicSnappingJs.Grid;
    }, function (_srcClientStringsJs) {
      Strings = _srcClientStringsJs.default;
    }],
    execute: function () {
      const __SystemJSRewritingHack = {};
      _recorder_._src_components_halo_lively_halo_handle_item_js = _recorder_._src_components_halo_lively_halo_handle_item_js || {};
      Object.defineProperty(_recorder_._src_components_halo_lively_halo_handle_item_js, 'HaloItem', {
        get() {
          return HaloItem;
        },

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        enumerable: true,
        configurable: true
      });
      class HaloHandleItem extends HaloItem {

        get isMetaNode() {
          return true;
        }

        initialize() {
          lively.addEventListener("Morphic", this, 'mousedown', e => this.onMouseDown(e));
        }

        onMouseDown(evt) {
          HaloService.isDragging = true;
          this.start(evt);

          // attach mousemove handler to body only after mousedown occured
          lively.addEventListener("HaloResize", document.body.parentElement, 'pointermove', evt => {
            this.move(evt);
          });
          // and capture the following mouse up anywere 
          lively.addEventListener("HaloResize", document.body.parentElement, 'pointerup', e => this.onPointerUp(e));
        }

        onPointerUp(evt) {
          lively.removeEventListener("HaloResize", document.body.parentElement, 'pointerup');
          lively.removeEventListener("HaloResize", document.body.parentElement, 'pointermove');

          this.stop(evt);
        }

        start(evt) {
          evt.preventDefault();
          this.target = window.that;
          this.snapping = new Snapping(this.target);

          if (this.halo.info) this.halo.info.stop();
          this.halo.info = lively.showInfoBox(this.target);
          this.halo.info.update();

          if (this.target.haloResizeStart) {
            this.target.haloResizeStart(evt, this);
          } else {
            this.initialBounds = lively.getBounds(this.target);
            this.eventOffset = events.globalPosition(evt);
            this.removeRestrictions(this.target);
          }
        }

        move(evt) {

          evt.preventDefault();

          var delta = events.globalPosition(evt).subPt(this.eventOffset);

          var cornerOrSide = this.id;
          var newPos = this.initialBounds[cornerOrSide]().addPt(delta).rounded();

          newPos = Grid.optSnapPosition(newPos, evt);

          var newBounds = this.initialBounds[Strings.prefixSelector("with", cornerOrSide)](newPos);

          if (this.target.style.position == "absolute") {
            lively.setBounds(this.target, newBounds);
          } else {

            // this.target.style.left  =  + "px"
            // this.target.style.top  = ( newBounds.y - this.initialBounds.y) + "px"
            lively.setPosition(this.target, newBounds.topLeft().subPt(this.initialBounds.topLeft()), "relative");

            lively.setExtent(this.target, pt(newBounds.width, newBounds.height));
          }

          if (!evt.altKey && this.target.style.position == "absolute") {
            this.snapping.snapBounds(cornerOrSide);
          }

          this.halo.info.innerHTML = `${newPos.x}, ${newPos.y}`;
          HaloService.showHalos(window.that);
        }

        stop(evt) {
          HaloService.isDragging = false;
          this.halo.info.stop();
          evt.preventDefault();
        }

        removeRestrictions(node) {
          node.style.minWidth = null;
          node.style.minHeight = null;
          node.style.maxWidth = null;
          node.style.maxHeight = null;
        }

      }

      _export('default', HaloHandleItem);

      Object.defineProperty(_recorder_._src_components_halo_lively_halo_handle_item_js, 'HaloHandleItem', {
        get() {
          return HaloHandleItem;
        },

        set(thisIsVererySecretVariableName) {
          _export('default', HaloHandleItem = thisIsVererySecretVariableName);

          return true;
        },

        enumerable: true,
        configurable: true
      });
    }
  };
});