"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.RawBaseCircle = exports.BaseCircle = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _styles = require("@material-ui/core/styles");

var _point = require("../shared/point");

var _bgCircle = _interopRequireDefault(require("./bg-circle"));

var _utils = require("../../utils");

var _classnames = _interopRequireDefault(require("classnames"));

var _plot = require("../../../plot");

var _withRootEdge = require("../shared/line/with-root-edge");

var _reactDom = _interopRequireDefault(require("react-dom"));

var _markLabel = _interopRequireDefault(require("../../mark-label"));

var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));

var _renderUi = require("../../../render-ui");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var opacityPulsate = function opacityPulsate(opacity) {
  return {
    '0%': {
      opacity: '0.0'
    },
    '50%': {
      opacity: opacity
    },
    '100%': {
      opacity: '0.0'
    }
  };
};

var getRadius = function getRadius(from, outer) {
  var c = (0, _utils.point)(from);
  return c.dist((0, _utils.point)(outer));
};

var RawBaseCircle = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(RawBaseCircle, _React$Component);

  var _super = _createSuper(RawBaseCircle);

  function RawBaseCircle() {
    var _this;

    (0, _classCallCheck2["default"])(this, RawBaseCircle);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChangePoint", function (point) {
      var _this$props = _this.props,
          middle = _this$props.middle,
          onChange = _this$props.onChange;
      var from = point.from,
          to = point.to; // because point.from.label and point.to.label can be different

      if (!(0, _utils.equalPoints)(from, to)) {
        if (middle) {
          point.middle = _objectSpread(_objectSpread({}, middle), (0, _utils.getMiddleOfTwoPoints)(from, to));
        }

        onChange(point);
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragFrom", function (draggedFrom) {
      var _this$props2 = _this.props,
          from = _this$props2.from,
          to = _this$props2.to;

      if (from.label) {
        draggedFrom.label = from.label;
      }

      if (!(0, _utils.equalPoints)(draggedFrom, to)) {
        _this.onChangePoint({
          from: draggedFrom,
          to: to
        });
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragTo", function (draggedTo) {
      var _this$props3 = _this.props,
          from = _this$props3.from,
          to = _this$props3.to;

      if (to.label) {
        draggedTo.label = to.label;
      }

      if (!(0, _utils.equalPoints)(from, draggedTo)) {
        _this.onChangePoint({
          from: from,
          to: draggedTo
        });
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragCircle", function (draggedFrom) {
      var _this$props4 = _this.props,
          from = _this$props4.from,
          to = _this$props4.to,
          onChange = _this$props4.onChange,
          middle = _this$props4.middle;
      var diff = (0, _utils.point)(from).sub((0, _utils.point)(draggedFrom));
      var draggedTo = (0, _utils.point)(to).sub(diff);

      if (from.label) {
        draggedFrom.label = from.label;
      }

      if (to.label) {
        draggedTo.label = to.label;
      }

      var updated = {
        from: draggedFrom,
        to: draggedTo
      };

      if (middle) {
        updated.middle = _objectSpread(_objectSpread({}, middle), (0, _utils.getMiddleOfTwoPoints)(draggedFrom, draggedTo));
      }

      _this.setState({
        draggedroot: undefined,
        draggedOuter: undefined,
        isCircleDrag: false
      }, function () {
        onChange(updated);
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "labelChange", function (point, type) {
      var changeMarkProps = _this.props.changeMarkProps;

      var update = _objectSpread({}, point);

      if (!point.label || (0, _isEmpty["default"])(point.label)) {
        delete update.label;
      }

      changeMarkProps((0, _defineProperty2["default"])({}, type, update));
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "clickPoint", function (point, type, data) {
      var _this$props5 = _this.props,
          changeMarkProps = _this$props5.changeMarkProps,
          disabled = _this$props5.disabled,
          from = _this$props5.from,
          to = _this$props5.to,
          labelModeEnabled = _this$props5.labelModeEnabled,
          limitLabeling = _this$props5.limitLabeling,
          onClick = _this$props5.onClick;

      if (!labelModeEnabled) {
        onClick(point || data);
        return;
      }

      if (disabled) {
        return;
      } // limit labeling the points of the circle


      if (limitLabeling) {
        return;
      }

      if (type === 'middle' && !point && from && to) {
        point = _objectSpread(_objectSpread({}, point), (0, _utils.getMiddleOfTwoPoints)(from, to));
      }

      changeMarkProps((0, _defineProperty2["default"])({
        from: from,
        to: to
      }, type, _objectSpread({
        label: ''
      }, point)));

      if (_this.input[type]) {
        _this.input[type].focus();
      }
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "input", {});
    return _this;
  }

  (0, _createClass2["default"])(RawBaseCircle, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props6 = this.props,
          from = _this$props6.from,
          to = _this$props6.to,
          middle = _this$props6.middle,
          disabled = _this$props6.disabled,
          classes = _this$props6.classes,
          coordinatesOnHover = _this$props6.coordinatesOnHover,
          building = _this$props6.building,
          onDragStart = _this$props6.onDragStart,
          onDragStop = _this$props6.onDragStop,
          onClick = _this$props6.onClick,
          correctness = _this$props6.correctness,
          graphProps = _this$props6.graphProps,
          labelNode = _this$props6.labelNode,
          labelModeEnabled = _this$props6.labelModeEnabled;
      var common = {
        onDragStart: onDragStart,
        onDragStop: onDragStop,
        graphProps: graphProps,
        onClick: onClick
      };
      to = to || from;
      var radius = getRadius(from, to);
      var fromLabelNode = null;
      var toLabelNode = null;
      var circleLabelNode = null;

      if (labelNode) {
        if (from && from.hasOwnProperty('label')) {
          fromLabelNode = /*#__PURE__*/_reactDom["default"].createPortal( /*#__PURE__*/_react["default"].createElement(_markLabel["default"], {
            inputRef: function inputRef(r) {
              return _this2.input.from = r;
            },
            disabled: !labelModeEnabled,
            mark: from,
            graphProps: graphProps,
            onChange: function onChange(label) {
              return _this2.labelChange(_objectSpread(_objectSpread({}, from), {}, {
                label: label
              }), 'from');
            }
          }), labelNode);
        }

        if (to && to.hasOwnProperty('label')) {
          toLabelNode = /*#__PURE__*/_reactDom["default"].createPortal( /*#__PURE__*/_react["default"].createElement(_markLabel["default"], {
            inputRef: function inputRef(r) {
              return _this2.input.to = r;
            },
            disabled: !labelModeEnabled,
            mark: to,
            graphProps: graphProps,
            onChange: function onChange(label) {
              return _this2.labelChange(_objectSpread(_objectSpread({}, to), {}, {
                label: label
              }), 'to');
            }
          }), labelNode);
        }

        if (middle && middle.hasOwnProperty('label')) {
          circleLabelNode = /*#__PURE__*/_reactDom["default"].createPortal( /*#__PURE__*/_react["default"].createElement(_markLabel["default"], {
            inputRef: function inputRef(r) {
              return _this2.input.middle = r;
            },
            disabled: !labelModeEnabled,
            mark: middle,
            graphProps: graphProps,
            onChange: function onChange(label) {
              return _this2.labelChange(_objectSpread(_objectSpread({}, middle), {}, {
                label: label
              }), 'middle');
            }
          }), labelNode);
        }
      }

      return /*#__PURE__*/_react["default"].createElement("g", null, /*#__PURE__*/_react["default"].createElement(_bgCircle["default"], (0, _extends2["default"])({
        disabled: building || disabled,
        correctness: correctness,
        className: (0, _classnames["default"])(building && classes.bgCircleBuilding),
        x: from.x,
        y: from.y,
        radius: radius,
        onDrag: this.dragCircle
      }, common, {
        onClick: function onClick(data) {
          return _this2.clickPoint(middle, 'middle', data);
        }
      })), circleLabelNode, /*#__PURE__*/_react["default"].createElement(_point.BasePoint, (0, _extends2["default"])({
        disabled: building || disabled,
        coordinatesOnHover: coordinatesOnHover,
        correctness: correctness,
        labelNode: labelNode,
        x: to.x,
        y: to.y,
        onDrag: this.dragTo
      }, common, {
        onClick: function onClick(data) {
          return _this2.clickPoint(to, 'to', data);
        }
      })), toLabelNode, /*#__PURE__*/_react["default"].createElement(_point.BasePoint, (0, _extends2["default"])({
        disabled: building || disabled,
        coordinatesOnHover: coordinatesOnHover,
        correctness: correctness,
        labelNode: labelNode,
        x: from.x,
        y: from.y,
        className: classes.from,
        onDrag: this.dragFrom
      }, common, {
        onClick: function onClick(data) {
          return _this2.clickPoint(from, 'from', data);
        }
      })), fromLabelNode);
    }
  }]);
  return RawBaseCircle;
}(_react["default"].Component);

exports.RawBaseCircle = RawBaseCircle;
(0, _defineProperty2["default"])(RawBaseCircle, "propTypes", {
  building: _propTypes["default"].bool,
  classes: _propTypes["default"].object.isRequired,
  className: _propTypes["default"].string,
  coordinatesOnHover: _propTypes["default"].bool,
  correctness: _propTypes["default"].string,
  from: _plot.types.PointType,
  disabled: _propTypes["default"].bool,
  to: _plot.types.PointType,
  middle: _plot.types.PointType,
  onChange: _propTypes["default"].func.isRequired,
  onDragStart: _propTypes["default"].func,
  onDragStop: _propTypes["default"].func,
  graphProps: _plot.types.GraphPropsType.isRequired,
  onClick: _propTypes["default"].func,
  labelNode: _propTypes["default"].object,
  labelModeEnabled: _propTypes["default"].bool,
  changeMarkProps: _propTypes["default"].func,
  limitLabeling: _propTypes["default"].bool
});
(0, _defineProperty2["default"])(RawBaseCircle, "defaultProps", {
  onClick: function onClick() {
    return {};
  }
});
var BaseCircle = (0, _styles.withStyles)(function () {
  return {
    outerLine: {
      fill: 'rgb(0,0,0,0)',
      // TODO hardcoded color
      stroke: _renderUi.color.defaults.BLACK,
      strokeWidth: 4,
      '&:hover': {
        strokeWidth: 6,
        stroke: _renderUi.color.defaults.PRIMARY_DARK
      }
    },
    root: {},
    bgCircleBuilding: {
      stroke: _renderUi.color.defaults.BLACK,
      animation: 'opacityPulse 2s ease-out',
      animationIterationCount: 'infinite',
      opacity: 1
    },
    '@keyframes opacityPulse': opacityPulsate('0.3')
  };
})(RawBaseCircle);
exports.BaseCircle = BaseCircle;
var Component = (0, _withRootEdge.rootEdgeComponent)(BaseCircle);
var _default = Component;
exports["default"] = _default;
//# sourceMappingURL=component.js.map