"use strict";

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

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

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

var _lodash = _interopRequireDefault(require("lodash.uniq"));

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

var _d3Scale = require("d3-scale");

var _d3SvgLegend = require("d3-svg-legend");

var _d3Transition = require("d3-transition");

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

// needed for legendColor.
var Color =
/*#__PURE__*/
function (_Component) {
  _inherits(Color, _Component);

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

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

  _createClass(Color, [{
    key: "generateRandomHEXValue",
    value: function generateRandomHEXValue() {
      return "#000000".replace(/0/g, function () {
        return (~~(Math.random() * 16)).toString(16);
      });
    }
  }, {
    key: "getUniqueItems",
    value: function getUniqueItems() {
      var _this$props = this.props,
          data = _this$props.data,
          subset = _this$props.subset;
      var objectItems = data.map(function (item) {
        return item[subset];
      });
      var uniqueObjectItems = (0, _lodash.default)(objectItems);
      return uniqueObjectItems;
    }
  }, {
    key: "determineFillColor",
    value: function determineFillColor() {
      var _this = this;

      var objectItems = this.getUniqueItems();
      var colors = objectItems.map(function (item) {
        return {
          subset: item,
          fill: _this.generateRandomHEXValue()
        };
      });
      return colors;
    }
  }, {
    key: "colorCodeNodes",
    value: function colorCodeNodes() {
      var _this$props2 = this.props,
          chartType = _this$props2.chartType,
          subset = _this$props2.subset;
      var fills = this.determineFillColor();
      (0, _d3Selection.select)(".".concat(chartType)).selectAll("circle").attr("fill", function (d) {
        var colorToUse = fills.filter(function (item) {
          return item["subset"] === d[subset];
        });
        return colorToUse[0]["fill"];
      });
      this.createLegend(fills);
    }
  }, {
    key: "createLegend",
    value: function createLegend(colors) {
      var _this$props3 = this.props,
          dimensions = _this$props3.dimensions,
          subset = _this$props3.subset; // create scale

      var legendValues = colors.map(function (item) {
        return item.subset;
      });
      var scale = (0, _d3Scale.scaleBand)().domain(legendValues);
      var legendColors = colors.map(function (item) {
        return item.fill;
      }); // get rid of previous legend

      if (this.node.children.length > 0) {
        (0, _d3Selection.select)(this.node).select("g").remove();
        (0, _d3Selection.select)(this.node).select("text").remove();
      } // move "g" to right side of graph


      (0, _d3Selection.select)(this.node).attr("transform", "translate(".concat(dimensions.width * 0.9 - dimensions.padding, ", ").concat(dimensions.padding + 15, ")")); // append text

      (0, _d3Selection.select)(this.node).append("text").text("".concat(subset)).attr("transform", "translate(0,-15)"); // give a little padding to label
      // create scale for legend

      var colorLegend = (0, _d3SvgLegend.legendColor)().scale(scale); // append new legend

      (0, _d3Selection.select)(this.node).call(colorLegend).selectAll("rect").attr("paddingBottom", "10px").attr("fill", function (value, index) {
        return legendColors[index];
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

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

  return Color;
}(_react.Component);

_defineProperty(Color, "propTypes", {
  data: _propTypes.default.array,
  subset: _propTypes.default.string,
  chartType: _propTypes.default.string,
  dimensions: _propTypes.default.shape({
    width: _propTypes.default.number,
    height: _propTypes.default.number,
    padding: _propTypes.default.number
  })
});

var _default = Color;
exports.default = _default;