import { clsx } from "clsx"
import { twMerge } from "tailwind-merge"

export function cn(...inputs) {
  return twMerge(clsx(inputs))
}

// Utility function to get correct image path with robust fallback
export function getImagePath(imageName) {
  if (!imageName) {
    console.warn('getImagePath called with empty imageName');
    return '';
  }

  // Normalize the image name to handle any path separators
  const normalizedName = imageName.replace(/^\/+/, '').replace(/\/+$/, '');
  
  // Check if we're in development or production
  if (import.meta.env.DEV) {
    // In development, use absolute path from public folder
    return `/image/${normalizedName}`;
  } else {
    // In production, use absolute path from the root
    // This ensures images are served correctly regardless of the deployment path
    return `/image/${normalizedName}`;
  }
}

// Alternative approach using import.meta.env.BASE_URL with fallback
export function getImagePathWithBase(imageName) {
  if (!imageName) {
    console.warn('getImagePathWithBase called with empty imageName');
    return '';
  }

  const baseUrl = import.meta.env.BASE_URL || '/';
  const normalizedName = imageName.replace(/^\/+/, '').replace(/\/+$/, '');
  
  // Ensure baseUrl ends with / and doesn't have double slashes
  const cleanBaseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
  return `${cleanBaseUrl}image/${normalizedName}`;
}

// Enhanced version with error handling and multiple fallback strategies
export function getImagePathRobust(imageName) {
  if (!imageName) {
    console.warn('getImagePathRobust called with empty imageName');
    return '';
  }

  const normalizedName = imageName.replace(/^\/+/, '').replace(/\/+$/, '');
  
  // Strategy 1: Use BASE_URL if available
  if (import.meta.env.BASE_URL) {
    const baseUrl = import.meta.env.BASE_URL;
    const cleanBaseUrl = baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`;
    return `${cleanBaseUrl}image/${normalizedName}`;
  }
  
  // Strategy 2: Use absolute path (current implementation)
  return `/image/${normalizedName}`;
}

// Function to validate if an image exists (for debugging)
export function validateImagePath(imageName) {
  const path = getImagePath(imageName);
  console.log(`Image path for "${imageName}": ${path}`);
  return path;
}


