// URL utilities for consistent URL management across the application

// Get site URL from environment variable or default
export const getSiteUrl = (): string => {
    // For client-side (Vite)
    if (typeof window !== 'undefined') {
        return import.meta.env.VITE_SITE_URL || window.location.origin;
    }

    // For server-side (Node.js)
    return process.env.SITE_URL || 'http://localhost:5173';
};

// Get API URL from environment variable or default
export const getApiUrl = (): string => {
    // For client-side (Vite)
    if (typeof window !== 'undefined') {
        return import.meta.env.VITE_API_URL || 'http://localhost:5050';
    }

    // For server-side (Node.js)
    return process.env.API_URL || 'http://localhost:5050';
};

// Build full API endpoint URL
export const buildApiUrl = (endpoint: string): string => {
    const baseUrl = getApiUrl();
    const cleanEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
    return `${baseUrl}${cleanEndpoint}`;
};

// Build full site URL
export const buildSiteUrl = (path: string): string => {
    const baseUrl = getSiteUrl();
    const cleanPath = path.startsWith('/') ? path : `/${path}`;
    return `${baseUrl}${cleanPath}`;
};

// For hash-based routing (React Router with HashRouter)
export const buildHashUrl = (path: string): string => {
    const baseUrl = getSiteUrl();
    const cleanPath = path.startsWith('/') ? path : `/${path}`;
    return `${baseUrl}/#${cleanPath}`;
};

export default {
    getSiteUrl,
    getApiUrl,
    buildApiUrl,
    buildSiteUrl,
    buildHashUrl
};
