<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Theme_Bridge
{
    public const STYLE_HANDLE = 'puffergo';

    public static function get_quick_style_css()
    {
        static $cached = null;

        if (null !== $cached) {
            return $cached;
        } $file = plugin_dir_path(PUFFERGO_PLUGIN_FILE) . 'assets/css/puffergo-quick-style.css';
        $cached = is_readable($file) ? (string) file_get_contents($file) : '';

        return $cached;
    }

    public const THEME_KADENCE = 'kadence';

    public const THEME_FSE = 'fse';

    public const THEME_GENERIC = 'generic';

    public const GOOGLE_FONTS_MAP = array('Inter' => 'Inter:wght@300;400;500;600;700', 'Roboto' => 'Roboto:wght@300;400;500;700', 'Poppins' => 'Poppins:wght@300;400;500;600;700', 'Lato' => 'Lato:wght@300;400;700', 'Montserrat' => 'Montserrat:wght@300;400;500;600;700', 'Open Sans' => 'Open+Sans:wght@300;400;600;700', 'Nunito' => 'Nunito:wght@300;400;600;700', 'Source Sans 3' => 'Source+Sans+3:wght@300;400;600;700', 'Playfair Display' => 'Playfair+Display:wght@400;500;600;700', 'Merriweather' => 'Merriweather:wght@300;400;700', 'Lora' => 'Lora:wght@400;500;600;700', 'Cormorant Garamond' => 'Cormorant+Garamond:wght@300;400;500;600', 'DM Sans' => 'DM+Sans:wght@300;400;500;600;700', 'Outfit' => 'Outfit:wght@300;400;500;600;700', 'Plus Jakarta Sans' => 'Plus+Jakarta+Sans:wght@300;400;500;600;700', 'Noto Sans SC' => 'Noto+Sans+SC:wght@300;400;500;700', 'Noto Serif SC' => 'Noto+Serif+SC:wght@300;400;500;700', );

    public function __construct()
    {
        add_action('enqueue_block_assets', array($this, 'load_core_styles'), 20);
        add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_extras'), 20);
        add_action('enqueue_block_assets', array($this, 'enqueue_editor_fix'), 21);
    }

    public function enqueue_frontend_extras()
    {
        wp_enqueue_style('puffergo-animations', plugins_url('assets/css/puffergo-animations.css', PUFFERGO_PLUGIN_FILE), array(self::STYLE_HANDLE), PUFFERGO_CORE_VERSION);
        wp_enqueue_script('puffergo-animations', plugins_url('assets/js/puffergo-animations.js', PUFFERGO_PLUGIN_FILE), array(), PUFFERGO_CORE_VERSION, true);
    }

    public function enqueue_editor_fix()
    {
        if (! is_admin()) {
            return;
        } $editor_fix_css = "
            .editor-styles-wrapper {
                --pg-max-width: var(--global-content-width, 1200px);
            }
        ";
        wp_add_inline_style(self::STYLE_HANDLE, $editor_fix_css);
    }

    public function load_core_styles()
    {
        wp_enqueue_style(self::STYLE_HANDLE, plugins_url('assets/css/puffergo.css', PUFFERGO_PLUGIN_FILE), array(), PUFFERGO_CORE_VERSION);
        $design_css = $this->get_css_variables();

        if ('' !== $design_css) {
            wp_add_inline_style(self::STYLE_HANDLE, $design_css);
        }

if (class_exists('PufferGo_Design_System')) {
            $active_tokens = PufferGo_Design_System::get_active_tokens();

            if ($active_tokens) {
                $this->enqueue_google_fonts($active_tokens);
            }
        } wp_enqueue_style('puffergo-contact-form', plugins_url('assets/css/contact-form.css', PUFFERGO_PLUGIN_FILE), array(self::STYLE_HANDLE), PUFFERGO_CORE_VERSION);
        wp_enqueue_style('puffergo-quick-style', plugins_url('assets/css/puffergo-quick-style.css', PUFFERGO_PLUGIN_FILE), array(self::STYLE_HANDLE), PUFFERGO_CORE_VERSION);
    }

    private function enqueue_google_fonts($tokens)
    {
        $families_to_load = array();

        foreach (array('fontHeading', 'fontBody') as $field) {
            $value = isset($tokens[ $field ]) ? trim($tokens[ $field ]) : '';

            if ('' === $value) {
                continue;
            }

foreach (explode(',', $value) as $part) {
                $font_name = trim($part, " \t\n\r\0\x0B\"'");

                if ('' !== $font_name && isset(self::GOOGLE_FONTS_MAP[ $font_name ])) {
                    $families_to_load[] = self::GOOGLE_FONTS_MAP[ $font_name ];
                }
            }
        } $families_to_load = array_unique($families_to_load);

        if (empty($families_to_load)) {
            return;
        } $query = implode('&family=', $families_to_load);
        $url = 'https://fonts.googleapis.com/css2?family=' . $query . '&display=swap';
        wp_enqueue_style('puffergo-google-fonts', esc_url($url), array(), PUFFERGO_CORE_VERSION);
    }

    public function detect_theme_type()
    {
        $theme = wp_get_theme();

        if (stripos($theme->get('Name'), 'kadence') !== false || stripos($theme->get_template(), 'kadence') !== false) {
            return self::THEME_KADENCE;
        }

if (file_exists(get_template_directory() . '/theme.json')) {
            return self::THEME_FSE;
        }

return self::THEME_GENERIC;
    }

    public function get_css_variables()
    {
        $type = $this->detect_theme_type();
        $overrides = $this->get_theme_variable_overrides($type);

        if (class_exists('PufferGo_Design_System')) {
            $overrides .= PufferGo_Design_System::get_override_css();
        }

if (class_exists('PufferGo_Design_System')) {
            $compiled = PufferGo_Design_System::get_compiled_css();

            if ($compiled) {
                $css = $compiled;

                if ($overrides) {
                    $css .= "\n:root { {$overrides} }";
                }

return $css;
            }
        }

return $overrides ? ":root { {$overrides} }" : '';
    }

    private function get_theme_variable_overrides($theme_type)
    {
        switch ($theme_type) {
            case self::THEME_KADENCE: return '
    --global-palette1: var(--pg-brand-600);
    --global-palette2: var(--pg-accent-600);
    --global-palette3: var(--pg-neutral-900);
    --global-palette4: var(--pg-neutral-600);
    --global-palette6: var(--pg-neutral-200);
    --global-palette8: var(--pg-neutral-100);
    --global-palette9: var(--pg-neutral-50);
    --global-button-border-radius: var(--pg-radius);
    ';
            case self::THEME_FSE: return '
    --wp--preset--color--primary: var(--pg-brand-600);
    --wp--preset--color--secondary: var(--pg-accent-600);
    --wp--preset--color--base: var(--pg-neutral-50);
    --wp--preset--color--background: var(--pg-neutral-50);
    --wp--preset--color--contrast: var(--pg-neutral-900);
    --wp--preset--color--foreground: var(--pg-neutral-900);
    ';
            default: return '';
        }
    }
} new PufferGo_Theme_Bridge();
