'use strict';

exports.__esModule = true;
exports.FormProvider = undefined;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _context = require('../Input/context');

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

/* eslint-disable react/no-unused-state */

var Form = function Form(props) {
  return _react2.default.createElement(
    _context.FormContext.Consumer,
    null,
    function (formContext) {
      return _react2.default.createElement(
        'form',
        { className: 'ma__form-page', action: '#' },
        props.children(formContext)
      );
    }
  );
};

Form.propTypes = process.env.NODE_ENV !== "production" ? {
  children: _propTypes2.default.node
} : {};

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

  function FormProvider(props) {
    _classCallCheck(this, FormProvider);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.getValues = function () {
      var values = {};
      Object.keys(_this.state.value).forEach(function (inputId) {
        values[inputId] = _this.getValue(inputId);
      });
      return values;
    };

    _this.getValue = function (inputId) {
      if (_this.hasId(inputId)) {
        return _this.state.value[inputId].getValue();
      }
      return null;
    };

    _this.setValue = function (input, afterUpdate) {
      if (Object.prototype.hasOwnProperty.call(_this.state.value, input.id)) {
        _this.state.value[input.id].setValue(input.value, afterUpdate);
      }
    };

    _this.hasId = function (inputId) {
      return Object.prototype.hasOwnProperty.call(_this.state.value, inputId);
    };

    _this.updateState = function (newState) {
      _this.setState(newState);
    };

    _this.state = {
      isActive: _this.props.isActive,
      value: {},
      getValue: _this.getValue,
      hasId: _this.hasId,
      setValue: _this.setValue,
      updateState: _this.updateState,
      getValues: _this.getValues
    };
    return _this;
  }

  FormProvider.prototype.render = function render() {
    return _react2.default.createElement(
      _context.FormContext.Provider,
      { value: this.state },
      this.props.children
    );
  };

  return FormProvider;
}(_react.Component);

FormProvider.defaultProps = {
  isActive: true
};

FormProvider.propTypes = process.env.NODE_ENV !== "production" ? {
  /** Controls if child Input components should hook into FormContext or not */
  isActive: _propTypes2.default.bool,
  children: _propTypes2.default.node
} : {};

FormProvider.contextType = _context.FormContext;
exports.FormProvider = FormProvider;
exports.default = Form;