<?php
/**
 * iframe-tokens.css manifest URL resolution (wp-admin embeds — no Tailwind).
 *
 * Loaded once from libs/assets.php via require_once to avoid duplicate declarations
 * if assets.php is ever merged with a duplicated block.
 *
 * @package FleekDash
 */

declare(strict_types=1);

namespace FleekDash\Libs\Assets;

const IFRAME_TOKENS_STYLES_RELATIVE_PATH = 'src/styles/iframe-tokens.css';

const IFRAME_TOKENS_VITE_ENTRY = 'iframe-tokens';

const IFRAME_TOKENS_FILENAME_NEEDLE = 'iframe-tokens';

/**
 * @return string|null URL or null if not resolved from manifest scan
 */
function resolveIframeTokensCssUrlFromManifest(object $manifest): ?string
{
    return resolveViteCssUrlFromManifest(
        $manifest,
        IFRAME_TOKENS_VITE_ENTRY,
        IFRAME_TOKENS_STYLES_RELATIVE_PATH,
        IFRAME_TOKENS_FILENAME_NEEDLE,
    );
}

/**
 * Resolve iframe-tokens stylesheet URL in development (Vite-processed CSS).
 *
 * @param string $manifest_dir Absolute path to the directory containing vite/manifest files
 */
function resolveIframeTokensCssUrlDevelopment(string $manifest_dir): ?string
{
    $result = null;

    try {
        $manifest = getManifest($manifest_dir);
        $hasViteOrigin = $manifest->is_dev
            && isset($manifest->data->origin)
            && (string) $manifest->data->origin !== '';

        if ($hasViteOrigin) {
            $result = generateDevelopmentAssetSrc($manifest, IFRAME_TOKENS_STYLES_RELATIVE_PATH);
        } elseif (!$manifest->is_dev) {
            $result = resolveIframeTokensCssUrlFromManifest($manifest);
        } else {
            $result = resolveViteCssUrlFromProductionManifestFile(
                $manifest_dir,
                IFRAME_TOKENS_VITE_ENTRY,
                IFRAME_TOKENS_STYLES_RELATIVE_PATH,
                IFRAME_TOKENS_FILENAME_NEEDLE,
            );
        }
    } catch (AssetManifestException) {
        $result = resolveViteCssUrlFromProductionManifestFile(
            $manifest_dir,
            IFRAME_TOKENS_VITE_ENTRY,
            IFRAME_TOKENS_STYLES_RELATIVE_PATH,
            IFRAME_TOKENS_FILENAME_NEEDLE,
        );
    }

    return $result;
}

/**
 * Get the iframe-tokens.css file with hash from manifest (wp-admin embeds — no Tailwind).
 *
 * @param string $manifest_dir Manifest directory path (defaults to assets)
 *
 * @return string URL to the iframe-tokens.css file with hash
 */
function getIframeTokensCssUrl(string $manifest_dir = ''): string
{
    static $cache = null;

    if ($cache !== null) {
        return $cache;
    }

    if (empty($manifest_dir)) {
        $manifest_dir = FLEEKDASH_PLUGIN_DIR . ASSETS_DIRECTORY_NAME;
    }

    $resolved = null;

    if (isDevelopmentEnvironment()) {
        $resolved = resolveIframeTokensCssUrlDevelopment($manifest_dir);
    } else {
        $manifest_path = $manifest_dir . '/' . MANIFEST_JSON_FILENAME;
        $manifest_exists = is_file($manifest_path) && is_readable($manifest_path);

        if ($manifest_exists) {
            try {
                $manifest = getManifest($manifest_dir);
                $resolved = resolveIframeTokensCssUrlFromManifest($manifest);
            } catch (AssetManifestException $e) {
                $resolved = null;
            }
        }

        if ($resolved === null) {
            $resolved = resolveViteCssUrlFromGlobFallback($manifest_dir, IFRAME_TOKENS_FILENAME_NEEDLE);
        }
    }

    if ($resolved === null) {
        $resolved = resolveViteCssUrlFromGlobFallback($manifest_dir, IFRAME_TOKENS_FILENAME_NEEDLE);
    }

    if ($resolved === null) {
        $resolved = FLEEKDASH_PLUGIN_URL . IFRAME_TOKENS_STYLES_RELATIVE_PATH;
    }

    $cache = $resolved;

    return $resolved;
}

function resolveIframeTokensCssDiskPathProduction(): ?string
{
    $manifest_dir = FLEEKDASH_PLUGIN_DIR . ASSETS_DIRECTORY_NAME;

    return resolveViteCssDiskPathProduction(
        $manifest_dir,
        IFRAME_TOKENS_VITE_ENTRY,
        IFRAME_TOKENS_STYLES_RELATIVE_PATH,
        IFRAME_TOKENS_FILENAME_NEEDLE,
    );
}

/**
 * Version for iframe-tokens stylesheet enqueue (filemtime-based cache busting).
 */
function getIframeTokensCssVersion(): string
{
    static $cached = null;

    if ($cached !== null) {
        return $cached;
    }

    $base = defined('FLEEKDASH_VERSION') ? \FLEEKDASH_VERSION : '1.0';
    $disk_path = null;

    if (isDevelopmentEnvironment()) {
        $candidate = FLEEKDASH_PLUGIN_DIR . IFRAME_TOKENS_STYLES_RELATIVE_PATH;
        if (is_readable($candidate)) {
            $disk_path = $candidate;
        }
    } else {
        $disk_path = resolveIframeTokensCssDiskPathProduction();

        if ($disk_path === null) {
            $fallback = FLEEKDASH_PLUGIN_DIR . IFRAME_TOKENS_STYLES_RELATIVE_PATH;
            if (is_readable($fallback)) {
                $disk_path = $fallback;
            }
        }
    }

    $result = $base;
    if ($disk_path !== null) {
        $mtime = filemtime($disk_path);
        if ($mtime !== false) {
            $result .= '.' . (string) $mtime;
        }
    }

    $cached = $result;

    return $result;
}
