import * as _vue from "vue";

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); }

import { inject, cloneVNode } from 'vue';
import warning from '../_util/warning';
import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve';
import { ConfigConsumerProps } from '../config-provider';
import Col from './Col';
import PropTypes from '../_util/vue-types';
import { initDefaultProps, getOptionProps, getComponent, isValidElement } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
export var DescriptionsItemProps = {
  prefixCls: PropTypes.string,
  label: PropTypes.any,
  span: PropTypes.number
};

function toArray(value) {
  var ret = value;

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

  return ret;
}

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

var generateChildrenRows = function generateChildrenRows(children, column) {
  var rows = [];
  var columns = null;
  var leftSpans;
  var itemNodes = toArray(children);
  itemNodes.forEach(function (node, index) {
    var itemProps = 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 = 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;
      warning(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],
  props: initDefaultProps(DescriptionsProps, {
    column: defaultColumnMap
  }),
  setup: function setup() {
    return {
      configProvider: inject('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 < responsiveArray.length; i++) {
          var breakpoint = 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, {
          "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.subscribe(function (screens) {
      if (_typeof(column) !== 'object') {
        return;
      }

      _this.setState({
        screens: screens
      });
    });
  },
  beforeUnmount: function beforeUnmount() {
    ResponsiveObserve.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 = 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 (isValidElement(child)) {
        return 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);
};

export default Descriptions;