"use strict";

var __extends = void 0 && (void 0).__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) {
        if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
      }
    };

    return _extendStatics(d, b);
  };

  return function (d, b) {
    if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");

    _extendStatics(d, b);

    function __() {
      this.constructor = d;
    }

    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();

var __assign = void 0 && (void 0).__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

var __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var omit_1 = __importDefault(require("lodash/omit"));

var debounce_1 = __importDefault(require("lodash/debounce"));

var icon_1 = __importDefault(require("../icon"));

require("./style/spin.css");

var prefixCls = 'dmc-spin';

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

var Spin =
/** @class */
function (_super) {
  __extends(Spin, _super);

  function Spin(props) {
    var _this = _super.call(this, props) || this;

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

      if (delay) {
        _this.cancelExistingSpin();

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

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

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

    _this.cancelExistingSpin = function () {
      var us = _this.updateSpinning;
      us && us.cancel && us.cancel();
    };

    _this.isNestedPattern = function () {
      return !!(_this.props && _this.props.children);
    };

    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;
  }

  Spin.prototype.componentDidMount = function () {
    this.updateSpinning();
  };

  Spin.prototype.componentDidUpdate = function () {
    this.debouncifyUpdateSpinning();
    this.updateSpinning();
  };

  Spin.prototype.componentWillUnmount = function () {
    this.cancelExistingSpin();
  };

  Spin.prototype.render = function () {
    var _a, _b;

    var _c = this.props,
        className = _c.className,
        size = _c.size,
        tip = _c.tip,
        wrapperClassName = _c.wrapperClassName,
        style = _c.style,
        restProps = __rest(_c, ["className", "size", "tip", "wrapperClassName", "style"]);

    var spinning = this.state.spinning;
    var spinClassName = classnames_1["default"](prefixCls, (_a = {}, _a[prefixCls + "-" + size] = size, _a[prefixCls + "-spinning"] = spinning, _a[prefixCls + "-show-text"] = !!tip, _a), className);
    var divProps = omit_1["default"](restProps, ['spinning', 'delay']);
    var spinElement = jsx_runtime_1.jsxs("div", __assign({}, divProps, {
      style: style,
      className: spinClassName
    }, {
      children: [jsx_runtime_1.jsx(icon_1["default"], {
        icon: 'loading',
        className: prefixCls + "-loading"
      }, void 0), tip ? jsx_runtime_1.jsx("div", __assign({
        className: prefixCls + "-text"
      }, {
        children: tip
      }), void 0) : null]
    }), void 0);

    if (this.isNestedPattern()) {
      var containerClassName = classnames_1["default"](prefixCls + "-container", (_b = {}, _b[prefixCls + "-blur"] = spinning, _b));
      return jsx_runtime_1.jsxs("div", __assign({}, divProps, {
        className: classnames_1["default"](prefixCls + "-nested-loading", wrapperClassName)
      }, {
        children: [spinning && jsx_runtime_1.jsx("div", {
          children: spinElement
        }, "loading"), jsx_runtime_1.jsx("div", __assign({
          className: containerClassName
        }, {
          children: this.props.children
        }), "container")]
      }), void 0);
    }

    return spinElement;
  };

  Spin.defaultProps = {
    spinning: true
  };
  return Spin;
}(React.Component);

exports["default"] = Spin;

