'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

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

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) : subClass.__proto__ = superClass; }

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

  function Spinner(props) {
    _classCallCheck(this, Spinner);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Spinner).call(this, props));

    _this.state = {
      loading: _this.props.loading
    };

    _this.getStyle = function () {
      return _this.props.color ? { backgroundColor: _this.props.color } : null;
    };

    _this.innerHtml = function () {
      if (_this.props.type === 'spinner-dots') {
        return _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement('div', { className: _style2.default.dot1, style: _this.getStyle() }),
          _react2.default.createElement('div', { className: _style2.default.dot2, style: _this.getStyle() })
        );
      }
      if (_this.props.type === 'spinner-bounce') {
        return _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement('div', { className: _style2.default.bounce1, style: _this.getStyle() }),
          _react2.default.createElement('div', { className: _style2.default.bounce2, style: _this.getStyle() }),
          _react2.default.createElement('div', { className: _style2.default.bounce3, style: _this.getStyle() })
        );
      }
    };

    return _this;
  }

  _createClass(Spinner, [{
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var loadingClass = this.state.loading ? _style2.default['loading'] : null;
      var spinnerWrap = cx(_style2.default['spinner-wrap'], loadingClass, _style2.default[this.props.position], this.props.optClass);
      var spinnerClass = cx(_style2.default[this.props.type], this.props.optClass);

      return _react2.default.createElement(
        'div',
        { className: spinnerWrap },
        _react2.default.createElement(
          'div',
          { className: spinnerClass },
          this.innerHtml()
        )
      );
    }
  }]);

  return Spinner;
}(_react2.default.Component);

Spinner.defaultProps = {
  loading: false
};
Spinner.propTypes = {
  /**
   * Whether the spinner is displayed
   */
  loading: _react2.default.PropTypes.bool,
  /**
   * CSS positioning option for the loader. Default is 'absolute'
   */
  position: _react2.default.PropTypes.oneOf(['fixed']),
  /**
   * The type of loader you want to display.
   */
  type: _react2.default.PropTypes.oneOf(['spinner-dots', 'spinner-bounce']).isRequired,
  /**
   * The hex code of the loader color.
   */
  color: _react2.default.PropTypes.string,
  /**
   * Optional styles to add to the textarea.
   */
  optClass: _react2.default.PropTypes.string
};
exports.default = Spinner;