'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Card = require('./Card');

var _Card2 = _interopRequireDefault(_Card);

var _Icon = require('./Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _SectionTitle = require('./SectionTitle');

var _SectionTitle2 = _interopRequireDefault(_SectionTitle);

var _SectionBody = require('./SectionBody');

var _SectionBody2 = _interopRequireDefault(_SectionBody);

var _Banner = require('./Banner');

var _Banner2 = _interopRequireDefault(_Banner);

var _SectionFooter = require('./SectionFooter');

var _SectionFooter2 = _interopRequireDefault(_SectionFooter);

var _Section = require('./Section');

var _Section2 = _interopRequireDefault(_Section);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var MAX_ITEMS_BY_ROW = 4;

var propTypes = {
  /** @ignore */
  className: _propTypes2.default.bool,
  /** @ignore */
  children: _propTypes2.default.node,
  /**
  * Max number of card per row
  */
  itemsPerRow: _propTypes2.default.number,
  horizontal: _propTypes2.default.bool
};

var defaultProps = {
  className: undefined,
  children: null,
  itemsPerRow: null,
  horizontal: false
};

var Cards = function Cards(_ref) {
  var className = _ref.className,
      children = _ref.children,
      itemsPerRow = _ref.itemsPerRow,
      horizontal = _ref.horizontal,
      rest = _objectWithoutProperties(_ref, ['className', 'children', 'itemsPerRow', 'horizontal']);

  var childrenArray = _react.Children.toArray(children);
  var cleanChildren = childrenArray.filter(function (child) {
    return (0, _react.isValidElement)(child);
  });
  var resultItemsPerRow = itemsPerRow ? Math.min(itemsPerRow, MAX_ITEMS_BY_ROW) : Math.min(cleanChildren.length, MAX_ITEMS_BY_ROW);
  var resultChildren = childrenArray.map(function (child, row) {
    var componentName = child.type.componentName;

    if (!(0, _react.isValidElement)(child)) {
      return child;
    }
    switch (componentName) {
      case 'Card':
        return (0, _react.cloneElement)(child, { itemsPerRow: resultItemsPerRow, key: 'item-' + row, horizontal: horizontal });
      default:
        return child;
    }
  });

  return _react2.default.createElement(
    'div',
    _extends({ className: (className ? className + ' ' : '') + 'flive_cards_container' }, rest),
    resultChildren
  );
};

Cards.propTypes = propTypes;
Cards.defaultProps = defaultProps;

Cards.Card = _Card2.default;
Cards.Icon = _Icon2.default;
Cards.Banner = _Banner2.default;
Cards.Section = _Section2.default;
Cards.SectionTitle = _SectionTitle2.default;
Cards.SectionBody = _SectionBody2.default;
Cards.SectionFooter = _SectionFooter2.default;
exports.default = Cards;
module.exports = exports['default'];