<?php
/**
 * Social network presets for maintenance page links.
 *
 * @package MacaHold
 */

defined( 'ABSPATH' ) || exit;

/**
 * Social network metadata and icons.
 */
class Maca_Hold_Social_Networks {

	/**
	 * Supported social networks.
	 *
	 * @return array<string, array{label: string, placeholder: string}>
	 */
	public static function get_networks() {
		return array(
			'facebook'  => array(
				'label'       => 'Facebook',
				'placeholder' => 'https://facebook.com/your-page',
			),
			'instagram' => array(
				'label'       => 'Instagram',
				'placeholder' => 'https://instagram.com/your-account',
			),
			'x'         => array(
				'label'       => 'X',
				'placeholder' => 'https://x.com/your-account',
			),
			'linkedin'  => array(
				'label'       => 'LinkedIn',
				'placeholder' => 'https://linkedin.com/company/your-page',
			),
			'youtube'   => array(
				'label'       => 'YouTube',
				'placeholder' => 'https://youtube.com/@your-channel',
			),
			'tiktok'    => array(
				'label'       => 'TikTok',
				'placeholder' => 'https://tiktok.com/@your-account',
			),
			'custom'    => array(
				'label'       => __( 'Custom', 'maca-hold' ),
				'placeholder' => 'https://',
			),
		);
	}

	/**
	 * Human-readable label for a network slug.
	 *
	 * @param string $network Network slug.
	 * @return string
	 */
	public static function get_label( $network ) {
		$networks = self::get_networks();
		$network  = sanitize_key( $network );

		if ( isset( $networks[ $network ] ) ) {
			return (string) $networks[ $network ]['label'];
		}

		return __( 'Link', 'maca-hold' );
	}

	/**
	 * Whether a network slug is valid.
	 *
	 * @param string $network Network slug.
	 * @return bool
	 */
	public static function is_valid( $network ) {
		$network = sanitize_key( $network );

		return isset( self::get_networks()[ $network ] );
	}

	/**
	 * Resolve bundled icon file slug for a network.
	 *
	 * @param string $network Network slug.
	 * @return string
	 */
	public static function get_icon_slug( $network ) {
		$network = sanitize_key( $network );

		if ( 'custom' === $network ) {
			return 'link';
		}

		$file = MACA_HOLD_PATH . 'assets/icons/' . $network . '.svg';

		return file_exists( $file ) ? $network : 'link';
	}

	/**
	 * Get bundled icon image URL for a network.
	 *
	 * @param string $network Network slug.
	 * @return string
	 */
	public static function get_icon_url( $network ) {
		$slug = self::get_icon_slug( $network );

		return MACA_HOLD_URL . 'assets/icons/' . $slug . '.svg';
	}

	/**
	 * Render a social icon from bundled artwork or a custom image URL.
	 *
	 * @param string $network  Network slug.
	 * @param string $icon_url Optional custom icon image URL.
	 * @return string
	 */
	public static function render_icon( $network, $icon_url = '' ) {
		$icon_url = esc_url_raw( $icon_url );

		if ( '' !== $icon_url ) {
			return sprintf(
				'<img class="maca-hold-social-icon maca-hold-social-icon--image" src="%1$s" alt="" width="22" height="22" decoding="async" loading="lazy" />',
				esc_url( $icon_url )
			);
		}

		$mask_url = esc_url( self::get_icon_url( $network ) );

		return sprintf(
			'<span class="maca-hold-social-icon" style="-webkit-mask-image:url(%1$s);mask-image:url(%1$s)" aria-hidden="true"></span>',
			$mask_url
		);
	}
}
