'use strict';

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

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 _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _Anchor = require('../../../components/Anchor');

var _Anchor2 = _interopRequireDefault(_Anchor);

var _FlowItem = require('../FlowItem');

var _FlowItem2 = _interopRequireDefault(_FlowItem);

var _themeGet = require('../../../extensions/themeGet');

var _themeGet2 = _interopRequireDefault(_themeGet);

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

var List = _styledComponents2.default.ul.withConfig({
  displayName: 'FlowMulti__List'
})(['appearance:none;border:1px solid #e1e1e1;width:100%;padding:2px;']);

var SubFieldContainer = _styledComponents2.default.li.withConfig({
  displayName: 'FlowMulti__SubFieldContainer'
})(['display:flex;flex-direction:row;margin:', ';& > *{flex:1;}& > *:last-child{flex-grow:0;flex-basis:auto;flex-shrink:0;margin-left:8px;}& label{display:none;}'], (0, _themeGet2.default)('spacing.small'));

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

  function FlowMultiField() {
    var _ref;

    var _temp, _this, _ret;

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

    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 = FlowMultiField.__proto__ || (0, _getPrototypeOf2.default)(FlowMultiField)).call.apply(_ref, [this].concat(args))), _this), _this.renderSubField = function (name, index, fields) {
      return _react2.default.createElement(
        SubFieldContainer,
        { key: name },
        _this.props.renderField(name, index),
        _react2.default.createElement(
          _Anchor2.default,
          { onClick: function onClick() {
              return fields.remove(index);
            } },
          'remove'
        )
      );
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(FlowMultiField, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          label = _props.label,
          helpText = _props.helpText,
          required = _props.required,
          id = _props.id,
          className = _props.className,
          fields = _props.fields;

      return _react2.default.createElement(
        _FlowItem2.default,
        {
          label: label,
          helpText: helpText,
          required: required,
          id: id,
          className: className,
          flexibleContent: true
        },
        _react2.default.createElement(
          List,
          null,
          fields.map(this.renderSubField)
        ),
        _react2.default.createElement(
          _Anchor2.default,
          { onClick: function onClick() {
              return fields.push('');
            } },
          'add'
        )
      );
    }
  }]);
  return FlowMultiField;
}(_react2.default.Component);

FlowMultiField.propTypes = {
  /**
   * redux-form defined fields object.
   */
  fields: _propTypes2.default.shape({
    name: _propTypes2.default.string,
    forEach: _propTypes2.default.func,
    get: _propTypes2.default.func,
    getAll: _propTypes2.default.func,
    insert: _propTypes2.default.func,
    length: _propTypes2.default.number,
    map: _propTypes2.default.func,
    move: _propTypes2.default.func,
    pop: _propTypes2.default.func,
    push: _propTypes2.default.func,
    remove: _propTypes2.default.func,
    removeAll: _propTypes2.default.func,
    shift: _propTypes2.default.func,
    swap: _propTypes2.default.func,
    unshift: _propTypes2.default.func
  }).isRequired,
  /**
   * Function which renders a field. Function signature is (name, index) => field
   */
  renderField: _propTypes2.default.func.isRequired,
  /**
   * Contents of the label above the fields.
   */
  label: _propTypes2.default.string,
  /**
   * Contents of the help text below the fields.
   */
  helpText: _propTypes2.default.string,
  /**
   * Indicates whether this value is required for form submission.
   */
  required: _propTypes2.default.bool,
  /**
   * Adds an id to the container element.
   */
  id: _propTypes2.default.string,
  /**
   * Adds a class name to the container element.
   */
  className: _propTypes2.default.string
};
FlowMultiField.defaultProps = {
  label: null,
  helpText: null,
  required: false
};


FlowMultiField.List = List;
FlowMultiField.SubFieldContainer = SubFieldContainer;

exports.default = FlowMultiField;