import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var _excluded = ["className", "size", "prefixCls", "tip", "wrapperClassName", "children", "style"];
import React, { cloneElement, Component, isValidElement } from 'react';
import classNames from 'classnames';
import omit from 'lodash/omit';
import debounce from 'lodash/debounce';
import { global } from 'choerodon-ui/shared';
import Animate from '../animate';
import Progress from '../progress/progress';
import { Size } from '../_util/enum';
import { ProgressType } from '../progress/enum';
import ConfigContext from '../config-provider/ConfigContext';

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

  if ( /*#__PURE__*/isValidElement(indicator)) {
    return /*#__PURE__*/cloneElement(indicator, {
      className: classNames(indicator.props.className, dotClassName)
    });
  }

  var defaultIndicator = global.DEFAULT_SPIN_INDICATOR;

  if ( /*#__PURE__*/isValidElement(defaultIndicator)) {
    return /*#__PURE__*/cloneElement(defaultIndicator, {
      className: classNames(defaultIndicator.props.className, dotClassName)
    });
  }

  return /*#__PURE__*/React.createElement(Progress, {
    size: size,
    className: dotClassName,
    type: ProgressType.loading
  });
}

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

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

  var _super = _createSuper(Spin);

  function Spin(props, context) {
    var _this;

    _classCallCheck(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 = debounce(_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;
  }

  _createClass(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 = _objectWithoutProperties(_this$props, _excluded);

      var spinning = this.state.spinning;
      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = getPrefixCls('spin', customizePrefixCls);
      var spinClassName = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === Size.small), _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === Size.large), _defineProperty(_classNames, "".concat(prefixCls, "-spinning"), spinning), _defineProperty(_classNames, "".concat(prefixCls, "-show-text"), !!tip), _classNames), className); // fix https://fb.me/react-unknown-prop

      var divProps = omit(restProps, ['spinning', 'delay', 'indicator']);
      var spinElement = /*#__PURE__*/React.createElement("div", _extends({}, divProps, {
        className: spinClassName,
        style: style,
        key: "loading"
      }), renderIndicator(this.props, prefixCls), tip ? /*#__PURE__*/React.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 = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-container"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-blur"), spinning), _classNames2));
        return /*#__PURE__*/React.createElement(Animate, _extends({}, divProps, {
          component: "div",
          className: animateClassName,
          transitionName: "fade"
        }), spinning && spinElement, /*#__PURE__*/React.createElement("div", {
          className: containerClassName,
          key: "container"
        }, children));
      }

      return spinElement;
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }, {
    key: "setDefaultIndicator",
    value: function setDefaultIndicator(indicator) {
      global.DEFAULT_SPIN_INDICATOR = indicator;
    }
  }]);

  return Spin;
}(Component);

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