"use strict";

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

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

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

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

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

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 _react = _interopRequireDefault(require("react"));

var _axis = require("@vx/axis");

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

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

var _arrow = _interopRequireDefault(require("./arrow"));

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

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

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 AxisPropTypes = {
  includeArrows: _propTypes["default"].object,
  graphProps: _propTypes["default"].object
};
exports.AxisPropTypes = AxisPropTypes;
var AxisDefaultProps = {
  includeArrows: {
    left: true,
    right: true,
    up: true,
    down: true
  }
};

var axisStyles = function axisStyles(theme) {
  return {
    line: {
      stroke: _renderUi.color.defaults.PRIMARY,
      strokeWidth: 3
    },
    arrow: {
      fill: _renderUi.color.defaults.PRIMARY
    },
    tick: {
      fill: _renderUi.color.defaults.PRIMARY,
      '& > line': {
        stroke: _renderUi.color.defaults.PRIMARY
      }
    },
    labelFontSize: {
      fontSize: theme.typography.fontSize
    },
    axisLabelHolder: {
      padding: 0,
      margin: 0,
      textAlign: 'center',
      '* > *': {
        margin: 0,
        padding: 0
      },
      fontSize: theme.typography.fontSize
    }
  };
};

var tickLabelStyles = {
  fontFamily: 'Roboto',
  fontSize: '14px',
  cursor: 'inherit'
};

var sharedValues = function sharedValues(firstNegativeX, firstNegativeY, distanceFromOriginToFirstNegativeX, distanceFromOriginToFirstNegativeY, deltaAllowance, dy) {
  var result = [];

  if (firstNegativeX === firstNegativeY && distanceFromOriginToFirstNegativeX - deltaAllowance < distanceFromOriginToFirstNegativeY && distanceFromOriginToFirstNegativeY < distanceFromOriginToFirstNegativeX + deltaAllowance && distanceFromOriginToFirstNegativeX - deltaAllowance < dy && dy < distanceFromOriginToFirstNegativeX + deltaAllowance) {
    result.push(firstNegativeX);
  }

  return result;
};

exports.sharedValues = sharedValues;

var firstNegativeValue = function firstNegativeValue(interval) {
  return (interval || []).find(function (element) {
    return element < 0;
  });
};

exports.firstNegativeValue = firstNegativeValue;

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

  var _super = _createSuper(RawXAxis);

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

  (0, _createClass2["default"])(RawXAxis, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          includeArrows = _this$props.includeArrows,
          classes = _this$props.classes,
          graphProps = _this$props.graphProps,
          columnTicksValues = _this$props.columnTicksValues,
          skipValues = _this$props.skipValues,
          distanceFromOriginToFirstNegativeY = _this$props.distanceFromOriginToFirstNegativeY,
          dy = _this$props.dy;

      var _ref = graphProps || {},
          scale = _ref.scale,
          domain = _ref.domain,
          size = _ref.size,
          range = _ref.range; // Having 0 as a number in columnTicksValues does not make 0 to show up
      // so we use this trick, by defining it as a string:


      var tickValues = (domain.labelStep || range.labelStep) && domain.min <= 0 ? ['0'].concat((0, _toConsumableArray2["default"])(columnTicksValues)) : columnTicksValues; // However, the '0' has to be displayed only if other tick labels (y-axis or x-axis) are displayed

      var labelProps = function labelProps(label) {
        var y = skipValues && skipValues[0] === label ? distanceFromOriginToFirstNegativeY + 4 : dy;
        return _objectSpread(_objectSpread({}, tickLabelStyles), {}, {
          textAnchor: 'middle',
          y: y,
          dx: label === '0' ? -10 : 0,
          dy: label === '0' ? -7 : 0
        });
      };

      var necessaryRows = (0, _utils.countWords)(domain.axisLabel);
      var longestWord = (0, _utils.findLongestWord)(domain.axisLabel);
      var necessaryWidth = (0, _utils.amountToIncreaseWidth)(longestWord) + 2;
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_axis.Axis, {
        axisLineClassName: classes.line,
        scale: scale.x,
        top: scale.y(0),
        left: 0,
        label: domain.label,
        rangePadding: 8,
        tickClassName: classes.tick,
        tickFormat: function tickFormat(value) {
          return value;
        },
        tickLabelProps: labelProps,
        tickValues: tickValues
      }), includeArrows && includeArrows.left && /*#__PURE__*/_react["default"].createElement(_arrow["default"], {
        direction: "left",
        x: domain.min,
        y: 0,
        className: classes.arrow,
        scale: scale
      }), includeArrows && includeArrows.right && /*#__PURE__*/_react["default"].createElement(_arrow["default"], {
        direction: "right",
        x: domain.max,
        y: 0,
        className: classes.arrow,
        scale: scale
      }), domain.axisLabel && /*#__PURE__*/_react["default"].createElement("foreignObject", {
        x: size.width + 17,
        y: scale.y(0) - 9,
        width: necessaryWidth,
        height: 20 * necessaryRows
      }, /*#__PURE__*/_react["default"].createElement("div", {
        dangerouslySetInnerHTML: {
          __html: domain.axisLabel
        },
        className: classes.labelFontSize
      })));
    }
  }]);
  return RawXAxis;
}(_react["default"].Component);

