import { defineComponent, ref, onBeforeMount, onBeforeUnmount, toRef, computed, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../constants/index.mjs';
import '../../../hooks/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import { createProviderContext } from './useContext.mjs';
import Row from './Row.mjs';
import { descriptionsProps } from './props.mjs';
import ADescriptionItem from './Item.mjs';
import { DEFAULT_COLUMN_MAP } from './constants.mjs';
import { useResponsiveObserver } from '../../../hooks/use-responsive-observe/index.mjs';
import { responsiveArray } from '../../../constants/size.mjs';
import { cloneElement } from '../../../utils/vue/vnode.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { flattenChildren } from '../../../utils/vue/props.mjs';

var ADescriptions = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ADescriptions",
  inheritAttrs: false,
  props: descriptionsProps(),
  slots: Object,
  Item: ADescriptionItem,
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject("descriptions", props);
    let token;
    const screens = ref({});
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const responsiveObserve = useResponsiveObserver();
    onBeforeMount(() => {
      token = responsiveObserve.value.subscribe((screen) => {
        if (typeof props.column !== "object")
          return;
        screens.value = screen;
      });
    });
    onBeforeUnmount(() => {
      responsiveObserve.value.unsubscribe(token);
    });
    createProviderContext({
      labelStyle: toRef(props, "labelStyle"),
      contentStyle: toRef(props, "contentStyle")
    });
    function getColumn(column, _screens) {
      if (typeof column === "number")
        return column;
      if (typeof column === "object") {
        for (let i = 0; i < responsiveArray.length; i++) {
          const breakpoint = responsiveArray[i];
          if (_screens[breakpoint] && column[breakpoint] !== void 0)
            return column[breakpoint] || DEFAULT_COLUMN_MAP[breakpoint];
        }
      }
      return 3;
    }
    function getFilledItem(node, rowRestCol, span) {
      let clone = node;
      if (span === void 0 || span > rowRestCol) {
        clone = cloneElement(node, {
          span: rowRestCol
        });
        devWarning(span === void 0, "Descriptions", "Sum of column `span` in a line not match `column` of Descriptions.");
      }
      return clone;
    }
    const mergeColumn = computed(() => getColumn(props.column, screens.value));
    function getRows(children, column) {
      const childNodes = flattenChildren(children);
      const rows = [];
      let tmpRow = [];
      let rowRestCol = column;
      childNodes.forEach((node, index) => {
        var _a;
        const span = (_a = node.props) == null ? void 0 : _a.span;
        const mergedSpan = span || 1;
        if (index === childNodes.length - 1) {
          tmpRow.push(getFilledItem(node, rowRestCol, span));
          rows.push(tmpRow);
          return;
        }
        if (mergedSpan < rowRestCol) {
          rowRestCol -= mergedSpan;
          tmpRow.push(node);
        } else {
          tmpRow.push(getFilledItem(node, rowRestCol, mergedSpan));
          rows.push(tmpRow);
          rowRestCol = column;
          tmpRow = [];
        }
      });
      return rows;
    }
    return () => {
      var _a, _b, _c;
      const {
        size,
        bordered = false,
        layout = "horizontal",
        colon = true,
        title = (_a = slots.title) == null ? void 0 : _a.call(slots),
        extra = (_b = slots.extra) == null ? void 0 : _b.call(slots)
      } = props;
      const children = (_c = slots.default) == null ? void 0 : _c.call(slots);
      const rows = getRows(children, mergeColumn.value);
      return wrapSSR(createVNode("div", {
        ...attrs,
        "class": [prefixCls.value, {
          [`${prefixCls.value}-${size}`]: size !== "default",
          [`${prefixCls.value}-bordered`]: !!bordered,
          [`${prefixCls.value}-rtl`]: direction.value === "rtl"
        }, attrs.class, hashId.value]
      }, [(title || extra) && createVNode("div", {
        "class": `${prefixCls.value}-header`
      }, [title && createVNode("div", {
        "class": `${prefixCls.value}-title`
      }, [title]), extra && createVNode("div", {
        "class": `${prefixCls.value}-extra`
      }, [extra])]), createVNode("div", {
        "class": `${prefixCls.value}-view`
      }, [createVNode("table", null, [createVNode("tbody", null, [rows.map((row, index) => {
        return createVNode(Row, {
          "key": index,
          "index": index,
          "colon": colon,
          "prefixCls": prefixCls.value,
          "vertical": layout === "vertical",
          "bordered": bordered,
          "row": row
        }, null);
      })])])])]));
    };
  }
});

export { ADescriptions as default };
//# sourceMappingURL=Descriptions.mjs.map
