"use strict";

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

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

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

var _d3Shape = require("d3-shape");

var _d3Selection = require("d3-selection");

var _Utilities = require("../Utilities/");

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

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 Line =
/*#__PURE__*/
function (_Component) {
  _inherits(Line, _Component);

  function Line() {
    _classCallCheck(this, Line);

    return _possibleConstructorReturn(this, _getPrototypeOf(Line).apply(this, arguments));
  }

  _createClass(Line, [{
    key: "getXScale",
    value: function getXScale() {
      // get props and aesthetic to render
      var _this$props = this.props,
          data = _this$props.data,
          aes = _this$props.aes; // get x-values

      var xValues = data.map(function (item) {
        return item[aes[0]];
      });
      var scaleObj = new _Utilities.scaleFinder(xValues);
      return scaleObj;
    }
  }, {
    key: "getXScaleType",
    value: function getXScaleType() {
      var _this$props2 = this.props,
          dimensions = _this$props2.dimensions,
          scaleTypes = _this$props2.scaleTypes;
      var scaleObj = this.getXScale();
      var xScale;
      if (scaleTypes[0] === "linear") xScale = scaleObj.getLinearScale().nice();
      if (scaleTypes[0] === "time") xScale = scaleObj.getTimeScale().nice(); //if(scaleTypes[0] === "ordinal")
      //    xScale = scaleObj.getOrdinalScale(0.5); // .nice() method not available
      // set scale range

      xScale.range([dimensions.padding, dimensions.width - dimensions.padding]);
      return xScale;
    }
  }, {
    key: "getYScale",
    value: function getYScale() {
      // get y-values
      var _this$props3 = this.props,
          data = _this$props3.data,
          aes = _this$props3.aes;
      var yValues = data.map(function (item) {
        return item[aes[1]];
      }); // create xScale

      var scaleObj = new _Utilities.scaleFinder(yValues);
      return scaleObj;
    }
  }, {
    key: "getYScaleType",
    value: function getYScaleType() {
      var _this$props4 = this.props,
          scaleTypes = _this$props4.scaleTypes,
          dimensions = _this$props4.dimensions;
      var scaleObj = this.getYScale();
      var yScale;
      if (scaleTypes[1] === "linear") yScale = scaleObj.getLinearScale().nice();
      if (scaleTypes[1] === "time") yScale = scaleObj.getTimeScale().nice(); //if(scaleTypes[1] === "ordinal")
      //    yScale = scaleObj.getOrdinalScale(0.5); // .nice() method not available
      // set scale range

      yScale.range([dimensions.height - dimensions.padding, dimensions.padding]);
      return yScale;
    }
  }, {
    key: "createLine",
    value: function createLine() {
      var aes = this.props.aes;
      var xScale = this.getXScaleType();
      var yScale = this.getYScaleType(); // create line for chart

      var chartLine = (0, _d3Shape.line)().x(function (d) {
        return xScale(d[aes[0]]);
      }).y(function (d) {
        return yScale(d[aes[1]]);
      }).curve(_d3Shape.curveCatmullRom);
      return chartLine;
    }
  }, {
    key: "appendLine",
    value: function appendLine() {
      var _this$props5 = this.props,
          data = _this$props5.data,
          color = _this$props5.color,
          lineWidth = _this$props5.lineWidth;
      var lineToAppend = this.createLine(); // append line to plot

      (0, _d3Selection.select)(this.node).datum(data).attr("fill", "none").attr("stroke", color).attr("stroke-width", lineWidth).attr("d", lineToAppend);
    }
  }, {
    key: "render",
    value: function render() {
      var _this = this;

      return _react.default.createElement("g", {
        className: this.props.className
      }, _react.default.createElement("path", {
        ref: function ref(node) {
          return _this.node = node;
        }
      }));
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.appendLine();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.appendLine();
    }
  }]);

  return Line;
}(_react.Component);

_defineProperty(Line, "defaultProps", {
  dimensions: {
    width: window.innerWidth * 0.9,
    height: window.innerHeight * 0.9,
    padding: 50
  },
  className: null,
  color: "orange",
  lineWidth: 1
});

_defineProperty(Line, "propTypes", {
  data: _propTypes.default.array.isRequired,
  dimensions: _propTypes.default.shape({
    width: _propTypes.default.number,
    height: _propTypes.default.number,
    padding: _propTypes.default.number
  }),
  scaleTypes: _propTypes.default.array,
  className: _propTypes.default.string,
  aes: _propTypes.default.array,
  color: _propTypes.default.string,
  lineWidth: _propTypes.default.number
});

var _default = Line;
exports.default = _default;