"use strict";

exports.__esModule = true;
exports.extractStandardErrorMessagesFromResponse = void 0;
var _propertyBagHelper = require("./propertyBagHelper");
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var parseFailureContext = function parseFailureContext(context, formatMessage, formatDate, formatTime) {
  if (!context) {
    return context;
  }
  var newContext = {};
  Object.keys(context).forEach(function (key) {
    newContext[key] = (0, _propertyBagHelper.getPropertyBagFormattedPrimitiveValue)(context[key], formatMessage, formatDate, formatTime);
  });
  return newContext;
};
var extractStandardErrorMessagesFromResponse = exports.extractStandardErrorMessagesFromResponse = function extractStandardErrorMessagesFromResponse(response, validationLookupModule, validationLookupName, formatMessage, formatDate, formatTime, lookupKeyCustomizer) {
  var hasErrors = false;
  var messages = [];
  if (response != null && response.error) {
    var _response$payload, _response$payload3;
    hasErrors = true;
    if (((_response$payload = response.payload) == null ? void 0 : _response$payload.status) === 422) {
      var _response$payload2;
      if ((_response$payload2 = response.payload) != null && (_response$payload2 = _response$payload2.response) != null && _response$payload2.failures) {
        // uses structure from our .Net ValidationFailuresExceptionHandler
        response.payload.response.failures.forEach(function (failure) {
          if (failure.errorCode) {
            messages.push({
              message: failure.errorMessage,
              lookupModule: validationLookupModule,
              lookupName: validationLookupName,
              lookupKey: lookupKeyCustomizer ? lookupKeyCustomizer(failure.errorCode) : failure.errorCode,
              lookupReplacementValues: parseFailureContext(failure.context, formatMessage, formatDate, formatTime)
            });
          }
        });
      }
    } else if (((_response$payload3 = response.payload) == null ? void 0 : _response$payload3.status) === 500) {
      var _response$payload4;
      if ((_response$payload4 = response.payload) != null && (_response$payload4 = _response$payload4.response) != null && _response$payload4.errors) {
        // uses structure from our .Net OrckestraExceptionErrorHandler
        response.payload.response.errors.forEach(function (err) {
          messages.push({
            message: err.message,
            lookupModule: err.lookupModule,
            lookupName: err.lookupName,
            lookupKey: lookupKeyCustomizer ? lookupKeyCustomizer(err.lookupKey) : err.lookupKey,
            lookupReplacementValues: err.lookupReplacementValues
          });
        });
      }
    }
  }
  return {
    hasErrors: hasErrors,
    messages: messages
  };
};
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(parseFailureContext, "parseFailureContext", "/home/vsts/work/1/s/src/utils/responseProcessingHelper.js");
  reactHotLoader.register(extractStandardErrorMessagesFromResponse, "extractStandardErrorMessagesFromResponse", "/home/vsts/work/1/s/src/utils/responseProcessingHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(parseFailureContext, "parseFailureContext", "/home/vsts/work/1/s/src/utils/responseProcessingHelper.js");
  reactHotLoader.register(extractStandardErrorMessagesFromResponse, "extractStandardErrorMessagesFromResponse", "/home/vsts/work/1/s/src/utils/responseProcessingHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();