'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

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) {
  _inherits(FlowMultiField, _React$Component);

  function FlowMultiField() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, FlowMultiField);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = FlowMultiField.__proto__ || Object.getPrototypeOf(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), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(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;