exports.RawXAxis = RawXAxis;
(0, _defineProperty2["default"])(RawXAxis, "propTypes", _objectSpread(_objectSpread({}, AxisPropTypes), {}, {
  classes: _propTypes["default"].object,
  graphProps: _plot.types.GraphPropsType.isRequired
}));
(0, _defineProperty2["default"])(RawXAxis, "defaultProps", AxisDefaultProps);
var XAxis = (0, _core.withStyles)(axisStyles)(RawXAxis);

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

  var _super2 = _createSuper(RawYAxis);

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

  (0, _createClass2["default"])(RawYAxis, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          classes = _this$props2.classes,
          includeArrows = _this$props2.includeArrows,
          graphProps = _this$props2.graphProps,
          skipValues = _this$props2.skipValues,
          rowTickValues = _this$props2.rowTickValues;

      var _ref2 = graphProps || {},
          scale = _ref2.scale,
          range = _ref2.range,
          size = _ref2.size;

      var necessaryWidth = range.axisLabel ? (0, _utils.amountToIncreaseWidth)(range.axisLabel.length) : 0;

      var customTickFormat = function customTickFormat(value) {
        return skipValues && skipValues.indexOf(value) >= 0 ? '' : value;
      };

      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_axis.Axis, {
        axisLineClassName: classes.line,
        orientation: 'left',
        scale: scale.y,
        top: 0,
        height: size.height,
        left: scale.x(0),
        label: range.label,
        labelProps: {
          'data-pie-readable': false
        },
        rangePadding: 8,
        tickLength: 10,
        tickClassName: classes.tick,
        tickFormat: customTickFormat,
        tickLabelProps: function tickLabelProps(value) {
          var digits = value.toLocaleString().replace(/[.-]/g, '').length || 1;
          return _objectSpread(_objectSpread({}, tickLabelStyles), {}, {
            dy: 4,
            dx: -10 - digits * 9,
            'data-pie-readable': false
          });
        },
        hideZero: true,
        tickTextAnchor: 'bottom',
        tickValues: rowTickValues
      }), includeArrows && includeArrows.down && /*#__PURE__*/_react["default"].createElement(_arrow["default"], {
        direction: "down",
        x: 0,
        y: range.min,
        className: classes.arrow,
        scale: scale
      }), includeArrows && includeArrows.up && /*#__PURE__*/_react["default"].createElement(_arrow["default"], {
        direction: "up",
        x: 0,
        y: range.max,
        className: classes.arrow,
        scale: scale
      }), range.axisLabel && /*#__PURE__*/_react["default"].createElement("foreignObject", {
        x: scale.x(0) - necessaryWidth / 2,
        y: -33,
        width: necessaryWidth,
        height: "20"
      }, /*#__PURE__*/_react["default"].createElement(_renderUi.Readable, {
        "false": true
      }, /*#__PURE__*/_react["default"].createElement("div", {
        dangerouslySetInnerHTML: {
          __html: range.axisLabel
        },
        className: classes.axisLabelHolder
      }))));
    }
  }]);
  return RawYAxis;
}(_react["default"].Component);

