<?php

if ( ! defined( 'ABSPATH' ) ) { return; }

$post_id = 0;
if ( isset( $block ) && is_object( $block ) && isset( $block->context['postId'] ) ) {
    $post_id = (int) $block->context['postId'];
}
if ( ! $post_id ) {
    $post_id = (int) get_the_ID();
}
if ( ! $post_id || ! function_exists( 'wc_get_product' ) ) {
    return;
}

$product = wc_get_product( $post_id );
if ( ! $product instanceof WC_Product || ! $product->is_type( 'variable' ) ) {
    
    
    return;
}

$map = array();
foreach ( $product->get_children() as $child_id ) {
    $variation = wc_get_product( (int) $child_id );
    if ( ! $variation ) { continue; }

    $images = array();

    $thumb_id = (int) $variation->get_image_id();
    if ( $thumb_id > 0 ) {
        $main = wp_get_attachment_image_src( $thumb_id, 'woocommerce_single' );
        if ( $main && ! empty( $main[0] ) ) { $images[] = (string) $main[0]; }
    }

    
    
    
    $extra_ids = class_exists( 'AIEO_DMM_Variations_Gallery' )
        ? AIEO_DMM_Variations_Gallery::resolve_image_ids( (int) $child_id, 'block_render' )
        : array_map( 'intval', $variation->get_gallery_image_ids() );
    foreach ( $extra_ids as $att_id ) {
        if ( $att_id <= 0 ) { continue; }
        $img = wp_get_attachment_image_src( $att_id, 'woocommerce_single' );
        if ( $img && ! empty( $img[0] ) ) { $images[] = (string) $img[0]; }
    }

    if ( ! empty( $images ) ) {
        $map[ (int) $child_id ] = array_values( array_unique( $images ) );
    }
}

if ( empty( $map ) ) {
    
    
    
    return;
}

$reset_url = '';
$parent_thumb = (int) $product->get_image_id();
if ( $parent_thumb > 0 ) {
    $img = wp_get_attachment_image_src( $parent_thumb, 'woocommerce_single' );
    if ( $img && ! empty( $img[0] ) ) { $reset_url = (string) $img[0]; }
}

if ( class_exists( 'OM_Product_Page_Essentials' ) ) {
    $om_plugin = OM_Product_Page_Essentials::instance();
    if ( isset( $om_plugin->modules['variations-gallery'] ) ) {
        $om_module = $om_plugin->modules['variations-gallery'];
        if ( is_object( $om_module ) && method_exists( $om_module, 'add_variation_gallery_data' ) ) {
            remove_filter( 'woocommerce_available_variation', array( $om_module, 'add_variation_gallery_data' ), 10 );
        }
    }
}
if ( function_exists( 'wp_dequeue_script' ) ) {
    wp_dequeue_script( 'om-ppe-variations-gallery' );
}
add_action( 'wp_print_footer_scripts', static function () {
    if ( function_exists( 'wp_dequeue_script' ) ) {
        wp_dequeue_script( 'om-ppe-variations-gallery' );
    }
}, 0 );

$payload = array(
    'product_id'      => (int) $post_id,
    'variations'      => $map,
    'reset_image_url' => $reset_url,
);

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'             => 'aieo-pdp-vg-bridge',
        'data-product-id'   => (string) $post_id,
        'data-aieo-pdp-vg'  => '1',
    ) )
    : 'class="aieo-pdp-vg-bridge" data-product-id="' . esc_attr( (string) $post_id ) . '" data-aieo-pdp-vg="1"';

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>>
    <script type="application/json" data-aieo-pdp-vg-data><?php
        
        
        
        echo wp_json_encode( $payload );
    ?></script>
</div>
<?php
echo ob_get_clean(); 
