'use strict';

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

var vue = require('vue');
var throttleDebounce = require('throttle-debounce');
var index = require('../_util/vue-types/index.js');
var index$2 = require('../_util/props-util/index.js');
var initDefaultProps = require('../_util/props-util/initDefaultProps.js');
require('../hooks/index.js');
var index$1 = require('./style/index.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');

const spinProps = () => ({
  prefixCls: String,
  spinning: {
    type: Boolean,
    default: void 0
  },
  size: String,
  wrapperClassName: String,
  tip: index.default.any,
  delay: Number,
  indicator: index.default.any
});
let defaultIndicator = null;
function shouldDelay(spinning, delay) {
  return !!spinning && !!delay && !isNaN(Number(delay));
}
function setDefaultIndicator(Content) {
  const Indicator = Content.indicator;
  defaultIndicator = typeof Indicator === "function" ? Indicator : () => vue.createVNode(Indicator, null, null);
}
var Spin = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ASpin",
  inheritAttrs: false,
  props: initDefaultProps.default(spinProps(), {
    size: "default",
    spinning: true,
    wrapperClassName: ""
  }),
  setup(props, {
    attrs,
    slots
  }) {
    const {
      prefixCls,
      size,
      direction
    } = useConfigInject.default("spin", props);
    const [wrapSSR, hashId] = index$1.default(prefixCls);
    const sSpinning = vue.shallowRef(props.spinning && !shouldDelay(props.spinning, props.delay));
    let updateSpinning;
    vue.watch([() => props.spinning, () => props.delay], () => {
      updateSpinning == null ? void 0 : updateSpinning.cancel();
      updateSpinning = throttleDebounce.debounce(props.delay, () => {
        sSpinning.value = props.spinning;
      });
      updateSpinning == null ? void 0 : updateSpinning();
    }, {
      immediate: true,
      flush: "post"
    });
    vue.onBeforeUnmount(() => {
      updateSpinning == null ? void 0 : updateSpinning.cancel();
    });
    return () => {
      var _a, _b;
      const {
        class: cls,
        ...divProps
      } = attrs;
      const {
        tip = (_a = slots.tip) == null ? void 0 : _a.call(slots)
      } = props;
      const children = (_b = slots.default) == null ? void 0 : _b.call(slots);
      const spinClassName = {
        [hashId.value]: true,
        [prefixCls.value]: true,
        [`${prefixCls.value}-sm`]: size.value === "small",
        [`${prefixCls.value}-lg`]: size.value === "large",
        [`${prefixCls.value}-spinning`]: sSpinning.value,
        [`${prefixCls.value}-show-text`]: !!tip,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl",
        [cls]: !!cls
      };
      function renderIndicator(prefixCls2) {
        const dotClassName = `${prefixCls2}-dot`;
        let indicator = index$2.getPropsSlot(slots, props, "indicator");
        if (indicator === null)
          return null;
        if (Array.isArray(indicator))
          indicator = indicator.length === 1 ? indicator[0] : indicator;
        if (vue.isVNode(indicator))
          return vue.cloneVNode(indicator, {
            class: dotClassName
          });
        if (defaultIndicator && vue.isVNode(defaultIndicator()))
          return vue.cloneVNode(defaultIndicator(), {
            class: dotClassName
          });
        return vue.createVNode("span", {
          "class": `${dotClassName} ${prefixCls2}-dot-spin`
        }, [vue.createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null), vue.createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null), vue.createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null), vue.createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null)]);
      }
      const spinElement = vue.createVNode("div", vue.mergeProps(divProps, {
        "class": spinClassName,
        "aria-live": "polite",
        "aria-busy": sSpinning.value
      }), [renderIndicator(prefixCls.value), tip ? vue.createVNode("div", {
        "class": `${prefixCls.value}-text`
      }, [tip]) : null]);
      if (children && index$2.filterEmpty(children).length) {
        const containerClassName = {
          [`${prefixCls.value}-container`]: true,
          [`${prefixCls.value}-blur`]: sSpinning.value
        };
        return wrapSSR(vue.createVNode("div", {
          "class": [`${prefixCls.value}-nested-loading`, props.wrapperClassName, hashId.value]
        }, [sSpinning.value && vue.createVNode("div", {
          "key": "loading"
        }, [spinElement]), vue.createVNode("div", {
          "class": containerClassName,
          "key": "container"
        }, [children])]));
      }
      return wrapSSR(spinElement);
    };
  }
});

exports.default = Spin;
exports.setDefaultIndicator = setDefaultIndicator;
exports.spinProps = spinProps;
//# sourceMappingURL=Spin.js.map
