"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useFormField = useFormField;

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

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

var _context = require("./context");

var _formValidation = require("./formValidation");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function useFormField(props) {
  var _React$useContext = _react["default"].useContext(_context.FormContext),
      getFieldValue = _React$useContext.getFieldValue,
      onChange = _React$useContext.onChange,
      registerField = _React$useContext.registerField,
      getFormFieldError = _React$useContext.getFormFieldError,
      fields = _React$useContext.fields,
      deleteFormFieldError = _React$useContext.deleteFormFieldError;

  var errors = getFormFieldError && getFormFieldError(props.name);

  var onValidate = function onValidate(_ref) {
    var isSubmitValid = _ref.isValid;

    if (isSubmitValid) {
      deleteFormFieldError(props.name);
    }
  }; // Register the form field into Form Validator


  (0, _react.useEffect)(function () {
    if (props.schema) {
      registerField(props);
    }
  }, [props.schema]);

  var $onChange = _react["default"].useCallback(function (e) {
    var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

    if (onChange) {
      var newValue = value || typeof value === 'boolean' ? value : e && e.target && e.target.value;
      onChange({
        value: newValue,
        name: props.name
      });
      var formValue = (0, _defineProperty2["default"])({}, props.name, newValue); // setTimeout((): void => {
      // need to run validation *after* the update from field change
      // since that can potentially change schema
      // if anyone has better ideas i'm all ears - tushar

      (0, _formValidation.validate)(formValue, fields, onValidate); // }, 0);
    }
  }, [props.name, errors]);

  return {
    onChange: $onChange,
    value: getFieldValue && getFieldValue(props.name),
    errors: errors
  };
}
//# sourceMappingURL=useFormField.js.map