"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DescriptionsProps = exports.DescriptionsItem = exports.DescriptionsItemProps = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _warning = _interopRequireDefault(require("../_util/warning"));

var _responsiveObserve = _interopRequireWildcard(require("../_util/responsiveObserve"));

var _configProvider = require("../config-provider");

var _Col = _interopRequireDefault(require("./Col"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _propsUtil = require("../_util/props-util");

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var DescriptionsItemProps = {
  prefixCls: _vueTypes.default.string,
  label: _vueTypes.default.any,
  span: _vueTypes.default.number
};
exports.DescriptionsItemProps = DescriptionsItemProps;

function toArray(value) {
  var ret = value;

  if (value === undefined) {
    ret = [];
  } else if (!Array.isArray(value)) {
    ret = [value];
  }

  return ret;
}

var DescriptionsItem = {
  name: 'ADescriptionsItem',
  props: (0, _propsUtil.initDefaultProps)(DescriptionsItemProps, {
    span: 1
  }),
  render: function render() {
    return null;
  }
};
exports.DescriptionsItem = DescriptionsItem;
var DescriptionsProps = {
  prefixCls: _vueTypes.default.string,
  bordered: _vueTypes.default.bool,
  size: _vueTypes.default.oneOf(['default', 'middle', 'small']).def('default'),
  title: _vueTypes.default.any,
  column: _vueTypes.default.oneOfType([_vueTypes.default.number, _vueTypes.default.object]),
  layout: _vueTypes.default.oneOf(['horizontal', 'vertical']),
  colon: _vueTypes.default.bool
};
/**
 * Convert children into `column` groups.
 * @param children: DescriptionsItem
 * @param column: number
 */

exports.DescriptionsProps = DescriptionsProps;

var generateChildrenRows = function generateChildrenRows(children, column) {
  var rows = [];
  var columns = null;
  var leftSpans;
  var itemNodes = toArray(children);
  itemNodes.forEach(function (node, index) {
    var itemProps = (0, _propsUtil.getOptionProps)(node);
    var itemNode = node;

    if (!columns) {
      leftSpans = column;
      columns = [];
      rows.push(columns);
    } // Always set last span to align the end of Descriptions


    var lastItem = index === itemNodes.length - 1;
    var lastSpanSame = true;

    if (lastItem) {
      lastSpanSame = !itemProps.span || itemProps.span === leftSpans;
      itemNode = (0, _vue.cloneVNode)(itemNode, {
        span: leftSpans
      });
    } // Calculate left fill span


    var _itemProps$span = itemProps.span,
        span = _itemProps$span === void 0 ? 1 : _itemProps$span;
    columns.push(itemNode);
    leftSpans -= span;

    if (leftSpans <= 0) {
      columns = null;
      (0, _warning.default)(leftSpans === 0 && lastSpanSame, 'Descriptions', 'Sum of column `span` in a line exceeds `column` of Descriptions.');
    }
  });
  return rows;
};

var defaultColumnMap = {
  xxl: 3,
  xl: 3,
  lg: 3,
  md: 3,
  sm: 2,
  xs: 1
};
var Descriptions = {
  name: 'ADescriptions',
  Item: DescriptionsItem,
  mixins: [_BaseMixin.default],
  props: (0, _propsUtil.initDefaultProps)(DescriptionsProps, {
    column: defaultColumnMap
  }),
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  data: function data() {
    return {
      screens: {},
      token: undefined
    };
  },
  methods: {
    getColumn: function getColumn() {
      var column = this.$props.column;

      if (_typeof(column) === 'object') {
        for (var i = 0; i < _responsiveObserve.responsiveArray.length; i++) {
          var breakpoint = _responsiveObserve.responsiveArray[i];

          if (this.screens[breakpoint] && column[breakpoint] !== undefined) {
            return column[breakpoint] || defaultColumnMap[breakpoint];
          }
        }
      } // If the configuration is not an object, it is a number, return number


      if (typeof column === 'number') {
        return column;
      } // If it is an object, but no response is found, this happens only in the test.
      // Maybe there are some strange environments


      return 3;
    },
    renderRow: function renderRow(children, index, _ref, bordered, layout, colon) {
      var prefixCls = _ref.prefixCls;

      var renderCol = function renderCol(colItem, type, idx) {
        return _vue.createVNode(_Col.default, {
          "child": colItem,
          "bordered": bordered,
          "colon": colon,
          "type": type,
          "key": "".concat(type, "-").concat(colItem.key || idx),
          "colKey": "".concat(type, "-").concat(colItem.key || idx),
          "layout": layout
        }, null);
      };

      var cloneChildren = [];
      var cloneContentChildren = [];
      toArray(children).forEach(function (childrenItem, idx) {
        cloneChildren.push(renderCol(childrenItem, 'label', idx));

        if (layout === 'vertical') {
          cloneContentChildren.push(renderCol(childrenItem, 'content', idx));
        } else if (bordered) {
          cloneChildren.push(renderCol(childrenItem, 'content', idx));
        }
      });

      if (layout === 'vertical') {
        return [_vue.createVNode("tr", {
          "class": "".concat(prefixCls, "-row"),
          "key": "label-".concat(index)
        }, [cloneChildren]), _vue.createVNode("tr", {
          "class": "".concat(prefixCls, "-row"),
          "key": "content-".concat(index)
        }, [cloneContentChildren])];
      }

      return _vue.createVNode("tr", {
        "class": "".concat(prefixCls, "-row"),
        "key": index
      }, [cloneChildren]);
    }
  },
  mounted: function mounted() {
    var _this = this;

    var column = this.$props.column;
    this.token = _responsiveObserve.default.subscribe(function (screens) {
      if (_typeof(column) !== 'object') {
        return;
      }

      _this.setState({
        screens: screens
      });
    });
  },
  beforeUnmount: function beforeUnmount() {
    _responsiveObserve.default.unsubscribe(this.token);
  },
  render: function render() {
    var _ref2,
        _this2 = this;

    var _this$$props = this.$props,
        customizePrefixCls = _this$$props.prefixCls,
        size = _this$$props.size,
        _this$$props$bordered = _this$$props.bordered,
        bordered = _this$$props$bordered === void 0 ? false : _this$$props$bordered,
        _this$$props$layout = _this$$props.layout,
        layout = _this$$props$layout === void 0 ? 'horizontal' : _this$$props$layout,
        _this$$props$colon = _this$$props.colon,
        colon = _this$$props$colon === void 0 ? true : _this$$props$colon;
    var title = (0, _propsUtil.getComponent)(this, 'title');
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('descriptions', customizePrefixCls);
    var column = this.getColumn();
    var children = this.$slots.default && this.$slots.default();
    var cloneChildren = toArray(children).map(function (child) {
      if ((0, _propsUtil.isValidElement)(child)) {
        return (0, _vue.cloneVNode)(child, {
          prefixCls: prefixCls
        });
      }

      return null;
    }).filter(function (node) {
      return node;
    });
    var childrenArray = generateChildrenRows(cloneChildren, column);
    return _vue.createVNode("div", {
      "class": [prefixCls, (_ref2 = {}, _defineProperty(_ref2, "".concat(prefixCls, "-").concat(size), size !== 'default'), _defineProperty(_ref2, "".concat(prefixCls, "-bordered"), !!bordered), _ref2)]
    }, [title && _vue.createVNode("div", {
      "class": "".concat(prefixCls, "-title")
    }, [title]), _vue.createVNode("div", {
      "class": "".concat(prefixCls, "-view")
    }, [_vue.createVNode("table", null, [_vue.createVNode("tbody", null, [childrenArray.map(function (child, index) {
      return _this2.renderRow(child, index, {
        prefixCls: prefixCls
      }, bordered, layout, colon);
    })])])])]);
  }
};

Descriptions.install = function (app) {
  app.component(Descriptions.name, Descriptions);
  app.component(Descriptions.Item.name, Descriptions.Item);
};

var _default = Descriptions;
exports.default = _default;