text: import _orderBy from "lodash/orderBy";
  import _some from "lodash/some";
  import _uniq from "lodash/uniq";
  import _keys from "lodash/keys";
  import _isFunction from "lodash/isFunction";
  import _flatten from "lodash/flatten";
  import _defaults from "lodash/defaults";
  import _assign from "lodash/assign";
  
  function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  
  import React from "react";
  import Axis from "./axis";
  import { Style, Transitions, Collection, Data, Domain, Events } from "victory-core";
  
  export default {
    getData: function (props, childComponents) {
      if (props.data) {
        return Data.getData(props);
      }
      childComponents = childComponents || React.Children.toArray(props.children);
      return this.getDataFromChildren(childComponents);
    },
    getDefaultDomainPadding: function (props, axis, childComponents) {
      var horizontalChildren = childComponents.some(function (component) {
        return component.props && component.props.horizontal;
      });
      var horizontal = props && props.horizontal || horizontalChildren;
      var groupComponent = childComponents.filter(function (child) {
        return child.type && child.type.role && child.type.role === "group";
      });
  
      if (groupComponent.length < 1) {
        return undefined;
      }
      var _groupComponent$0$pro = groupComponent[0].props,
          offset = _groupComponent$0$pro.offset,
          children = _groupComponent$0$pro.children;
  
      var defaultDomainPadding = horizontal ? { y: offset * children.length / 2 } : { x: offset * children.length / 2 };
      return defaultDomainPadding[axis];
    },
    getDomain: function (props, axis, childComponents) {
      childComponents = childComponents || React.Children.toArray(props.children);
      var propsDomain = Domain.getDomainFromProps(props, axis);
      var domainPadding = props.polar ? 0 : this.getDefaultDomainPadding(props, axis, childComponents);
      var domain = void 0;
      if (propsDomain) {
        domain = propsDomain;
      } else {
        var dataset = (props.data || props.y) && Data.getData(props);
        var dataDomain = dataset ? Domain.getDomainFromData(props, axis, dataset) : [];
        var childDomain = this.getDomainFromChildren(props, axis, childComponents);
        var min = Collection.getMinValue([].concat(_toConsumableArray(dataDomain), _toConsumableArray(childDomain)));
        var max = Collection.getMaxValue([].concat(_toConsumableArray(dataDomain), _toConsumableArray(childDomain)));
        domain = [min, max];
      }
      var paddedDomain = Domain.padDomain(domain, _assign({ domainPadding: domainPadding }, props), axis);
      return Domain.cleanDomain(paddedDomain, props, axis);
    },
    setAnimationState: function (props, nextProps) {
      if (!props.animate) {
        return;
      }
      if (props.animate.parentState) {
        var nodesWillExit = props.animate.parentState.nodesWillExit;
        var oldProps = nodesWillExit ? props : null;
        this.setState(_defaults({ oldProps: oldProps, nextProps: nextProps }, props.animate.parentState));
      } else {
        var oldChildren = React.Children.toArray(props.children);
        var nextChildren = React.Children.toArray(nextProps.children);
        var isContinuous = function (child) {
          var check = function (c) {
            return c.type && c.type.continuous;
          };
          return Array.isArray(child) ? _some(child, check) : check(child);
        };
  
        var continuous = !props.polar && _some(oldChildren, function (child) {
          return isContinuous(child) || child.props.children && isContinuous(child.props.children);
        });
  
        var _Transitions$getIniti = Transitions.getInitialTransitionState(oldChildren, nextChildren),
            _nodesWillExit = _Transitions$getIniti.nodesWillExit,
            nodesWillEnter = _Transitions$getIniti.nodesWillEnter,
            childrenTransitions = _Transitions$getIniti.childrenTransitions,
            nodesShouldEnter = _Transitions$getIniti.nodesShouldEnter;
  
        this.setState({
          nodesWillExit: _nodesWillExit,
          nodesWillEnter: nodesWillEnter,
          nodesShouldEnter: nodesShouldEnter,
          childrenTransitions: Collection.isArrayOfArrays(childrenTransitions) ? childrenTransitions[0] : childrenTransitions,
          oldProps: _nodesWillExit ? props : null,
          nextProps: nextProps,
          continuous: continuous
        });
      }
    },
    getAllEvents: function (props) {
      var components = ["groupComponent", "containerComponent", "labelComponent"];
      this.componentEvents = Events.getComponentEvents(props, components);
      if (Array.isArray(this.componentEvents)) {
        var _componentEvents;
  
        return Array.isArray(props.events) ? (_componentEvents = this.componentEvents).concat.apply(_componentEvents, _toConsumableArray(props.events)) : this.componentEvents;
      }
      return props.events;
    },
    getAnimationProps: function (props, child, index) {
      var _this = this;
  
      if (!props.animate) {
        return child.props.animate;
      }
      var getFilteredState = function () {
        var childrenTransitions = _this.state && _this.state.childrenTransitions;
        childrenTransitions = Collection.isArrayOfArrays(childrenTransitions) ? childrenTransitions[index] : childrenTransitions;
        return _defaults({ childrenTransitions: childrenTransitions }, _this.state);
      };
  
      var getTransitions = props.animate && props.animate.getTransitions;
      var state = getFilteredState();
      var parentState = props.animate && props.animate.parentState || state;
      if (!getTransitions) {
        var getTransitionProps = Transitions.getTransitionPropsFactory(props, state, function (newState) {
          return _this.setState(newState);
        });
        getTransitions = function (childComponent) {
          return getTransitionProps(childComponent, index);
        };
      }
      return _defaults({ getTransitions: getTransitions, parentState: parentState }, props.animate, child.props.animate);
    },
    getDomainFromChildren: function (props, axis, childComponents) {
      // eslint-disable-line max-statements, complexity, max-len
      var childDomains = [];
      var childDomainsLength = 0;
  
      var children = childComponents ? childComponents.slice(0) : React.Children.toArray(props.children);
      var childrenLength = children.length;
  
      var horizontalChildren = childComponents.some(function (component) {
        return component.props && component.props.horizontal;
      });
      var horizontal = props && props.horizontal || horizontalChildren.length > 0;
      var currentAxis = Axis.getCurrentAxis(axis, horizontal);
  
      var parentData = props.data ? Data.getData(props, axis) : undefined;
      var polar = props.polar,
          startAngle = props.startAngle,
          endAngle = props.endAngle;
  
      var parentProps = parentData ? { data: parentData, polar: polar, startAngle: startAngle, endAngle: endAngle } : { polar: polar, startAngle: startAngle, endAngle: endAngle };
  
      while (childrenLength > 0) {
        var child = children[--childrenLength];
  
        if (child.type && _isFunction(child.type.getDomain)) {
          var sharedProps = _assign({}, child.props, parentProps);
          var childDomain = child.props && child.type.getDomain(sharedProps, currentAxis);
          if (childDomain) {
            var childDomainLength = childDomain.length;
            for (var index = 0; index < childDomainLength; index++) {
              childDomains[childDomainsLength++] = childDomain[index];
            }
          }
        } else if (child.props && child.props.children) {
          var newChildren = React.Children.toArray(child.props.children);
          var newChildrenLength = newChildren.length;
          for (var _index = 0; _index < newChildrenLength; _index++) {
            children[childrenLength++] = newChildren[_index];
          }
        }
      }
      var min = Collection.getMinValue(childDomains);
      var max = Collection.getMaxValue(childDomains);
      return childDomains.length === 0 ? [0, 1] : [min, max];
    },
    getDataFromChildren: function (props, childComponents) {
      // eslint-disable-line max-statements
      var getData = function (childProps) {
        var data = Data.getData(childProps);
        return Array.isArray(data) && data.length > 0 ? data : undefined;
      };
  
      // Reverse the child array to maintain correct order when looping over
      // children starting from the end of the array.
      var children = childComponents ? childComponents.slice(0).reverse() : React.Children.toArray(props.children).reverse();
  
      var childrenLength = children.length;
  
      var dataArr = [];
      var dataArrLength = 0;
      while (childrenLength > 0) {
        var child = children[--childrenLength];
        if (child.type && child.type.role === "axis") {
          dataArrLength = dataArrLength;
        } else if (child.type && child.type.role !== "axis" && _isFunction(child.type.getData)) {
          dataArr[dataArrLength++] = child.type.getData(child.props);
        } else if (child.props && child.props.children) {
          var newChildren = React.Children.toArray(child.props.children);
          var newChildrenLength = newChildren.length;
          for (var index = 0; index < newChildrenLength; index++) {
            children[childrenLength++] = newChildren[index];
          }
        } else {
          dataArr[dataArrLength++] = getData(child.props);
        }
      }
  
      return dataArr;
    },
  
  
    // Assumes data in `datasets` is sorted by `Data.getData`.
    fillInMissingData: function (props, datasets) {
      var fillInMissingData = props.fillInMissingData;
  
      var xMap = datasets.reduce(function (prev, dataset) {
        dataset.forEach(function (datum) {
          prev[datum._x instanceof Date ? datum._x.getTime() : datum._x] = true;
        });
        return prev;
      }, {});
      var xKeys = _keys(xMap).map(function (k) {
        return +k;
      });
      var xArr = _orderBy(xKeys);
  
      return datasets.map(function (dataset) {
        var indexOffset = 0;
        var isDate = dataset[0] && dataset[0]._x instanceof Date;
        var filledInData = xArr.map(function (x, index) {
          x = +x;
          var datum = dataset[index - indexOffset];
  
          if (datum) {
            var x1 = isDate ? datum._x.getTime() : datum._x;
            if (x1 === x) {
              return datum;
            } else {
              indexOffset++;
              var y = fillInMissingData ? 0 : null;
              x = isDate ? new Date(x) : x;
              return { x: x, y: y, _x: x, _y: y };
            }
          } else {
            var _y = fillInMissingData ? 0 : null;
            x = isDate ? new Date(x) : x;
            return { x: x, y: _y, _x: x, _y: _y };
          }
        });
  
        return filledInData;
      });
    },
    getStackedDomain: function (props, axis) {
      var propsDomain = Domain.getDomainFromProps(props, axis);
      if (propsDomain) {
        return propsDomain;
      }
      var horizontal = props.horizontal;
  
      var ensureZero = function (domain) {
        var isDependent = axis === "y" && !horizontal || axis === "x" && horizontal;
        return isDependent ? [Collection.getMinValue(domain, 0), Collection.getMaxValue(domain, 0)] : domain;
      };
      var datasets = this.getDataFromChildren(props);
      return ensureZero(Domain.getDomainFromGroupedData(props, axis, datasets));
    },
    getColor: function (calculatedProps, child, index) {
      // check for styles first
      var style = calculatedProps.style;
      var colorScale = calculatedProps.colorScale,
          color = calculatedProps.color;
  
      if (style && style.data && style.data.fill) {
        return style.data.fill;
      }
      colorScale = child.props && child.props.colorScale ? child.props.colorScale : colorScale;
      color = child.props && child.props.color ? child.props.color : color;
      if (!colorScale && !color) {
        return undefined;
      }
      var colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale);
      return color || colors[index % colors.length];
    },
    getWidth: function (props) {
      var datasets = props.datasets,
          scale = props.scale,
          horizontal = props.horizontal;
  
      var range = horizontal ? scale.y.range() : scale.x.range();
      var extent = Math.abs(range[1] - range[0]);
      var bars = datasets.length * (datasets[0].length || 1) + 2;
      var barRatio = 0.5;
      return { width: Math.round(barRatio * extent / bars) };
    },
    getChildStyle: function (child, index, calculatedProps) {
      var style = calculatedProps.style,
          role = calculatedProps.role;
  
      var childStyle = child.props.style || {};
      if (Array.isArray(childStyle)) {
        return childStyle;
      }
      var childRole = child.type && child.type.role;
      var defaultFill = childRole === "stack" ? undefined : this.getColor(calculatedProps, child, index);
      var defaultColor = childRole === "line" ? { fill: "none", stroke: defaultFill } : { fill: defaultFill };
      var dataWidth = role === "stack" ? {} : this.getWidth(calculatedProps);
      var dataStyle = _defaults({}, childStyle.data, _assign({}, dataWidth, style.data, defaultColor));
      var labelsStyle = _defaults({}, childStyle.labels, style.labels);
      return {
        parent: style.parent,
        data: dataStyle,
        labels: labelsStyle
      };
    },
    getStringsFromCategories: function (childComponents, axis) {
      // eslint-disable-line max-statements
      var strings = [];
      var stringsLength = 0;
  
      var children = childComponents.slice(0).reverse();
      var childrenLength = children.length;
  
      while (childrenLength > 0) {
        var child = children[--childrenLength];
  
        if (child.props && child.props.categories) {
          var newStrings = Data.getStringsFromCategories(child.props, axis);
          var newStringsLength = newStrings.length;
          for (var index = 0; index < newStringsLength; index++) {
            strings[stringsLength++] = newStrings[index];
          }
        } else if (child.props && child.props.children) {
          var newChildren = React.Children.toArray(child.props.children);
          var newChildrenLength = newChildren.length;
          for (var _index2 = 0; _index2 < newChildrenLength; _index2++) {
            children[childrenLength++] = newChildren[_index2];
          }
        }
      }
  
      return strings;
    },
    getStringsFromData: function (childComponents, axis) {
      // eslint-disable-line max-statements
      var strings = [];
      var stringsLength = 0;
  
      var children = childComponents.slice(0).reverse();
      var childrenLength = children.length;
  
      while (childrenLength > 0) {
        var child = children[--childrenLength];
  
        if (child.props && child.props.data) {
          var newStrings = Data.getStringsFromData(child.props, axis);
          var newStringsLength = newStrings.length;
          for (var index = 0; index < newStringsLength; index++) {
            strings[stringsLength++] = newStrings[index];
          }
        } else if (child.type && _isFunction(child.type.getData)) {
          var data = _flatten(child.type.getData(child.props));
          var attr = axis === "x" ? "xName" : "yName";
          for (var _index3 = 0; _index3 < data.length; _index3++) {
            var datum = data[_index3];
            if (datum[attr]) {
              strings[stringsLength++] = datum[attr];
            }
          }
        } else if (child.props && child.props.children) {
          var newChildren = React.Children.toArray(child.props.children);
          var newChildrenLength = newChildren.length;
          for (var _index4 = 0; _index4 < newChildrenLength; _index4++) {
            children[childrenLength++] = newChildren[_index4];
          }
        }
      }
  
      return strings;
    },
    getStringsFromChildren: function (props, axis, childComponents) {
      childComponents = childComponents || React.Children.toArray(props.children);
      var axisComponent = Axis.getAxisComponent(childComponents, axis);
      var axisStrings = axisComponent ? Data.getStringsFromAxes(axisComponent.props, axis) : [];
      var categoryStrings = this.getStringsFromCategories(childComponents, axis);
      var dataStrings = this.getStringsFromData(childComponents, axis);
      return _uniq(_flatten([].concat(_toConsumableArray(categoryStrings), _toConsumableArray(dataStrings), _toConsumableArray(axisStrings))));
    },
    getCategories: function (props, axis) {
      var categories = Data.getCategories(props, axis) || this.getStringsFromChildren(props, axis);
      return categories.length > 0 ? categories : undefined;
    },
    getY0: function (datum, index, calculatedProps) {
      if (datum.y0) {
        return datum.y0;
      }
      var datasets = calculatedProps.datasets;
  
      var y = datum._y;
      var previousDatasets = datasets.slice(0, index);
      var previousPoints = previousDatasets.reduce(function (prev, dataset) {
        return prev.concat(dataset.filter(function (previousDatum) {
          return datum._x instanceof Date ? previousDatum._x.getTime() === datum._x.getTime() : previousDatum._x === datum._x;
        }).map(function (previousDatum) {
          return previousDatum._y || 0;
        }));
      }, []);
      var y0 = previousPoints.length && previousPoints.reduce(function (memo, value) {
        var sameSign = y < 0 && value < 0 || y >= 0 && value >= 0;
        return sameSign ? +value + memo : memo;
      }, 0);
      return previousPoints.some(function (point) {
        return point instanceof Date;
      }) ? new Date(y0) : y0;
    }
  };