import { __assign, __rest } from "tslib";
import React, { useContext } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import Row from './row';
var Descriptions = function (props) {
    var _a;
    var _b = props.size, size = _b === void 0 ? 'md' : _b, _c = props.bordered, bordered = _c === void 0 ? false : _c, _d = props.column, column = _d === void 0 ? 3 : _d, _e = props.layout, layout = _e === void 0 ? 'horizontal' : _e, title = props.title, className = props.className, children = props.children, customisedCls = props.prefixCls, otherProps = __rest(props, ["size", "bordered", "column", "layout", "title", "className", "children", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('descriptions', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className, prefixCls + "_" + size, (_a = {},
        _a[prefixCls + "_bordered"] = bordered,
        _a));
    var getRows = function () {
        var rows = [];
        var columns = null;
        var leftSpans;
        var numOfChildren = React.Children.count(children);
        React.Children.forEach(children, function (child, idx) {
            var childElement = child;
            if (childElement.type.displayName === 'DescriptionsItem') {
                var itemNode = childElement;
                if (!columns) {
                    leftSpans = column;
                    columns = [];
                    rows.push(columns);
                }
                if (idx === numOfChildren - 1) {
                    var props_1 = { span: leftSpans };
                    itemNode = React.cloneElement(childElement, props_1);
                }
                var _a = itemNode.props.span, span = _a === void 0 ? 1 : _a;
                columns.push(itemNode);
                leftSpans -= span;
                if (leftSpans <= 0) {
                    columns = null;
                }
            }
        });
        return rows;
    };
    var rows = getRows();
    var displayColon = 'colon' in props ? props.colon : !bordered;
    return (React.createElement("div", __assign({}, otherProps, { className: cls }),
        title && React.createElement("div", { className: prefixCls + "__title" }, title),
        React.createElement("div", { className: prefixCls + "__body" },
            React.createElement("table", null,
                React.createElement("tbody", null, rows.map(function (row, idx) { return (React.createElement(Row, { key: idx, index: idx, colon: displayColon, bordered: bordered, prefixCls: prefixCls, layout: layout, row: row })); }))))));
};
Descriptions.displayName = 'Descriptions';
export default Descriptions;
//# sourceMappingURL=descriptions.js.map