"use strict";

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

exports.__esModule = true;
exports.validateBillingAddress = void 0;

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

/* eslint-disable eqeqeq */

/* eslint-disable prefer-const */
var requiredCompanyFields = ['vatId', 'companyId', 'companyPerson', 'companyName'];
var requiredFields = ['additionalAddressInfo'];

var validateBillingAddress = function validateBillingAddress(data, errorLabels) {
  var _data$custom;

  var isWithInvoice = (_data$custom = data.custom) == null ? void 0 : _data$custom.some(function (custom) {
    return custom.field === 'withInvoice' && custom.value == 1;
  });
  if (!isWithInvoice) return null;
  var companyErrors = requiredCompanyFields.reduce(function (errors, field) {
    var _data$company, _data$company$field;

    if (!(data != null && (_data$company = data.company) != null && (_data$company$field = _data$company[field]) != null && _data$company$field.trim())) {
      var _extends2;

      return (0, _extends4["default"])({}, errors, (_extends2 = {}, _extends2[field] = errorLabels.fieldIsRequired, _extends2));
    }

    return errors;
  }, {});
  var fieldErrors = requiredFields.reduce(function (errors, field) {
    var _data$field;

    if (!((_data$field = data[field]) != null && _data$field.trim())) {
      var _extends3;

      return (0, _extends4["default"])({}, errors, (_extends3 = {}, _extends3[field] = errorLabels.fieldIsRequired, _extends3));
    }

    return errors;
  }, {});
  var errors = (0, _extends4["default"])({}, companyErrors, fieldErrors);
  return Object.keys(errors).length ? errors : null;
};

exports.validateBillingAddress = validateBillingAddress;
//# sourceMappingURL=validateBillingAddress.js.map