'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _utils = require('./utils');

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }

var ProgressCircle = function (_React$Component) {
  _inherits(ProgressCircle, _React$Component);

  function ProgressCircle() {
    _classCallCheck(this, ProgressCircle);

    return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
  }

  ProgressCircle.prototype.getPathStyles = function getPathStyles() {
    var _props = this.props,
        percent = _props.percent,
        strokeWidth = _props.strokeWidth,
        gapDegree = _props.gapDegree,
        gapPosition = _props.gapPosition,
        trailColor = _props.trailColor,
        strokeColor = _props.strokeColor;


    var radius = 50 - strokeWidth / 2;

    var beginPositionX = 0;
    var beginPositionY = -radius;
    var endPositionX = 0;
    var endPositionY = -2 * radius;

    switch (gapPosition) {
      case 'left':
        beginPositionX = -radius;
        beginPositionY = 0;
        endPositionX = 2 * radius;
        endPositionY = 0;
        break;
      case 'right':
        beginPositionX = radius;
        beginPositionY = 0;
        endPositionX = -2 * radius;
        endPositionY = 0;
        break;
      case 'bottom':
        beginPositionY = radius;
        endPositionY = 2 * radius;
        break;
      default:
    }

    var pathString = 'M 50,50 m ' + beginPositionX + ',' + beginPositionY + '\n     a ' + radius + ',' + radius + ' 0 1 1 ' + endPositionX + ',' + -endPositionY + '\n     a ' + radius + ',' + radius + ' 0 1 1 ' + -endPositionX + ',' + endPositionY;

    var len = Math.PI * 2 * radius;
    var trailPathStyle = {
      stroke: trailColor,
      strokeDasharray: len - gapDegree + 'px ' + len + 'px',
      strokeDashoffset: '-' + gapDegree / 2 + 'px'
    };

    var strokePathStyle = {
      stroke: strokeColor,
      strokeDasharray: percent / 100 * (len - gapDegree) + 'px ' + len + 'px',
      strokeDashoffset: '-' + gapDegree / 2 + 'px'
    };

    return {
      pathString: pathString,
      trailPathStyle: trailPathStyle,
      strokePathStyle: strokePathStyle
    };
  };

  ProgressCircle.prototype.render = function render() {
    var _classNames;

    var _props2 = this.props,
        strokeWidth = _props2.strokeWidth,
        trailWidth = _props2.trailWidth,
        strokeColor = _props2.strokeColor,
        percent = _props2.percent,
        trailColor = _props2.trailColor,
        strokeLinecap = _props2.strokeLinecap,
        className = _props2.className,
        showInfo = _props2.showInfo,
        status = _props2.status,
        gapDegree = _props2.gapDegree,
        gapPosition = _props2.gapPosition,
        classPrefix = _props2.classPrefix,
        rest = _objectWithoutProperties(_props2, ['strokeWidth', 'trailWidth', 'strokeColor', 'percent', 'trailColor', 'strokeLinecap', 'className', 'showInfo', 'status', 'gapDegree', 'gapPosition', 'classPrefix']);

    var _getPathStyles = this.getPathStyles(),
        pathString = _getPathStyles.pathString,
        trailPathStyle = _getPathStyles.trailPathStyle,
        strokePathStyle = _getPathStyles.strokePathStyle;

    var addPrefix = (0, _utils.prefix)(classPrefix);
    var classes = (0, _classnames2.default)(classPrefix, addPrefix('circle'), (_classNames = {}, _classNames[addPrefix('circle-' + (status || ''))] = !!status, _classNames), className);

    var showIcon = status && status !== 'active';
    var info = showIcon ? React.createElement('span', { className: addPrefix('icon-' + (status || '')) }) : React.createElement(
      'span',
      { key: 1 },
      percent,
      '%'
    );

    return React.createElement(
      'div',
      { className: classes },
      showInfo ? React.createElement(
        'span',
        { className: addPrefix('circle-info') },
        info
      ) : null,
      React.createElement(
        'svg',
        _extends({ className: addPrefix('svg'), viewBox: '0 0 100 100' }, rest),
        React.createElement('path', {
          className: addPrefix('trail'),
          d: pathString,
          strokeWidth: trailWidth || strokeWidth,
          fillOpacity: '0',
          style: trailPathStyle
        }),
        React.createElement('path', {
          d: pathString,
          strokeLinecap: strokeLinecap,
          className: addPrefix('stroke'),
          strokeWidth: this.props.percent === 0 ? 0 : strokeWidth,
          fillOpacity: '0',
          style: strokePathStyle
        })
      )
    );
  };

  return ProgressCircle;
}(React.Component);

ProgressCircle.defaultProps = {
  percent: 0,
  strokeWidth: 6,
  trailWidth: 6,
  gapDegree: 0,
  showInfo: true,
  strokeLinecap: 'round',
  gapPosition: 'top'
};
ProgressCircle.handledProps = ['className', 'classPrefix', 'gapDegree', 'gapPosition', 'percent', 'showInfo', 'status', 'strokeColor', 'strokeLinecap', 'strokeWidth', 'trailColor', 'trailWidth'];
ProgressCircle.propTypes = {
  className: _propTypes2.default.string,
  strokeColor: _propTypes2.default.string,
  strokeLinecap: _propTypes2.default.oneOf(['butt', 'round', 'square']).isRequired,
  trailColor: _propTypes2.default.string,
  percent: _propTypes2.default.number.isRequired,
  strokeWidth: _propTypes2.default.number.isRequired,
  trailWidth: _propTypes2.default.number.isRequired,
  gapDegree: _propTypes2.default.number.isRequired,
  gapPosition: _propTypes2.default.oneOf(['top', 'bottom', 'left', 'right']).isRequired,
  showInfo: _propTypes2.default.bool,
  status: _propTypes2.default.oneOf(['success', 'fail', 'active']),
  classPrefix: _propTypes2.default.string
};
exports.default = (0, _utils.defaultProps)({
  classPrefix: 'progress'
})(ProgressCircle);