import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__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;
};
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Spin from '../spin';
import Pagination from '../pagination';
import { Row } from '../grid';
import Item from './Item';

var List = function (_Component) {
    _inherits(List, _Component);

    function List() {
        _classCallCheck(this, List);

        var _this = _possibleConstructorReturn(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;
    }

    _createClass(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 = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-vertical', itemLayout === 'vertical'), _defineProperty(_classNames, prefixCls + '-' + sizeCls, sizeCls), _defineProperty(_classNames, prefixCls + '-split', split), _defineProperty(_classNames, prefixCls + '-bordered', bordered), _defineProperty(_classNames, prefixCls + '-loading', loading), _defineProperty(_classNames, prefixCls + '-grid', grid), _classNames));
            var paginationContent = React.createElement(
                'div',
                { className: prefixCls + '-pagination' },
                React.createElement(Pagination, pagination)
            );
            var childrenList = React.Children.map(dataSource.map(function (item, index) {
                return _this2.renderItem(item, index);
            }), function (child, index) {
                return React.cloneElement(child, {
                    key: _this2.keys[index]
                });
            });
            var childrenContent = grid ? React.createElement(
                Row,
                { gutter: grid.gutter },
                childrenList
            ) : childrenList;
            var content = loading ? React.createElement(
                'div',
                null,
                React.createElement(
                    Spin,
                    null,
                    childrenContent
                ),
                loadMore,
                !loadMore && pagination && paginationContent
            ) : React.createElement(
                'div',
                null,
                childrenContent,
                loadMore,
                !loadMore && pagination && paginationContent
            );
            return React.createElement(
                'div',
                _extends({ className: classString }, rest),
                header && React.createElement(
                    'div',
                    { className: prefixCls + '-header' },
                    header
                ),
                content,
                children,
                footer && React.createElement(
                    'div',
                    { className: prefixCls + '-footer' },
                    footer
                )
            );
        }
    }]);

    return List;
}(Component);

export default List;

List.Item = Item;
List.childContextTypes = {
    grid: PropTypes.any
};