import _extends from "@babel/runtime/helpers/extends";

/* eslint-disable prefer-const */
var alwaysRequiredField = ['firstName', 'lastName', 'email', 'phoneNumber'];

var genericAddressValidation = (data, mod, errorLabels) => {
  var _mod$moduleData, _data$additionalAddre;

  var address = mod == null ? void 0 : (_mod$moduleData = mod.moduleData) == null ? void 0 : _mod$moduleData.address; // City is always required

  if (!(address != null && address.city)) {
    return {
      city: errorLabels.cityIsRequired
    };
  } // case 1: city + quarter + anything else


  if (address != null && address.quarter) {
    var _data$streetNumber, _data$entrance, _data$building, _data$apartment;

    if (!((_data$streetNumber = data.streetNumber) != null && _data$streetNumber.trim()) && !((_data$entrance = data.entrance) != null && _data$entrance.trim()) && !((_data$building = data.building) != null && _data$building.trim()) && !((_data$apartment = data.apartment) != null && _data$apartment.trim())) {
      return {
        any: errorLabels.fillStreetNumberBuildingEntranceOrApartment
      };
    }
  } // case 2: city + street + anything else
  else if (address != null && address.street) {
      var _data$streetNumber2, _data$entrance2, _data$building2, _data$apartment2;

      if (!((_data$streetNumber2 = data.streetNumber) != null && _data$streetNumber2.trim()) && !((_data$entrance2 = data.entrance) != null && _data$entrance2.trim()) && !((_data$building2 = data.building) != null && _data$building2.trim()) && !((_data$apartment2 = data.apartment) != null && _data$apartment2.trim())) {
        return {
          any: errorLabels.fillStreetNumberBuildingEntranceOrApartment
        };
      }
    } // case 3: city + additional info
    else if (!((_data$additionalAddre = data.additionalAddressInfo) != null && _data$additionalAddre.trim())) {
        return {
          additionalAddressInfo: errorLabels.cantFindStreetOrQuarterMessage,
          street: errorLabels.streetIsRequired,
          quarter: errorLabels.quarterIsRequired
        };
      }
};

var genericOfficeValidation = (_, mod, errorLabels) => {
  var _mod$moduleData2;

  var errors = {};
  var address = mod == null ? void 0 : (_mod$moduleData2 = mod.moduleData) == null ? void 0 : _mod$moduleData2.address;

  if (!(address != null && address.city)) {
    errors['city'] = errorLabels.cityIsRequired;
  }

  if (!(address != null && address.office)) {
    errors['office'] = errorLabels.officeIsRequired;
  }

  return Object.keys(errors).length ? errors : null;
};

var pickupFromStoreValidation = (_, mod, errorLabels) => {
  var _mod$moduleData3;

  return !(mod != null && (_mod$moduleData3 = mod.moduleData) != null && _mod$moduleData3.stockLocation) ? {
    pickupFromStore: errorLabels.noStoreSelected
  } : undefined;
};

var validations = {
  Speedy: {
    Address: genericAddressValidation,
    Office: genericOfficeValidation
  },
  Econt: {
    Address: genericAddressValidation,
    Office: genericOfficeValidation
  },
  Evropat: {
    Address: genericAddressValidation,
    Office: genericOfficeValidation
  },
  PickUpFromStore: {
    Default: pickupFromStoreValidation
  }
};
export var validateShipping = (data, mod, errorLabels) => {
  var _validations$mod$modu;

  var globalErrors = alwaysRequiredField.reduce((errors, field) => {
    var _data$field;

    if (!(data != null && (_data$field = data[field]) != null && _data$field.trim != null && _data$field.trim())) {
      return _extends({}, errors, {
        [field]: errorLabels.fieldIsRequired
      });
    }

    return errors;
  }, {});

  if (!mod.moduleKey || !mod.moduleMethodKey) {
    return _extends({}, globalErrors, {
      courier: errorLabels.selectCourier
    });
  }

  var validate = validations == null ? void 0 : (_validations$mod$modu = validations[mod.moduleKey]) == null ? void 0 : _validations$mod$modu[mod.moduleMethodKey];

  if (!validate) {
    throw new Error("Validation for shipping with provider: " + mod.moduleKey + " and method key: " + mod.moduleMethodKey + " not found");
  }

  var validateErrors = validate(data, mod, errorLabels) || {};

  var errors = _extends({}, globalErrors, validateErrors);

  return Object.keys(errors).length ? errors : null;
};
//# sourceMappingURL=validateShipping.js.map