"use strict";

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

var _typeof = require("@babel/runtime/helpers/typeof");

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

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

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

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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

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

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

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

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

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

var _shape = require("@vx/shape");

var _group = require("@vx/group");

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

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

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

var _line = _interopRequireDefault(require("./common/line"));

var _excluded = ["classes", "className", "scale", "x", "y", "r", "correctness"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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 DraggableComponent = function DraggableComponent(props) {
  var _props$classes = props.classes,
      classes = _props$classes === void 0 ? {} : _props$classes,
      className = props.className,
      scale = props.scale,
      x = props.x,
      y = props.y,
      r = props.r,
      correctness = props.correctness,
      rest = (0, _objectWithoutProperties2["default"])(props, _excluded);

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      hover = _useState2[0],
      setHover = _useState2[1];

  var squareSize = r * 4;
  var squareHalf = squareSize / 2;
  var cx = scale.x(x);
  var cy = scale.y(y);
  return /*#__PURE__*/_react["default"].createElement(_group.Group, {
    className: (0, _classnames["default"])(className, classes.line, correctness && correctness.value)
  }, /*#__PURE__*/_react["default"].createElement(_shape.LinePath, {
    data: [{
      x: scale.x(x) - r,
      y: scale.y(y) + r
    }, {
      x: scale.x(x) + r,
      y: scale.y(y) - r
    }],
    key: "point-".concat(x, "-").concat(y, "-1"),
    x: function x(d) {
      return d.x;
    },
    y: function y(d) {
      return d.y;
    },
    strokeWidth: 5,
    style: {
      pointerEvents: 'none'
    }
  }), /*#__PURE__*/_react["default"].createElement(_shape.LinePath, {
    data: [{
      x: scale.x(x) - r,
      y: scale.y(y) - r
    }, {
      x: scale.x(x) + r,
      y: scale.y(y) + r
    }],
    key: "point-".concat(x, "-").concat(y, "-2"),
    x: function x(d) {
      return d.x;
    },
    y: function y(d) {
      return d.y;
    },
    strokeWidth: 5,
    style: {
      pointerEvents: 'none'
    }
  }), hover && /*#__PURE__*/_react["default"].createElement("rect", {
    x: cx - squareHalf,
    y: cy - squareHalf,
    width: squareSize,
    height: squareSize,
    stroke: _renderUi.color.defaults.BORDER_GRAY,
    fill: "none",
    strokeWidth: 2,
    pointerEvents: "none"
  }), /*#__PURE__*/_react["default"].createElement("circle", (0, _extends2["default"])({
    cx: cx,
    cy: cy,
    r: r * 2,
    className: (0, _classnames["default"])(classes.transparentHandle),
    onMouseEnter: function onMouseEnter() {
      return setHover(true);
    },
    onMouseLeave: function onMouseLeave() {
      return setHover(false);
    }
  }, rest)));
};

DraggableComponent.propTypes = {
  scale: _propTypes["default"].object,
  x: _propTypes["default"].number,
  y: _propTypes["default"].number,
  r: _propTypes["default"].number,
  className: _propTypes["default"].string,
  classes: _propTypes["default"].object,
  correctness: _propTypes["default"].shape({
    value: _propTypes["default"].string,
    label: _propTypes["default"].string
  })
};

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

  var _super = _createSuper(LineCross);

  function LineCross() {
    (0, _classCallCheck2["default"])(this, LineCross);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(LineCross, [{
    key: "render",
    value: function render() {
      var props = this.props;
      var data = props.data,
          graphProps = props.graphProps;

      var _ref = graphProps || {},
          _ref$scale = _ref.scale,
          scale = _ref$scale === void 0 ? {} : _ref$scale,
          _ref$size = _ref.size,
          size = _ref$size === void 0 ? {} : _ref$size;

      var xBand = (0, _utils.dataToXBand)(scale.x, data, size.width, 'lineCross');
      return /*#__PURE__*/_react["default"].createElement(_line["default"], (0, _extends2["default"])({}, props, {
        xBand: xBand,
        CustomDraggableComponent: DraggableComponent
      }));
    }
  }]);
  return LineCross;
}(_react["default"].Component);

exports.LineCross = LineCross;
(0, _defineProperty2["default"])(LineCross, "propTypes", {
  data: _propTypes["default"].array,
  onChange: _propTypes["default"].func,
  graphProps: _plot.types.GraphPropsType.isRequired
});

var _default = function _default() {
  return {
    type: 'lineCross',
    Component: LineCross,
    name: 'Line Cross'
  };
};

exports["default"] = _default;
//# sourceMappingURL=line-cross.js.map