"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 _ChartContainer = _interopRequireDefault(require("../ChartContainer"));

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

var _SimpleLegend = _interopRequireDefault(require("../SimpleLegend"));

var _DataChecker = _interopRequireDefault(require("../utils/DataChecker"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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

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["default"].createElement(_ChartContainer["default"], {
    title: title,
    subtitle: subtitle,
    loading: loading,
    error: error,
    aspectRatio: aspectRatio
  }, _react["default"].createElement(_DataChecker["default"], {
    dataAccessors: {
      dataValue: dataValue
    },
    data: safeData
  }, useLegend && _react["default"].createElement(_SimpleLegend["default"], {
    className: "legend",
    legendData: legendLabels,
    colorScale: colorScale
  }), _react["default"].createElement(_victory.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["default"].createElement(_victory.VictoryLabel, {
      style: _objectSpread({}, theme.pieLabel.style)
    })
  }, legendProps, {
    containerComponent: _react["default"].createElement(_victory.VictoryContainer, {
      height: adjustedHeight,
      width: width
    })
  }))));
};

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