"use strict";

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

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

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

var _victory = require("victory");

var _ChartContainer = _interopRequireDefault(require("../ChartContainer"));

var _SimpleLegend = _interopRequireDefault(require("../SimpleLegend"));

var _civicFormat = _interopRequireDefault(require("../utils/civicFormat"));

var _protectData = _interopRequireDefault(require("../utils/protectData"));

var _DataChecker = _interopRequireDefault(require("../utils/DataChecker"));

var _chartHelpers = require("../utils/chartHelpers");

var _index = require("../_Themes/index");

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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 Scatterplot = function Scatterplot(_ref) {
  var data = _ref.data,
      dataKey = _ref.dataKey,
      dataKeyLabel = _ref.dataKeyLabel,
      dataValue = _ref.dataValue,
      dataValueLabel = _ref.dataValueLabel,
      dataSeries = _ref.dataSeries,
      dataSeriesLabel = _ref.dataSeriesLabel,
      domain = _ref.domain,
      size = _ref.size,
      style = _ref.style,
      subtitle = _ref.subtitle,
      title = _ref.title,
      xLabel = _ref.xLabel,
      yLabel = _ref.yLabel,
      xNumberFormatter = _ref.xNumberFormatter,
      yNumberFormatter = _ref.yNumberFormatter,
      invertX = _ref.invertX,
      invertY = _ref.invertY,
      legendComponent = _ref.legendComponent,
      theme = _ref.theme,
      loading = _ref.loading,
      protect = _ref.protect;
  var safeData = data && data.length ? protect ? (0, _protectData["default"])(data, {
    dataSeries: dataSeries,
    dataSeriesLabel: dataSeriesLabel
  }) : data : [{}];
  var safeDataSeriesLabel = dataSeriesLabel && dataSeriesLabel.length ? protect ? (0, _protectData["default"])(dataSeriesLabel, {
    x: "category",
    y: "label"
  }) : dataSeriesLabel : null;
  var chartDomain = domain || (0, _chartHelpers.getDefaultDomain)(safeData, dataKey, dataValue);
  var dataSeriesLabels = dataSeries ? safeDataSeriesLabel || (0, _chartHelpers.getDefaultDataSeriesLabels)(safeData, dataSeries) : null;
  var scatterPlotStyle = style || (0, _chartHelpers.getDefaultFillStyle)(dataSeriesLabels, theme);
  var legendData = dataSeriesLabels && dataSeriesLabels.length ? dataSeriesLabels.map(function (series) {
    return {
      name: series.label
    };
  }) : null;
  return _react["default"].createElement(_ChartContainer["default"], {
    title: title,
    subtitle: subtitle,
    loading: loading
  }, legendData && (legendComponent ? legendComponent(legendData, theme) : _react["default"].createElement(_SimpleLegend["default"], {
    className: "legend",
    legendData: legendData,
    theme: theme
  })), _react["default"].createElement(_DataChecker["default"], {
    dataAccessors: {
      dataKey: dataKey,
      dataValue: dataValue
    },
    data: safeData
  }, _react["default"].createElement(_victory.VictoryChart, {
    domain: chartDomain,
    theme: theme,
    animate: {
      duration: 200
    }
  }, _react["default"].createElement(_victory.VictoryAxis, {
    animate: {
      onEnter: {
        duration: 500
      }
    },
    style: {
      grid: {
        stroke: "none"
      }
    },
    tickFormat: function tickFormat(x) {
      return xNumberFormatter(x);
    },
    title: "X Axis",
    invertAxis: invertX
  }), _react["default"].createElement(_victory.VictoryAxis, {
    dependentAxis: true,
    animate: {
      onEnter: {
        duration: 500
      }
    },
    tickFormat: function tickFormat(y) {
      return yNumberFormatter(y);
    },
    title: "Y Axis",
    invertAxis: invertY
  }), _react["default"].createElement(_victory.VictoryPortal, null, _react["default"].createElement(_victory.VictoryLabel, {
    style: _objectSpread({}, theme.axisLabel.style),
    text: yLabel,
    textAnchor: "middle",
    title: "Y Axis Label",
    verticalAnchor: "end",
    x: 50,
    y: 45
  })), _react["default"].createElement(_victory.VictoryPortal, null, _react["default"].createElement(_victory.VictoryLabel, {
    style: _objectSpread({}, theme.axisLabel.style),
    text: xLabel,
    textAnchor: "end",
    title: "X Axis Label",
    verticalAnchor: "end",
    x: 600,
    y: 295
  })), _react["default"].createElement(_victory.VictoryScatter, {
    animate: {
      onEnter: {
        duration: 500
      }
    },
    bubbleProperty: "bubbleSize",
    minBubbleSize: size && size.minSize,
    maxBubbleSize: size && size.maxSize,
    data: safeData.map(function (d) {
      return _objectSpread({
        dataKey: d[dataKey],
        dataValue: d[dataValue],
        label: "".concat(dataKeyLabel ? d[dataKeyLabel] : xLabel, ": ").concat(xNumberFormatter(d[dataKey]), " \u2022 ").concat(dataValueLabel ? d[dataValueLabel] : yLabel, ": ").concat(yNumberFormatter(d[dataValue])),
        series: d[dataSeries]
      }, size && {
        bubbleSize: d[size.key]
      });
    }),
    events: (0, _chartHelpers.chartEvents)(theme),
    labelComponent: _react["default"].createElement(_victory.VictoryTooltip, {
      x: 325,
      y: 0,
      orientation: "bottom",
      pointerLength: 0,
      cornerRadius: 0,
      theme: theme
    }),
    size: 3,
    style: scatterPlotStyle,
    title: "Scatter Plot",
    x: "dataKey",
    y: "dataValue"
  }))));
};

