'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../constants/index.cjs');
require('../../../hooks/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var useContext = require('./useContext.cjs');
var Row = require('./Row.cjs');
var props = require('./props.cjs');
var Item = require('./Item.cjs');
var constants = require('./constants.cjs');
var index$1 = require('../../../hooks/use-responsive-observe/index.cjs');
var size = require('../../../constants/size.cjs');
var vnode = require('../../../utils/vue/vnode.cjs');
var log = require('../../../utils/log.cjs');
var props$1 = require('../../../utils/vue/props.cjs');

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

exports.default = ADescriptions;
//# sourceMappingURL=Descriptions.cjs.map
