'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.Form = void 0;

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _lodash = _interopRequireDefault(require("lodash"));

var _FormErrorAutoFocusContainer = require("./FormErrorAutoFocusContainer");

var Form =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(Form, _React$Component);

  function Form(props, context) {
    var _this;

    _this = _React$Component.call(this, props, context) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getNeededAutoFocus", function () {
      return _this.formErrorAutoFocusContainer.getNeededAutoFocus();
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setNeededAutoFocus", function (value) {
      _this.formErrorAutoFocusContainer.setNeededAutoFocus(value);
    });
    _this.setValue = _this.setOutputValue.bind((0, _assertThisInitialized2.default)(_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(_lodash.default.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.default.createElement(_FormErrorAutoFocusContainer.FormErrorAutoFocusContainer, {
      ref: function ref(el) {
        return _this2.formErrorAutoFocusContainer = el;
      }
    }, this.props.noContainer ? this.props.children : _react.default.createElement("div", {
      "data-test-automation-id": this.props.testAutomationId,
      className: (0, _classnames.default)('rc-form', this.props.className)
    }, this.props.children));
  };

  return Form;
}(_react.default.Component);

exports.Form = Form;
(0, _defineProperty2.default)(Form, "childContextTypes", {
  form: _propTypes.default.object
});
(0, _defineProperty2.default)(Form, "contextTypes", {
  form: _propTypes.default.object
});
(0, _defineProperty2.default)(Form, "propTypes", {
  onChange: _propTypes.default.func,
  name: _propTypes.default.string,
  inputConverter: _propTypes.default.func,
  outputConverter: _propTypes.default.func,
  noContainer: _propTypes.default.bool,
  testAutomationId: _propTypes.default.string,
  children: _propTypes.default.node,
  className: _propTypes.default.string
});
(0, _defineProperty2.default)(Form, "defaultProps", {
  onChange: null,
  name: '',
  inputConverter: null,
  outputConverter: null,
  children: null,
  className: null,
  testAutomationId: 'form',
  noContainer: false
});