<?php
/**
 * Frontend Hover Effects
 *
 * This file handles the frontend rendering of hover effects
 * by filtering block content and adding necessary classes and styles.
 *
 * @package Blynex
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Apply hover effects to blocks on frontend
 */
function blynex_apply_frontend_hover_effects($block_content, $block) {
    // Only apply to cover and image blocks
    if (!in_array($block['blockName'], ['core/cover', 'core/image'])) {
        return $block_content;
    }

    // Get hover effect attributes
    $hover_effect = isset($block['attrs']['blynexHoverEffect']) ? $block['attrs']['blynexHoverEffect'] : 'none';
    $hover_intensity = isset($block['attrs']['blynexHoverIntensity']) ? $block['attrs']['blynexHoverIntensity'] : 0.5;
    $hover_scale = isset($block['attrs']['blynexHoverScale']) ? $block['attrs']['blynexHoverScale'] : 1.05;
    $hover_duration = isset($block['attrs']['blynexHoverDuration']) ? $block['attrs']['blynexHoverDuration'] : 0.3;

    // If no hover effect is set, return content as is
    if (empty($hover_effect) || $hover_effect === 'none') {
        return $block_content;
    }

    // Create wrapper classes and styles
    $wrapper_classes = 'blynex-hover-enabled blynex-hover-' . esc_attr($hover_effect);
    $wrapper_styles = sprintf(
        '--blynex-intensity: %s; --blynex-duration: %ss; --blynex-scale: %s;',
        esc_attr($hover_intensity),
        esc_attr($hover_duration),
        esc_attr($hover_scale)
    );

    // Wrap the block content with hover effect classes and styles
    $wrapped_content = sprintf(
        '<div class="%s" style="%s">%s</div>',
        $wrapper_classes,
        $wrapper_styles,
        $block_content
    );

    return $wrapped_content;
}

// Apply the filter to render blocks
add_filter('render_block', 'blynex_apply_frontend_hover_effects', 10, 2);

/**
 * Enqueue frontend hover effect styles and debug script
 */
function blynex_enqueue_frontend_hover_styles() {
    if (!is_admin()) {
        wp_enqueue_style(
            'blynex-hover-effects-frontend',
            get_template_directory_uri() . '/build/style-index.css',
            array(),
            wp_get_theme()->get('Version')
        );
    }
}

add_action('wp_enqueue_scripts', 'blynex_enqueue_frontend_hover_styles'); 