System.register(['reactive-jsx', 'src/components/widgets/lively-morph.js', 'src/client/graphics.js', 'src/client/morphic/snapping.js', 'src/client/preferences.js'], function (_export, _context) {
  "use strict";

  var _childExpression, _children, _attributeStringLiteral, _attributes, _element, Morph, pt, Grid, Preferences;

  return {
    setters: [function (_reactiveJsx) {
      _childExpression = _reactiveJsx.childExpression;
      _children = _reactiveJsx.children;
      _attributeStringLiteral = _reactiveJsx.attributeStringLiteral;
      _attributes = _reactiveJsx.attributes;
      _element = _reactiveJsx.element;
    }, function (_srcComponentsWidgetsLivelyMorphJs) {
      Morph = _srcComponentsWidgetsLivelyMorphJs.default;
    }, function (_srcClientGraphicsJs) {
      pt = _srcClientGraphicsJs.pt;
    }, function (_srcClientMorphicSnappingJs) {
      Grid = _srcClientMorphicSnappingJs.Grid;
    }, function (_srcClientPreferencesJs) {
      Preferences = _srcClientPreferencesJs.default;
    }],
    execute: function () {
      const __SystemJSRewritingHack = {};
      _recorder_._src_components_widgets_lively_resizer_js = _recorder_._src_components_widgets_lively_resizer_js || {};
      Object.defineProperty(_recorder_._src_components_widgets_lively_resizer_js, 'Morph', {
        get() {
          return Morph;
        },

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

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

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

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

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

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

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

        enumerable: true,
        configurable: true
      });
      class Resizer extends Morph {
        initialize() {
          this.addEventListener('pointerdown', evt => {
            this.onPointerMoveStart(evt);
          });
        }

        getElement() {
          if (this.target) {
            return this.target;
          } else if (this.parentElement) {
            return this.parentElement;
          } else if (this.parentNode && this.parentNode.host) {
            return this.parentNode.host;
          }
          return null;
        }

        onPointerMoveStart(evt) {
          this.count = 0;
          var element = this.getElement();
          if (!element) return; // do nothging... should this happen?

          this.originalExtent = lively.getExtent(element);
          this.originalPosition = lively.getPosition(element);

          this.dragOffset = lively.getPosition(evt);
          evt.stopPropagation();

          lively.addEventListener('lively-resizer-drag', document.documentElement, 'pointermove', evt => this.onPointerMove(evt), true);
          lively.addEventListener('lively-resizer-drag', document.documentElement, 'pointerup', evt => this.onPointerMoveEnd(evt));
        }

        onPointerMove(evt) {
          if (!evt.clientX) return;

          var element = this.getElement();
          if (!element) return; // do nothging... should this happen?

          this.count++;
          if (this.count == 1) return; // ignore the first event because it seems to be off

          // 1. calculate values
          var pos = lively.getPosition(evt);
          // lively.showPoint(pos.addPt(lively.getGlobalPosition(document.body)))

          var delta = pos.subPt(this.dragOffset

          // 3. update new values

          // #TODO transfer this to all corners!
          );function maybeSnapToGrid(point) {
            if (!Preferences.get("SnapWindowsInGrid")) {
              return point;
            }
            return Grid.snapPt(point, 100, 10);
          }

          let newPosition, newExtent;
          if (this.classList.contains("top-left")) {
            newPosition = this.originalPosition.addPt(pt(Math.min(delta.x, this.originalExtent.x), Math.min(delta.y, this.originalExtent.y)));
            newExtent = this.originalExtent.addPt(pt(-delta.x, -delta.y));
          } else if (this.classList.contains("top-right")) {
            newPosition = this.originalPosition.addPt(pt(0, Math.min(delta.y, this.originalExtent.y)));
            newExtent = this.originalExtent.addPt(pt(delta.x, -delta.y));
          } else if (this.classList.contains("bottom-left")) {
            newPosition = this.originalPosition.addPt(pt(Math.min(delta.x, this.originalExtent.x), 0));
            newExtent = this.originalExtent.addPt(pt(-delta.x, delta.y));
          } else if (this.classList.contains("bottom-right")) {
            newPosition = this.originalPosition;
            newExtent = maybeSnapToGrid(this.originalExtent.addPt(delta));
          } else if (this.classList.contains("left")) {
            newPosition = this.originalPosition.addX(Math.min(delta.x, this.originalExtent.x));
            newExtent = this.originalExtent.addX(-delta.x);
          } else if (this.classList.contains("top")) {
            newPosition = this.originalPosition.addY(Math.min(delta.y, this.originalExtent.y));
            newExtent = this.originalExtent.addY(-delta.y);
          } else if (this.classList.contains("bottom")) {
            newPosition = this.originalPosition;
            newExtent = maybeSnapToGrid(this.originalExtent.addY(delta.y));
          } else if (this.classList.contains("right")) {
            newPosition = this.originalPosition;
            newExtent = maybeSnapToGrid(this.originalExtent.addX(delta.x));
          } else {
            lively.notify('unknown resizer anchor');
            return;
          }

          lively.setPosition(element, newPosition);
          lively.setExtent(element, newExtent);

          element.dispatchEvent(new CustomEvent("extent-changed"));

          evt.stopPropagation();
          evt.preventDefault();
        }

        onPointerMoveEnd(evt) {
          evt.stopPropagation();
          evt.preventDefault();

          lively.removeEventListener('lively-resizer-drag', document.documentElement);
        }

        async livelyExample() {

          var div2 = _element('div', _attributes(_attributeStringLiteral('style', 'background-color: gray; top:100px; position: absolute; left:100px;width:100px; height:100px')), _children(), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/components/widgets/lively-resizer.js',
            end: {
              column: 126,
              line: 115
            },
            start: {
              column: 15,
              line: 115
            }
          });
          var div = _element('div', _attributes(_attributeStringLiteral('style', 'background-color: lightgray')), _children(_childExpression(div2)), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/components/widgets/lively-resizer.js',
            end: {
              column: 67,
              line: 116
            },
            start: {
              column: 14,
              line: 116
            }
          });

          var topRight = await _element('lively-resizer', _attributes(_attributeStringLiteral('class', 'top-right')), _children(), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/components/widgets/lively-resizer.js',
            end: {
              column: 77,
              line: 118
            },
            start: {
              column: 26,
              line: 118
            }
          });
          topRight.target = div2;
          topRight.style.color = "green";
          div2.appendChild(topRight);

          var topLeft = await _element('lively-resizer', _attributes(_attributeStringLiteral('class', 'top-left')), _children(), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/components/widgets/lively-resizer.js',
            end: {
              column: 75,
              line: 123
            },
            start: {
              column: 25,
              line: 123
            }
          });
          topLeft.target = div2;
          topLeft.style.color = "yellow";
          div2.appendChild(topLeft);

          var bottomLeft = await _element('lively-resizer', _attributes(_attributeStringLiteral('class', 'bottom-left')), _children(), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/components/widgets/lively-resizer.js',
            end: {
              column: 81,
              line: 129
            },
            start: {
              column: 28,
              line: 129
            }
          });
          bottomLeft.target = div2;
          bottomLeft.style.color = "blue";
          div2.appendChild(bottomLeft

          //default    
          );var bottomRight = await _element('lively-resizer', _attributes(_attributeStringLiteral('class', 'bottom-right')), _children(), {
            file: 'https://lively-kernel.org/lively4/composed-offset/src/components/widgets/lively-resizer.js',
            end: {
              column: 83,
              line: 135
            },
            start: {
              column: 29,
              line: 135
            }
          });
          bottomRight.target = div2;
          bottomRight.style.color = "red";
          div2.appendChild(bottomRight);

          this.parentElement.appendChild(div);
          this.remove();
        }

      }

      _export('default', Resizer);

      Object.defineProperty(_recorder_._src_components_widgets_lively_resizer_js, 'Resizer', {
        get() {
          return Resizer;
        },

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

          return true;
        },

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