"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AreaGraph = exports.BarGraph = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _recharts = require("recharts");

var _Box = require("../Box");

var _Heading = require("../Heading");

var _Text = require("../Text");

var _styles = _interopRequireDefault(require("./styles"));

var __jsx = _react["default"].createElement;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var CustomTooltip = function CustomTooltip(_ref) {
  var _ref$active = _ref.active,
      active = _ref$active === void 0 ? null : _ref$active,
      _ref$payload = _ref.payload,
      payload = _ref$payload === void 0 ? null : _ref$payload,
      _ref$label = _ref.label,
      label = _ref$label === void 0 ? null : _ref$label,
      items = _ref.items;

  if (active) {
    return __jsx(_Box.Box, {
      bg: "cardBg",
      boxShadow: "menu",
      p: 3
    }, __jsx(_Heading.Heading, {
      kind: "h6"
    }, payload && payload[0].payload.name), items.map(function (item, index) {
      return __jsx(_Text.Text, {
        key: item.key
      }, "".concat(item.title, ": ").concat(payload && payload[index].value));
    }));
  }

  return null;
};

var renderLegendText = function renderLegendText(value, entry, index, items) {
  var _items$index;

  return __jsx(_Text.Text, {
    d: "inline-block"
  }, (_items$index = items[index]) === null || _items$index === void 0 ? void 0 : _items$index.title);
};

var BarGraph = function BarGraph(_ref2) {
  var data = _ref2.data,
      _ref2$items = _ref2.items,
      items = _ref2$items === void 0 ? [] : _ref2$items,
      _ref2$height = _ref2.height,
      height = _ref2$height === void 0 ? 300 : _ref2$height,
      props = (0, _objectWithoutProperties2["default"])(_ref2, ["data", "items", "height"]);

  var _useGraphStyle = (0, _styles["default"])({
    variant: 'bar'
  }),
      rootStyleProps = _useGraphStyle.root,
      graphStyle = _useGraphStyle.graph,
      legendStyleProps = _useGraphStyle.legend,
      axisStyleProps = _useGraphStyle.axis,
      colors = _useGraphStyle.colors;

  return __jsx(_recharts.ResponsiveContainer, {
    height: height
  }, __jsx(_recharts.BarChart, (0, _extends2["default"])({
    data: data
  }, rootStyleProps, props), __jsx(_recharts.CartesianGrid, {
    strokeDasharray: "3 3",
    vertical: false
  }), __jsx(_recharts.XAxis, (0, _extends2["default"])({
    dataKey: "name"
  }, axisStyleProps)), __jsx(_recharts.YAxis, axisStyleProps), __jsx(_recharts.Tooltip, {
    content: __jsx(CustomTooltip, {
      items: items
    })
  }), __jsx(_recharts.Legend, (0, _extends2["default"])({
    formatter: function formatter(value, entry, index) {
      return renderLegendText(value, entry, index, items);
    }
  }, legendStyleProps)), items.map(function (item, index) {
    return __jsx(_recharts.Bar, (0, _extends2["default"])({
      dataKey: item.key,
      key: item.key,
      fill: colors[index % colors.length]
    }, graphStyle));
  })));
};

exports.BarGraph = BarGraph;

var AreaGraph = function AreaGraph(_ref3) {
  var data = _ref3.data,
      _ref3$items = _ref3.items,
      items = _ref3$items === void 0 ? [] : _ref3$items,
      _ref3$height = _ref3.height,
      height = _ref3$height === void 0 ? 300 : _ref3$height,
      props = (0, _objectWithoutProperties2["default"])(_ref3, ["data", "items", "height"]);

  var _useGraphStyle2 = (0, _styles["default"])({
    variant: 'area'
  }),
      rootStyleProps = _useGraphStyle2.root,
      graphStyle = _useGraphStyle2.graph,
      legendStyleProps = _useGraphStyle2.legend,
      axisStyleProps = _useGraphStyle2.axis,
      colors = _useGraphStyle2.colors;

  return __jsx(_recharts.ResponsiveContainer, {
    width: "99%",
    height: height,
    debounce: 1
  }, __jsx(_recharts.AreaChart, (0, _extends2["default"])({
    data: data,
    margin: {
      top: 10,
      right: 10,
      left: 0,
      bottom: 0
    }
  }, rootStyleProps, props), __jsx("defs", null, items.map(function (item, index) {
    var color = colors[index % colors.length];
    return __jsx("linearGradient", {
      id: "colorUv_".concat(item.key),
      key: item.key,
      x1: "0",
      y1: "0",
      x2: "0",
      y2: "1"
    }, __jsx("stop", {
      offset: "5%",
      stopColor: color,
      stopOpacity: 0.1
    }), __jsx("stop", {
      offset: "95%",
      stopColor: color,
      stopOpacity: 0.04
    }));
  })), __jsx(_recharts.XAxis, (0, _extends2["default"])({
    dataKey: "name"
  }, axisStyleProps)), __jsx(_recharts.YAxis, axisStyleProps), __jsx(_recharts.CartesianGrid, {
    strokeDasharray: "3 3",
    vertical: false
  }), __jsx(_recharts.Tooltip, {
    content: __jsx(CustomTooltip, {
      items: items
    })
  }), __jsx(_recharts.Legend, (0, _extends2["default"])({
    formatter: function formatter(value, entry, index) {
      return renderLegendText(value, entry, index, items);
    }
  }, legendStyleProps)), items.map(function (item, index) {
    var color = colors[index % colors.length];
    return __jsx(_recharts.Area, (0, _extends2["default"])({
      dataKey: item.key,
      key: item.key,
      stroke: color,
      fill: "url(#colorUv_".concat(item.key, ")")
    }, graphStyle, {
      dot: _objectSpread(_objectSpread({}, graphStyle.dot), {}, {
        stroke: color
      })
    }));
  })));
};

exports.AreaGraph = AreaGraph;
//# sourceMappingURL=Graph.js.map