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

import PropTypes from "prop-types";
import React from "react";
import { VictoryAxis, VictoryChart, VictoryLabel, VictoryPortal, VictoryScatter, VictoryTooltip } from "victory";
import ChartContainer from "../ChartContainer";
import SimpleLegend from "../SimpleLegend";
import civicFormat from "../utils/civicFormat";
import protectData from "../utils/protectData";
import DataChecker from "../utils/DataChecker";
import { chartEvents, getDefaultDomain, getDefaultDataSeriesLabels, getDefaultFillStyle } from "../utils/chartHelpers";
import { VictoryTheme } from "../_Themes/index";

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 ? protectData(data, {
    dataSeries: dataSeries,
    dataSeriesLabel: dataSeriesLabel
  }) : data : [{}];
  var safeDataSeriesLabel = dataSeriesLabel && dataSeriesLabel.length ? protect ? protectData(dataSeriesLabel, {
    x: "category",
    y: "label"
  }) : dataSeriesLabel : null;
  var chartDomain = domain || getDefaultDomain(safeData, dataKey, dataValue);
  var dataSeriesLabels = dataSeries ? safeDataSeriesLabel || getDefaultDataSeriesLabels(safeData, dataSeries) : null;
  var scatterPlotStyle = style || getDefaultFillStyle(dataSeriesLabels, theme);
  var legendData = dataSeriesLabels && dataSeriesLabels.length ? dataSeriesLabels.map(function (series) {
    return {
      name: series.label
    };
  }) : null;
  return React.createElement(ChartContainer, {
    title: title,
    subtitle: subtitle,
    loading: loading
  }, legendData && (legendComponent ? legendComponent(legendData, theme) : React.createElement(SimpleLegend, {
    className: "legend",
    legendData: legendData,
    theme: theme
  })), React.createElement(DataChecker, {
    dataAccessors: {
      dataKey: dataKey,
      dataValue: dataValue
    },
    data: safeData
  }, React.createElement(VictoryChart, {
    domain: chartDomain,
    theme: theme,
    animate: {
      duration: 200
    }
  }, React.createElement(VictoryAxis, {
    animate: {
      onEnter: {
        duration: 500
      }
    },
    style: {
      grid: {
        stroke: "none"
      }
    },
    tickFormat: function tickFormat(x) {
      return xNumberFormatter(x);
    },
    title: "X Axis",
    invertAxis: invertX
  }), React.createElement(VictoryAxis, {
    dependentAxis: true,
    animate: {
      onEnter: {
        duration: 500
      }
    },
    tickFormat: function tickFormat(y) {
      return yNumberFormatter(y);
    },
    title: "Y Axis",
    invertAxis: invertY
  }), React.createElement(VictoryPortal, null, React.createElement(VictoryLabel, {
    style: _objectSpread({}, theme.axisLabel.style),
    text: yLabel,
    textAnchor: "middle",
    title: "Y Axis Label",
    verticalAnchor: "end",
    x: 50,
    y: 45
  })), React.createElement(VictoryPortal, null, React.createElement(VictoryLabel, {
    style: _objectSpread({}, theme.axisLabel.style),
    text: xLabel,
    textAnchor: "end",
    title: "X Axis Label",
    verticalAnchor: "end",
    x: 600,
    y: 295
  })), React.createElement(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: chartEvents(theme),
    labelComponent: React.createElement(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.arrayOf(PropTypes.shape({
    x: PropTypes.number,
    y: PropTypes.number
  })),
  dataKey: PropTypes.string,
  dataKeyLabel: PropTypes.string,
  dataValue: PropTypes.string,
  dataValueLabel: PropTypes.string,
  dataSeries: PropTypes.string,
  dataSeriesLabel: PropTypes.arrayOf(PropTypes.shape({
    category: PropTypes.string,
    label: PropTypes.string
  })),
  domain: PropTypes.shape({
    x: PropTypes.arrayOf(PropTypes.number),
    y: PropTypes.arrayOf(PropTypes.number)
  }),
  size: PropTypes.shape({
    key: PropTypes.string,
    minSize: PropTypes.number,
    maxSize: PropTypes.number
  }),
  style: PropTypes.arrayOf(PropTypes.shape({})),
  title: PropTypes.string,
  subtitle: PropTypes.string,
  xLabel: PropTypes.string,
  yLabel: PropTypes.string,
  xNumberFormatter: PropTypes.func,
  yNumberFormatter: PropTypes.func,
  invertX: PropTypes.bool,
  invertY: PropTypes.bool,
  legendComponent: PropTypes.func,
  theme: PropTypes.shape({}),
  loading: PropTypes.bool,
  protect: PropTypes.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.numeric,
  yNumberFormatter: civicFormat.numeric,
  invertX: false,
  invertY: false,
  legendComponent: null,
  theme: VictoryTheme,
  loading: null,
  protect: false
};
export default Scatterplot;