/**
 * Asset Path Utility
 * 
 * Resolves asset paths based on runtime environment:
 * - Electron: Returns relative path (assets bundled with app, using ./)
 * - Browser: Returns absolute path (served by PWA nginx)
 */

// 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:'
}

/**
 * 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, use absolute path - PWA nginx serves images at /images/
    return `/${cleanPath}`
}

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