<?php
if (!defined('ABSPATH')) exit;

/**
 * 3D Product Customizer Block Render
 * 
 * @var array $attributes Block attributes
 * @var string $content Block content
 * @var WP_Block $block Block instance
 */

$dprcu_product_id = !empty($attributes['productId']) ? intval($attributes['productId']) : 0;

if (!$dprcu_product_id) {
	if (!function_exists('is_product') || !is_product()) {
		return '';
	}
	$dprcu_product_id = get_the_ID();
}

$dprcu_model_url = get_post_meta($dprcu_product_id, '_dprcu_3d_model_url', true);
$dprcu_viewer_only = get_post_meta($dprcu_product_id, '_dprcu_3d_viewer_only', true);

if (empty($dprcu_model_url)) {
	return '';
}

dprcu_enqueue_scripts($dprcu_product_id);

ob_start();
if ('1' === $dprcu_viewer_only) {
	dprcu_viewer_canvas_html($dprcu_product_id);
} else {
	dprcu_html_output($dprcu_product_id);
}
return ob_get_clean() ?? '';
