<?php
/**
 * Markup for social login buttons (shared styles via classes).
 *
 * @package WP_PowerSuite\OAuth\UI
 */

namespace WP_PowerSuite\OAuth\UI;

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

final class ButtonRenderer {

	/**
	 * @param array<string, mixed> $opts type, style, size, show_text, provider, width, width_custom, align, display
	 */
	public static function render( string $label, string $url, string $icon_svg, array $opts = [] ): string {
		$type  = in_array( $opts['type'] ?? 'button', [ 'button', 'icon' ], true ) ? (string) ( $opts['type'] ?? 'button' ) : 'button';
		$style = in_array( $opts['style'] ?? 'default', [ 'default', 'outline', 'minimal' ], true ) ? (string) ( $opts['style'] ?? 'default' ) : 'default';
		$size  = in_array( $opts['size'] ?? 'medium', [ 'small', 'medium', 'large' ], true ) ? (string) ( $opts['size'] ?? 'medium' ) : 'medium';
		$show  = ! isset( $opts['show_text'] ) || (bool) ( $opts['show_text'] );

		$width         = ButtonStyleHelper::sanitizeWidth( (string) ( $opts['width'] ?? 'full' ) );
		$width_custom  = ButtonStyleHelper::sanitizeWidthCustom( (string) ( $opts['width_custom'] ?? '280px' ) );
		$align         = ButtonStyleHelper::sanitizeAlign( (string) ( $opts['align'] ?? 'center' ) );

		$classes = [ 'wpps-sl-btn', 'wpps-sl-btn--' . $style, 'wpps-sl-btn--' . $size, 'wpps-sl--' . $type ];
		$classes[] = 'wpps-sl--w-' . $width;
		$classes[] = 'wpps-sl--a-' . $align;
		if ( ! empty( $opts['provider'] ) ) {
			$classes[] = 'wpps-sl--' . sanitize_key( (string) $opts['provider'] );
		}
		$icon_only = ( $type === 'icon' ) || ! $show;
		if ( $icon_only ) {
			$classes[] = 'wpps-sl-btn--icon-only';
		}
		$inline_style = '';
		if ( 'custom' === $width ) {
			$inline_style = ' style="width:' . esc_attr( $width_custom ) . ';max-width:100%;"';
		}
		$cls     = esc_attr( implode( ' ', $classes ) );
		$label_t = ( $icon_only && $label ) ? ' aria-label="' . esc_attr( $label ) . '"' : '';
		$text    = $show ? '<span class="wpps-sl-btn__text">' . esc_html( $label ) . '</span>' : '';
		$icon    = $icon_svg ? '<span class="wpps-sl-btn__icon" aria-hidden="true">' . $icon_svg . '</span>' : '';

		return '<a href="' . esc_url( $url ) . '" class="' . $cls . '"' . $inline_style . ' rel="nofollow"' . $label_t . '>' . $icon . $text . '</a>';
	}
}
