import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import * as d3 from 'd3';
import classnames from 'classnames';
var COLORS = {
  Green: '#5FB95C',
  Red: '#F95B5C',
  Black: '#2F2F2F',
  MediumGrey: '#C8CCD1',
  LightGrey: '#E2E6E9',
  White: '#FFFFFF',
  Orange: '#FF8800'
};
var VALUES = {
  BASIC_RADIUS_RATE: 2.4,
  ENTITY_WIDTH_RATE: 0.14,
  NO_CALL_OFFSET_RATE: 0.49,
  WARNING_TEXT_OFFSET_RATE: 0.37
};
export var DonutChart =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(DonutChart, _React$Component);

  function DonutChart(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;

    var radius = _this.calculateRadius();

    var entityOuterRadius = radius * (1 + VALUES.ENTITY_WIDTH_RATE / 2);
    var entityInnerRadius = radius * (1 - VALUES.ENTITY_WIDTH_RATE / 2);
    _this.state = {
      entityArc: d3.arc().outerRadius(entityOuterRadius).innerRadius(entityInnerRadius).cornerRadius(15).startAngle(0)
    };
    return _this;
  }

  var _proto = DonutChart.prototype;

  _proto.componentDidMount = function componentDidMount() {
    this.drawProgressBarLine();
  };

  _proto.componentDidUpdate = function componentDidUpdate() {
    this.drawProgressBarLine();
  };

  _proto.drawProgressBarLine = function drawProgressBarLine() {
    var _this$props = this.props,
        animation = _this$props.animation,
        percent = _this$props.percent;
    var entityPath = this.refs.entityPath;
    var endAngle = this.calculateEndAngle(percent);
    var entityColor = this.getEntityColor();
    var entityArc = this.state.entityArc;

    if (animation) {
      d3.select(entityPath).attr('fill', entityColor).datum({
        endAngle: endAngle
      }).attr('d', entityArc);
    } else {
      d3.select(entityPath).attr('fill', entityColor).attr('d', entityArc({
        endAngle: endAngle
      }));
    }
  };

  _proto.calculateRadius = function calculateRadius() {
    return this.props.height / VALUES.BASIC_RADIUS_RATE;
  };

  _proto.calculateEndAngle = function calculateEndAngle(percent) {
    return 2 * Math.PI * percent;
  };

  _proto.getEntityColor = function getEntityColor() {
    var _this$props2 = this.props,
        percent = _this$props2.percent,
        thresholdValue = _this$props2.thresholdValue,
        theme = _this$props2.theme;
    var upperLimit = thresholdValue.upperLimit,
        lowerLimits = thresholdValue.lowerLimits;

    if (_.isEmpty(thresholdValue)) {
      return theme.CHART_GREEN;
    }

    if (percent < lowerLimits) {
      return theme.CHART_GREEN;
    }

    if (percent >= lowerLimits && percent < upperLimit) {
      return theme.CHART_ORANGE;
    }

    if (percent >= upperLimit) {
      return theme.CHART_RED;
    }
  };

  _proto.getPercent = function getPercent() {
    var _this$props3 = this.props,
        percent = _this$props3.percent,
        percentText = _this$props3.percentText,
        percentFontSize = _this$props3.percentFontSize;
    var textConfig = {
      textAnchor: 'middle',
      y: 12,
      fill: COLORS.Black,
      style: {
        fontSize: percentFontSize + 'px'
      }
    };

    if (_.isNull(percentText)) {
      return React.createElement("text", _extends({}, textConfig, {
        "data-test-automation-id": "percent",
        id: "percent"
      }), React.createElement("tspan", null, parseInt(percent * 100)), React.createElement("tspan", null, "%"));
    }

    return null;
  };

  _proto.getNoCall = function getNoCall() {
    var _this$props4 = this.props,
        percentText = _this$props4.percentText,
        height = _this$props4.height;
    var noCallStyle = {
      position: 'relative',
      bottom: height * VALUES.NO_CALL_OFFSET_RATE + 22 + "px",
      color: COLORS.Black,
      fontSize: '20px',
      margin: '0 auto',
      width: '145px',
      wordWrap: 'break-word'
    };

    if (_.isNull(percentText)) {
      return null;
    }

    return React.createElement("div", {
      style: noCallStyle,
      "data-test-automation-id": "noCalls"
    }, percentText);
  };

  _proto.getWarningText = function getWarningText() {
    var _this$props5 = this.props,
        warningText = _this$props5.warningText,
        height = _this$props5.height;
    var warningTextStyle = {
      position: 'relative',
      bottom: height * VALUES.WARNING_TEXT_OFFSET_RATE + 3 + "px",
      fontSize: '13px',
      margin: '0 auto',
      width: '120px',
      wordWrap: 'break-word'
    };
    return React.createElement("div", {
      style: warningTextStyle,
      "data-test-automation-id": "warning"
    }, warningText);
  };

  _proto.render = function render() {
    var _this$props6 = this.props,
        width = _this$props6.width,
        height = _this$props6.height;
    var containerStyle = {
      width: width + 'px',
      height: height + 'px'
    };
    var gTransform = 'translate(' + height / 2 + ',' + height / 2 + ')';
    var className = classnames(this.props.className);
    return React.createElement("div", {
      style: containerStyle,
      className: className
    }, React.createElement("svg", {
      width: width,
      height: height,
      role: "img",
      "aria-labelledby": "percent"
    }, React.createElement("g", {
      transform: gTransform
    }, React.createElement("circle", {
      cx: "0",
      cy: "0",
      r: this.calculateRadius(),
      stroke: COLORS.LightGrey,
      fill: "none",
      strokeWidth: "2"
    }), React.createElement("path", {
      ref: "entityPath"
    }), this.getPercent())), this.getWarningText(), this.getNoCall());
  };

  return DonutChart;
}(React.Component);

_defineProperty(DonutChart, "propTypes", {
  theme: PropTypes.object,
  percent: PropTypes.number,
  width: PropTypes.number,
  height: PropTypes.number,
  alignCenter: PropTypes.bool,
  thresholdValue: PropTypes.object,
  percentText: PropTypes.string,
  warningText: PropTypes.string,
  animation: PropTypes.bool,
  percentFontSize: PropTypes.number,
  testAutomationId: PropTypes.string,
  className: PropTypes.string
});

_defineProperty(DonutChart, "defaultProps", {
  width: 220,
  height: 220,
  alignCenter: true,
  percent: 0,
  thresholdValue: {},
  percentText: null,
  warningText: null,
  theme: {
    CHART_GREEN: COLORS.Green,
    CHART_RED: COLORS.Red,
    CHART_ORANGE: COLORS.Orange
  },
  animation: true,
  percentFontSize: 40,
  testAutomationId: 'donutChart',
  className: null
});