<?php
/**
 * Fundraiser Share Block
 *
 * Renders social sharing buttons for the current fundraiser. All share targets
 * are standard platform share-intent URLs — no external scripts or styles are
 * loaded. SVG icons are inlined. The optional "copy link" button is handled by
 * a tiny self-contained inline script (no external dependencies).
 *
 * @package AmbassadorHub\Fundraisers\Blocks\ShareButtons
 */

namespace AmbassadorHub\Fundraisers\Blocks\ShareButtons;

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

use AmbassadorHub\Admin\InlineAssets;
use AmbassadorHub\Fundraisers\FundraisersModule;

/**
 * Share Buttons block registration and rendering.
 */
final class Block {

	/**
	 * Initialize the block.
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'init', array( self::class, 'register_block' ) );
	}

	/**
	 * Register the block type, editor script, and frontend styles.
	 *
	 * @return void
	 */
	public static function register_block(): void {
		$handle    = 'ambh-fundraiser-share-block';
		$block_dir = AMBASSADOR_HUB_PATH . 'src/Fundraisers/Blocks/ShareButtons/';
		$block_url = AMBASSADOR_HUB_URL . 'src/Fundraisers/Blocks/ShareButtons/';

		// Editor script — registers block type in the block editor.
		$script_file = $block_dir . 'index.js';
		if ( file_exists( $script_file ) ) {
			wp_register_script(
				$handle,
				$block_url . 'index.js',
				array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components' ),
				(string) filemtime( $script_file ),
				true
			);
		}

		// Frontend + editor styles.
		$style_file = $block_dir . 'style.css';
		wp_register_style(
			'ambh-fundraiser-share',
			$block_url . 'style.css',
			array(),
			file_exists( $style_file ) ? (string) filemtime( $style_file ) : AMBASSADOR_HUB_VERSION
		);

		$block_args = array(
			'style'           => 'ambh-fundraiser-share',
			'render_callback' => array( self::class, 'render_block' ),
		);

		if ( file_exists( $script_file ) ) {
			$block_args['editor_script'] = $handle;
		}

		register_block_type( $block_dir, $block_args );

		// Shortcode alias: [ambh_fundraiser_share].
		add_shortcode( 'ambh_fundraiser_share', array( self::class, 'render_shortcode' ) );
	}

	/**
	 * Server-side render callback.
	 *
	 * @param array $attributes Block attributes.
	 * @return string Rendered HTML.
	 */
	public static function render_block( array $attributes = array() ): string {
		// Show a placeholder in the block editor preview canvas.
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			return '<div class="ambh-block-preview">'
				. esc_html__( 'Fundraiser Share (renders on the frontend)', 'ambassadorhub' )
				. '</div>';
		}

		$fundraiser_id = (int) get_the_ID();
		$fundraiser    = $fundraiser_id ? get_post( $fundraiser_id ) : null;

		if ( ! $fundraiser || FundraisersModule::POST_TYPE !== $fundraiser->post_type ) {
			return '';
		}

		$url   = (string) get_permalink( $fundraiser_id );
		$title = (string) get_the_title( $fundraiser_id );

		// Attribute values with safe defaults.
		$show_facebook     = (bool) ( $attributes['showFacebook'] ?? true );
		$show_twitter      = (bool) ( $attributes['showTwitter'] ?? true );
		$show_whatsapp     = (bool) ( $attributes['showWhatsapp'] ?? true );
		$show_linkedin     = (bool) ( $attributes['showLinkedin'] ?? false );
		$show_threads      = (bool) ( $attributes['showThreads'] ?? false );
		$show_mastodon     = (bool) ( $attributes['showMastodon'] ?? false );
		$show_bluesky      = (bool) ( $attributes['showBluesky'] ?? false );
		$show_email        = (bool) ( $attributes['showEmail'] ?? true );
		$show_copy_link    = (bool) ( $attributes['showCopyLink'] ?? true );
		$mastodon_instance = sanitize_text_field( (string) ( $attributes['mastodonInstance'] ?? '' ) );
		$share_text        = sanitize_text_field( (string) ( $attributes['shareText'] ?? '' ) );
		$display_style     = in_array( $attributes['displayStyle'] ?? '', array( 'buttons', 'icons' ), true )
			? (string) $attributes['displayStyle']
			: 'buttons';
		$alignment         = in_array( $attributes['alignment'] ?? '', array( 'left', 'center', 'right' ), true )
			? (string) $attributes['alignment']
			: 'left';
		$label             = sanitize_text_field( (string) ( $attributes['label'] ?? '' ) );

