'use strict';

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

var vue = require('vue');
var warning = require('../_util/warning.js');
var responsiveObserve = require('../_util/responsiveObserve.js');
var index = require('../_util/vue-types/index.js');
var vnode = require('../_util/vnode.js');
var index$1 = require('../_util/props-util/index.js');
require('../hooks/index.js');
var useContext = require('./useContext.js');
var index$2 = require('./style/index.js');
var Row = require('./Row.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');

const DescriptionsItemProps = {
  prefixCls: String,
  label: index.default.any,
  span: Number
};
const descriptionsItemProp = () => ({
  prefixCls: String,
  label: index.default.any,
  labelStyle: {
    type: Object,
    default: void 0
  },
  contentStyle: {
    type: Object,
    default: void 0
  },
  span: {
    type: Number,
    default: 1
  }
});
const DescriptionsItem = /* @__PURE__ */ vue.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 < responsiveObserve.responsiveArray.length; i++) {
      const breakpoint = responsiveObserve.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 = vnode.cloneElement(node, {
      span: rowRestCol
    });
    warning.default(span === void 0, "Descriptions", "Sum of column `span` in a line not match `column` of Descriptions.");
  }
  return clone;
}
function getRows(children, column) {
  const childNodes = index$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;
}
const descriptionsProps = () => ({
  prefixCls: String,
  bordered: {
    type: Boolean,
    default: void 0
  },
  size: {
    type: String,
    default: "default"
  },
  title: index.default.any,
  extra: index.default.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__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ADescriptions",
  inheritAttrs: false,
  props: descriptionsProps(),
  slots: Object,
  Item: DescriptionsItem,
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject.default("descriptions", props);
    let token;
    const screens = vue.ref({});
    const [wrapSSR, hashId] = index$2.default(prefixCls);
    const responsiveObserve$1 = responsiveObserve.default();
    vue.onBeforeMount(() => {
      token = responsiveObserve$1.value.subscribe((screen) => {
        if (typeof props.column !== "object")
          return;
        screens.value = screen;
      });
    });
    vue.onBeforeUnmount(() => {
      responsiveObserve$1.value.unsubscribe(token);
    });
    useContext.createProviderContext({
      labelStyle: vue.toRef(props, "labelStyle"),
      contentStyle: vue.toRef(props, "contentStyle")
    });
    const mergeColumn = vue.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(vue.createVNode("div", vue.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) && 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) => vue.createVNode(Row.default, {
        "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;
};

exports.DescriptionsItem = DescriptionsItem;
exports.DescriptionsItemProps = DescriptionsItemProps;
exports.default = Descriptions;
exports.descriptionsProps = descriptionsProps;
//# sourceMappingURL=index.js.map
