/**
 * Reusable validation helpers for form fields.
 * Each function returns an error i18n key string if invalid, or '' if valid.
 */

/**
 * Validates that a string is non-empty after trimming.
 * @param {string} value
 * @returns {string} error key or ''
 */
export function validateRequired(value) {
  if (!value || !value.trim()) {
    return 'validation.required'
  }
  return ''
}

/**
 * Validates that a number is >= min.
 * @param {number|string} value
 * @param {number} min
 * @returns {{ key: string, params: object } | ''} error descriptor or ''
 */
export function validateMin(value, min = 0) {
  const num = Number(value)
  if (isNaN(num) || num < min) {
    return { key: 'validation.minValue', params: { min } }
  }
  return ''
}

/**
 * Validates a URL format. Empty strings are considered valid (optional field).
 * @param {string} value
 * @returns {string} error key or ''
 */
export function validateUrl(value) {
  if (!value || !value.trim()) {
    return ''
  }
  try {
    const url = new URL(value.trim())
    if (!['http:', 'https:'].includes(url.protocol)) {
      return 'validation.invalidUrl'
    }
    return ''
  } catch {
    return 'validation.invalidUrl'
  }
}
