"use strict";

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

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

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

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

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

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

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

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

  _createClass(Points, [{
    key: "getXScale",
    value: function getXScale() {
      // get data and x and y value pair
      var _this$props = this.props,
          data = _this$props.data,
          aes = _this$props.aes; // convert data to a scale to be used for our 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; // find appropiate scale

      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: "appendCircles",
    value: function appendCircles() {
      // scales
      var xScale = this.getXScaleType();
      var yScale = this.getYScaleType(); // all props needed for points

      var _this$props5 = this.props,
          color = _this$props5.color,
          radius = _this$props5.radius;
      var _this$props6 = this.props,
          data = _this$props6.data,
          aes = _this$props6.aes; // clear graph for next set of data points

      if (this.node.children.length > 0) (0, _d3Selection.select)(this.node).selectAll("circle").remove(); // append new points

      (0, _d3Selection.select)(this.node).selectAll("circle").data(data).enter().append("circle").attr("cx", function (d) {
        return xScale(d[aes[0]]);
      }).attr("cy", function (d) {
        return yScale(d[aes[1]]);
      }).attr("r", radius).attr("fill", color);
    }
  }, {
    key: "render",
    value: function render() {
      var _this = this;

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

  return Points;
}(_react.Component);

_defineProperty(Points, "defaultProps", {
  dimensions: {
    width: window.innerWidth * 0.9,
    height: window.innerHeight * 0.9,
    padding: 50
  },
  className: "points",
  color: "orange",
  radius: 3
});

_defineProperty(Points, "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,
  radius: _propTypes.default.number
});

var _default = Points;
exports.default = _default;