"use strict";

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

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

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 _d3Selection = require("d3-selection");

var _plot = require("@pie-lib/plot");

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

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

  var _super = _createSuper(Bg);

  function Bg() {
    var _this;

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

    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), "getRectPadding", function () {
      var graphProps = _this.props.graphProps;
      return (0, _utils.thinnerShapesNeeded)(graphProps) ? 6 : 10;
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onRectClick", function (rect) {
      var _this$props = _this.props,
          onClick = _this$props.onClick,
          graphProps = _this$props.graphProps;
      var scale = graphProps.scale;

      var padding = _this.getRectPadding();

      var coords = (0, _d3Selection.mouse)(rect._groups[0][0]); // decrease the padding from coordinates to indicate the correct point clicked

      var x = scale.x.invert(coords[0] - padding);
      var y = scale.y.invert(coords[1] - padding);
      var rowTicks = (0, _utils.getTickValues)(graphProps.range);
      var columnTicks = (0, _utils.getTickValues)(graphProps.domain);

      var closest = function closest(ticks, value) {
        return ticks.length && ticks.reduce(function (prev, curr) {
          var currentDistance = Math.abs(curr - value);
          var previousDistance = Math.abs(prev - value);
          return currentDistance <= previousDistance ? curr : prev;
        });
      };

      var snapped = {};

      if (columnTicks.indexOf(x) >= 0 && rowTicks.indexOf(y) >= 0) {
        snapped.x = x;
        snapped.y = y;
      } else {
        snapped.x = closest(columnTicks, x);
        snapped.y = closest(rowTicks, y);
      }

      onClick(snapped);
    });
    return _this;
  }

  (0, _createClass2["default"])(Bg, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var rect = (0, _d3Selection.select)(this.rect);
      rect.on('click', this.onRectClick.bind(this, rect));
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      return !_plot.utils.isDomainRangeEqual(this.props.graphProps, nextProps.graphProps) || this.props.width !== nextProps.width || this.props.height !== nextProps.height;
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          width = _this$props2.width,
          height = _this$props2.height;
      var padding = this.getRectPadding(); // expand the size of clickable area so a small area outside the edges of the grid lines to be clickable

      return /*#__PURE__*/_react["default"].createElement("rect", {
        ref: function ref(rect) {
          return _this2.rect = rect;
        },
        transform: "translate(-".concat(padding, ", -").concat(padding, ")"),
        fill: "red",
        fillOpacity: "0.0",
        width: width + padding * 2,
        height: height + padding * 2
      });
    }
  }]);
  return Bg;
}(_react["default"].Component);

exports["default"] = Bg;
(0, _defineProperty2["default"])(Bg, "propTypes", {
  width: _propTypes["default"].number.isRequired,
  height: _propTypes["default"].number.isRequired,
  onClick: _propTypes["default"].func.isRequired,
  graphProps: _plot.types.GraphPropsType.isRequired
});
(0, _defineProperty2["default"])(Bg, "defaultProps", {});
//# sourceMappingURL=bg.js.map