"use strict";

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

var _reactEmotion = require("react-emotion");

var _freeSolidSvgIcons = require("@fortawesome/free-solid-svg-icons");

var _reactFontawesome = require("@fortawesome/react-fontawesome");

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

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

var _reactCraftAiTooltip = _interopRequireDefault(require("react-craft-ai-tooltip"));

var _d = require("d3");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var rectangleCssClass =
/*#__PURE__*/
(0, _reactEmotion.css)("stroke-width:2;stroke:none;fill:rgba(46,204,113,0.5);");

var basicTextCssClass = function basicTextCssClass(opacity) {
  return (
    /*#__PURE__*/
    (0, _reactEmotion.css)("opacity:", opacity, ";text-anchor:middle;font-size:10px;")
  );
};

var totalTextCssClass = basicTextCssClass(0.3);
var localTextCssClass = basicTextCssClass(1);

var basicLineCssClass = function basicLineCssClass(strokeWidth) {
  return (
    /*#__PURE__*/
    (0, _reactEmotion.css)("stroke-width:", strokeWidth, ";stroke:black;")
  );
};

var boxPlotMargin = {
  top: 5,
  down: 5,
  left: 15,
  right: 15
};
var stdRectangleHeight = 20;

var BoxPlot =
/*#__PURE__*/
function (_React$Component) {
  _inherits(BoxPlot, _React$Component);

  function BoxPlot() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, BoxPlot);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(BoxPlot)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      tooltipShown: false
    });

    _defineProperty(_assertThisInitialized(_this), "showTooltip", function () {
      _this.setState({
        tooltipShown: !_this.state.tooltipShown
      });
    });

    _defineProperty(_assertThisInitialized(_this), "createBoxPlot", function (_ref) {
      var totalMin = _ref.totalMin,
          totalMax = _ref.totalMax,
          mean = _ref.mean,
          std = _ref.std,
          min = _ref.min,
          max = _ref.max,
          width = _ref.width,
          height = _ref.height;
      // Scalers
      var scaleX = (0, _d.scaleLinear)().domain([totalMin, totalMax]).range([boxPlotMargin.left, width - boxPlotMargin.right]);
      var scaleY = (0, _d.scaleLinear)().domain([0, 1]).range([boxPlotMargin.top, height - boxPlotMargin.down]); // constant positions:

      var minPosX = scaleX(min);
      var maxPosX = scaleX(max);
      var middleY = scaleY(0.5);
      var scaleVal = (0, _d.scaleLinear)().domain([0, Math.abs(totalMax - totalMin)]).range([0, width]); // Since there is no animation, we clean the svg:

      (0, _d.select)(_this.node).selectAll('*').remove(); // Begin here to define the svg:

      var svg = (0, _d.select)(_this.node).attr('width', width).attr('height', height); // identifier
      // Add the TotalMin - TotalMax line

      svg.append('line').attr('class', basicLineCssClass(2)).attr('opacity', 0.3).attr('x1', scaleX(totalMin)).attr('x2', scaleX(totalMax)).attr('y1', middleY).attr('y2', middleY); // Add Total Min text

      svg.append('text').attr('class', totalTextCssClass).attr('y', middleY - 5).attr('x', scaleX(totalMin)).text(Math.round(totalMin * 100) / 100); // Add Total Max text

      svg.append('text').attr('class', totalTextCssClass).attr('y', middleY - 5).attr('x', scaleX(totalMax)).text(Math.round(totalMax * 100) / 100); // Add the min - max line

      svg.append('line').attr('class', basicLineCssClass(4)).attr('x1', minPosX).attr('x2', maxPosX).attr('y1', middleY).attr('y2', middleY); // Add lines and text to show local min/max

      if (maxPosX - minPosX > 20) {
        svg.append('line').attr('class', basicLineCssClass(1)).attr('x1', minPosX).attr('x2', minPosX).attr('y1', middleY).attr('y2', scaleY(0.6));
        svg.append('line').attr('class', basicLineCssClass(1)).attr('x1', maxPosX).attr('x2', maxPosX).attr('y1', middleY).attr('y2', scaleY(0.6));
        svg.append('text').attr('class', localTextCssClass).attr('y', scaleY(0.6) + 10).attr('x', minPosX).text(Math.round(min * 100) / 100);
        svg.append('text').attr('class', localTextCssClass).attr('y', scaleY(0.6) + 10).attr('x', maxPosX).text(Math.round(max * 100) / 100);
      } // If two local min/max are too close draw a bezier line and separate them more
      else if (maxPosX - minPosX > 0) {
          var shift = 15;
          svg.append('path').attr('d', function () {
            var source = {
              x: minPosX + 1,
              // +1 for the line thickness
              y: middleY
            };
            var target = {
              x: minPosX - shift,
              y: scaleY(0.65)
            };
            return _this.diagonal(source, target);
          }).attr('fill', 'none').attr('stroke-width', 1).attr('stroke', 'black');
          svg.append('path', 'g').attr('d', function () {
            var source = {
              x: maxPosX - 1,
              // -1 for the line thickness
              y: scaleY(0.5)
            };
            var target = {
              x: maxPosX + shift,
              y: scaleY(0.65)
            };
            return _this.diagonal(source, target);
          }).attr('fill', 'none').attr('stroke-width', 1).attr('stroke', 'black');
          svg.append('text').attr('class', localTextCssClass).attr('y', scaleY(0.65) + 10).attr('x', minPosX - shift).text(Math.round(min * 100) / 100);
          svg.append('text').attr('class', localTextCssClass).attr('y', scaleY(0.65) + 10).attr('x', maxPosX + shift).text(Math.round(max * 100) / 100);
        } // Add the standard deviation rectangle


      svg.append('rect').attr('class', rectangleCssClass).attr('x', scaleX(mean) - scaleVal(std)).attr('y', (height - stdRectangleHeight) / 2).attr('width', 2 * scaleVal(std)).attr('height', stdRectangleHeight); // Add the mean line

      svg.append('line').attr('stroke-width', 2).attr('stroke', 'coral').attr('x1', scaleX(mean)).attr('x2', scaleX(mean)).attr('y1', (height - stdRectangleHeight) / 2 - 2).attr('y2', (height + stdRectangleHeight) / 2 + 2);
      svg.append('text').attr('class', localTextCssClass).attr('y', (height - stdRectangleHeight) / 2 - 10).attr('x', scaleX(mean)).text(Math.round(mean * 100) / 100);
    });

    _defineProperty(_assertThisInitialized(_this), "getSVGRef", function (node) {
      _this.node = node;
    });

    _defineProperty(_assertThisInitialized(_this), "getIconRef", function (info) {
      _this.info = info;
    });

    return _this;
  }

  _createClass(BoxPlot, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.createBoxPlot(this.props);
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      // Delegate rendering the tree to a d3 function on prop change
      this.createBoxPlot(nextProps); // Do not allow react to render the component on prop change

      return nextState.tooltipShown != this.state.tooltipShown;
    }
  }, {
    key: "diagonal",
    value: function diagonal(source, target) {
      return "M".concat(source.x, ",").concat(source.y, "C").concat(source.x, ",").concat((source.y + target.y) / 2, " ").concat(target.x, ",").concat((source.y + target.y) / 2, " ").concat(target.x, ",").concat(target.y);
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        style: {
          display: 'inline-flex'
        }
      }, _react.default.createElement("svg", {
        ref: this.getSVGRef
      }), _react.default.createElement("div", {
        ref: this.getIconRef
      }, _react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
        icon: _freeSolidSvgIcons.faInfoCircle,
        onMouseEnter: this.showTooltip,
        onMouseLeave: this.showTooltip
      })), _react.default.createElement(_reactCraftAiTooltip.default // disable click on tooltip
      , {
        style: {
          pointerEvents: 'none',
          fontFamily: 'monospace',
          whiteSpace: 'pre'
        },
        show: this.state.tooltipShown,
        placement: 'top',
        target: this.info
      }, "global  -std _  _ +std  global", _react.default.createElement("br", null), "min       | || |       max", _react.default.createElement("br", null), "|---|====| || |====|---|", _react.default.createElement("br", null), "min   |_||_|   max", _react.default.createElement("br", null), "mean", _react.default.createElement("br", null)));
    }
  }]);

  return BoxPlot;
}(_react.default.Component);

BoxPlot.defaultProps = {
  width: 180,
  height: 100
};
BoxPlot.propTypes = {
  mean: _propTypes.default.number.isRequired,
  std: _propTypes.default.number.isRequired,
  min: _propTypes.default.number.isRequired,
  max: _propTypes.default.number.isRequired,
  totalMin: _propTypes.default.number,
  totalMax: _propTypes.default.number,
  width: _propTypes.default.number,
  height: _propTypes.default.number
};
var _default = BoxPlot;
exports.default = _default;