<?php
/**
 * Footer social icons component.
 *
 * @package Kutby
 * @since   2.7.0
 */

declare(strict_types=1);

namespace Kutby\Footer\Components;

use Kutby\Footer\Contracts\FooterComponentInterface;
use Kutby\Footer\Support\FooterContext;
use Kutby\Helpers\Utils;

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

/**
 * Class SocialIcons
 */
final class SocialIcons implements FooterComponentInterface {

	/**
	 * @var array<string, string>
	 */
	private const NETWORKS = array(
		'facebook'  => 'فيسبوك',
		'twitter'   => 'إكس',
		'telegram'  => 'تيليجرام',
		'whatsapp'  => 'واتساب',
		'youtube'   => 'يوتيوب',
		'instagram' => 'إنستغرام',
	);

	/**
	 * {@inheritdoc}
	 */
	public static function render( FooterContext $ctx, array $args = array() ): void {
		if ( ! $ctx->bool( 'show_social' ) ) {
			return;
		}

		$social = $ctx->social();
		$links  = array();
		foreach ( self::NETWORKS as $key => $label ) {
			$url = isset( $social[ $key ] ) ? trim( (string) $social[ $key ] ) : '';
			if ( '' !== $url ) {
				$links[ $key ] = array(
					'url'   => $url,
					'label' => $label,
				);
			}
		}

		if ( empty( $links ) ) {
			return;
		}

		$class = isset( $args['class'] ) ? (string) $args['class'] : 'kutby-social kutby-footer-social';
		?>
		<ul class="<?php echo esc_attr( $class ); ?>" aria-label="روابط التواصل الاجتماعي">
			<?php foreach ( $links as $network => $item ) : ?>
				<li class="kutby-footer-social__item kutby-footer-social__item--<?php echo esc_attr( $network ); ?>">
					<a
						href="<?php echo esc_url( $item['url'] ); ?>"
						rel="noopener noreferrer"
						target="_blank"
					>
						<span class="kutby-sr-only"><?php echo esc_html( $item['label'] ); ?></span>
						<?php
						$icon = Utils::icon( $network );
						if ( '' !== $icon ) {
							echo $icon; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG via Utils.
						} else {
							echo '<span aria-hidden="true">' . esc_html( strtoupper( substr( $network, 0, 1 ) ) ) . '</span>';
						}
						?>
					</a>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
	}
}
