"use strict";

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

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

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

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 _DataChecker = _interopRequireDefault(require("../utils/DataChecker"));

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

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

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

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

var BarChart = function BarChart(_ref) {
  var data = _ref.data,
      dataKey = _ref.dataKey,
      dataValue = _ref.dataValue,
      dataSeries = _ref.dataSeries,
      dataSeriesLabel = _ref.dataSeriesLabel,
      domain = _ref.domain,
      title = _ref.title,
      subtitle = _ref.subtitle,
      xLabel = _ref.xLabel,
      yLabel = _ref.yLabel,
      xNumberFormatter = _ref.xNumberFormatter,
      yNumberFormatter = _ref.yNumberFormatter,
      barWidth = _ref.barWidth,
      loading = _ref.loading,
      error = _ref.error,
      theme = _ref.theme,
      legendComponent = _ref.legendComponent,
      protect = _ref.protect;
  var safeData = data && data.length ? data : [{}];
  var chartDomain = domain || (0, _chartHelpers.getDefaultDomain)(safeData, dataKey, dataValue);

  var categories = dataSeries && _toConsumableArray(new Set(safeData.map(function (d) {
    return d[dataSeries];
  })));

  var safeDataSeriesLabel = dataSeriesLabel && dataSeriesLabel.length ? protect ? (0, _protectData["default"])(dataSeriesLabel, {
    x: "category",
    y: "label"
  }) : dataSeriesLabel : null;
  var dataSeriesLabels = dataSeries ? safeDataSeriesLabel || (0, _chartHelpers.getDefaultDataSeriesLabels)(safeData, dataSeries) : null;
  var legendData = dataSeriesLabels && dataSeriesLabels.length ? dataSeriesLabels.map(function (series) {
    return {
      name: series.label
    };
  }) : null;
  return _react["default"].createElement(_emotionTheming.ThemeProvider, {
    theme: theme
  }, _react["default"].createElement(_ChartContainer["default"], {
    title: title,
    subtitle: subtitle,
    loading: loading,
    error: error
  }, 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, {
    padding: {
      left: 90,
      right: 50,
      bottom: 50,
      top: 50
    },
    domainPadding: {
      x: [40, 40],
      y: [0, 0]
    },
    domain: chartDomain,
    theme: theme
  }, _react["default"].createElement(_victory.VictoryAxis, {
    tickFormat: xNumberFormatter,
    style: {
      grid: {
        stroke: "none"
      }
    },
    title: "X Axis"
  }), _react["default"].createElement(_victory.VictoryAxis, {
    dependentAxis: true,
    tickFormat: yNumberFormatter,
    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: 85,
    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
  })), !dataSeries && _react["default"].createElement(_victory.VictoryBar, {
    alignment: "middle",
    labelComponent: _react["default"].createElement(_victory.VictoryTooltip, {
      x: 325,
      y: 0,
      orientation: "bottom",
      pointerLength: 0,
      cornerRadius: 0,
      theme: theme
    }),
    data: safeData.map(function (d) {
      return {
        dataKey: d[dataKey],
        dataValue: d[dataValue],
        label: "".concat(xLabel, ": ").concat(xNumberFormatter(d[dataKey]), " \u2022 ").concat(yLabel, ": ").concat(yNumberFormatter(d[dataValue]))
      };
    }),
    events: (0, _chartHelpers.chartEvents)(theme),
    x: "dataKey",
    y: "dataValue",
    title: "Bar Chart",
    style: {
      data: {
        width: barWidth
      }
    },
    animate: true
  }), dataSeries && _react["default"].createElement(_victory.VictoryGroup, {
    offset: (barWidth || theme.bar.style.data.width) * 1.2,
    colorScale: theme.group.colorScale
  }, categories.map(function (category) {
    return _react["default"].createElement(_victory.VictoryBar, {
      alignment: "middle",
      labelComponent: _react["default"].createElement(_victory.VictoryTooltip, {
        x: 325,
        y: 0,
        orientation: "bottom",
        pointerLength: 0,
        cornerRadius: 0,
        theme: theme
      }),
      data: safeData.filter(function (d) {
        return d[dataSeries] === category;
      }).map(function (d) {
        return {
          dataKey: d[dataKey],
          dataValue: d[dataValue],
          label: "".concat(xLabel, ": ").concat(xNumberFormatter(d[dataKey]), " \u2022 ").concat(yLabel, ": ").concat(yNumberFormatter(d[dataValue]))
        };
      }),
      events: (0, _chartHelpers.chartEvents)(theme),
      x: "dataKey",
      y: "dataValue",
      title: "Bar Chart",
      style: {
        data: {
          width: barWidth || theme.bar.style.data.width
        }
      },
      animate: true
    });
  }))))));
};

BarChart.propTypes = {
  data: _propTypes["default"].arrayOf(_propTypes["default"].shape({})),
  loading: _propTypes["default"].bool,
  error: _propTypes["default"].string,
  dataKey: _propTypes["default"].string,
  dataValue: _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)
  }),
  title: _propTypes["default"].string,
  subtitle: _propTypes["default"].string,
  xLabel: _propTypes["default"].string,
  yLabel: _propTypes["default"].string,
  xNumberFormatter: _propTypes["default"].func,
  yNumberFormatter: _propTypes["default"].func,
  barWidth: _propTypes["default"].number,
  theme: _propTypes["default"].shape({}),
  legendComponent: _propTypes["default"].node,
  protect: _propTypes["default"].bool
};
BarChart.defaultProps = {
  data: null,
  dataKey: "x",
  dataValue: "y",
  dataSeries: null,
  domain: null,
  title: null,
  subtitle: null,
  xLabel: "X",
  yLabel: "Y",
  xNumberFormatter: _civicFormat["default"].year,
  yNumberFormatter: _civicFormat["default"].numeric,
  barWidth: null,
  theme: _index.VictoryTheme,
  legendComponent: null,
  protect: false
};
var _default = BarChart;
exports["default"] = _default;