Scatterplot.propTypes = {
  data: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    x: _propTypes["default"].number,
    y: _propTypes["default"].number
  })),
  dataKey: _propTypes["default"].string,
  dataKeyLabel: _propTypes["default"].string,
  dataValue: _propTypes["default"].string,
  dataValueLabel: _propTypes["default"].string,
  dataSeries: _propTypes["default"].string,
  dataSeriesLabel: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    category: _propTypes["default"].string,
    label: _propTypes["default"].string
  })),
  domain: _propTypes["default"].shape({
    x: _propTypes["default"].arrayOf(_propTypes["default"].number),
    y: _propTypes["default"].arrayOf(_propTypes["default"].number)
  }),
  size: _propTypes["default"].shape({
    key: _propTypes["default"].string,
    minSize: _propTypes["default"].number,
    maxSize: _propTypes["default"].number
  }),
  style: _propTypes["default"].arrayOf(_propTypes["default"].shape({})),
  title: _propTypes["default"].string,
  subtitle: _propTypes["default"].string,
  xLabel: _propTypes["default"].string,
  yLabel: _propTypes["default"].string,
  xNumberFormatter: _propTypes["default"].func,
  yNumberFormatter: _propTypes["default"].func,
  invertX: _propTypes["default"].bool,
  invertY: _propTypes["default"].bool,
  legendComponent: _propTypes["default"].func,
  theme: _propTypes["default"].shape({}),
  loading: _propTypes["default"].bool,
  protect: _propTypes["default"].bool
};
Scatterplot.defaultProps = {
  data: null,
  dataKey: "x",
  dataKeyLabel: null,
  dataValue: "y",
  dataValueLabel: null,
  dataSeries: null,
  dataSeriesLabel: null,
  domain: null,
  size: null,
  style: null,
  title: null,
  subtitle: null,
  xLabel: "X",
  yLabel: "Y",
  xNumberFormatter: _civicFormat["default"].numeric,
  yNumberFormatter: _civicFormat["default"].numeric,
  invertX: false,
  invertY: false,
  legendComponent: null,
  theme: _index.VictoryTheme,
  loading: null,
  protect: false
};
var _default = Scatterplot;
exports["default"] = _default;