<?php

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

$valid_surfaces = array( 'same_needs', 'crosssells', 'upsells', 'related', 'recently_viewed' );
$surface = isset( $attributes['surface'] ) ? (string) $attributes['surface'] : 'related';
if ( ! in_array( $surface, $valid_surfaces, true ) ) {
    $surface = 'related';
}

if ( $surface !== 'recently_viewed' ) {
    if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; }
}

$id_map = array(
    'same_needs'      => 'aieo-same-needs-placeholder',
    'crosssells'      => 'aieo-cross-sells-placeholder',
    'upsells'         => 'aieo-up-sells-placeholder',
    'related'         => 'aieo-related-placeholder',
    'recently_viewed' => 'aieo-recently-viewed-placeholder',
);
$placeholder_id = $id_map[ $surface ];

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'          => 'aieo-recommendations',
        'data-aieo-reco' => $surface,
    ) )
    : 'class="aieo-recommendations" data-aieo-reco="' . esc_attr( $surface ) . '"';

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>>
    <div id="<?php echo esc_attr( $placeholder_id ); ?>" data-aieo-rec-type="<?php echo esc_attr( $surface ); ?>"></div>
</div>
<?php
echo ob_get_clean(); 
