"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.matchScreen = exports.descriptionsProps = exports.descriptionsContext = exports.default = exports.DescriptionsItemProps = exports.DescriptionsItem = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _vue = require("vue");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _responsiveObserve = _interopRequireWildcard(require("../_util/responsiveObserve"));
var _Row = _interopRequireDefault(require("./Row"));
var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
var _propsUtil = require("../_util/props-util");
var _useConfigInject = _interopRequireDefault(require("../config-provider/hooks/useConfigInject"));
var _style = _interopRequireDefault(require("./style"));
var _warning = _interopRequireDefault(require("../_util/warning"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
const DescriptionsItemProps = exports.DescriptionsItemProps = {
  prefixCls: String,
  label: _vueTypes.default.any,
  span: Number
};
const descriptionsItemProp = () => ({
  prefixCls: String,
  label: _vueTypes.default.any,
  labelStyle: {
    type: Object,
    default: undefined
  },
  contentStyle: {
    type: Object,
    default: undefined
  },
  span: {
    type: [Number, String, Object],
    default: 1
  }
});
const DescriptionsItem = exports.DescriptionsItem = (0, _vue.defineComponent)({
  compatConfig: {
    MODE: 3
  },
  name: 'ADescriptionsItem',
  props: descriptionsItemProp(),
  setup(_, {
    slots
  }) {
    return () => {
      var _a;
      return (_a = slots.default) === null || _a === void 0 ? 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] !== undefined) {
        return column[breakpoint] || DEFAULT_COLUMN_MAP[breakpoint];
      }
    }
  }
  return 3;
}
const matchScreen = (screens, screenSizes) => {
  if (!screenSizes) {
    return;
  }
  for (const breakpoint of _responsiveObserve.responsiveArray) {
    if (screens[breakpoint] && (screenSizes === null || screenSizes === void 0 ? void 0 : screenSizes[breakpoint]) !== undefined) {
      return screenSizes[breakpoint];
    }
  }
};
exports.matchScreen = matchScreen;
const transChildren2Items = childNodes => childNodes.map(node => {
  var _a, _b, _c, _d, _e;
  return (0, _extends2.default)((0, _extends2.default)({}, node === null || node === void 0 ? void 0 : node.props), {
    children: (_a = node.children) === null || _a === void 0 ? void 0 : _a.default(),
    label: (_c = (_b = node === null || node === void 0 ? void 0 : node.props) === null || _b === void 0 ? void 0 : _b.label) !== null && _c !== void 0 ? _c : (_e = (_d = node.children) === null || _d === void 0 ? void 0 : _d.label) === null || _e === void 0 ? void 0 : _e.call(_d),
    key: node.key,
    style: (0, _propsUtil.getStyle)(node),
    class: (0, _propsUtil.getClass)(node)
  });
});
function getItems(screens, items, children) {
  const childNodes = (0, _propsUtil.flattenChildren)(children);
  const mergedItems = items || transChildren2Items(childNodes);
  return mergedItems.map(_a => {
    var {
        span
      } = _a,
      restItem = __rest(_a, ["span"]);
    if (span === 'filled') {
      return (0, _extends2.default)((0, _extends2.default)({}, restItem), {
        filled: true
      });
    }
    return (0, _extends2.default)((0, _extends2.default)({}, restItem), {
      span: typeof span === 'number' ? span : matchScreen(screens, span)
    });
  });
}
function getRows(rowItems, column) {
  let rows = [];
  let tempRow = [];
  let exceed = false;
  let count = 0;
  rowItems.filter(n => n).forEach(rowItem => {
    const {
        filled
      } = rowItem,
      restItem = __rest(rowItem, ["filled"]);
    if (filled) {
      tempRow.push(restItem);
      rows.push(tempRow);
      tempRow = [];
      count = 0;
      return;
    }
    const restSpan = column - count;
    count += rowItem.span || 1;
    if (count >= column) {
      if (count > column) {
        exceed = true;
        tempRow.push((0, _extends2.default)((0, _extends2.default)({}, restItem), {
          span: restSpan
        }));
      } else {
        tempRow.push(restItem);
      }
      rows.push(tempRow);
      tempRow = [];
      count = 0;
    } else {
      tempRow.push(restItem);
    }
  });
  if (tempRow.length) {
    rows.push(tempRow);
  }
  rows = rows.map(rows => {
    const count = rows.reduce((acc, item) => acc + (item.span || 1), 0);
    if (count < column) {
      // If the span of the last element in the current row is less than the column, then add its span to the remaining columns
      const last = rows[rows.length - 1];
      last.span = column - (count - (last.span || 1));
      return rows;
    }
    return rows;
  });
  return [rows, exceed];
}
const descriptionsProps = () => ({
  prefixCls: String,
  bordered: {
    type: Boolean,
    default: undefined
  },
  size: {
    type: String,
    default: 'default'
  },
  title: _vueTypes.default.any,
  extra: _vueTypes.default.any,
  column: {
    type: [Number, Object],
    default: () => DEFAULT_COLUMN_MAP
  },
  layout: String,
  colon: {
    type: Boolean,
    default: undefined
  },
  labelStyle: {
    type: Object,
    default: undefined
  },
  contentStyle: {
    type: Object,
    default: undefined
  },
  items: {
    type: Array,
    default: undefined
  }
});
exports.descriptionsProps = descriptionsProps;
const descriptionsContext = exports.descriptionsContext = Symbol('descriptionsContext');
const Descriptions = (0, _vue.defineComponent)({
  compatConfig: {
    MODE: 3
  },
  name: 'ADescriptions',
  inheritAttrs: false,
  props: descriptionsProps(),
  slots: Object,
  Item: DescriptionsItem,
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls,
      direction
    } = (0, _useConfigInject.default)('descriptions', props);
    let token;
    const screens = (0, _vue.ref)({});
    const [wrapSSR, hashId] = (0, _style.default)(prefixCls);
    const responsiveObserve = (0, _responsiveObserve.default)();
    (0, _vue.onBeforeMount)(() => {
      token = responsiveObserve.value.subscribe(screen => {
        if (typeof props.column !== 'object') {
          return;
        }
        screens.value = screen;
      });
    });
    (0, _vue.onBeforeUnmount)(() => {
      responsiveObserve.value.unsubscribe(token);
    });
    (0, _vue.provide)(descriptionsContext, {
      labelStyle: (0, _vue.toRef)(props, 'labelStyle'),
      contentStyle: (0, _vue.toRef)(props, 'contentStyle')
    });
    const mergeColumn = (0, _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 || _a === void 0 ? void 0 : _a.call(slots),
        extra = (_b = slots.extra) === null || _b === void 0 ? void 0 : _b.call(slots)
      } = props;
      const children = (_c = slots.default) === null || _c === void 0 ? void 0 : _c.call(slots);
      const items = getItems(screens.value, props.items, children);
      const [rows, exceed] = getRows(items, mergeColumn.value);
      (0, _warning.default)(!exceed, 'Descriptions', 'Sum of column `span` in a line not match `column` of Descriptions.');
      return wrapSSR((0, _vue.createVNode)("div", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, 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) && (0, _vue.createVNode)("div", {
        "class": `${prefixCls.value}-header`
      }, [title && (0, _vue.createVNode)("div", {
        "class": `${prefixCls.value}-title`
      }, [title]), extra && (0, _vue.createVNode)("div", {
        "class": `${prefixCls.value}-extra`
      }, [extra])]), (0, _vue.createVNode)("div", {
        "class": `${prefixCls.value}-view`
      }, [(0, _vue.createVNode)("table", null, [(0, _vue.createVNode)("tbody", null, [rows.map((row, index) => (0, _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;
};
var _default = exports.default = Descriptions;