import { defineComponent, computed, ref, watch, createVNode, Fragment } from 'vue';
import '../../../utils/index.mjs';
import '../../../hooks/index.mjs';
import useStyle from '../style/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import { spaceProps } from './props.mjs';
import { useFlexGapSupport } from '../../../hooks/use-flex-gap-support/index.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import { filterEmpty } from '../../../utils/vue/props.mjs';

const spaceSize = {
  small: 8,
  middle: 16,
  large: 24
};
function getNumberSize(size) {
  return typeof size === "string" ? spaceSize[size] : size || 0;
}
var ASpace = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ASpace",
  inheritAttrs: false,
  props: spaceProps(),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls,
      space,
      direction: directionConfig
    } = useConfigInject("space", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const supportFlexGap = useFlexGapSupport();
    const size = computed(() => {
      var _a, _b, _c;
      return (_c = (_b = props.size) != null ? _b : (_a = space == null ? void 0 : space.value) == null ? void 0 : _a.size) != null ? _c : "small";
    });
    const horizontalSize = ref();
    const verticalSize = ref();
    watch(size, () => {
      [horizontalSize.value, verticalSize.value] = (Array.isArray(size.value) ? size.value : [size.value, size.value]).map((item) => getNumberSize(item));
    }, {
      immediate: true
    });
    const mergedAlign = computed(() => props.align === void 0 && props.direction === "horizontal" ? "center" : props.align);
    const cn = computed(() => {
      return classNames(prefixCls.value, hashId.value, `${prefixCls.value}-${props.direction}`, {
        [`${prefixCls.value}-rtl`]: directionConfig.value === "rtl",
        [`${prefixCls.value}-align-${mergedAlign.value}`]: mergedAlign.value
      });
    });
    const marginDirection = computed(() => directionConfig.value === "rtl" ? "marginLeft" : "marginRight");
    const style = computed(() => {
      const gapStyle = {};
      if (supportFlexGap.value) {
        gapStyle.columnGap = `${horizontalSize.value}px`;
        gapStyle.rowGap = `${verticalSize.value}px`;
      }
      return {
        ...gapStyle,
        ...props.wrap && {
          flexWrap: "wrap",
          marginBottom: `${-verticalSize.value}px`
        }
      };
    });
    return () => {
      var _a, _b;
      const {
        wrap,
        direction = "horizontal"
      } = props;
      const children = (_a = slots.default) == null ? void 0 : _a.call(slots);
      const items = filterEmpty(children);
      const len = items.length;
      if (len === 0)
        return null;
      const split = (_b = slots.split) == null ? void 0 : _b.call(slots);
      const itemClassName = `${prefixCls.value}-item`;
      const horizontalSizeVal = horizontalSize.value;
      const latestIndex = len - 1;
      return createVNode("div", {
        ...attrs,
        "class": [cn.value, attrs.class],
        "style": [style.value, attrs.style]
      }, [items.map((child, index) => {
        let originIndex = children.indexOf(child);
        if (originIndex === -1)
          originIndex = `$$space-${index}`;
        let itemStyle = {};
        if (!supportFlexGap.value) {
          if (direction === "vertical") {
            if (index < latestIndex)
              itemStyle = {
                marginBottom: `${horizontalSizeVal / (split ? 2 : 1)}px`
              };
          } else {
            itemStyle = {
              ...index < latestIndex && {
                [marginDirection.value]: `${horizontalSizeVal / (split ? 2 : 1)}px`
              },
              ...wrap && {
                paddingBottom: `${verticalSize.value}px`
              }
            };
          }
        }
        return wrapSSR(createVNode(Fragment, {
          "key": originIndex
        }, [createVNode("div", {
          "class": itemClassName,
          "style": itemStyle
        }, [child]), index < latestIndex && split && createVNode("span", {
          "class": `${itemClassName}-split`,
          "style": itemStyle
        }, [split])]));
      })]);
    };
  }
});

export { ASpace as default };
//# sourceMappingURL=Space.mjs.map
