<?php
/**
 * Dynamic block registration.
 *
 * @package CertaCart_EU_Product_Readiness
 */

namespace CertaCart\EU_Product_Readiness;

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

final class Blocks {
	public static function register(): void {
		add_action( 'init', array( __CLASS__, 'register_block' ) );
	}

	public static function register_block(): void {
		wp_register_style( 'ccpr-frontend', CCPR_URL . 'assets/css/frontend.css', array(), CCPR_VERSION );
		register_block_type(
			CCPR_PATH . 'blocks/product-safety',
			array( 'render_callback' => array( __CLASS__, 'render' ) )
		);
	}

	public static function render( array $attributes = array() ): string {
		$product_id = ! empty( $attributes['productId'] ) ? absint( $attributes['productId'] ) : get_the_ID();
		$product    = wc_get_product( $product_id );
		return $product instanceof \WC_Product ? Storefront_Renderer::instance()->render( $product ) : '';
	}
}