exports.RawYAxis = RawYAxis;
(0, _defineProperty2["default"])(RawYAxis, "propTypes", _objectSpread(_objectSpread({}, AxisPropTypes), {}, {
  graphProps: _plot.types.GraphPropsType.isRequired
}));
(0, _defineProperty2["default"])(RawYAxis, "defaultProps", AxisDefaultProps);
var YAxis = (0, _core.withStyles)(axisStyles)(RawYAxis);

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

  var _super3 = _createSuper(Axes);

  function Axes() {
    var _this;

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

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

    _this = _super3.call.apply(_super3, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "xValues", function () {
      var graphProps = _this.props.graphProps;

      var _ref3 = graphProps || {},
          scale = _ref3.scale,
          domain = _ref3.domain;

      if (!domain || !scale) {
        return;
      }

      var ticks = (0, _utils.getTickValues)(_objectSpread(_objectSpread({}, domain), {}, {
        step: domain.labelStep
      }));
      var negative = firstNegativeValue(ticks);
      return {
        columnTicksValues: ticks,
        firstNegativeX: negative,
        distanceFromOriginToFirstNegativeX: Math.abs(scale.y(0) - scale.y(negative))
      };
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "yValues", function () {
      var graphProps = _this.props.graphProps;

      var _ref4 = graphProps || {},
          scale = _ref4.scale,
          range = _ref4.range;

      if (!range || !scale) {
        return;
      }

      var ticks = (0, _utils.getTickValues)(_objectSpread(_objectSpread({}, range), {}, {
        step: range.labelStep
      }));
      var negative = firstNegativeValue(ticks);
      return {
        rowTickValues: ticks,
        firstNegativeY: negative,
        distanceFromOriginToFirstNegativeY: Math.abs(scale.x(0) - scale.x(negative))
      };
    });
    return _this;
  }

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

      var _ref5 = graphProps || {},
          domain = _ref5.domain,
          range = _ref5.range;

      var _this$xValues = this.xValues(),
          columnTicksValues = _this$xValues.columnTicksValues,
          firstNegativeX = _this$xValues.firstNegativeX,
          distanceFromOriginToFirstNegativeX = _this$xValues.distanceFromOriginToFirstNegativeX;

      var _this$yValues = this.yValues(),
          rowTickValues = _this$yValues.rowTickValues,
          firstNegativeY = _this$yValues.firstNegativeY,
          distanceFromOriginToFirstNegativeY = _this$yValues.distanceFromOriginToFirstNegativeY;

      var deltaAllowance = 6;
      var dy = 25;
      var skipValues = sharedValues(firstNegativeX, firstNegativeY, distanceFromOriginToFirstNegativeX, distanceFromOriginToFirstNegativeY, deltaAllowance, dy); // each axis has to be displayed only if the domain & range include it

      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, range.min <= 0 ? /*#__PURE__*/_react["default"].createElement(XAxis, (0, _extends2["default"])({}, this.props, {
        skipValues: skipValues,
        columnTicksValues: columnTicksValues,
        distanceFromOriginToFirstNegativeY: distanceFromOriginToFirstNegativeY,
        dy: dy
      })) : null, domain.min <= 0 ? /*#__PURE__*/_react["default"].createElement(YAxis, (0, _extends2["default"])({}, this.props, {
        skipValues: skipValues,
        rowTickValues: rowTickValues,
        distanceFromOriginToFirstNegativeX: distanceFromOriginToFirstNegativeX
      })) : null);
    }
  }]);
  return Axes;
}(_react["default"].Component);

exports["default"] = Axes;
(0, _defineProperty2["default"])(Axes, "propTypes", _objectSpread(_objectSpread({}, AxisPropTypes), {}, {
  classes: _propTypes["default"].object,
  graphProps: _plot.types.GraphPropsType.isRequired
}));
(0, _defineProperty2["default"])(Axes, "defaultProps", AxisDefaultProps);
//# sourceMappingURL=axes.js.map