"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _debounce = _interopRequireDefault(require("lodash/debounce"));

var _lottieAnimation = _interopRequireDefault(require("../lottie-animation"));

var _blank = _interopRequireDefault(require("../blank"));

var _cycle_big = _interopRequireDefault(require("./assets/animation-json/black/cycle_big.json"));

var _cycle_big2 = _interopRequireDefault(require("./assets/animation-json/white/cycle_big.json"));

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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var animationDataMap = {
  black: _cycle_big["default"],
  white: _cycle_big2["default"]
};

var Spin =
/*#__PURE__*/
function (_Component) {
  _inherits(Spin, _Component);

  function Spin(_props) {
    var _this;

    _classCallCheck(this, Spin);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Spin).call(this, _props));

    _defineProperty(_assertThisInitialized(_this), "updateSpinning", function () {
      var spinning = _this.props.spinning;
      var currentSpinning = _this.state.spinning;

      if (currentSpinning !== spinning) {
        _this.setState({
          spinning: spinning
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "debounceUpdateSpinning", function (props) {
      var _ref = props || _this.props,
          delay = _ref.delay;

      if (delay) {
        _this.updateSpinning = (0, _debounce["default"])(_this.originalUpdateSpinning, delay);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onClickBtn", function () {
      _this.setState({
        reloading: true
      });

      var onClickBtn = _this.props.onClickBtn;

      if (onClickBtn) {
        onClickBtn();
      }
    });

    var _spinning = _props.spinning,
        _delay = _props.delay;

    var shouldBeDelayed = _this.shouldDelay(_spinning, _delay);

    _this.originalUpdateSpinning = _this.updateSpinning;

    _this.debounceUpdateSpinning(_props);

    _this.state = {
      reloading: false,
      spinning: _spinning && !shouldBeDelayed
    };
    return _this;
  }

  _createClass(Spin, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.updateSpinning();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.debounceUpdateSpinning();
      this.updateSpinning();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var updateSpinning = this.updateSpinning;

      if (updateSpinning && updateSpinning.cancel) {
        updateSpinning.cancel();
      }
    }
  }, {
    key: "shouldDelay",
    value: function shouldDelay(spinning, delay) {
      return !!spinning && !!delay && !Number.isNaN(Number(delay));
    }
  }, {
    key: "isNestedPattern",
    value: function isNestedPattern() {
      var children = this.props.children;
      return !!(this.props && children);
    }
  }, {
    key: "render",
    value: function render() {
      var prefixCls = 'eh-spin';
      var _this$props = this.props,
          children = _this$props.children,
          className = _this$props.className,
          wrapperClassName = _this$props.wrapperClassName,
          type = _this$props.type,
          showError = _this$props.showError;
      var _this$state = this.state,
          reloading = _this$state.reloading,
          spinning = _this$state.spinning;

      var spinElement = _react["default"].createElement(_lottieAnimation["default"], {
        className: (0, _classnames["default"])("".concat(prefixCls, "-icon"), className),
        animationData: animationDataMap[type]
      });

      if (this.isNestedPattern()) {
        var _classNames, _classNames2;

        var isDarkTheme = type === 'white';
        var blankTheme = isDarkTheme ? 'dark' : 'light';
        var spinningCls = (0, _classnames["default"])("".concat(prefixCls, "-spinning"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-dark"), isDarkTheme), _defineProperty(_classNames, "".concat(prefixCls, "-fade-in"), reloading), _defineProperty(_classNames, "".concat(prefixCls, "-fade-out"), !reloading), _classNames));
        var containerClassName = (0, _classnames["default"])("".concat(prefixCls, "-container"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-fade-in"), !reloading), _defineProperty(_classNames2, "".concat(prefixCls, "-fade-out"), reloading), _classNames2));
        return _react["default"].createElement("div", {
          className: (0, _classnames["default"])("".concat(prefixCls, "-nested-loading"), wrapperClassName)
        }, _react["default"].createElement("div", {
          className: spinningCls,
          style: {
            opacity: spinning ? 1 : 0,
            zIndex: spinning ? 9 : -1
          }
        }, _react["default"].createElement("div", null, spinElement)), _react["default"].createElement("div", {
          className: containerClassName,
          style: {
            opacity: spinning ? 0 : 1
          }
        }, showError ? _react["default"].createElement(_blank["default"], {
          theme: blankTheme,
          imageType: "api_error",
          content: "\u63A5\u53E3\u51FA\u4E86\u70B9\u5C0F\u95EE\u9898\uFF0C\u8BF7\u91CD\u8BD5",
          btnText: "\u91CD\u8BD5",
          clickBtn: this.onClickBtn
        }) : children));
      }

      return spinElement;
    }
  }]);

  return Spin;
}(_react.Component);

_defineProperty(Spin, "propTypes", {
  // 加载中 icon 的 className
  className: _propTypes["default"].string,
  // 包装器的 className
  wrapperClassName: _propTypes["default"].string,
  // 活动指示器的两种样式
  type: _propTypes["default"].oneOf(['black', 'white']),
  // 延迟显示加载效果的时间（防止闪烁）,单位为毫秒
  delay: _propTypes["default"].number,
  // 是否为加载中状态
  spinning: _propTypes["default"].bool,
  // 是否展示异常页面
  showError: _propTypes["default"].bool,
  // 点击重试的回调函数
  onClickBtn: _propTypes["default"].func
});

_defineProperty(Spin, "defaultProps", {
  type: 'black',
  spinning: true,
  showError: false
});

var _default = Spin;
exports["default"] = _default;
module.exports = exports.default;