import { defineComponent, ref, onBeforeMount, onBeforeUnmount, toRef, computed, createVNode, mergeProps } from 'vue';
import warning from '../_util/warning.mjs';
import useResponsiveObserver, { responsiveArray } from '../_util/responsiveObserve.mjs';
import PropTypes from '../_util/vue-types/index.mjs';
import { cloneElement } from '../_util/vnode.mjs';
import { flattenChildren } from '../_util/props-util/index.mjs';
import '../hooks/index.mjs';
import { createProviderContext } from './useContext.mjs';
import useStyle from './style/index.mjs';
import Row from './Row.mjs';
import useConfigInject from '../config-provider/hooks/useConfigInject.mjs';

const DescriptionsItemProps = {
  prefixCls: String,
  label: PropTypes.any,
  span: Number
};
const descriptionsItemProp = () => ({
  prefixCls: String,
  label: PropTypes.any,
  labelStyle: {
    type: Object,
    default: void 0
  },
  contentStyle: {
    type: Object,
    default: void 0
  },
  span: {
    type: Number,
    default: 1
  }
});
const DescriptionsItem = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ADescriptionsItem",
  props: descriptionsItemProp(),
  setup(_, {
    slots
  }) {
    return () => {
      var _a;
      return (_a = slots.default) == null ? void 0 : _a.call(slots);
    };
  }
});
const DEFAULT_COLUMN_MAP = {
  xxxl: 3,
  xxl: 3,
  xl: 3,
  lg: 3,
  md: 3,
  sm: 2,
  xs: 1
};
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
    });
    warning(span === void 0, "Descriptions", "Sum of column `span` in a line not match `column` of Descriptions.");
  }
  return clone;
}
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;
}
const descriptionsProps = () => ({
  prefixCls: String,
  bordered: {
    type: Boolean,
    default: void 0
  },
  size: {
    type: String,
    default: "default"
  },
  title: PropTypes.any,
  extra: PropTypes.any,
  column: {
    type: [Number, Object],
    default: () => DEFAULT_COLUMN_MAP
  },
  layout: String,
  colon: {
    type: Boolean,
    default: void 0
  },
  labelStyle: {
    type: Object,
    default: void 0
  },
  contentStyle: {
    type: Object,
    default: void 0
  }
});
const Descriptions = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ADescriptions",
  inheritAttrs: false,
  props: descriptionsProps(),
  slots: Object,
  Item: DescriptionsItem,
  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")
    });
    const mergeColumn = computed(() => getColumn(props.column, screens.value));
    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", mergeProps(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) => createVNode(Row, {
        "key": index,
        "index": index,
        "colon": colon,
        "prefixCls": prefixCls.value,
        "vertical": layout === "vertical",
        "bordered": bordered,
        "row": row
      }, null))])])])]));
    };
  }
});
Descriptions.install = function(app) {
  app.component(Descriptions.name, Descriptions);
  app.component(Descriptions.Item.name, Descriptions.Item);
  return app;
};

export { DescriptionsItem, DescriptionsItemProps, Descriptions as default, descriptionsProps };
//# sourceMappingURL=index.mjs.map
