"use strict";

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

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

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

var _reactIntl = require("react-intl");

var _immutable = _interopRequireDefault(require("immutable"));

var _get = _interopRequireDefault(require("lodash/get"));

var _recordDataHelpers = require("../../helpers/recordDataHelpers");

var _configHelpers = require("../../helpers/configHelpers");

var _ValidationErrorMessage = _interopRequireDefault(require("../../../styles/cspace-ui/ValidationErrorMessage.css"));

var _errorCodes = require("../../constants/errorCodes");

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

const messages = (0, _reactIntl.defineMessages)({
  [_errorCodes.ERR_DATA_TYPE]: {
    "id": "validationErrorMessage.ERR_DATA_TYPE",
    "defaultMessage": "{dataType, select, DATA_TYPE_INT {{fieldName} must be an integer. Correct the value {value}.} DATA_TYPE_FLOAT {{fieldName} must be a number. Correct the value {value}.} DATA_TYPE_DATE {{fieldName} must be a date in the format YYYY-MM-DD. Correct the value {value}.} other {{fieldName} has an invalid value for the data type {dataType}. Correct the value {value}.}}"
  },
  [_errorCodes.ERR_MISSING_REQ_FIELD]: {
    "id": "validationErrorMessage.ERR_MISSING_REQ_FIELD",
    "defaultMessage": "{fieldName} is required. Please enter a value."
  },
  [_errorCodes.ERR_UNABLE_TO_VALIDATE]: {
    "id": "validationErrorMessage.ERR_UNABLE_TO_VALIDATE",
    "defaultMessage": "An unexpected error occurred while validating this record."
  },
  default: {
    "id": "validationErrorMessage.default",
    "defaultMessage": "{fieldName} has an invalid value."
  }
});
const propTypes = {
  errors: _propTypes.default.instanceOf(_immutable.default.Map),
  fieldDescriptor: _propTypes.default.objectOf(_propTypes.default.object)
};

const formatErrors = (fieldDescriptor, errors, path = []) => {
  const formattedErrors = [];
  errors.entrySeq().forEach(entry => {
    const [key, value] = entry;

    if (key === _recordDataHelpers.ERROR_KEY) {
      const id = path.join('.');
      const fieldDescriptorPath = (0, _configHelpers.dataPathToFieldDescriptorPath)(path);
      const fieldMessages = (0, _get.default)(fieldDescriptor, [...fieldDescriptorPath, _configHelpers.configKey, 'messages']);
      const fieldNameMessage = (0, _get.default)(fieldMessages, 'fullName') || (0, _get.default)(fieldMessages, 'name');
      const fieldName = fieldNameMessage ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, fieldNameMessage) : path[path.length - 1];
      const error = value;

      if (error) {
        const errorMessage = error.get('message') || messages[error.get('code')] || messages.default;
        const values = error.set('fieldName', fieldName).toJS();
        const className = error.get('nonblocking') ? _ValidationErrorMessage.default.nonblocking : undefined;

        const formattedMessage = /*#__PURE__*/_react.default.createElement("li", {
          className: className,
          key: id
        }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, errorMessage, {
          values: values
        })));

        formattedErrors.push(formattedMessage);
      }
    } else if (value) {
      formattedErrors.push(...formatErrors(fieldDescriptor, value, [...path, key]));
    }
  });
  return formattedErrors;
};

function ValidationErrorMessage(props) {
  const {
    errors,
    fieldDescriptor
  } = props;

  if (!errors) {
    return null;
  }

  const formattedErrors = formatErrors(fieldDescriptor, errors);
  return /*#__PURE__*/_react.default.createElement("ul", null, formattedErrors);
}

ValidationErrorMessage.propTypes = propTypes;