"use strict";

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

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

var _FormContext = require("./FormContext");

var _immer = _interopRequireDefault(require("immer"));

var _set = _interopRequireDefault(require("lodash/set"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

/**
 * Generic form input field component that handles simple use cases,
 * where the data displayed in the field and updated by the user maps
 * directly to the necessary updates to form data state.
 *
 * For complex use cases (e.g. adding or removing array element),
 * custom form field components should be created
 */
// TData must extend object for lodash set.
// TComponentProps must extend {} for React.
var FormField = function FormField(_ref) {
  var defaultValue = _ref.defaultValue,
      getValue = _ref.getValue,
      preprocessForDisplay = _ref.preprocessForDisplay,
      _ref$parseOnChangeEve = _ref.parseOnChangeEvent,
      parseOnChangeEvent = _ref$parseOnChangeEve === void 0 ? function (e) {
    return e.target.value;
  } : _ref$parseOnChangeEve,
      _ref$status = _ref.status,
      status = _ref$status === void 0 ? function () {
    return undefined;
  } : _ref$status,
      InputComponent = _ref.inputComponent,
      children = _ref.children,
      props = _objectWithoutProperties(_ref, ["defaultValue", "getValue", "preprocessForDisplay", "parseOnChangeEvent", "status", "inputComponent", "children"]);

  var _useGenericContext = (0, _FormContext.useGenericContext)(_FormContext.FormContext),
      data = _useGenericContext.data,
      dataDriller = _useGenericContext.dataDriller,
      updateData = _useGenericContext.updateData,
      hideStatus = _useGenericContext.hideStatus;

  var accessor = getValue(dataDriller);
  var value = accessor.value || defaultValue;
  var updatePath = accessor.path;

  var onChange = function onChange(e) {
    var processedData = parseOnChangeEvent(e, dataDriller);
    updateData((0, _immer.default)(data, function (draft) {
      return (0, _set.default)(draft, updatePath, processedData);
    }));
  };

  return /*#__PURE__*/_react.default.createElement(InputComponent, _extends({
    defaultValue: preprocessForDisplay ? preprocessForDisplay(value) : value,
    onChange: onChange,
    status: hideStatus ? undefined : status(dataDriller, updatePath, props)
  }, props), children);
};

exports.FormField = FormField;