"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.RawBar = exports.Bars = 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 _plot = require("../../../plot");

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

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

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

var _index = require("@material-ui/core/styles/index");

var _debug = _interopRequireDefault(require("debug"));

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

var _dragHandle = _interopRequireWildcard(require("../../common/drag-handle"));

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 log = (0, _debug["default"])('pie-lib:chart:bars');
var histogramColors = ['#006699', '#F59B00', '#08916D', '#529EE0', '#52B7D8', '#D9A6C2', '#FFB03B', '#54A77B', '#E16032', '#4FD2D2', '#F0E442', '#E287B2'];
var hoverHistogramColors = ['#003754', '#975616', '#00503B', '#225982', '#1F687D', '#825E6F', '#996428', '#255E44', '#8A331F', '#167A7A', '#91862D', '#894A65'];

var calculateFillColor = function calculateFillColor(isHovered, barColor, index, hoverHistogramColors) {
  if (isHovered && barColor) {
    return hoverHistogramColors[index % hoverHistogramColors.length];
  }

  if (isHovered) {
    return _renderUi.color.primaryDark();
  }

  return barColor || null;
};

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

  var _super = _createSuper(RawBar);

  function RawBar(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, RawBar);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseEnter", function () {
      _this.setState({
        isHovered: true
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseLeave", function () {
      _this.setState({
        isHovered: false
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setDragValue", function (dragValue) {
      return _this.setState({
        dragValue: dragValue
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragStop", function () {
      var _this$props = _this.props,
          label = _this$props.label,
          onChangeCategory = _this$props.onChangeCategory;
      var dragValue = _this.state.dragValue;
      log('[dragStop]', dragValue);

      if (dragValue !== undefined) {
        onChangeCategory({
          label: label,
          value: dragValue
        });
      }

      _this.setDragValue(undefined);
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragValue", function (existing, next) {
      log('[dragValue] next:', next);

      _this.setDragValue(next);
    });
    _this.state = {
      dragValue: undefined,
      isHovered: false
    };
    return _this;
  }

  (0, _createClass2["default"])(RawBar, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          graphProps = _this$props2.graphProps,
          value = _this$props2.value,
          label = _this$props2.label,
          classes = _this$props2.classes,
          xBand = _this$props2.xBand,
          index = _this$props2.index,
          interactive = _this$props2.interactive,
          correctness = _this$props2.correctness,
          barColor = _this$props2.barColor,
          defineChart = _this$props2.defineChart;
      var scale = graphProps.scale,
          range = graphProps.range;
      var _this$state = this.state,
          dragValue = _this$state.dragValue,
          isHovered = _this$state.isHovered;
      var fillColor = calculateFillColor(isHovered, barColor, index, hoverHistogramColors);
      var v = Number.isFinite(dragValue) ? dragValue : value;
      var barWidth = xBand.bandwidth();
      var barHeight = scale.y(range.max - v);
      var barX = xBand((0, _utils.bandKey)({
        label: label
      }, index));
      var rawY = range.max - v;
      var yy = range.max - rawY;
      log('label:', label, 'barX:', barX, 'v: ', v, 'barHeight:', barHeight, 'barWidth: ', barWidth);
      var Component = interactive ? _dragHandle["default"] : _dragHandle.DragHandle;
      return /*#__PURE__*/_react["default"].createElement("g", {
        onMouseEnter: this.handleMouseEnter,
        onMouseLeave: this.handleMouseLeave
      }, /*#__PURE__*/_react["default"].createElement(_shape.Bar, {
        x: barX,
        y: scale.y(yy),
        width: barWidth,
        height: barHeight,
        className: classes.bar,
        style: {
          fill: fillColor
        }
      }), /*#__PURE__*/_react["default"].createElement(Component, {
        x: barX,
        y: v,
        defineChart: defineChart,
        interactive: interactive,
        width: barWidth,
        onDrag: function onDrag(v) {
          return _this2.dragValue(value, v);
        },
        onDragStop: this.dragStop,
        graphProps: graphProps,
        correctness: correctness,
        isHovered: isHovered,
        color: fillColor
      }));
    }
  }]);
  return RawBar;
}(_react["default"].Component);

exports.RawBar = RawBar;
(0, _defineProperty2["default"])(RawBar, "propTypes", {
  barColor: _propTypes["default"].string,
  onChangeCategory: _propTypes["default"].func,
  value: _propTypes["default"].number,
  classes: _propTypes["default"].object,
  label: _propTypes["default"].string,
  xBand: _propTypes["default"].func,
  index: _propTypes["default"].number.isRequired,
  graphProps: _plot.types.GraphPropsType.isRequired,
  interactive: _propTypes["default"].bool,
  correctness: _propTypes["default"].shape({
    value: _propTypes["default"].string,
    label: _propTypes["default"].string
  })
});
var Bar = (0, _index.withStyles)(function () {
  return {
    bar: {
      fill: _renderUi.color.primaryLight(),
      '&:hover': {
        fill: _renderUi.color.primaryDark()
      }
    }
  };
})(RawBar);

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

  var _super2 = _createSuper(Bars);

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

  (0, _createClass2["default"])(Bars, [{
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          data = _this$props3.data,
          graphProps = _this$props3.graphProps,
          xBand = _this$props3.xBand,
          _onChangeCategory = _this$props3.onChangeCategory,
          defineChart = _this$props3.defineChart,
          histogram = _this$props3.histogram;
      return /*#__PURE__*/_react["default"].createElement(_group.Group, null, (data || []).map(function (d, index) {
        return /*#__PURE__*/_react["default"].createElement(Bar, {
          value: d.value,
          interactive: defineChart || d.interactive,
          defineChart: defineChart,
          label: d.label,
          xBand: xBand,
          index: index,
          key: "bar-".concat(d.label, "-").concat(d.value, "-").concat(index),
          onChangeCategory: function onChangeCategory(category) {
            return _onChangeCategory(index, category);
          },
          graphProps: graphProps,
          correctness: d.correctness,
          barColor: histogram && (histogramColors[index] ? histogramColors[index] : histogramColors[index % histogramColors.length])
        });
      }));
    }
  }]);
  return Bars;
}(_react["default"].Component);

exports.Bars = Bars;
(0, _defineProperty2["default"])(Bars, "propTypes", {
  data: _propTypes["default"].array,
  onChangeCategory: _propTypes["default"].func,
  defineChart: _propTypes["default"].bool,
  xBand: _propTypes["default"].func,
  graphProps: _plot.types.GraphPropsType.isRequired,
  histogram: _propTypes["default"].bool
});
var _default = Bars;
exports["default"] = _default;
//# sourceMappingURL=bars.js.map