"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDefaultDomain = getDefaultDomain;
exports.getDefaultDataSeriesLabels = getDefaultDataSeriesLabels;
exports.getDefaultFillStyle = getDefaultFillStyle;
exports.getDefaultLineStyle = getDefaultLineStyle;
exports.getDefaultAreaStyle = getDefaultAreaStyle;
exports.getDefaultStackedDomain = getDefaultStackedDomain;
exports.transformDatato100 = exports.categoricalColors = exports.chartEvents = void 0;

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

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 chartEvents = function chartEvents() {
  var theme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _index.VictoryTheme;
  var tooltipColor = theme.tooltip.style.customHoverColor;
  return [{
    target: "data",
    eventHandlers: {
      onMouseOver: function onMouseOver() {
        return [{
          target: "data",
          mutation: function mutation(props) {
            return {
              style: Object.assign(props.style, {
                fill: tooltipColor
              })
            };
          }
        }, {
          target: "labels",
          mutation: function mutation() {
            return {
              active: true
            };
          }
        }];
      },
      onMouseOut: function onMouseOut() {
        return [{
          target: "data",
          mutation: function mutation() {}
        }, {
          target: "labels",
          mutation: function mutation() {
            return {
              active: false
            };
          }
        }];
      }
    }
  }];
};

exports.chartEvents = chartEvents;

function getDefaultDomain(data, dataKey, dataLabel) {
  var xValues = data.map(function (value) {
    return value[dataKey];
  });
  var yValues = data.map(function (value) {
    return value[dataLabel];
  });
  return {
    x: [Math.min.apply(Math, _toConsumableArray(xValues)) < 0 ? Math.min.apply(Math, _toConsumableArray(xValues)) : Math.min.apply(Math, _toConsumableArray(xValues)), Math.max.apply(Math, _toConsumableArray(xValues))],
    y: [Math.min.apply(Math, _toConsumableArray(yValues)) < 0 ? Math.min.apply(Math, _toConsumableArray(yValues)) : 0, Math.max.apply(Math, _toConsumableArray(yValues))]
  };
}

function getDefaultStackedDomain(data, dataKey, dataLabel) {
  function groupBy(objectArray, property) {
    return objectArray.reduce(function (acc, obj) {
      var key = obj[property];

      if (!acc[key]) {
        acc[key] = [];
      }

      acc[key].push(obj);
      return acc;
    }, []);
  }

  var xValues = data.map(function (value) {
    return value[dataKey];
  });
  var yValues = Object.values(groupBy(data, dataKey).map(function (value) {
    return value.reduce(function (acc, obj) {
      return acc + obj[dataLabel];
    }, 0);
  }));
  return {
    x: [Math.min.apply(Math, _toConsumableArray(xValues)) < 0 ? Math.min.apply(Math, _toConsumableArray(xValues)) : Math.min.apply(Math, _toConsumableArray(xValues)), Math.max.apply(Math, _toConsumableArray(xValues))],
    y: [Math.min.apply(Math, _toConsumableArray(yValues)) < 0 ? Math.min.apply(Math, _toConsumableArray(yValues)) : 0, Math.max.apply(Math, _toConsumableArray(yValues))]
  };
}

function getDefaultDataSeriesLabels(data, series) {
  var categories = data.map(function (value) {
    return value[series];
  });

  var uniqueCategories = _toConsumableArray(new Set(categories));

  return uniqueCategories.map(function (cat) {
    return {
      category: cat,
      label: cat
    };
  });
}

function getDefaultFillStyle(dataSeriesLabel) {
  var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _index.VictoryTheme;
  var dataSeriesCategories = dataSeriesLabel && dataSeriesLabel.length ? dataSeriesLabel.map(function (series) {
    return series.category;
  }) : null;
  return {
    data: {
      fill: function fill(d) {
        if (!dataSeriesCategories) return theme.group.colorScale[0];
        var idx = dataSeriesCategories.findIndex(function (series) {
          return series === d.series;
        });
        return theme.group.colorScale[idx];
      }
    }
  };
}

function getDefaultLineStyle(idx) {
  var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _index.VictoryTheme;
  return {
    data: {
      stroke: theme.group.colorScale[idx]
    }
  };
}

function getDefaultAreaStyle(idx) {
  var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _index.VictoryTheme;
  return {
    data: {
      fill: theme.group.colorScale[idx]
    }
  };
}

var categoricalColors = function categoricalColors(dataLength) {
  var theme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _index.VictoryTheme;
  var colorScheme = [];

  for (var i = 0; i < dataLength; i += 1) {
    colorScheme.push(theme.group.colorScale[i]);
  }

  return colorScheme;
};

exports.categoricalColors = categoricalColors;

var transformDatato100 = function transformDatato100(dataset, value, label) {
  var totals = dataset[0].map(function (currentData, i) {
    return dataset.reduce(function (memo, curr) {
      return memo + curr[i][label];
    }, 0);
  });
  var newData = dataset.map(function (indvData) {
    return indvData.map(function (datum, i) {
      var _objectSpread2;

      var newObj = _objectSpread({}, datum, (_objectSpread2 = {}, _defineProperty(_objectSpread2, "".concat(value), datum[value]), _defineProperty(_objectSpread2, "".concat(label), datum[label] / totals[i]), _objectSpread2));

      return newObj;
    });
  });
  return newData;
};

exports.transformDatato100 = transformDatato100;