'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 _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _pathHelpers = require('../helpers/pathHelpers');

var _inputHelpers = require('../helpers/inputHelpers');

var _CompoundInput = require('../../styles/cspace-input/CompoundInput.css');

var _CompoundInput2 = _interopRequireDefault(_CompoundInput);

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 propTypes = {
  children: _react.PropTypes.node,
  className: _react.PropTypes.string,
  defaultChildSubpath: _pathHelpers.pathPropType,
  name: _react.PropTypes.string,
  parentPath: _pathHelpers.pathPropType, // eslint-disable-line react/no-unused-prop-types
  subpath: _pathHelpers.pathPropType, // eslint-disable-line react/no-unused-prop-types
  value: _react.PropTypes.oneOfType([_react.PropTypes.object, // eslint-disable-line react/forbid-prop-types
  _react.PropTypes.instanceOf(_immutable2.default.Map)])
};

var defaultProps = {
  value: {}
};

var getChildValue = function getChildValue(value, subpath, name) {
  var keyPath = [];

  if (subpath) {
    keyPath = keyPath.concat(subpath);
  }

  if (name) {
    keyPath = keyPath.concat(name);
  }

  if (keyPath.length === 0) {
    return value;
  }

  if (_immutable2.default.Map.isMap(value)) {
    return value.getIn(keyPath);
  }

  return (0, _get2.default)(value, keyPath);
};

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

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

    return _possibleConstructorReturn(this, (CustomCompoundInput.__proto__ || Object.getPrototypeOf(CustomCompoundInput)).apply(this, arguments));
  }

  _createClass(CustomCompoundInput, [{
    key: 'decorateInputs',
    value: function decorateInputs(children) {
      var _this2 = this;

      return _react2.default.Children.map(children, function (child) {
        if (!child.type) {
          // Text node. Just return it.
          return child;
        }

        if ((0, _inputHelpers.isInput)(child)) {
          var name = child.props.name;
          var subpath = child.props.subpath;
          var _props = _this2.props,
              defaultChildSubpath = _props.defaultChildSubpath,
              value = _props.value;


          if (typeof subpath === 'undefined') {
            subpath = defaultChildSubpath;
          }

          return _react2.default.cloneElement(child, {
            subpath: subpath,
            parentPath: (0, _pathHelpers.getPath)(_this2.props),
            value: getChildValue(value, subpath, name)
          });
        }

        return _react2.default.cloneElement(child, {
          children: _this2.decorateInputs(child.props.children)
        });
      }, this);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          children = _props2.children,
          className = _props2.className,
          name = _props2.name;


      var classes = (0, _classnames2.default)(className, _CompoundInput2.default.common);

      return _react2.default.createElement(
        'fieldset',
        {
          className: classes,
          name: name
        },
        this.decorateInputs(children)
      );
    }
  }]);

  return CustomCompoundInput;
}(_react.Component);

exports.default = CustomCompoundInput;


CustomCompoundInput.propTypes = propTypes;
CustomCompoundInput.defaultProps = defaultProps;