<?php
/**
 * File Path: includes/blocks/class-mbs-block-buy-links.php
 *
 * Buy Links Block — Render
 *
 * PHP render_callback for the mbs/buy-links Gutenberg block.
 * Registered by MBS_Blocks_Registry::register_buy_links_block().
 *
 * Delegates to MBS_Buy_Link_Renderer::render_order_now() — the same
 * renderer used by front-end templates (R5-adjacent rule: all buy-link
 * rendering goes through MBS_Buy_Link_Renderer).
 *
 * @package    My_Book_Showroom
 * @since      1.0.0
 */

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

class MBS_Block_Buy_Links { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render the Buy Links block.
	 *
	 * Called by WordPress as the register_block_type render_callback.
	 * Returns an empty string for an invalid or missing post ID.
	 *
	 * @param array $attributes Block attributes from the editor.
	 * @return string Rendered HTML.
	 */
	public static function render( array $attributes ): string {
		$post_id = ! empty( $attributes['bookId'] ) ? (int) $attributes['bookId'] : 0;
		if ( ! $post_id ) {
			return '';
		}

		$context = sanitize_key( $attributes['context'] ?? 'single' );

		return MBS_Buy_Link_Renderer::render_order_now( $post_id, null, array( 'context' => $context ) );
	}
}
