"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.RawLine = exports.Line = void 0;

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

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 _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 _group = require("@vx/group");

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

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

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

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

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

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

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

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; } }

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; }

var getData = function getData(data, domain) {
  var _ref = domain || {},
      max = _ref.max;

  var length = data.length;

  if (!max || !length) {
    return [];
  }

  return data.map(function (el, i) {
    return _objectSpread(_objectSpread({}, el), {}, {
      x: length > 1 ? i * (max / (length - 1)) : 0.5,
      y: el.value
    });
  });
};

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

  var _super = _createSuper(RawLine);

  function RawLine(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, RawLine);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "setDragValue", function (line) {
      return _this.setState({
        line: line
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragStop", function (index) {
      var onChange = _this.props.onChange;

      _this.setState({
        dragging: false
      }, function () {
        onChange(index, _this.state.line[index]);
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "dragValue", function (index, existing, next) {
      var newLine = (0, _toConsumableArray2["default"])(_this.state.line);
      newLine[index].dragValue = next;

      _this.setDragValue(newLine);
    });
    _this.state = {
      dragValue: undefined,
      line: getData(props.data, props.graphProps.domain)
    };
    return _this;
  }

  (0, _createClass2["default"])(RawLine, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (!(0, _isEqual["default"])(this.props.data, nextProps.data)) {
        this.setState({
          line: getData(nextProps.data, nextProps.graphProps.domain)
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          graphProps = _this$props.graphProps,
          data = _this$props.data,
          classes = _this$props.classes,
          CustomDraggableComponent = _this$props.CustomDraggableComponent,
          defineChart = _this$props.defineChart;
      var _this$state = this.state,
          lineState = _this$state.line,
          dragging = _this$state.dragging;
      var scale = graphProps.scale;
      var lineToUse = dragging ? lineState : getData(data, graphProps.domain);
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_shape.LinePath, {
        data: lineToUse,
        x: function x(d) {
          return scale.x(d.x);
        },
        y: function y(d) {
          return scale.y(d.dragValue !== undefined ? d.dragValue : d.y);
        },
        className: classes.line
      }), lineToUse && lineToUse.map(function (point, i) {
        var r = 6;
        var enableDraggable = defineChart || point.interactive;
        var Component = enableDraggable ? _dragHandle["default"] : _dragHandle.DragHandle;
        return /*#__PURE__*/_react["default"].createElement(Component, {
          key: "point-".concat(point.x, "-").concat(i),
          x: point.x,
          y: point.dragValue !== undefined ? point.dragValue : point.y,
          interactive: enableDraggable,
          r: r,
          onDragStart: function onDragStart() {
            return _this2.setState({
              dragging: true
            });
          },
          onDrag: function onDrag(v) {
            return _this2.dragValue(i, point.dragValue !== undefined ? point.dragValue : point.y, v);
          },
          onDragStop: function onDragStop() {
            return _this2.dragStop(i);
          },
          graphProps: graphProps,
          CustomDraggableComponent: CustomDraggableComponent,
          correctness: point.correctness
        });
      }));
    }
  }]);
  return RawLine;
}(_react["default"].Component);

exports.RawLine = RawLine;
(0, _defineProperty2["default"])(RawLine, "propTypes", {
  onChange: _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,
  defineChart: _propTypes["default"].bool,
  data: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    label: _propTypes["default"].string,
    value: _propTypes["default"].number
  })),
  CustomDraggableComponent: _propTypes["default"].func
});
(0, _defineProperty2["default"])(RawLine, "defaultProps", {
  index: 0
});
var StyledLine = (0, _index.withStyles)(function () {
  return {
    line: {
      fill: 'transparent',
      stroke: _renderUi.color.primaryLight(),
      strokeWidth: 3,
      transition: 'stroke 200ms ease-in, stroke-width 200ms ease-in',
      '&:hover': {
        strokeWidth: 6,
        stroke: _renderUi.color.primaryDark()
      }
    }
  };
})(RawLine);

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

  var _super2 = _createSuper(Line);

  function Line() {
    var _this3;

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

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

    _this3 = _super2.call.apply(_super2, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this3), "changeLine", function (index, category) {
      var onChange = _this3.props.onChange;
      var newLine = (0, _toConsumableArray2["default"])(_this3.props.data);
      var dragValue = category.dragValue,
          value = category.value;
      newLine[index].value = dragValue >= 0 ? dragValue : value;
      onChange(newLine);
    });
    return _this3;
  }

  (0, _createClass2["default"])(Line, [{
    key: "render",
    value: function render() {
      var props = this.props;
      return /*#__PURE__*/_react["default"].createElement(_group.Group, null, /*#__PURE__*/_react["default"].createElement(StyledLine, (0, _extends2["default"])({}, props, {
        onChange: this.changeLine
      })));
    }
  }]);
  return Line;
}(_react["default"].Component);

exports.Line = Line;
(0, _defineProperty2["default"])(Line, "propTypes", {
  data: _propTypes["default"].array,
  onChange: _propTypes["default"].func,
  xBand: _propTypes["default"].func,
  graphProps: _plot.types.GraphPropsType.isRequired
});
var _default = Line;
exports["default"] = _default;
//# sourceMappingURL=line.js.map