<?php
/**
 * Direct asset URL and version helpers for non-manifest includes/assets entries.
 *
 * @package FleekDash
 */

declare(strict_types=1);

namespace FleekDash\Libs\Assets;

/**
 * JS entries built by Vite from ESM sources (import/export).
 * Must be served from assets/ — not raw includes/js/*.js (classic script tag breaks on import).
 */
const VITE_BUNDLED_JS_ENTRIES = array(
    'iframe-navigation',
);

/**
 * Get the correct asset URL for a specific entry
 *
 * @since 0.1.0
 *
 * @param string $entry Asset entry name (e.g., 'core', 'index', 'forms', etc.)
 * @param string $type Asset type ('css' or 'js')
 * @param string $fallback_url Fallback URL if manifest is not available
 * @param string $manifest_dir Manifest directory path (defaults to assets)
 */
function getAssetUrl(string $entry, string $type = 'css', string $fallback_url = '', string $manifest_dir = ''): string
{
    static $cache = array();

    $cache_key = $entry . '_' . $type . '_' . md5($fallback_url . $manifest_dir);

    if (isset($cache[$cache_key])) {
        return $cache[$cache_key];
    }

    // Local dev host: serve plain CSS/JS from includes/ (matches source on disk). Production host:
    // use built copies under assets/. Vite/HMR for the SPA is gated separately via isDevelopmentEnvironment().
    $use_includes_path = isLocalDevelopmentHost();

    if ($type === 'js' && in_array($entry, VITE_BUNDLED_JS_ENTRIES, true)) {
        $bundled_path = FLEEKDASH_PLUGIN_DIR . ASSETS_DIRECTORY_NAME . '/' . $entry . '.js';
        if (is_readable($bundled_path)) {
            $use_includes_path = false;
        }
    }

    if ($use_includes_path) {
        $url = FLEEKDASH_PLUGIN_URL . 'includes/' . $type . '/' . $entry . '.' . $type;
    } else {
        $url = FLEEKDASH_PLUGIN_URL . ASSETS_DIRECTORY_NAME . '/' . $entry . '.' . $type;
    }

    $cache[$cache_key] = $url;

    return $url;
}

/**
 * Get the correct CSS URL for a specific asset entry
 *
 * @since 0.1.0
 */
function getCssUrl(string $entry, string $fallback_url = '', string $manifest_dir = ''): string
{
    return getAssetUrl($entry, 'css', $fallback_url, $manifest_dir);
}

/**
 * Version string for wp_enqueue_style to avoid stale browser cache when CSS files change.
 *
 * @param string $entry Asset entry name (e.g. 'index', 'voxel').
 */
function getCssVersion(string $entry): string
{
    static $cache = array();

    if (array_key_exists($entry, $cache)) {
        return $cache[$entry];
    }

    $use_includes_path = isLocalDevelopmentHost();
    $relative_path = $use_includes_path
        ? 'includes/css/' . $entry . '.css'
        : ASSETS_DIRECTORY_NAME . '/' . $entry . '.css';
    $file_path = FLEEKDASH_PLUGIN_DIR . $relative_path;

    $version = defined('FLEEKDASH_VERSION') ? \FLEEKDASH_VERSION : '1.0';

    if (is_readable($file_path)) {
        $mtime = filemtime($file_path);
        if ($mtime !== false) {
            $version .= '.' . (string) $mtime;
        }
    }

    $cache[$entry] = $version;

    return $version;
}

/**
 * Get the correct JS URL for a specific asset entry
 *
 * @since 0.1.0
 */
function getJsUrl(string $entry, string $fallback_url = '', string $manifest_dir = ''): string
{
    return getAssetUrl($entry, 'js', $fallback_url, $manifest_dir);
}
