"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

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

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

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

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

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

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var charts = _interopRequireWildcard(require("react-chartjs-2"));

var _Title = _interopRequireDefault(require("../Title"));

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

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

// Style

/* istanbul ignore next */
if (typeof window !== 'undefined') {}
/**
 * Display some `data` as a numeric metric or a chart.
 */


var Metric = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Metric, _React$Component);

  var _super = _createSuper(Metric);

  function Metric() {
    var _this;

    (0, _classCallCheck2["default"])(this, Metric);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "chart", function () {
      var chartType = _this.props.chart.charAt(0).toUpperCase() + _this.props.chart.substring(1);

      return new charts[chartType]();
    });
    return _this;
  }

  (0, _createClass2["default"])(Metric, [{
    key: "render",
    value: function render() {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: ['Metric', this.props.className, this.props.chart ? 'Metric_chart' : ''].join(' '),
        style: this.props.style
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "Metric-value"
      }, this.props.chart ? /*#__PURE__*/_react["default"].createElement(this.chart, {
        data: this.props.data,
        options: _objectSpread({
          maintainAspectRatio: false
        }, this.props.data.options)
      }) : /*#__PURE__*/_react["default"].createElement(_Title["default"], {
        className: "Metric-label",
        level: 1
      }, this.props.data)), this.props.children || /*#__PURE__*/_react["default"].createElement(_Title["default"], {
        level: 5
      }, /*#__PURE__*/_react["default"].createElement("b", null, this.props.label)));
    }
  }]);
  return Metric;
}(_react["default"].Component);

exports["default"] = Metric;
(0, _defineProperty2["default"])(Metric, "propTypes", {
  chart: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].bool]),
  data: _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].number, _propTypes["default"].string]).isRequired,
  label: _propTypes["default"].string
});
(0, _defineProperty2["default"])(Metric, "defaultProps", {
  label: ''
});