'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 _domLib = require('dom-lib');

var _compose = require('recompose/compose');

var _compose2 = _interopRequireDefault(_compose);

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

var _BrowserDetection = require('./utils/BrowserDetection');

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 Loader = function (_React$Component) {
  _inherits(Loader, _React$Component);

  function Loader() {
    var _temp, _this, _ret;

    _classCallCheck(this, Loader);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.loader = null, _this.loaderRef = function (ref) {
      _this.loader = ref;
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  Loader.prototype.componentDidMount = function componentDidMount() {
    var _props = this.props,
        center = _props.center,
        backdrop = _props.backdrop;


    if (center || backdrop) {
      var width = (0, _domLib.getWidth)(this.loader);
      (0, _domLib.addStyle)(this.loader, {
        display: _BrowserDetection.isIE11 || _BrowserDetection.isEdge ? 'block' : 'table',
        width: width + 'px'
      });
    }
  };

  Loader.prototype.addPrefix = function addPrefix(name) {
    return (0, _utils.prefix)(this.props.classPrefix)(name);
  };

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

    var _props2 = this.props,
        classPrefix = _props2.classPrefix,
        className = _props2.className,
        inverse = _props2.inverse,
        backdrop = _props2.backdrop,
        speed = _props2.speed,
        center = _props2.center,
        vertical = _props2.vertical,
        content = _props2.content,
        props = _objectWithoutProperties(_props2, ['classPrefix', 'className', 'inverse', 'backdrop', 'speed', 'center', 'vertical', 'content']);

    var hasContent = !!content;
    var addPrefix = (0, _utils.prefix)(classPrefix);
    var classes = (0, _classnames2.default)(addPrefix('wrapper'), addPrefix('speed-' + speed), className, (_classNames = {}, _classNames[addPrefix('backdrop-wrapper')] = backdrop, _classNames[addPrefix('vertical')] = vertical, _classNames[addPrefix('inverse')] = inverse, _classNames[addPrefix('center')] = center, _classNames[addPrefix('has-content')] = hasContent, _classNames));

    return React.createElement(
      'div',
      _extends({}, props, { className: classes }),
      backdrop && React.createElement('div', { className: addPrefix('backdrop') }),
      React.createElement(
        'div',
        { className: classPrefix, ref: this.loaderRef },
        React.createElement('span', { className: addPrefix('spin') }),
        hasContent && React.createElement(
          'span',
          { className: addPrefix('content') },
          content
        )
      )
    );
  };

  return Loader;
}(React.Component);

Loader.defaultProps = {
  speed: 'normal'
};
Loader.handledProps = ['backdrop', 'center', 'className', 'classPrefix', 'content', 'inverse', 'speed', 'vertical'];
Loader.propTypes = {
  className: _propTypes2.default.string,
  classPrefix: _propTypes2.default.string.isRequired,
  center: _propTypes2.default.bool,
  backdrop: _propTypes2.default.bool,
  inverse: _propTypes2.default.bool,
  vertical: _propTypes2.default.bool,
  content: _propTypes2.default.node,
  speed: _propTypes2.default.oneOf(['normal', 'fast', 'slow']).isRequired
};
exports.default = (0, _compose2.default)((0, _utils.withStyleProps)({
  hasSize: true
}), (0, _utils.defaultProps)({
  classPrefix: 'loader'
}))(Loader);