<?php

declare(strict_types=1);

namespace FleekDash\Core;

defined('ABSPATH') || exit;

/**
 * Injects CSS custom properties for plugin asset URLs.
 *
 * Enables subfolder WordPress installs (e.g. domain.com/folder/) by providing
 * correct full URLs instead of hardcoded /wp-content/ paths in CSS.
 *
 * @package FleekDash\Core
 */
class PluginUrlCssVariables
{
    /**
     * Register the admin_head hook to inject variables.
     */
    public static function init(): void
    {
        \add_action('admin_head', [self::class, 'inject'], 0);
        \add_action('wp_head', [self::class, 'inject'], 0);
    }

    /**
     * Inject plugin URL CSS variables into :root.
     */
    public static function inject(): void
    {
        $plugin_url = \plugins_url('', FLEEKDASH_PLUGIN_FILE);
        $plugin_url = \rtrim($plugin_url, '/');

        $spinner_url = \esc_url($plugin_url . '/public/images/spinner.svg');
        $illustration_success = \esc_url($plugin_url . '/public/images/illustrations/32.svg');
        $illustration_success_dark = \esc_url($plugin_url . '/public/images/illustrations/32-dark.svg');

        $css = <<<CSS
:root {
  --fleekdash-spinner: url("{$spinner_url}");
  --fleekdash-illustration-success: url("{$illustration_success}");
  --fleekdash-illustration-success-dark: url("{$illustration_success_dark}");
}
CSS;

        printf('<style id="fleekdash-plugin-url-vars">%s</style>', $css);
    }
}
