'use strict';

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

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

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 _FieldRowContainer = require('./styles/FieldRowContainer');

var _FieldRowContainer2 = _interopRequireDefault(_FieldRowContainer);

var _FieldRow = require('./styles/FieldRow');

var _FieldRow2 = _interopRequireDefault(_FieldRow);

var _Field = require('./Field');

var _Field2 = _interopRequireDefault(_Field);

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

var Fields = function (_React$Component) {
  (0, _inherits3.default)(Fields, _React$Component);

  function Fields() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Fields);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Fields.__proto__ || (0, _getPrototypeOf2.default)(Fields)).call.apply(_ref, [this].concat(args))), _this), _this.partitionFields = function () {
      var _this$props = _this.props,
          columns = _this$props.columns,
          children = _this$props.children,
          Row = _this$props.Row;

      var childArray = _react2.default.Children.toArray(children);

      var rows = childArray.reduce(function (rows, child) {
        var childColumnSpan = child.props.columnSpan || 1;

        if (childColumnSpan > columns) {
          throw new Error('A Field has a columnSpan of ' + childColumnSpan + ', but the containing Fields only has ' + columns + ' columns');
        }

        var currentRow = rows[rows.length - 1];
        if (currentRow.columnWidth + childColumnSpan <= columns) {
          // add to current row
          return [].concat((0, _toConsumableArray3.default)(rows.slice(0, rows.length - 1)), [{
            children: [].concat((0, _toConsumableArray3.default)(currentRow.children), [{
              column: currentRow.columnWidth,
              element: child
            }]),
            columnWidth: currentRow.columnWidth + childColumnSpan
          }]);
        } else {
          // create new row
          return [].concat((0, _toConsumableArray3.default)(rows), [{
            children: [{ column: 0, element: child }],
            columnWidth: childColumnSpan
          }]);
        }
      }, [{ children: [], columnWidth: 0 }]);

      return rows.map(function (row, rowIdx) {
        return _react2.default.createElement(
          Row,
          { key: rowIdx, columns: columns },
          row.children.map(function (_ref2) {
            var element = _ref2.element,
                column = _ref2.column;
            return _react2.default.cloneElement(element, { column: column });
          })
        );
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Fields, [{
    key: 'render',
    value: function render() {
      var RowContainer = this.props.RowContainer;


      return _react2.default.createElement(
        RowContainer,
        null,
        this.partitionFields()
      );
    }
  }]);
  return Fields;
}(_react2.default.Component);

Fields.Field = _Field2.default;
Fields.propTypes = {
  /**
   * The number of columns to divide the fieldset into when rendering. Fields can span multiple columns if provided
   * with a columnSpan prop. Fields which overflow the column limit will be added to new rows. If a Field's
   * columnSpan is too large to fit in the current row, it will be wrapped to the next one.
   */
  columns: _propTypes2.default.number,
  /**
   * A component prop to override the component used to render the outer container which renders field rows.
   * Defaults to FieldRowContainer.
   */
  RowContainer: _propTypes2.default.func,
  /**
   * A component prop to override the component which is injected to wrap Field elements into discrete rows.
   * Row receives the same `columns` prop provided to this component. Defaults to FieldRow.
   */
  Row: _propTypes2.default.func
};
Fields.defaultProps = {
  RowContainer: _FieldRowContainer2.default,
  Row: _FieldRow2.default,
  columns: 2
};
exports.default = Fields;