"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

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

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

var _omit = _interopRequireDefault(require("lodash/omit"));

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

var _shared = require("choerodon-ui/shared");

var _animate = _interopRequireDefault(require("../animate"));

var _progress = _interopRequireDefault(require("../progress/progress"));

var _enum = require("../_util/enum");

var _enum2 = require("../progress/enum");

var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));

var _excluded = ["className", "size", "prefixCls", "tip", "wrapperClassName", "children", "style"];

function renderIndicator(props, prefixCls) {
  var indicator = props.indicator,
      size = props.size;
  var dotClassName = "".concat(prefixCls, "-dot");

  if ( /*#__PURE__*/(0, _react.isValidElement)(indicator)) {
    return /*#__PURE__*/(0, _react.cloneElement)(indicator, {
      className: (0, _classnames["default"])(indicator.props.className, dotClassName)
    });
  }

  var defaultIndicator = _shared.global.DEFAULT_SPIN_INDICATOR;

  if ( /*#__PURE__*/(0, _react.isValidElement)(defaultIndicator)) {
    return /*#__PURE__*/(0, _react.cloneElement)(defaultIndicator, {
      className: (0, _classnames["default"])(defaultIndicator.props.className, dotClassName)
    });
  }

  return /*#__PURE__*/_react["default"].createElement(_progress["default"], {
    size: size,
    className: dotClassName,
    type: _enum2.ProgressType.loading
  });
}

function shouldDelay(spinning, delay) {
  return !!spinning && !!delay && !isNaN(Number(delay));
}

var Spin = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(Spin, _Component);

  var _super = (0, _createSuper2["default"])(Spin);

  function Spin(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(this, Spin);
    _this = _super.call(this, props, context);

    _this.debouncifyUpdateSpinning = function (props) {
      var _ref = props || _this.props,
          delay = _ref.delay;

      if (delay) {
        _this.cancelExistingSpin();

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

    _this.updateSpinning = function () {
      var spinning = _this.props.spinning;
      var currentSpinning = _this.state.spinning;

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

    var spinning = props.spinning,
        delay = props.delay;
    var shouldBeDelayed = shouldDelay(spinning, delay);
    _this.state = {
      spinning: spinning && !shouldBeDelayed
    };
    _this.originalUpdateSpinning = _this.updateSpinning;

    _this.debouncifyUpdateSpinning(props);

    return _this;
  }

  (0, _createClass2["default"])(Spin, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.updateSpinning();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.debouncifyUpdateSpinning();
      this.updateSpinning();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.cancelExistingSpin();
    }
  }, {
    key: "cancelExistingSpin",
    value: function cancelExistingSpin() {
      var updateSpinning = this.updateSpinning;

      if (updateSpinning && updateSpinning.cancel) {
        updateSpinning.cancel();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props = this.props,
          className = _this$props.className,
          size = _this$props.size,
          customizePrefixCls = _this$props.prefixCls,
          tip = _this$props.tip,
          wrapperClassName = _this$props.wrapperClassName,
          children = _this$props.children,
          style = _this$props.style,
          restProps = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var spinning = this.state.spinning;
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('spin', customizePrefixCls);
      var spinClassName = (0, _classnames["default"])(prefixCls, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-sm"), size === _enum.Size.small), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-lg"), size === _enum.Size.large), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-spinning"), spinning), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-show-text"), !!tip), _classNames), className); // fix https://fb.me/react-unknown-prop

      var divProps = (0, _omit["default"])(restProps, ['spinning', 'delay', 'indicator']);

      var spinElement = /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, divProps, {
        className: spinClassName,
        style: style,
        key: "loading"
      }), renderIndicator(this.props, prefixCls), tip ? /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-text")
      }, tip) : null);

      if (children) {
        var _classNames2;

        var animateClassName = "".concat(prefixCls, "-nested-loading");

        if (wrapperClassName) {
          animateClassName += " ".concat(wrapperClassName);
        }

        var containerClassName = (0, _classnames["default"])((_classNames2 = {}, (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-container"), true), (0, _defineProperty2["default"])(_classNames2, "".concat(prefixCls, "-blur"), spinning), _classNames2));
        return /*#__PURE__*/_react["default"].createElement(_animate["default"], (0, _extends2["default"])({}, divProps, {
          component: "div",
          className: animateClassName,
          transitionName: "fade"
        }), spinning && spinElement, /*#__PURE__*/_react["default"].createElement("div", {
          className: containerClassName,
          key: "container"
        }, children));
      }

      return spinElement;
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }, {
    key: "setDefaultIndicator",
    value: function setDefaultIndicator(indicator) {
      _shared.global.DEFAULT_SPIN_INDICATOR = indicator;
    }
  }]);
  return Spin;
}(_react.Component);

exports["default"] = Spin;
Spin.displayName = 'Spin';
Spin.defaultProps = {
  spinning: true,
  size: _enum.Size["default"],
  wrapperClassName: ''
};
//# sourceMappingURL=index.js.map
