<?php
/**
 * Social Sharing block — server-side registration and render callback.
 *
 * Renders the social sharing buttons for the current context:
 * product page or wishlist page.
 *
 * @package JWP_STK
 */

namespace JWP_STK\Blocks\SocialSharing;

defined( 'ABSPATH' ) || exit;

/**
 * Block class.
 */
class Block {

	/**
	 * Register this block type with WordPress.
	 *
	 * @param string $asset_dir Absolute path to the compiled block directory.
	 * @return void
	 */
	public function register( string $asset_dir ) {
		register_block_type(
			$asset_dir,
			array(
				'render_callback' => array( $this, 'render' ),
			)
		);
	}

	/**
	 * Render the block on the frontend.
	 *
	 * If the Social Sharing module is inactive, nothing is output.
	 * The `context` block attribute ('product' | 'wishlist') controls
	 * which sharing configuration is used.
	 *
	 * @param array<string, mixed> $attributes Block attributes.
	 * @param string               $content    Inner block content (unused — dynamic block).
	 * @return string
	 */
	public function render( array $attributes, string $content ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $content required by WP block render callback signature.
		if ( ! shortcode_exists( 'storetoolkit_social_sharing' ) ) {
			return '';
		}

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

		return do_shortcode( '[storetoolkit_social_sharing context="' . esc_attr( $context ) . '"]' );
	}
}
