<?php
/**
 * Shared front-end and admin layout rules for captcha widgets.
 *
 * @package WP_PowerSuite\Captcha\Shared
 * @since 1.5.0
 */

namespace WP_PowerSuite\Captcha\Shared;

// phpcs:disable PluginCheck.CodeAnalysis.Heredoc.NotAllowed -- generated CSS strings, not user-facing PHP.

/**
 * Global widget containment (Turnstile + reCAPTCHA v2).
 */
final class WidgetLayoutStyles {

	private const STYLE_ID = 'wpps-captcha-widget-layout';

	private static bool $front_end_printed = false;

	private static bool $core_auth_layout_enqueued = false;

	/**
	 * Mark that wp-login / core auth layout CSS is already on the login stylesheet.
	 */
	public static function markCoreAuthLayoutEnqueued(): void {
		self::$core_auth_layout_enqueued = true;
	}

	/**
	 * Whether the widget should stretch to the form field width (default on).
	 *
	 * @param array<string, mixed> $settings Module settings from getSettings().
	 */
	public static function widgetFullWidthEnabled( array $settings ): bool {
		if ( ! array_key_exists( 'widget_full_width', $settings ) ) {
			return true;
		}

		return ! empty( $settings['widget_full_width'] );
	}

	/**
	 * Merge render options when full-width widget is enabled.
	 *
	 * @param array<string, mixed> $settings Module settings.
	 * @param array<string, mixed> $options  Render options passed to renderWidget().
	 * @return array<string, mixed>
	 */
	public static function mergeRenderOptions( array $settings, array $options = [] ): array {
		if ( self::widgetFullWidthEnabled( $settings ) ) {
			$options['match_container_width'] = true;
		}

		return $options;
	}

	/**
	 * Turnstile data-size for the current settings.
	 *
	 * @param array<string, mixed> $settings Module settings.
	 */
	public static function turnstileApiSize( array $settings ): string {
		return self::widgetFullWidthEnabled( $settings ) ? 'flexible' : 'normal';
	}

	/**
	 * Print layout CSS once per front-end request (before widget markup).
	 */
	public static function printFrontEndOnce(): void {
		if ( self::$front_end_printed || is_admin() ) {
			return;
		}
		self::$front_end_printed = true;

		if ( self::$core_auth_layout_enqueued ) {
			return;
		}

		echo '<style id="' . esc_attr( self::STYLE_ID ) . '">';
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- hardcoded plugin CSS; esc_html() would break selectors.
		echo wp_strip_all_tags( self::getFrontEndCss() );
		echo '</style>';
	}

	/**
	 * Enqueue layout CSS in wp-admin (Basic Forms preview, module settings test widget).
	 */
	public static function enqueueAdmin(): void {
		if ( ! is_admin() ) {
			return;
		}

		$handle = self::STYLE_ID . '-admin';
		if ( ! wp_style_is( $handle, 'registered' ) ) {
			wp_register_style( $handle, false, [], '1.0.2' );
		}
		wp_enqueue_style( $handle );
		wp_add_inline_style( $handle, self::getFrontEndCss() . self::getAdminTestWidgetCss() );
	}

	public static function getFrontEndCss(): string {
		return <<<'CSS'
.wpps-captcha-cushion{
	box-sizing:border-box;
	width:100%;
	max-width:100%;
	clear:both;
	overflow:visible;
	text-align:left;
}
.wpps-captcha-cushion .wpps-turnstile-embed{
	box-sizing:border-box;
	min-width:0;
	max-width:100%;
	display:block;
	line-height:0;
	text-align:left;
}
.wpps-captcha-cushion .wpps-g-recaptcha-v2{
	display:block;
	width:304px;
	max-width:100%;
	min-height:78px;
	overflow:visible;
}
.wpps-captcha-cushion .wpps-g-recaptcha-v2>div,
.wpps-captcha-cushion .wpps-g-recaptcha-v2 iframe{
	max-width:none!important;
}
/* Google injects this with display:none; themes that force textarea{display:block!important}
   reveal the raw token next to Place Order / form submits. Keep it visually hidden. */
textarea.g-recaptcha-response{
	display:none!important;
	position:absolute!important;
	width:1px!important;
	height:1px!important;
	min-height:0!important;
	max-height:1px!important;
	padding:0!important;
	margin:-1px!important;
	overflow:hidden!important;
	clip:rect(0,0,0,0)!important;
	border:0!important;
	opacity:0!important;
	pointer-events:none!important;
	resize:none!important;
	visibility:hidden!important;
}
.woocommerce .wpps-captcha-cushion,
#commentform .wpps-captcha-cushion,
.wpcf7-form .wpps-captcha-cushion,
.wpforms-form .wpps-captcha-cushion,
.fluentform .wpps-captcha-cushion,
.frm_forms .wpps-captcha-cushion,
form.elementor-form .wpps-captcha-cushion{
	max-width:100%;
}
CSS
			. self::getCoreFormFitCss();
	}

	public static function getCoreFormFitCss(): string {
		return <<<'CSS'
.wpps-captcha-cushion.wpps-captcha-core-form-fit,
.wpps-bf-form .wpps-captcha-cushion{
	box-sizing:border-box;
	max-width:100%;
	clear:both;
}
.wpps-captcha-cushion.wpps-captcha-core-form-fit .wpps-turnstile-embed{
	width:100%;
	max-width:100%;
}
.wpps-captcha-cushion.wpps-captcha-core-form-fit .cf-turnstile,
.wpps-captcha-cushion.wpps-captcha-core-form-fit .wpps-turnstile-widget{
	width:100%!important;
	max-width:100%;
}
CSS;
	}

	public static function getAdminTestWidgetCss(): string {
		return <<<'CSS'
.wp-powersuite-modal .wpps-captcha-test-widget,
.wpps-captcha-test-widget{
	box-sizing:border-box;
	display:block;
	width:100%;
	max-width:min(100%,350px);
	min-width:0;
	min-height:70px;
	overflow:visible;
	text-align:left;
	margin:0;
}
.wp-powersuite-modal .wpps-captcha-test-row{
	align-items:flex-start;
}
CSS;
	}
}
