import { createVNode, defineComponent, shallowRef, watch, onBeforeUnmount, isVNode, cloneVNode } from 'vue';
import { debounce } from 'lodash-es';
import '../../../utils/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import { spinProps } from './props.mjs';
import { initDefaultProps } from '../../../utils/vue/initDefaultProps.mjs';
import { getPropsSlot, filterEmpty } from '../../../utils/vue/props.mjs';

let defaultIndicator = null;
function shouldDelay(spinning, delay) {
  return !!spinning && !!delay && !Number.isNaN(Number(delay));
}
function setDefaultIndicator(Content) {
  const Indicator = Content.indicator;
  defaultIndicator = typeof Indicator === "function" ? Indicator : () => createVNode(Indicator, null, null);
}
var ASpin = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ASpin",
  inheritAttrs: false,
  props: initDefaultProps(spinProps(), {
    size: "default",
    spinning: true,
    wrapperClassName: ""
  }),
  setDefaultIndicator,
  setup(props, {
    attrs,
    slots
  }) {
    const {
      prefixCls,
      size,
      direction
    } = useConfigInject("spin", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const sSpinning = shallowRef(props.spinning && !shouldDelay(props.spinning, props.delay));
    let updateSpinning;
    watch([() => props.spinning, () => props.delay], () => {
      updateSpinning == null ? void 0 : updateSpinning.cancel();
      updateSpinning = debounce(() => {
        sSpinning.value = props.spinning;
      }, props.delay);
      updateSpinning == null ? void 0 : updateSpinning();
    }, {
      immediate: true,
      flush: "post"
    });
    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 = getPropsSlot(slots, props, "indicator");
        if (indicator === null)
          return null;
        if (Array.isArray(indicator))
          indicator = indicator.length === 1 ? indicator[0] : indicator;
        if (isVNode(indicator))
          return cloneVNode(indicator, {
            class: dotClassName
          });
        if (defaultIndicator && isVNode(defaultIndicator()))
          return cloneVNode(defaultIndicator(), {
            class: dotClassName
          });
        return createVNode("span", {
          "class": `${dotClassName} ${prefixCls2}-dot-spin`
        }, [createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null), createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null), createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null), createVNode("i", {
          "class": `${prefixCls2}-dot-item`
        }, null)]);
      }
      const spinElement = createVNode("div", {
        ...divProps,
        "class": spinClassName,
        "aria-live": "polite",
        "aria-busy": sSpinning.value
      }, [renderIndicator(prefixCls.value), tip ? createVNode("div", {
        "class": `${prefixCls.value}-text`
      }, [tip]) : null]);
      if (children && filterEmpty(children).length) {
        const containerClassName = {
          [`${prefixCls.value}-container`]: true,
          [`${prefixCls.value}-blur`]: sSpinning.value
        };
        return wrapSSR(createVNode("div", {
          "class": [`${prefixCls.value}-nested-loading`, props.wrapperClassName, hashId.value]
        }, [sSpinning.value && createVNode("div", {
          "key": "loading"
        }, [spinElement]), createVNode("div", {
          "class": containerClassName,
          "key": "container"
        }, [children])]));
      }
      return wrapSSR(spinElement);
    };
  }
});

export { ASpin as default, setDefaultIndicator };
//# sourceMappingURL=Spin.mjs.map
