"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.Point = 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 _point = require("../shared/point");

var _types = require("../shared/types");

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

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

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

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

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 Point = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Point, _React$Component);

  var _super = _createSuper(Point);

  function Point(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Point);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "move", function (p) {
      var mark = _objectSpread(_objectSpread({}, _this.state.mark), p);

      _this.setState({
        mark: mark
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "startDrag", function () {
      var update = _objectSpread({}, _this.props.mark);

      if (update.label === '') {
        delete update.label;
      }

      _this.setState({
        mark: update
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "stopDrag", function () {
      var onChange = _this.props.onChange;

      var mark = _objectSpread({}, _this.state.mark);

      _this.setState({
        mark: undefined
      }, function () {
        if (!(0, _isEqual["default"])(_this.props.mark, mark)) {
          onChange(_this.props.mark, mark);
        }
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "labelChange", function (label) {
      var onChange = _this.props.onChange;

      var update = _objectSpread(_objectSpread({}, _this.props.mark), {}, {
        label: label
      });

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

      _this.setState({
        mark: update
      }, function () {
        onChange(_this.props.mark, update);
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "clickPoint", function () {
      var _this$props = _this.props,
          labelModeEnabled = _this$props.labelModeEnabled,
          onChange = _this$props.onChange,
          onClick = _this$props.onClick,
          mark = _this$props.mark;

      if (labelModeEnabled) {
        onChange(mark, _objectSpread({
          label: ''
        }, mark));

        if (_this.input) {
          _this.input.focus();
        }
      } else {
        onClick(mark);
      }
    });
    _this.state = {};
    return _this;
  }

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

      var _this$props2 = this.props,
          coordinatesOnHover = _this$props2.coordinatesOnHover,
          graphProps = _this$props2.graphProps,
          labelNode = _this$props2.labelNode,
          labelModeEnabled = _this$props2.labelModeEnabled;
      var mark = this.state.mark ? this.state.mark : this.props.mark;
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_point.BasePoint, (0, _extends2["default"])({}, mark, {
        coordinatesOnHover: coordinatesOnHover,
        graphProps: graphProps,
        labelNode: labelNode,
        onDrag: this.move,
        onDragStart: this.startDrag,
        onDragStop: this.stopDrag,
        onClick: this.clickPoint,
        onTouchStart: function onTouchStart(e) {
          e.stopPropagation();

          _this2.clickPoint();
        },
        onTouchEnd: function onTouchEnd(e) {
          e.stopPropagation();

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

exports.Point = Point;
(0, _defineProperty2["default"])(Point, "propTypes", _objectSpread({
  graphProps: _plot.types.GraphPropsType.isRequired
}, _types.ToolPropTypeFields));
(0, _defineProperty2["default"])(Point, "defaultProps", {});
var _default = Point;
exports["default"] = _default;
//# sourceMappingURL=component.js.map