'use strict';

import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import _ from 'lodash';
import { FormErrorAutoFocusContainer } from './FormErrorAutoFocusContainer';
export var Form =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(Form, _React$Component);

  function Form(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;

    _defineProperty(_assertThisInitialized(_this), "getNeededAutoFocus", function () {
      return _this.formErrorAutoFocusContainer.getNeededAutoFocus();
    });

    _defineProperty(_assertThisInitialized(_this), "setNeededAutoFocus", function (value) {
      _this.formErrorAutoFocusContainer.setNeededAutoFocus(value);
    });

    _this.setValue = _this.setOutputValue.bind(_assertThisInitialized(_this));
    return _this;
  }

  var _proto = Form.prototype;

  _proto.getChildContext = function getChildContext() {
    return {
      form: {
        readOnly: this.extractFlatProperty('readOnly'),
        value: this.getInputValue(),
        errors: this.extractProperty('errors'),
        setValue: this.setValue
      }
    };
  };

  _proto.getInputValue = function getInputValue() {
    return this.inputConverter(this.extractProperty('value'));
  };

  _proto.setOutputValue = function setOutputValue(value, diff) {
    if (diff === void 0) {
      diff = value;
    }

    value = this.outputConverter(_.extend({}, this.getInputValue(), value), diff);

    if (this.props.onChange) {
      this.props.onChange(value, diff);
    } else if (this.props.name && this.context.form) {
      var _this$context$form$se, _this$context$form$se2;

      this.context.form.setValue((_this$context$form$se = {}, _this$context$form$se[this.props.name] = value, _this$context$form$se), (_this$context$form$se2 = {}, _this$context$form$se2[this.props.name] = diff, _this$context$form$se2));
    }
  };

  _proto.inputConverter = function inputConverter(value) {
    return this.props.inputConverter ? this.props.inputConverter.call(this, value) : value;
  };

  _proto.outputConverter = function outputConverter(value, diff) {
    return this.props.outputConverter ? this.props.outputConverter.call(this, value, diff) : value;
  };

  _proto.extractProperty = function extractProperty(propName, props) {
    if (props === void 0) {
      props = this.props;
    }

    var value = props[propName];
    var form = this.context.form;

    if (form && value === undefined) {
      value = form[propName][props.name];
    }

    return value || {};
  };

  _proto.extractFlatProperty = function extractFlatProperty(propName) {
    var value = this.props[propName];
    var form = this.context.form;

    if (form && value === undefined) {
      value = form[propName];
    }

    return value;
  };

  _proto.render = function render() {
    var _this2 = this;

    return React.createElement(FormErrorAutoFocusContainer, {
      ref: function ref(el) {
        return _this2.formErrorAutoFocusContainer = el;
      }
    }, this.props.noContainer ? this.props.children : React.createElement("div", {
      "data-test-automation-id": this.props.testAutomationId,
      className: classnames('rc-form', this.props.className)
    }, this.props.children));
  };

  return Form;
}(React.Component);

_defineProperty(Form, "childContextTypes", {
  form: PropTypes.object
});

_defineProperty(Form, "contextTypes", {
  form: PropTypes.object
});

_defineProperty(Form, "propTypes", {
  onChange: PropTypes.func,
  name: PropTypes.string,
  inputConverter: PropTypes.func,
  outputConverter: PropTypes.func,
  noContainer: PropTypes.bool,
  testAutomationId: PropTypes.string,
  children: PropTypes.node,
  className: PropTypes.string
});

_defineProperty(Form, "defaultProps", {
  onChange: null,
  name: '',
  inputConverter: null,
  outputConverter: null,
  children: null,
  className: null,
  testAutomationId: 'form',
  noContainer: false
});