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 shortid from "shortid";
import { groupBy } from "lodash";
import { VictoryAxis, VictoryChart, VictoryLabel, VictoryPortal, VictoryScatter, VictoryTooltip, VictoryLine } from "victory";
import { ThemeProvider } from "emotion-theming";
import { VictoryTheme } from "../_Themes/index";
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, getDefaultLineStyle } from "../utils/chartHelpers";

var LineChart = function LineChart(_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,
      legendComponent = _ref.legendComponent,
      loading = _ref.loading,
      theme = _ref.theme,
      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;
  var lineData = dataSeries ? groupBy(safeData, dataSeries) : {
    category: safeData
  };
  var dataSeriesList = dataSeriesLabels || [{
    category: "category"
  }];
  var lines = lineData ? dataSeriesList.map(function (item, index) {
    return React.createElement(VictoryLine, {
      title: "Line Chart",
      key: shortid.generate(),
      data: lineData[item.category].map(function (d) {
        return {
          dataKey: d[dataKey],
          dataValue: d[dataValue],
          series: d[dataSeries]
        };
      }),
      x: "dataKey",
      y: "dataValue",
      style: getDefaultLineStyle(index, theme),
      standalone: false,
      animate: true
    });
  }) : null;
  return React.createElement(ThemeProvider, {
    theme: theme
  }, React.createElement(ChartContainer, {
    title: title,
    subtitle: subtitle,
    loading: loading
  }, React.createElement(DataChecker, {
    dataAccessors: {
      dataKey: dataKey,
      dataValue: dataValue
    },
    data: safeData
  }, legendData && (legendComponent ? legendComponent(legendData, theme) : React.createElement(SimpleLegend, {
    className: "legend",
    legendData: legendData,
    theme: theme
  })), React.createElement(VictoryChart, {
    domain: chartDomain,
    padding: {
      left: 75,
      right: 50,
      bottom: 50,
      top: 50
    },
    theme: theme
  }, React.createElement(VictoryAxis, {
    style: {
      grid: {
        stroke: "none"
      }
    },
    tickFormat: function tickFormat(x) {
      return xNumberFormatter(x);
    },
    title: "X Axis"
  }), React.createElement(VictoryAxis, {
    dependentAxis: true,
    tickFormat: function tickFormat(y) {
      return yNumberFormatter(y);
    },
    title: "Y Axis"
  }), React.createElement(VictoryPortal, null, React.createElement(VictoryLabel, {
    style: _objectSpread({}, theme.axisLabel.style),
    text: yLabel,
    textAnchor: "middle",
    title: "Y Axis Label",
    verticalAnchor: "end",
    x: 75,
    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
  })), lines, React.createElement(VictoryScatter, {
    data: safeData.map(function (d) {
      return {
        dataKey: d[dataKey],
        dataValue: d[dataValue],
        label: "".concat(dataKeyLabel || xLabel, ": ").concat(xNumberFormatter(d[dataKey]), " \u2022 ").concat(dataValueLabel || yLabel, ": ").concat(yNumberFormatter(d[dataValue])),
        series: d[dataSeries],
        size: size ? d[size.key] || size.value : 3
      };
    }),
    events: chartEvents(theme),
    labelComponent: React.createElement(VictoryTooltip, {
      x: 325,
      y: 0,
      orientation: "bottom",
      pointerLength: 0,
      cornerRadius: 0,
      theme: theme
    }),
    size: function size(d) {
      return d.size;
    },
    style: scatterPlotStyle,
    title: "Scatter Plot",
    x: "dataKey",
    y: "dataValue"
  })))));
};

LineChart.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,
    value: PropTypes.string
  }),
  style: PropTypes.arrayOf(PropTypes.shape({})),
  subtitle: PropTypes.string,
  title: PropTypes.string,
  xLabel: PropTypes.string,
  yLabel: PropTypes.string,
  xNumberFormatter: PropTypes.func,
  yNumberFormatter: PropTypes.func,
  legendComponent: PropTypes.func,
  theme: PropTypes.shape({}),
  loading: PropTypes.bool,
  protect: PropTypes.bool
};
LineChart.defaultProps = {
  data: null,
  dataKey: "x",
  dataKeyLabel: null,
  dataValue: "y",
  dataValueLabel: null,
  dataSeries: null,
  dataSeriesLabel: null,
  domain: null,
  size: null,
  style: null,
  subtitle: null,
  title: null,
  xLabel: "X",
  yLabel: "Y",
  xNumberFormatter: civicFormat.numeric,
  yNumberFormatter: civicFormat.numeric,
  legendComponent: null,
  theme: VictoryTheme,
  loading: null,
  protect: false
};
export default LineChart;