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

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { FormContext } from '../Input/context';

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

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

Form.propTypes = process.env.NODE_ENV !== "production" ? {
  children: PropTypes.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 React.createElement(
      FormContext.Provider,
      { value: this.state },
      this.props.children
    );
  };

  return FormProvider;
}(Component);

FormProvider.defaultProps = {
  isActive: true
};

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

FormProvider.contextType = FormContext;
export { FormProvider };
export default Form;