		// Message used in tweet / WhatsApp / email subject.
		$message = $share_text ? $share_text : $title;

		// Share intent URLs — plain href links, zero external page assets.
		$fb_url      = 'https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode( $url );
		$tw_url      = 'https://twitter.com/intent/tweet?text=' . rawurlencode( $message ) . '&url=' . rawurlencode( $url );
		$wa_url      = 'https://wa.me/?text=' . rawurlencode( $message . ' ' . $url );
		$li_url      = 'https://www.linkedin.com/sharing/share-offsite/?url=' . rawurlencode( $url );
		$em_url      = 'mailto:?subject=' . rawurlencode( $message ) . '&body=' . rawurlencode( $url );
		$threads_url = 'https://www.threads.net/intent/post?text=' . rawurlencode( $message . ' ' . $url );
		$bsky_url    = 'https://bsky.app/intent/compose?text=' . rawurlencode( $message . ' ' . $url );
		// Mastodon: federated — share text stored in a data attribute; JS resolves the instance.
		$mastodon_text = rawurlencode( $message . ' ' . $url );

		$wrapper_class = 'ambh-share-buttons'
			. ( 'icons' === $display_style ? ' ambh-share-buttons--icons' : '' )
			. ' ambh-share-buttons--align-' . $alignment;

		ob_start();
		?>
		<div class="<?php echo esc_attr( $wrapper_class ); ?>">
			<?php if ( $label ) : ?>
				<p class="ambh-share-buttons__label"><?php echo esc_html( $label ); ?></p>
			<?php endif; ?>

			<ul class="ambh-share-list" role="list">

