<?php

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

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

if ( get_post_type( $post_id ) !== 'product' ) {
    return '';
}

if ( ! function_exists( 'wc_get_product' ) ) {
    return '';
}
if ( ! class_exists( 'AIEO_DMM_Swatches_Runtime' ) ) {
    return '';
}

$product = wc_get_product( $post_id );
if ( ! $product instanceof WC_Product ) {
    return '';
}

$context = isset( $attributes['renderContext'] ) && $attributes['renderContext'] === 'product'
    ? 'product'
    : 'archive';

$html = AIEO_DMM_Swatches_Runtime::instance()->render_card_swatches_html( $product, $context );
if ( $html === '' ) {
    return;
}

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array( 'class' => 'aieo-product-card-swatches' ) )
    : 'class="aieo-product-card-swatches"';

echo sprintf( '<div %s>%s</div>', $wrapper_attrs, $html ); 
