'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _extends = require('@babel/runtime/helpers/extends');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.iterator.js');
require('core-js/modules/es.array.map.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.regexp.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
require('core-js/modules/web.dom-collections.iterator.js');
var React = require('react');
var classnames = require('classnames');
var colors = require('@carbon/colors');
var LayoutConstants = require('../../constants/LayoutConstants.js');
var CardPropTypes = require('../../constants/CardPropTypes.js');
var Card = require('../Card/Card.js');
var DataStateRenderer = require('../Card/DataStateRenderer.js');
var Settings = require('../../constants/Settings.js');
var cardUtilityFunctions = require('../../utils/cardUtilityFunctions.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var React__default = /*#__PURE__*/_interopDefault(React);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var _excluded = ["children", "id", "title", "tooltip", "titleTextTooltip", "content", "values", "data", "isLoading", "isResizable", "hasMoreData", "size", "className", "dataState", "testID", "testId", "padding"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var iotPrefix = Settings.settings.iotPrefix;
var STROKE_SIZE = 8;
// r value of the circle in SVG
var radius = 30;
// radius doubled plus stroke
var gaugeSize = radius * 2 + STROKE_SIZE;
// circumference of SVG.
var circum = 2 * Math.PI * radius;
var getStrokeDash = function getStrokeDash() {
  var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  // length of stroke to match percentage
  return value * circum / 100;
};
var getColor = function getColor(gauge, value) {
  var color = gauge.color;
  var grade = '';
  var comparisons = {
    '<': function _(a, b) {
      return a < b;
    },
    '>': function _(a, b) {
      return a > b;
    },
    '=': function _(a, b) {
      return a === b;
    },
    '<=': function _(a, b) {
      return a <= b;
    },
    '>=': function _(a, b) {
      return a >= b;
    }
  };
  if (gauge.thresholds) {
    gauge.thresholds.forEach(function (thresh) {
      if (comparisons[thresh.comparison](value, thresh.value)) {
        color = thresh.color; // eslint-disable-line prefer-destructuring
        grade = thresh.label;
      }
    });
    return {
      grade: grade,
      color: color
    };
  }
  return {
    grade: grade,
    color: color
  };
};
var GaugeCard = function GaugeCard(_ref) {
  var children = _ref.children,
    id = _ref.id,
    title = _ref.title,
    tooltip = _ref.tooltip,
    titleTextTooltip = _ref.titleTextTooltip,
    gauges = _ref.content.gauges,
    values = _ref.values;
    _ref.data;
    var isLoading = _ref.isLoading,
    isResizable = _ref.isResizable;
    _ref.hasMoreData;
    var size = _ref.size,
    className = _ref.className,
    dataState = _ref.dataState,
    testID = _ref.testID,
    testId = _ref.testId,
    padding = _ref.padding,
    others = _objectWithoutProperties__default.default(_ref, _excluded);
  var _useState = React.useState(false),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    loadedState = _useState2[0],
    setLoadedState = _useState2[1];
  React.useEffect(function () {
    var timeout = setTimeout(function () {
      setLoadedState(true);
    }, 1000);
    return function () {
      clearTimeout(timeout);
    };
  }, []);
  var myStyles = dataState ? {} : {
    paddingTop: 0,
    paddingRight: padding ? LayoutConstants.CARD_CONTENT_PADDING : 0,
    paddingBottom: padding ? LayoutConstants.CARD_CONTENT_PADDING : 0,
    paddingLeft: padding ? LayoutConstants.CARD_CONTENT_PADDING : 0,
    rowGap: size === LayoutConstants.CARD_SIZES.SMALL ? 0 : '1rem'
  };
  var resizeHandles = isResizable ? cardUtilityFunctions.getResizeHandles(children) : [];
  return /*#__PURE__*/React__default.default.createElement(Card.default, _extends__default.default({
    id: id,
    className: "".concat(iotPrefix, "--gauge-card"),
    title: title,
    size: size,
    resizeHandles: resizeHandles,
    "aria-label": "".concat(title)
    // TODO: remove deprecated testID in v3.
    ,
    testId: testID || testId
  }, others, {
    tooltip: tooltip || (!titleTextTooltip ? "".concat(title) : undefined),
    titleTextTooltip: titleTextTooltip,
    isLoading: isLoading
  }), /*#__PURE__*/React__default.default.createElement("div", {
    className: classnames__default.default("".concat(iotPrefix, "--gauge-container"), className),
    style: myStyles
  }, dataState && /*#__PURE__*/React__default.default.createElement(DataStateRenderer.default, {
    dataState: dataState,
    size: size,
    id: id
  }), !dataState && gauges.map(function (gauge, i) {
    var _getColor = getColor(gauge, values[gauge.dataSourceId]),
      color = _getColor.color;
    var valueLength = values[gauge.dataSourceId] && values[gauge.dataSourceId].toString().length;
    return /*#__PURE__*/React__default.default.createElement(React.Fragment, {
      key: "".concat(iotPrefix, "-gauge-").concat(i)
    }, /*#__PURE__*/React__default.default.createElement("svg", {
      "aria-labelledby": "gauge-label",
      className: classnames__default.default("".concat(iotPrefix, "--gauge"), _defineProperty__default.default({}, "".concat(iotPrefix, "--gauge__loaded"), loadedState), className),
      percent: "0",
      style: {
        '--gauge-value': values[gauge.dataSourceId] || 0,
        '--gauge-max-value': gauge.maximumValue,
        '--gauge-colors': color,
        '--gauge-bg': gauge.backgroundColor,
        '--stroke-dash': getStrokeDash(values[gauge.dataSourceId]) || 0,
        '--stroke-dash-array': circum,
        '--gauge-size': "".concat(gaugeSize, "px"),
        '--gauge-trend-color': gauge.trend.color
      }
    }, /*#__PURE__*/React__default.default.createElement("circle", {
      className: "".concat(iotPrefix, "--gauge-bg"),
      cx: gaugeSize / 2,
      cy: gaugeSize / 2,
      r: radius
    }), /*#__PURE__*/React__default.default.createElement("circle", {
      className: "".concat(iotPrefix, "--gauge-fg"),
      cx: gaugeSize / 2,
      cy: gaugeSize / 2,
      r: radius
    }), /*#__PURE__*/React__default.default.createElement("text", {
      id: "gauge-label",
      className: classnames__default.default("".concat(iotPrefix, "--gauge-value"), "".concat(iotPrefix, "--gauge-value__centered"), _defineProperty__default.default({}, "".concat(iotPrefix, "--gauge-value-sm"), valueLength === 4), _defineProperty__default.default({}, "".concat(iotPrefix, "--gauge-value-md"), valueLength === 3), _defineProperty__default.default({}, "".concat(iotPrefix, "--gauge-value-lg"), valueLength <= 2)),
      x: gaugeSize / 2,
      y: gaugeSize / 2 + STROKE_SIZE,
      textAnchor: "middle"
    }, /*#__PURE__*/React__default.default.createElement("tspan", null, values[gauge.dataSourceId]), /*#__PURE__*/React__default.default.createElement("tspan", null, gauge.units))), values[gauge.trend.dataSourceId] && /*#__PURE__*/React__default.default.createElement("div", {
      className: classnames__default.default("".concat(iotPrefix, "--gauge-trend"), _defineProperty__default.default(_defineProperty__default.default({}, "".concat(iotPrefix, "--gauge-trend__up"), gauge.trend.trend === 'up'), "".concat(iotPrefix, "--gauge-trend__down"), gauge.trend.trend === 'down')),
      key: "".concat(gauge.trend.dataSourceId, "-").concat(i)
    }, /*#__PURE__*/React__default.default.createElement("p", {
      style: {
        '--gauge-trend-color': gauge.trend.color
      }
    }, values[gauge.trend.dataSourceId])));
  })));
};
GaugeCard.propTypes = _objectSpread(_objectSpread({}, CardPropTypes.CardPropTypes), CardPropTypes.GaugeCardPropTypes);
GaugeCard.defaultProps = {
  isLoading: false,
  description: null,
  content: {
    gauges: [{
      dataSourceId: null,
      units: '%',
      minimumValue: 0,
      maximumValue: 100,
      renderValueFunction: null,
      color: colors.yellow,
      backgroundColor: colors.gray20,
      // @TODO: support half-circle and line gauge
      shape: 'circle',
      trend: null,
      thresholds: null
    }]
  },
  padding: true,
  values: {}
};
GaugeCard.displayName = 'GaugeCard';
GaugeCard.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "GaugeCard",
  "props": {
    "isLoading": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "required": false
    },
    "description": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "required": false
    },
    "content": {
      "defaultValue": {
        "value": "{\n  gauges: [\n    {\n      dataSourceId: null,\n      units: '%',\n      minimumValue: 0,\n      maximumValue: 100,\n      renderValueFunction: null,\n      color: yellow,\n      backgroundColor: gray20,\n      // @TODO: support half-circle and line gauge\n      shape: 'circle',\n      trend: null,\n      thresholds: null,\n    },\n  ],\n}",
        "computed": false
      },
      "required": false
    },
    "padding": {
      "defaultValue": {
        "value": "true",
        "computed": false
      },
      "required": false
    },
    "values": {
      "defaultValue": {
        "value": "{}",
        "computed": false
      },
      "required": false
    }
  },
  "composes": ["../../constants/CardPropTypes"]
};
var GaugeCard$1 = GaugeCard;

exports.default = GaugeCard$1;
exports.getColor = getColor;
exports.getStrokeDash = getStrokeDash;
