"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const size = require("../../../constants/size.js");
const types = require("../../../utils/types.js");
const runtime = require("../../../utils/vue/props/runtime.js");
const vnode = require("../../../utils/vue/vnode.js");
const item = require("./item.js");
const useSpace = require("./use-space.js");
const Vue = require("vue");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const spaceProps = runtime.buildProps({
  direction: {
    type: String,
    values: ["horizontal", "vertical"],
    default: "horizontal"
  },
  class: {
    type: runtime.definePropType([
      String,
      Object,
      Array
    ]),
    default: ""
  },
  style: {
    type: runtime.definePropType([
      String,
      Array,
      Object
    ]),
    default: ""
  },
  alignment: {
    type: runtime.definePropType(String),
    default: "center"
  },
  prefixCls: { type: String },
  spacer: {
    type: runtime.definePropType([
      Object,
      String,
      Number,
      Array
    ]),
    default: null,
    validator: (val) => Vue.isVNode(val) || types.isNumber(val) || shared_esmBundler.isString(val)
  },
  wrap: Boolean,
  fill: Boolean,
  fillRatio: {
    type: Number,
    default: 100
  },
  size: {
    type: [
      String,
      Array,
      Number
    ],
    values: size.componentSizes,
    validator: (val) => {
      return types.isNumber(val) || shared_esmBundler.isArray(val) && val.length === 2 && val.every(types.isNumber);
    }
  }
});
const Space = Vue.defineComponent({
  name: "ElSpace",
  props: spaceProps,
  setup(props, { slots }) {
    const { classes, containerStyle, itemStyle } = useSpace.useSpace(props);
    function extractChildren(children, parentKey = "", extractedChildren = []) {
      const { prefixCls } = props;
      children.forEach((child, loopKey) => {
        if (vnode.isFragment(child)) {
          if (shared_esmBundler.isArray(child.children)) child.children.forEach((nested, key) => {
            if (vnode.isFragment(nested) && shared_esmBundler.isArray(nested.children)) extractChildren(nested.children, `${parentKey + key}-`, extractedChildren);
            else if (Vue.isVNode(nested) && (nested == null ? void 0 : nested.type) === Vue.Comment) extractedChildren.push(nested);
            else extractedChildren.push(Vue.createVNode(item.default, {
              style: itemStyle.value,
              prefixCls,
              key: `nested-${parentKey + key}`
            }, { default: () => [nested] }, vnode.PatchFlags.PROPS | vnode.PatchFlags.STYLE, ["style", "prefixCls"]));
          });
        } else if (vnode.isValidElementNode(child)) extractedChildren.push(Vue.createVNode(item.default, {
          style: itemStyle.value,
          prefixCls,
          key: `LoopKey${parentKey + loopKey}`
        }, { default: () => [child] }, vnode.PatchFlags.PROPS | vnode.PatchFlags.STYLE, ["style", "prefixCls"]));
      });
      return extractedChildren;
    }
    return () => {
      const { spacer, direction } = props;
      const children = Vue.renderSlot(slots, "default", { key: 0 }, () => []);
      if ((children.children ?? []).length === 0) return null;
      if (shared_esmBundler.isArray(children.children)) {
        let extractedChildren = extractChildren(children.children);
        if (spacer) {
          const len = extractedChildren.length - 1;
          extractedChildren = extractedChildren.reduce((acc, child, idx) => {
            const children2 = [...acc, child];
            if (idx !== len) children2.push(Vue.createVNode("span", {
              style: [itemStyle.value, direction === "vertical" ? "width: 100%" : null],
              key: idx
            }, [Vue.isVNode(spacer) ? spacer : Vue.createTextVNode(spacer, vnode.PatchFlags.TEXT)], vnode.PatchFlags.STYLE));
            return children2;
          }, []);
        }
        return Vue.createVNode("div", {
          class: classes.value,
          style: containerStyle.value
        }, extractedChildren, vnode.PatchFlags.STYLE | vnode.PatchFlags.CLASS);
      }
      return children.children;
    };
  }
});
exports.default = Space;
exports.spaceProps = spaceProps;
