import { isUrl } from "../Line/Container/Wrapper/utils";

/**
 * Validate deep link format - supports custom schemes, HTTP/HTTPS, and relative paths
 * @param {string} url - The deep link to validate
 * @returns {boolean} - True if valid deep link format, false otherwise
 */
export const isDeepLink = (url) => {
  if (!url || typeof url !== 'string') {
    return false;
  }

  const trimmedUrl = url.trim();
  if (trimmedUrl === '') {
    return false;
  }

  // Pattern 1: Custom scheme URLs (e.g., myapp://path, com.example.app://action)
  const customSchemePattern = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s]*$/;
  
  // Pattern 2: HTTP/HTTPS URLs (standard web URLs)
  const httpPattern = /^https?:\/\/[^\s]*$/;
  
  // Pattern 3: Relative paths (e.g., /path, path, home/screen) - must be valid path format
  // Accept simple paths, paths with slashes, or paths starting with /
  // But reject URLs that look like invalid web URLs (containing common URL patterns)
  const relativePathPattern = /^(\/[a-zA-Z0-9\/\-_\.]+|[a-zA-Z0-9\/\-_\.]+(\/[a-zA-Z0-9\/\-_\.]*)*)$/;
  
  // Additional check: reject patterns that look like invalid URLs
  const invalidUrlPattern = /^(invalid|not|random|http|https|www|\.com|\.org|\.net)/i;
  
  // Pattern 4: App-specific deep links without scheme (e.g., myapp://)
  const appSpecificPattern = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/$/;

  // Check if it matches any valid pattern
  const isValidPattern = customSchemePattern.test(trimmedUrl) || 
         httpPattern.test(trimmedUrl) || 
         relativePathPattern.test(trimmedUrl) ||
         appSpecificPattern.test(trimmedUrl);
  
  // If it matches a valid pattern, also check that it's not an invalid URL pattern
  if (isValidPattern && relativePathPattern.test(trimmedUrl)) {
    return !invalidUrlPattern.test(trimmedUrl);
  }
  
  return isValidPattern;
};

/**
 * Common link validation function using isUrl utility
 * @param {string} url - The URL to validate
 * @param {Function} formatMessage - Function to format error messages
 * @param {Object} messages - Messages object containing invalidUrl message
 * @returns {string|null} - Error message if invalid, null if valid
 */
export const validateLink = (url, formatMessage, messages) => {
  if (!url || url.trim() === "") {
    return null; // Empty URL is valid (optional field)
  }
  return isUrl(url.trim()) ? null : formatMessage(messages.invalidUrl);
};

/**
 * Validate external link specifically
 * @param {string} url - The external URL to validate
 * @param {Function} formatMessage - Function to format error messages
 * @param {Object} messages - Messages object containing invalidUrl message
 * @returns {string|null} - Error message if invalid, null if valid
 */
export const validateExternalLink = (url, formatMessage, messages) => validateLink(url, formatMessage, messages);

/**
 * Validate deep link specifically using deep link validation
 * @param {string} linkValue - The deep link to validate
 * @param {Function} formatMessage - Function to format error messages
 * @param {Object} messages - Messages object containing invalidUrl message
 * @returns {string|null} - Error message if invalid, null if valid
 */
export const validateDeepLink = (linkValue, formatMessage, messages) => {
  if (!linkValue || linkValue.trim() === "") {
    return null; // Empty deep link is valid (optional field)
  }
  return isDeepLink(linkValue.trim()) ? null : formatMessage(messages.invalidUrl);
}; 