"use strict";

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

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

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

var _shortid = _interopRequireDefault(require("shortid"));

var _lodash = require("lodash");

var _victory = require("victory");

var _emotionTheming = require("emotion-theming");

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

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");

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 LineChart = function LineChart(_ref) {
  var customBackgroundPlot = _ref.customBackgroundPlot,
      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 ? (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;
  var lineData = dataSeries ? data && data.length ? (0, _lodash.groupBy)(safeData, dataSeries) : null : {
    category: safeData
  };
  var dataSeriesList = dataSeriesLabels || [{
    category: "category"
  }];
  var lines = lineData ? dataSeriesList.map(function (item, index) {
    return _react["default"].createElement(_victory.VictoryLine, {
      title: "Line Chart",
      key: _shortid["default"].generate(),
      data: lineData[item.category].map(function (d) {
        return {
          dataKey: d[dataKey],
          dataValue: d[dataValue],
          series: d[dataSeries]
        };
      }),
      x: "dataKey",
      y: "dataValue",
      style: (0, _chartHelpers.getDefaultLineStyle)(index, theme),
      standalone: false,
      animate: true
    });
  }) : null;
  return _react["default"].createElement(_emotionTheming.ThemeProvider, {
    theme: theme
  }, _react["default"].createElement(_ChartContainer["default"], {
    title: title,
    subtitle: subtitle,
    loading: loading
  }, _react["default"].createElement(_DataChecker["default"], {
    dataAccessors: {
      dataKey: dataKey,
      dataValue: dataValue
    },
    data: safeData
  }, legendData && (legendComponent ? legendComponent(legendData, theme) : _react["default"].createElement(_SimpleLegend["default"], {
    className: "legend",
    legendData: legendData,
    theme: theme
  })), _react["default"].createElement(_victory.VictoryChart, {
    domain: chartDomain,
    padding: {
      left: 75,
      right: 50,
      bottom: 50,
      top: 50
    },
    theme: theme
  }, _react["default"].createElement(_victory.VictoryAxis, {
    style: {
      grid: {
        stroke: "none"
      }
    },
    tickFormat: function tickFormat(x) {
      return xNumberFormatter(x);
    },
    title: "X Axis"
  }), _react["default"].createElement(_victory.VictoryAxis, {
    dependentAxis: true,
    tickFormat: function tickFormat(y) {
      return yNumberFormatter(y);
    },
    title: "Y Axis"
  }), _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: 75,
    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
  })), customBackgroundPlot, lines, _react["default"].createElement(_victory.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: (0, _chartHelpers.chartEvents)(theme),
    labelComponent: _react["default"].createElement(_victory.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 = {
  customBackgroundPlot: _propTypes["default"].node,
  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,
    value: _propTypes["default"].string
  }),
  style: _propTypes["default"].arrayOf(_propTypes["default"].shape({})),
  subtitle: _propTypes["default"].string,
  title: _propTypes["default"].string,
  xLabel: _propTypes["default"].string,
  yLabel: _propTypes["default"].string,
  xNumberFormatter: _propTypes["default"].func,
  yNumberFormatter: _propTypes["default"].func,
  legendComponent: _propTypes["default"].func,
  theme: _propTypes["default"].shape({}),
  loading: _propTypes["default"].bool,
  protect: _propTypes["default"].bool
};
LineChart.defaultProps = {
  customBackgroundPlot: null,
  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["default"].numeric,
  yNumberFormatter: _civicFormat["default"].numeric,
  legendComponent: null,
  theme: _index.VictoryTheme,
  loading: null,
  protect: false
};
var _default = LineChart;
exports["default"] = _default;