/**
 * Asset Path Utility
 * 
 * Resolves asset paths based on runtime environment:
 * - Electron: Returns relative path (assets bundled with app, using ./)
 * - Browser: Returns full server URL
 */

// Detect if running in Electron - check at runtime, not module load time
function checkIsElectron(): boolean {
    return !!(window as any).electronAPI?.isElectron ||
        window.location.protocol === 'file:'
}

// Base URL for remote assets (browser mode)
const ASSET_BASE_URL = import.meta.env.VITE_ASSET_URL ||
    (import.meta.env.PROD ? 'https://api-staging.echoesoftheash.com' : '')

/**
 * Resolves an asset path for the current environment
 * @param path - The asset path (e.g., "images/items/knife.webp" or "/images/items/knife.webp")
 * @returns The resolved path for the current environment
 */
export function getAssetPath(path: string): string {
    if (!path) return ''

    // Normalize path (remove leading slash for Electron compatibility)
    const cleanPath = path.startsWith('/') ? path.slice(1) : path

    if (checkIsElectron()) {
        // In Electron with base: './', use relative paths
        return `./${cleanPath}`
    }

    // In browser, prepend the server URL
    return `${ASSET_BASE_URL}/${cleanPath}`
}

/**
 * Check if we're running in Electron
 */
export function isElectronApp(): boolean {
    return checkIsElectron()
}
