import { isValidHttpUrl } from './urlValidation';
import { validateTags } from '../../../../utils/tagValidations';
import globalMessages from '../../../Cap/messages';
import { hasPersonalizationTags } from '../../../../utils/commonUtils';

/**
 * Validates template name (checks if empty)
 * @param {string} value - The template name value
 * @returns {boolean} True if invalid (empty), false if valid
 */
export const validateTemplateName = (value) => !value || value.trim() === '';

/**
 * Validates notification title (required and optional tag validation)
 * @param {string} value - The title value
 * @param {Function} formatMessage - i18n format message function
 * @param {Object} messages - Message definitions
 * @param {Object} [validationConfig] - Optional config for tag validation
 * @param {boolean} [isFullMode] - Optional; when set with validationConfig, runs tag validation
 * @returns {string} Error message if invalid, empty string if valid
 */
export const validateTitle = (value, formatMessage, messages, restrictPersonalization, validationConfig, isFullMode) => {
  if (!value || value.trim() === '') {
    return formatMessage(messages.titleRequired);
  }
  if (restrictPersonalization && hasPersonalizationTags(value)) {
    return formatMessage(messages.personalizationTokensErrorMessage);
  }

  if (validationConfig != null) {
    const validationResponse = validateTags({
      content: value,
      ...validationConfig,
      isFullMode,
    }) || {};

    if (validationResponse?.isBraceError) {
      return formatMessage(globalMessages.unbalanacedCurlyBraces);
    }
  }

  return '';
};

/**
 * Validates URL
 * @param {string} value - The URL value
 * @param {Function} formatMessage - i18n format message function
 * @param {Object} messages - Message definitions
 * @returns {string} Error message if invalid, empty string if valid
 */
export const validateUrl = (value, formatMessage, messages) => {
  if (!value || value.trim() === '') {
    return formatMessage(messages.urlRequired);
  }

  if (!isValidHttpUrl(value)) {
    return formatMessage(messages.urlInvalid);
  }

  return '';
};

/**
 * Validates message content including tag validation
 * @param {string} value - The message content
 * @param {Function} formatMessage - i18n format message function
 * @param {Object} messages - Message definitions
 * @param {Object} validationConfig - Configuration for tag validation
 * @returns {string} Error message if invalid, empty string if valid
 */
export const validateMessageContent = (value, formatMessage, messages, validationConfig, isFullMode, restrictPersonalization) => {
  if (!value || value.trim() === '') {
    return formatMessage(messages.messageRequired);
  }

  const validationResponse = validateTags({
    content: value,
    ...validationConfig,
    isFullMode,
  }) || {};

  if (validationResponse?.isBraceError) {
    return formatMessage(globalMessages.unbalanacedCurlyBraces);
  }

  if (restrictPersonalization && hasPersonalizationTags(value)) {
    return formatMessage(messages.personalizationTokensErrorMessage);
  }

  return '';
};
