function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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 React from "react";
import PropTypes from "prop-types";
import { VictoryPie, VictoryLabel, VictoryContainer } from "victory";
import ChartContainer from "../ChartContainer";
import { VictoryTheme } from "../_Themes/index";
import SimpleLegend from "../SimpleLegend";
import DataChecker from "../utils/DataChecker";

var PieChart = function PieChart(props) {
  var title = props.title,
      subtitle = props.subtitle,
      colors = props.colors,
      data = props.data,
      dataValue = props.dataValue,
      dataLabel = props.dataLabel,
      innerRadius = props.innerRadius,
      loading = props.loading,
      error = props.error,
      halfDoughnut = props.halfDoughnut,
      useLegend = props.useLegend,
      width = props.width,
      height = props.height,
      theme = props.theme;
  var safeData = data && data.length ? data : [{}];
  var startAngle = halfDoughnut ? -90 : 0;
  var endAngle = halfDoughnut ? 90 : 360;
  var adjustedHeight = halfDoughnut ? height / 2 : height;
  var legendLabels = safeData.map(function (value) {
    return {
      name: value[dataLabel]
    };
  });
  var legendProps = {};
  var colorScale = colors.length ? colors : theme.group.colorScale;
  var aspectRatio = halfDoughnut ? 650 / 175 : 650 / 350;

  if (useLegend) {
    legendProps.labels = function () {
      return null;
    };

    legendProps.padding = 25;
  }

  return React.createElement(ChartContainer, {
    title: title,
    subtitle: subtitle,
    loading: loading,
    error: error,
    aspectRatio: aspectRatio
  }, React.createElement(DataChecker, {
    dataAccessors: {
      dataValue: dataValue
    },
    data: safeData
  }, useLegend && React.createElement(SimpleLegend, {
    className: "legend",
    legendData: legendLabels,
    colorScale: colorScale
  }), React.createElement(VictoryPie, _extends({
    width: width,
    height: height,
    data: safeData,
    innerRadius: innerRadius,
    colorScale: colorScale,
    theme: theme,
    animate: {
      duration: 1000
    },
    x: dataLabel,
    y: dataValue,
    startAngle: startAngle,
    endAngle: endAngle,
    labelComponent: React.createElement(VictoryLabel, {
      style: _objectSpread({}, theme.pieLabel.style)
    })
  }, legendProps, {
    containerComponent: React.createElement(VictoryContainer, {
      height: adjustedHeight,
      width: width
    })
  }))));
};

PieChart.defaultProps = {
  useLegend: false
};
PieChart.propTypes = {
  colors: PropTypes.arrayOf(PropTypes.string),
  data: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
  dataLabel: PropTypes.string,
  dataValue: PropTypes.string,
  error: PropTypes.string,
  halfDoughnut: PropTypes.bool,
  height: PropTypes.number,
  innerRadius: PropTypes.number,
  loading: PropTypes.bool,
  subtitle: PropTypes.string,
  title: PropTypes.string,
  useLegend: PropTypes.bool,
  width: PropTypes.number,
  theme: PropTypes.shape({})
};
PieChart.defaultProps = {
  dataLabel: "x",
  dataValue: "y",
  theme: VictoryTheme,
  colors: []
};
export default PieChart;