'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _spin = require('../spin');

var _spin2 = _interopRequireDefault(_spin);

var _pagination = require('../pagination');

var _pagination2 = _interopRequireDefault(_pagination);

var _grid = require('../grid');

var _Item = require('./Item');

var _Item2 = _interopRequireDefault(_Item);

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

var __rest = undefined && undefined.__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) t[p[i]] = s[p[i]];
    }return t;
};

var List = function (_Component) {
    (0, _inherits3['default'])(List, _Component);

    function List() {
        (0, _classCallCheck3['default'])(this, List);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (List.__proto__ || Object.getPrototypeOf(List)).apply(this, arguments));

        _this.keys = {};
        _this.renderItem = function (item, index) {
            var _this$props = _this.props,
                dataSource = _this$props.dataSource,
                renderItem = _this$props.renderItem,
                rowKey = _this$props.rowKey;

            var key = void 0;
            if (typeof rowKey === 'function') {
                key = rowKey(dataSource[index]);
            } else if (typeof rowKey === 'string') {
                key = dataSource[rowKey];
            } else {
                key = dataSource.key;
            }
            if (!key) {
                key = 'list-item-' + index;
            }
            _this.keys[index] = key;
            return renderItem(item, index);
        };
        return _this;
    }

    (0, _createClass3['default'])(List, [{
        key: 'getChildContext',
        value: function getChildContext() {
            return {
                grid: this.props.grid
            };
        }
    }, {
        key: 'render',
        value: function render() {
            var _classNames,
                _this2 = this;

            var _a = this.props,
                _a$bordered = _a.bordered,
                bordered = _a$bordered === undefined ? false : _a$bordered,
                _a$split = _a.split,
                split = _a$split === undefined ? true : _a$split,
                className = _a.className,
                children = _a.children,
                _a$loading = _a.loading,
                loading = _a$loading === undefined ? false : _a$loading,
                itemLayout = _a.itemLayout,
                loadMore = _a.loadMore,
                _a$pagination = _a.pagination,
                pagination = _a$pagination === undefined ? false : _a$pagination,
                _a$prefixCls = _a.prefixCls,
                prefixCls = _a$prefixCls === undefined ? 'ant-list' : _a$prefixCls,
                grid = _a.grid,
                _a$dataSource = _a.dataSource,
                dataSource = _a$dataSource === undefined ? [] : _a$dataSource,
                size = _a.size,
                rowKey = _a.rowKey,
                renderItem = _a.renderItem,
                header = _a.header,
                footer = _a.footer,
                rest = __rest(_a, ["bordered", "split", "className", "children", "loading", "itemLayout", "loadMore", "pagination", "prefixCls", "grid", "dataSource", "size", "rowKey", "renderItem", "header", "footer"]);
            // large => lg
            // small => sm
            var sizeCls = '';
            switch (size) {
                case 'large':
                    sizeCls = 'lg';
                    break;
                case 'small':
                    sizeCls = 'sm';
                default:
                    break;
            }
            var classString = (0, _classnames2['default'])(prefixCls, className, (_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls + '-vertical', itemLayout === 'vertical'), (0, _defineProperty3['default'])(_classNames, prefixCls + '-' + sizeCls, sizeCls), (0, _defineProperty3['default'])(_classNames, prefixCls + '-split', split), (0, _defineProperty3['default'])(_classNames, prefixCls + '-bordered', bordered), (0, _defineProperty3['default'])(_classNames, prefixCls + '-loading', loading), (0, _defineProperty3['default'])(_classNames, prefixCls + '-grid', grid), _classNames));
            var paginationContent = _react2['default'].createElement(
                'div',
                { className: prefixCls + '-pagination' },
                _react2['default'].createElement(_pagination2['default'], pagination)
            );
            var childrenList = _react2['default'].Children.map(dataSource.map(function (item, index) {
                return _this2.renderItem(item, index);
            }), function (child, index) {
                return _react2['default'].cloneElement(child, {
                    key: _this2.keys[index]
                });
            });
            var childrenContent = grid ? _react2['default'].createElement(
                _grid.Row,
                { gutter: grid.gutter },
                childrenList
            ) : childrenList;
            var content = loading ? _react2['default'].createElement(
                'div',
                null,
                _react2['default'].createElement(
                    _spin2['default'],
                    null,
                    childrenContent
                ),
                loadMore,
                !loadMore && pagination && paginationContent
            ) : _react2['default'].createElement(
                'div',
                null,
                childrenContent,
                loadMore,
                !loadMore && pagination && paginationContent
            );
            return _react2['default'].createElement(
                'div',
                (0, _extends3['default'])({ className: classString }, rest),
                header && _react2['default'].createElement(
                    'div',
                    { className: prefixCls + '-header' },
                    header
                ),
                content,
                children,
                footer && _react2['default'].createElement(
                    'div',
                    { className: prefixCls + '-footer' },
                    footer
                )
            );
        }
    }]);
    return List;
}(_react.Component);

exports['default'] = List;

List.Item = _Item2['default'];
List.childContextTypes = {
    grid: _propTypes2['default'].any
};
module.exports = exports['default'];