"use strict";

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

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

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

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

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 margin = {
  top: 30,
  bottom: 50,
  left: 40,
  right: 10
};
var barWidthRatio = 0.5;
var rectColor = 'rgb(0,178,103)';
var maxLegendCharacters = 5;
var tooltipCssClass =
/*#__PURE__*/
(0, _reactEmotion.css)("position:absolute;text-align:center;margin-top:10px;font:12px sans-serif;background:", rectColor, ";color:white;border:0px;border-radius:8px;pointer-events:none;transform:translateX(-100%);width:100px;padding:1px 10px 1px 10px;white-space:normal;");

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

  function Histogram() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Histogram);

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

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

    _defineProperty(_assertThisInitialized(_this), "createHistogram", function (_ref) {
      var distribution = _ref.distribution,
          outputValues = _ref.outputValues,
          size = _ref.size,
          width = _ref.width,
          fullBarWidth = _ref.fullBarWidth;
      var height = fullBarWidth * distribution.length;
      var barWidth = barWidthRatio * fullBarWidth; // Set width and height

      (0, _d.select)(_this.node).attr('width', width + margin.left + margin.right).attr('height', height + margin.bottom + margin.top);
      var scaleX = (0, _d.scaleLinear)().domain([0, 1]).range([margin.left, width + margin.left]);
      var scaleY = (0, _d.scaleLinear)().domain([0, distribution.length]).range([margin.top, height + margin.top]);
      var xAxis = (0, _d.axisTop)().ticks(3).scale(scaleX);
      var yAxis = (0, _d.axisLeft)().tickFormat('').ticks(distribution.length).scale(scaleY);
      (0, _d.select)(_this.node).append('g').attr('transform', "translate(0, ".concat(scaleY(0), ")")).call(xAxis);
      (0, _d.select)(_this.node).append('g').attr('transform', "translate(".concat(scaleX(0), ", 0)")).call(yAxis);
      (0, _d.select)(_this.node).selectAll('text.legend').data(outputValues).enter().append('text').attr('class', 'legend').attr('text-anchor', 'middle').attr('font-size', 10).attr('color', 'black').attr('x', scaleX(0) - 17).attr('y', function (_, i) {
        return scaleY(i) + fullBarWidth / 2 + 4;
      }).text(function (d) {
        return d.length > maxLegendCharacters ? "".concat(d.substring(0, maxLegendCharacters), "...") : d;
      });

      var updateHistogram = function updateHistogram(_ref2) {
        var newSize = _ref2.size,
            newDistribution = _ref2.distribution;
        // Get the tooltip div, apply style and hide it initially
        var div = (0, _d.select)(_this.tooltip).attr('class', tooltipCssClass).style('opacity', 0);
        var histogram = (0, _d.select)(_this.node).selectAll('rect').data(newDistribution, function (d, i) {
          return i;
        }); // Define the rectangle drawing the distribution

        var histEnter = histogram.enter().append('rect').style('fill', "".concat(rectColor)).attr('x', scaleX(0)).attr('y', function (d, i) {
          return scaleY(i) + (scaleY(1) - margin.top - barWidth) / 2;
        }).attr('width', 0).attr('height', barWidth);
        var histUpdate = histEnter.merge(histogram);
        histUpdate.transition().duration(1000).attr('x', margin.left).attr('y', function (d, i) {
          return scaleY(i) + (scaleY(1) - margin.top - barWidth) / 2;
        }).attr('width', function (d) {
          return scaleX(d) - scaleX(0);
        }).attr('height', barWidth);
        histogram.exit().remove(); // Define the rectangle to hover the drawn distribution

        var rectback = (0, _d.select)(_this.node).selectAll('rect.fake').data(newDistribution, function (d, i) {
          return i;
        });
        rectback.enter().append('rect').attr('class', 'fake').attr('opacity', 0.0).style('fill', "".concat(rectColor)).attr('x', scaleX(0)).attr('y', function (_, i) {
          return scaleY(i);
        }).attr('width', scaleX(1) - scaleX(0)).attr('height', fullBarWidth).on('mouseover', function (d, i) {
          (0, _d.select)(this).transition().duration(100).style('opacity', 0.2);
          div.transition().duration(100).style('opacity', 0.9);
          return div.html("<b>".concat(outputValues[i], "</b></br>").concat(d.toFixed(2), "</br>").concat(Math.floor(newSize * d), " samples")).style('left', "".concat(scaleX(1), "px")).style('top', "".concat(scaleY(i) + fullBarWidth / 2, "px"));
        }).on('mouseout', function () {
          (0, _d.select)(this).transition().duration(100).style('opacity', 0.0);
          return div.transition().duration(100).style('opacity', 0);
        });
        rectback.exit().remove();
      };

      return updateHistogram;
    });

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

    _defineProperty(_assertThisInitialized(_this), "getToolTipRef", function (tooltip) {
      _this.tooltip = tooltip;
    });

    return _this;
  }

  _createClass(Histogram, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      // When the React Component is mounted, create the histogram
      // static elements such as axes and legend.
      // This function then returns a function to update the SVG.
      this.updateHistogram = this.createHistogram(this.props);
      this.updateHistogram(this.props);
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      // Delegate rendering the tree to a d3 function on prop change
      this.updateHistogram(nextProps); // Do not allow react to render the component on prop change

      return false;
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", {
        style: {
          position: 'relative',
          display: 'inline-block',
          marginBottom: '30px'
        }
      }, _react.default.createElement("svg", {
        ref: this.getSVGRef
      }), _react.default.createElement("div", {
        ref: this.getToolTipRef
      }));
    }
  }]);

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

Histogram.defaultProps = {
  width: 130,
  fullBarWidth: 25
};
Histogram.propTypes = {
  distribution: _propTypes.default.array.isRequired,
  outputValues: _propTypes.default.array.isRequired,
  size: _propTypes.default.number.isRequired,
  width: _propTypes.default.number,
  fullBarWidth: _propTypes.default.number
};
var _default = Histogram;
exports.default = _default;