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 () {
      _recorder_._src_components_halo_lively_halo_handle_item_js = _recorder_._src_components_halo_lively_halo_handle_item_js || {};
      _recorder_._src_components_halo_lively_halo_handle_item_js.HaloItem = HaloItem;
      _recorder_._src_components_halo_lively_halo_handle_item_js.nodes = nodes;
      _recorder_._src_components_halo_lively_halo_handle_item_js.events = events;
      _recorder_._src_components_halo_lively_halo_handle_item_js.pt = pt;
      _recorder_._src_components_halo_lively_halo_handle_item_js.Preferences = Preferences;
      _recorder_._src_components_halo_lively_halo_handle_item_js.Snapping = Snapping;
      _recorder_._src_components_halo_lively_halo_handle_item_js.Grid = Grid;
      _recorder_._src_components_halo_lively_halo_handle_item_js.Strings = Strings;
      class HaloHandleItem extends _recorder_._src_components_halo_lively_halo_handle_item_js.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 _recorder_._src_components_halo_lively_halo_handle_item_js.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 = _recorder_._src_components_halo_lively_halo_handle_item_js.events.globalPosition(evt);
            this.removeRestrictions(this.target);
          }
        }

        move(evt) {

          evt.preventDefault();

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

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

          newPos = _recorder_._src_components_halo_lively_halo_handle_item_js.Grid.optSnapPosition(newPos, evt);

          var newBounds = this.initialBounds[_recorder_._src_components_halo_lively_halo_handle_item_js.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, _recorder_._src_components_halo_lively_halo_handle_item_js.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);

      _recorder_._src_components_halo_lively_halo_handle_item_js.HaloHandleItem = HaloHandleItem;
    }
  };
});