				<?php if ( $show_facebook ) : ?>
				<li class="ambh-share-list__item">
					<a href="<?php echo esc_url( $fb_url ); ?>"
						class="ambh-share-btn ambh-share-btn--facebook"
						target="_blank"
						rel="noopener noreferrer"
						aria-label="<?php esc_attr_e( 'Share on Facebook', 'ambassadorhub' ); ?>"
						title="<?php esc_attr_e( 'Share on Facebook', 'ambassadorhub' ); ?>">
						<?php echo self::icon( 'facebook' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted static SVG ?>
						<span class="ambh-share-btn__text"><?php esc_html_e( 'Facebook', 'ambassadorhub' ); ?></span>
					</a>
				</li>
				<?php endif; ?>

				<?php if ( $show_twitter ) : ?>
				<li class="ambh-share-list__item">
					<a href="<?php echo esc_url( $tw_url ); ?>"
						class="ambh-share-btn ambh-share-btn--twitter"
						target="_blank"
						rel="noopener noreferrer"
						aria-label="<?php esc_attr_e( 'Share on X (Twitter)', 'ambassadorhub' ); ?>"
						title="<?php esc_attr_e( 'Share on X (Twitter)', 'ambassadorhub' ); ?>">
						<?php echo self::icon( 'twitter' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<span class="ambh-share-btn__text"><?php esc_html_e( 'X (Twitter)', 'ambassadorhub' ); ?></span>
					</a>
				</li>
				<?php endif; ?>

				<?php if ( $show_whatsapp ) : ?>
				<li class="ambh-share-list__item">
					<a href="<?php echo esc_url( $wa_url ); ?>"
						class="ambh-share-btn ambh-share-btn--whatsapp"
						target="_blank"
						rel="noopener noreferrer"
						aria-label="<?php esc_attr_e( 'Share via WhatsApp', 'ambassadorhub' ); ?>"
						title="<?php esc_attr_e( 'Share via WhatsApp', 'ambassadorhub' ); ?>">
						<?php echo self::icon( 'whatsapp' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<span class="ambh-share-btn__text"><?php esc_html_e( 'WhatsApp', 'ambassadorhub' ); ?></span>
					</a>
				</li>
				<?php endif; ?>

				<?php if ( $show_linkedin ) : ?>
				<li class="ambh-share-list__item">
					<a href="<?php echo esc_url( $li_url ); ?>"
						class="ambh-share-btn ambh-share-btn--linkedin"
						target="_blank"
						rel="noopener noreferrer"
						aria-label="<?php esc_attr_e( 'Share on LinkedIn', 'ambassadorhub' ); ?>"
						title="<?php esc_attr_e( 'Share on LinkedIn', 'ambassadorhub' ); ?>">
						<?php echo self::icon( 'linkedin' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<span class="ambh-share-btn__text"><?php esc_html_e( 'LinkedIn', 'ambassadorhub' ); ?></span>
					</a>
				</li>
				<?php endif; ?>

				
			<?php if ( $show_threads ) : ?>
			<li class="ambh-share-list__item">
				<a href="<?php echo esc_url( $threads_url ); ?>"
					class="ambh-share-btn ambh-share-btn--threads"
					target="_blank"
					rel="noopener noreferrer"
					aria-label="<?php esc_attr_e( 'Share on Threads', 'ambassadorhub' ); ?>"
					title="<?php esc_attr_e( 'Share on Threads', 'ambassadorhub' ); ?>">
					<?php echo self::icon( 'threads' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<span class="ambh-share-btn__text"><?php esc_html_e( 'Threads', 'ambassadorhub' ); ?></span>
				</a>
			</li>
			<?php endif; ?>

			<?php if ( $show_mastodon ) : ?>
			<li class="ambh-share-list__item">
				<button type="button"
						class="ambh-share-btn ambh-share-btn--mastodon"
						data-ambh-mastodon-text="<?php echo esc_attr( $mastodon_text ); ?>"
						data-ambh-mastodon-instance="<?php echo esc_attr( $mastodon_instance ); ?>"
						data-ambh-mastodon-prompt="<?php esc_attr_e( 'Enter your Mastodon instance (e.g. mastodon.social):', 'ambassadorhub' ); ?>"
						aria-label="<?php esc_attr_e( 'Share on Mastodon', 'ambassadorhub' ); ?>"
						title="<?php esc_attr_e( 'Share on Mastodon', 'ambassadorhub' ); ?>">
					<?php echo self::icon( 'mastodon' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<span class="ambh-share-btn__text"><?php esc_html_e( 'Mastodon', 'ambassadorhub' ); ?></span>
				</button>
			</li>
			<?php endif; ?>

			<?php if ( $show_bluesky ) : ?>
			<li class="ambh-share-list__item">
				<a href="<?php echo esc_url( $bsky_url ); ?>"
					class="ambh-share-btn ambh-share-btn--bluesky"
					target="_blank"
					rel="noopener noreferrer"
					aria-label="<?php esc_attr_e( 'Share on Bluesky', 'ambassadorhub' ); ?>"
					title="<?php esc_attr_e( 'Share on Bluesky', 'ambassadorhub' ); ?>">
					<?php echo self::icon( 'bluesky' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<span class="ambh-share-btn__text"><?php esc_html_e( 'Bluesky', 'ambassadorhub' ); ?></span>
				</a>
			</li>
			<?php endif; ?>

			<?php if ( $show_email ) : ?>
				<li class="ambh-share-list__item">
					<a href="<?php echo esc_url( $em_url ); ?>"
						class="ambh-share-btn ambh-share-btn--email"
						aria-label="<?php esc_attr_e( 'Share via Email', 'ambassadorhub' ); ?>"
						title="<?php esc_attr_e( 'Share via Email', 'ambassadorhub' ); ?>">
						<?php echo self::icon( 'email' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<span class="ambh-share-btn__text"><?php esc_html_e( 'Email', 'ambassadorhub' ); ?></span>
					</a>
				</li>
				<?php endif; ?>

				<?php if ( $show_copy_link ) : ?>
				<li class="ambh-share-list__item">
					<button type="button"
							class="ambh-share-btn ambh-share-btn--copy"
							data-ambh-copy-url="<?php echo esc_attr( $url ); ?>"
							data-ambh-copied-label="<?php esc_attr_e( 'Copied!', 'ambassadorhub' ); ?>"
							aria-label="<?php esc_attr_e( 'Copy link', 'ambassadorhub' ); ?>"
							title="<?php esc_attr_e( 'Copy link', 'ambassadorhub' ); ?>">
						<?php echo self::icon( 'copy' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<span class="ambh-share-btn__text"><?php esc_html_e( 'Copy link', 'ambassadorhub' ); ?></span>
					</button>
				</li>
				<?php endif; ?>

			</ul>
		</div>
		<?php

		// Inline scripts — self-contained, no external deps.
		if ( $show_copy_link || $show_mastodon ) {
			self::maybe_print_copy_script();
		}

		return (string) ob_get_clean();
	}

	/**
	 * Print the clipboard-copy and Mastodon inline scripts once per page load.
	 *
	 * Handles two delegated click patterns:
	 *  - [data-ambh-copy-url]       — copy link to clipboard
	 *  - [data-ambh-mastodon-text]  — prompt for instance (cached in localStorage), open share URL
	 *
	 * @return void
	 */
	private static function maybe_print_copy_script(): void {
		static $printed = false;
		if ( $printed ) {
			return;
		}
		$printed = true;

		InlineAssets::script(
			"(function(){\n"
			. "  if(window._ambhShareInit)return;\n"
			. "  window._ambhShareInit=true;\n"
			. "  document.addEventListener('click',function(e){\n"
			// Copy link handler.
			. "    var cb=e.target.closest('[data-ambh-copy-url]');\n"
			. "    if(cb){\n"
			. "      var url=cb.getAttribute('data-ambh-copy-url');\n"
			. "      var lbl=cb.getAttribute('data-ambh-copied-label')||'Copied!';\n"
			. "      var tx=cb.querySelector('.ambh-share-btn__text');\n"
			. "      var orig=tx?tx.textContent:'';\n"
			. "      function ok(){if(tx)tx.textContent=lbl;cb.setAttribute('aria-pressed','true');setTimeout(function(){if(tx)tx.textContent=orig;cb.removeAttribute('aria-pressed');},2000);}\n"
			. "      if(navigator.clipboard&&window.isSecureContext){navigator.clipboard.writeText(url).then(ok)['catch'](function(){cpFb(url);ok();});}else{cpFb(url);ok();}\n"
			. "      return;\n"
			. "    }\n"
			// Mastodon handler.
			. "    var mb=e.target.closest('[data-ambh-mastodon-text]');\n"
			. "    if(mb){\n"
			. "      var KEY='ambh_mastodon_instance';\n"
			. "      var inst=mb.getAttribute('data-ambh-mastodon-instance')||'';\n"
			. "      if(!inst){try{inst=localStorage.getItem(KEY)||'';}catch(ex){}}\n"
			. "      if(!inst){\n"
			. "        var msg=mb.getAttribute('data-ambh-mastodon-prompt')||'Enter your Mastodon instance (e.g. mastodon.social):';\n"
			. "        inst=(window.prompt(msg,'mastodon.social')||'').trim().replace(/^\\/+|\\/+\$/g,'').replace(/^https?:\\/\\//i,'');\n"
			. "        if(inst){try{localStorage.setItem(KEY,inst);}catch(ex){}}\n"
			. "      }\n"
			. "      if(inst){window.open('https://'+inst+'/share?text='+mb.getAttribute('data-ambh-mastodon-text'),'_blank','noopener,noreferrer');}\n"
			. "      return;\n"
			. "    }\n"
			. "  });\n"
			. "  function cpFb(t){var a=document.createElement('textarea');a.value=t;a.style.cssText='position:fixed;opacity:0;top:0;left:0';document.body.appendChild(a);a.focus();a.select();try{document.execCommand('copy');}catch(e){}document.body.removeChild(a);}\n"
			. '})();'
		);
	}

	/**
	 * Return an inline SVG icon for the given platform.
	 *
	 * All paths use viewBox="0 0 24 24" and fill="currentColor" so they inherit
	 * the button's text colour and scale with the font-size / width/height CSS.
	 *
	 * @param string $platform facebook|twitter|whatsapp|linkedin|email|copy
	 * @return string Safe SVG markup.
	 */
	private static function icon( string $platform ): string {
		$paths = array(
			'facebook' => '<path d="M9.198 21.5h4v-8.01h3.604l.396-3.98h-4V7.5a1 1 0 0 1 1-1h3v-4h-3a5 5 0 0 0-5 5v2.01h-2l-.396 3.98h2.396v8.01Z"/>',
			'twitter'  => '<path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-4.714-6.231-5.401 6.231H2.744l7.73-8.835L2.312 2.25h6.555l4.26 5.632 5.117-5.632Zm-1.161 17.52h1.833L7.084 4.126H5.117Z"/>',
			'whatsapp' => '<path d="M.057 24 1.744 17.837A11.949 11.949 0 0 1 .15 12C.154 5.379 5.533 0 12.154 0a11.94 11.94 0 0 1 8.491 3.516A11.94 11.94 0 0 1 24.145 12c-.004 6.622-5.383 12-12.004 12a12.01 12.01 0 0 1-5.735-1.456L.057 24Zm6.597-3.807a9.964 9.964 0 0 0 5.114 1.406c5.523 0 10.018-4.493 10.021-10.016A9.957 9.957 0 0 0 12.15 2.098C6.623 2.098 2.128 6.59 2.126 12.112a9.978 9.978 0 0 0 1.534 5.306l-.239.874 2.233-.599Zm11.387-5.464c-.074-.124-.272-.198-.571-.347-.297-.149-1.757-.867-2.029-.967-.272-.099-.47-.148-.669.15-.198.297-.768.967-.941 1.165-.173.198-.347.223-.644.074-.297-.149-1.255-.462-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.135-.133.298-.347.447-.521.149-.173.198-.297.297-.495.099-.199.05-.372-.025-.521-.074-.149-.669-1.612-.916-2.207-.242-.579-.487-.501-.669-.51a12.8 12.8 0 0 0-.57-.01c-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.095 3.2 5.076 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.757-.719 2.006-1.413.248-.695.248-1.29.173-1.414Z"/>',
			'linkedin' => '<path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286ZM5.337 7.433a2.062 2.062 0 0 1-2.063-2.065 2.064 2.064 0 1 1 2.063 2.065Zm1.782 13.019H3.555V9h3.564v11.452ZM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003Z"/>',
			'threads'  => '<path d="M141.537 88.9883C140.71 88.5919 139.87 88.2104 139.019 87.8451C137.537 60.5382 122.616 44.905 97.5619 44.745C97.4484 44.7443 97.3355 44.7443 97.222 44.7443C82.2364 44.7443 69.7731 51.1409 62.102 62.7807L75.881 72.2328C81.6116 63.5383 90.6052 61.6848 97.2286 61.6848C97.3051 61.6848 97.3819 61.6848 97.4576 61.6855C105.707 61.7381 111.932 64.1366 115.961 68.814C118.893 72.2193 120.854 76.925 121.825 82.8638C114.511 81.6207 106.601 81.2385 98.145 81.7233C74.3247 83.0954 59.0111 96.9879 60.0396 116.292C60.5615 126.084 65.4397 134.508 73.775 140.011C80.8224 144.663 89.899 146.938 99.3323 146.423C111.79 145.74 121.563 140.987 128.381 132.296C133.559 125.696 136.834 117.143 138.28 106.366C144.217 109.949 148.617 114.664 151.047 120.332C155.179 129.967 155.42 145.8 142.501 158.708C131.182 170.016 117.576 174.908 97.0135 175.059C74.2042 174.89 56.9538 167.575 45.7381 153.317C35.2355 139.966 29.8077 120.682 29.6052 96C29.8077 71.3178 35.2355 52.0336 45.7381 38.6827C56.9538 24.4249 74.2039 17.11 97.0132 16.9405C119.988 17.1113 137.539 24.4614 149.184 38.788C154.894 45.8136 159.199 54.6488 162.037 64.9503L178.184 60.6422C174.744 47.9622 169.331 37.0357 161.965 27.974C147.036 9.60668 125.202 0.195148 97.0695 0H96.9569C68.8816 0.19447 47.2921 9.6418 32.7883 28.0793C19.8819 44.4864 13.2244 67.3157 13.0007 95.9325L13 96L13.0007 96.0675C13.2244 124.684 19.8819 147.514 32.7883 163.921C47.2921 182.358 68.8816 191.806 96.9569 192H97.0695C122.03 191.827 139.624 185.292 154.118 170.811C173.081 151.866 172.51 128.119 166.26 113.541C161.776 103.087 153.227 94.5962 141.537 88.9883ZM98.4405 129.507C88.0005 130.095 77.1544 125.409 76.6196 115.372C76.2232 107.93 81.9158 99.626 99.0812 98.6368C101.047 98.5234 102.976 98.468 104.871 98.468C111.106 98.468 116.939 99.0737 122.242 100.233C120.264 124.935 108.662 128.946 98.4405 129.507Z"/>',
			'mastodon' => '<path d="M23.268 5.313c-.35-2.578-2.617-4.61-5.304-5.004C17.51.242 15.792 0 11.813 0h-.03c-3.98 0-4.835.242-5.288.309C3.882.692 1.496 2.518.917 5.127.64 6.412.61 7.837.661 9.143c.074 1.874.088 3.745.26 5.611.118 1.24.325 2.47.62 3.68.55 2.237 2.777 4.098 4.96 4.857 2.336.792 4.849.923 7.256.38.265-.061.527-.132.786-.213.585-.184 1.27-.39 1.774-.753a.057.057 0 0 0 .023-.043v-1.809a.052.052 0 0 0-.02-.041.053.053 0 0 0-.046-.01 20.282 20.282 0 0 1-4.709.545c-2.731 0-3.463-1.284-3.674-1.818a5.593 5.593 0 0 1-.319-1.433.053.053 0 0 1 .066-.054c1.517.363 3.072.546 4.632.546.376 0 .75 0 1.125-.01 1.57-.044 3.224-.124 4.768-.422.038-.008.077-.015.11-.024 2.435-.464 4.753-1.92 4.989-5.604.008-.145.03-1.52.03-1.67.002-.512.167-3.63-.024-5.545zm-3.748 9.195h-2.561V8.29c0-1.309-.55-1.976-1.67-1.976-1.23 0-1.846.79-1.846 2.35v3.403h-2.546V8.663c0-1.56-.617-2.35-1.848-2.35-1.112 0-1.668.668-1.67 1.977v6.218H4.822V8.102c0-1.31.337-2.35 1.011-3.12.696-.77 1.608-1.164 2.74-1.164 1.311 0 2.302.5 2.962 1.498l.638 1.06.638-1.06c.66-.999 1.65-1.498 2.96-1.498 1.13 0 2.043.395 2.74 1.164.675.77 1.012 1.81 1.012 3.12z"/>',
			'bluesky'  => '<path d="M12 10.8c-1.087-2.114-4.046-6.053-6.798-7.995C2.566.944 1.561 1.266.902 1.565.139 1.908 0 3.08 0 3.768c0 .69.378 5.65.624 6.479.815 2.736 3.713 3.66 6.383 3.364.136-.02.275-.039.415-.056-.138.022-.276.04-.415.056-3.912.58-7.387 2.005-2.83 7.078 5.013 5.19 6.87-1.113 7.823-4.308.953 3.195 2.05 9.271 7.733 4.308 4.267-4.308 1.172-6.498-2.74-7.078a8.741 8.741 0 0 1-.415-.056c.14.017.279.036.415.056 2.67.297 5.568-.628 6.383-3.364.246-.828.624-5.79.624-6.478 0-.69-.139-1.861-.902-2.206-.659-.298-1.664-.62-4.3 1.24C16.046 4.748 13.087 8.687 12 10.8Z"/>',
			'email'    => '<path d="M20 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2Zm0 4-8 5-8-5V6l8 5 8-5v2Z"/>',
			'copy'     => '<path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1ZM8 13h8v-2H8v2Zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5Z"/>',
		);

		// Per-platform viewBox overrides (default is "0 0 24 24").
		$viewboxes = array(
			'threads' => '0 0 192 192',
		);

		$path = $paths[ $platform ] ?? '';
		if ( ! $path ) {
			return '';
		}

		$viewbox = $viewboxes[ $platform ] ?? '0 0 24 24';

		return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="' . $viewbox . '" fill="currentColor" aria-hidden="true" focusable="false" width="18" height="18">'
			. $path
			. '</svg>';
	}

	/**
	 * Shortcode handler: [ambh_fundraiser_share].
	 *
	 * Supported attributes mirror the block attributes (snake_case shortcode style).
	 *  show_facebook, show_twitter, show_whatsapp, show_linkedin,
	 *  show_email, show_copy_link, share_text, display_style, label
	 *
	 * @param array $atts Shortcode attributes.
	 * @return string Rendered HTML.
	 */
	public static function render_shortcode( $atts ): string {
		$a = shortcode_atts(
			array(
				'show_facebook'     => '1',
				'show_twitter'      => '1',
				'show_whatsapp'     => '1',
				'show_linkedin'     => '0',
				'show_threads'      => '0',
				'show_mastodon'     => '0',
				'show_bluesky'      => '0',
				'show_email'        => '1',
				'show_copy_link'    => '1',
				'alignment'         => 'left',
				'mastodon_instance' => 'mastodon.social',
				'share_text'        => '',
				'display_style'     => 'buttons',
				'label'             => '',
			),
			$atts,
			'ambh_fundraiser_share'
		);

		return self::render_block(
			array(
				'showFacebook'     => (bool) $a['show_facebook'],
				'showTwitter'      => (bool) $a['show_twitter'],
				'showWhatsapp'     => (bool) $a['show_whatsapp'],
				'showLinkedin'     => (bool) $a['show_linkedin'],
				'showThreads'      => (bool) $a['show_threads'],
				'showMastodon'     => (bool) $a['show_mastodon'],
				'showBluesky'      => (bool) $a['show_bluesky'],
				'showEmail'        => (bool) $a['show_email'],
				'showCopyLink'     => (bool) $a['show_copy_link'],
				'mastodonInstance' => $a['mastodon_instance'],
				'shareText'        => $a['share_text'],
				'displayStyle'     => $a['display_style'],
				'alignment'        => $a['alignment'],
				'label'            => $a['label'],
			)
		);
	}
}
