/**
 * Function to use when user updates their profile
 *
 * @param {*} username
 * @param {*} email
 * @param {*} password
 * @param {*} newPassword
 * @param {*} usernameError
 * @param {*} emailError
 * @param {*} passwordError
 * @param {*} newPasswordError
 * @returns Boolean
 */
const validationSetting = (
  username,
  email,
  password,
  newPassword,
  usernameError,
  emailError,
  passwordError,
  newPasswordError
) => {
  let isValid = true

  const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/

  // Email validation
  if (email && email.value !== undefined) {
    if (email.value === '') {
      isValid = false
      emailError.textContent = 'Email est requis'
    } else if (!emailPattern.test(email.value)) {
      isValid = false
      emailError.textContent = 'Entrez une adresse email valide'
    } else {
      emailError.textContent = ''
    }
  }

  // Password validation
  if (password && password.value !== undefined) {
    if (password.value === '' || password.value.length < 6) {
      isValid = false
      passwordError.textContent = 'Le mot de passe doit comporter plus de 6 caractères.'
    } else {
      passwordError.textContent = ''
    }
  }

  // Username validation
  if (username && username.value !== undefined) {
    if (username.value === '') {
      isValid = false
      usernameError.textContent = 'Username requis.'
    } else {
      usernameError.textContent = ''
    }
  }

  // New password validation
  if (newPassword && newPassword.value !== undefined) {
    if (newPassword.value !== '' && newPassword.value.length < 6) {
      isValid = false
      newPasswordError.textContent = 'Le mot de passe doit comporter plus de 6 caractères.'
    } else {
      newPasswordError.textContent = ''
    }
  }

  return isValid
}

export default validationSetting
