<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
$stockbeacon_render_form = static function () use(
    $settings,
    $product_id,
    $variation_id,
    $prefilled_email,
    $count
) : void {
    $stockbeacon_template_class = 'stockbeacon-template-card';
    $stockbeacon_theme_mode = ( 'dark' === (string) ($settings['form_theme_mode'] ?? 'light') ? 'dark' : 'light' );
    $stockbeacon_show_waitlist_count = filter_var( $settings['show_waitlist_count'] ?? false, FILTER_VALIDATE_BOOLEAN );
    $stockbeacon_border_radius = 6;
    $stockbeacon_accent = (string) ($settings['form_accent_color'] ?? '#2563eb');
    $stockbeacon_accent_safe = ( sanitize_hex_color( $stockbeacon_accent ) ? $stockbeacon_accent : '#2563eb' );
    $stockbeacon_bg_raw = (string) ($settings['form_background_color'] ?? '#ffffff');
    $stockbeacon_text_raw = (string) ($settings['form_text_color'] ?? '#0f172a');
    $stockbeacon_muted_raw = (string) ($settings['form_description_color'] ?? '#64748b');
    $stockbeacon_input_raw = (string) ($settings['form_input_background_color'] ?? '#ffffff');
    $stockbeacon_count_raw = (string) ($settings['form_count_text_color'] ?? (( 'dark' === $stockbeacon_theme_mode ? '#14532d' : '#334155' )));
    $stockbeacon_bg = ( sanitize_hex_color( ( 'dark' === $stockbeacon_theme_mode && '#ffffff' === strtolower( $stockbeacon_bg_raw ) ? '#101827' : $stockbeacon_bg_raw ) ) ?: '#ffffff' );
    $stockbeacon_text = ( sanitize_hex_color( ( 'dark' === $stockbeacon_theme_mode && '#0f172a' === strtolower( $stockbeacon_text_raw ) ? '#f8fafc' : $stockbeacon_text_raw ) ) ?: '#0f172a' );
    $stockbeacon_muted = ( sanitize_hex_color( ( 'dark' === $stockbeacon_theme_mode && '#64748b' === strtolower( $stockbeacon_muted_raw ) ? '#cbd5e1' : $stockbeacon_muted_raw ) ) ?: '#64748b' );
    $stockbeacon_input_bg = ( sanitize_hex_color( ( 'dark' === $stockbeacon_theme_mode && '#ffffff' === strtolower( $stockbeacon_input_raw ) ? '#0f172a' : $stockbeacon_input_raw ) ) ?: '#ffffff' );
    $stockbeacon_count_text = ( sanitize_hex_color( ( 'dark' === $stockbeacon_theme_mode && '#334155' === strtolower( $stockbeacon_count_raw ) ? '#14532d' : $stockbeacon_count_raw ) ) ?: (( 'dark' === $stockbeacon_theme_mode ? '#14532d' : '#334155' )) );
    $btn_color = (string) ($settings['form_button_color'] ?? $stockbeacon_accent_safe);
    $stockbeacon_button = ( sanitize_hex_color( $btn_color ) ? $btn_color : $stockbeacon_accent_safe );
    $stockbeacon_input_readable = static function ( string $hex ) : array {
        $hex = ltrim( $hex, '#' );
        if ( 6 !== strlen( $hex ) || !ctype_xdigit( $hex ) ) {
            return array('#0f172a', '#64748b');
        }
        $r = hexdec( substr( $hex, 0, 2 ) ) / 255;
        $g = hexdec( substr( $hex, 2, 2 ) ) / 255;
        $b = hexdec( substr( $hex, 4, 2 ) ) / 255;
        $convert = static function ( float $channel ) : float {
            return ( $channel <= 0.03928 ? $channel / 12.92 : (($channel + 0.055) / 1.055) ** 2.4 );
        };
        $luminance = 0.2126 * $convert( $r ) + 0.7151999999999999 * $convert( $g ) + 0.0722 * $convert( $b );
        return ( $luminance > 0.42 ? array('#0f172a', '#64748b') : array('#f8fafc', '#94a3b8') );
    };
    list( $stockbeacon_input_text, $stockbeacon_input_placeholder ) = $stockbeacon_input_readable( $stockbeacon_input_bg );
    $stockbeacon_shadow_rule = ( filter_var( $settings['form_box_shadow_enabled'] ?? true, FILTER_VALIDATE_BOOLEAN ) ? '' : '--stockbeacon-shadow:none;' );
    $stockbeacon_title_font_size = min( 32, max( 12, absint( $settings['form_title_font_size'] ?? 17 ) ) );
    $stockbeacon_description_font_size = min( 24, max( 11, absint( $settings['form_description_font_size'] ?? 14 ) ) );
    $stockbeacon_input_font_size = min( 22, max( 12, absint( $settings['form_input_font_size'] ?? 16 ) ) );
    $stockbeacon_button_font_size = min( 22, max( 12, absint( $settings['form_button_font_size'] ?? 14 ) ) );
    $stockbeacon_count_font_size = min( 18, max( 10, absint( $settings['form_count_font_size'] ?? 13 ) ) );
    $stockbeacon_success_font_size = min( 24, max( 11, absint( $settings['form_success_font_size'] ?? 15 ) ) );
    $stockbeacon_inline_style = sprintf(
        '--stockbeacon-accent:%1$s;--stockbeacon-button:%2$s;--stockbeacon-radius:%3$dpx;--stockbeacon-bg:%4$s;--stockbeacon-text:%5$s;--stockbeacon-text-muted:%6$s;--stockbeacon-input-bg:%7$s;--stockbeacon-input-text:%8$s;--stockbeacon-input-placeholder:%9$s;--stockbeacon-count-text:%10$s;%11$s--stockbeacon-title-font-size:%12$dpx;--stockbeacon-description-font-size:%13$dpx;--stockbeacon-input-font-size:%14$dpx;--stockbeacon-button-font-size:%15$dpx;--stockbeacon-count-font-size:%16$dpx;--stockbeacon-success-font-size:%17$dpx;',
        esc_attr( $stockbeacon_accent_safe ),
        esc_attr( $stockbeacon_button ),
        $stockbeacon_border_radius,
        esc_attr( $stockbeacon_bg ),
        esc_attr( $stockbeacon_text ),
        esc_attr( $stockbeacon_muted ),
        esc_attr( $stockbeacon_input_bg ),
        esc_attr( $stockbeacon_input_text ),
        esc_attr( $stockbeacon_input_placeholder ),
        esc_attr( $stockbeacon_count_text ),
        $stockbeacon_shadow_rule,
        $stockbeacon_title_font_size,
        $stockbeacon_description_font_size,
        $stockbeacon_input_font_size,
        $stockbeacon_button_font_size,
        $stockbeacon_count_font_size,
        $stockbeacon_success_font_size
    );
    ?>
<div class="stockbeacon-form-wrapper <?php 
    echo esc_attr( $stockbeacon_template_class . ' stockbeacon-theme-' . $stockbeacon_theme_mode );
    ?>" style="<?php 
    echo esc_attr( $stockbeacon_inline_style );
    ?>" data-product-id="<?php 
    echo esc_attr( (string) $product_id );
    ?>" data-variation-id="<?php 
    echo esc_attr( (string) $variation_id );
    ?>" data-show-count="<?php 
    echo ( $stockbeacon_show_waitlist_count ? '1' : '0' );
    ?>">
	<div class="stockbeacon-form">
		<div class="stockbeacon-head">
			<h4 class="stockbeacon-title"><?php 
    echo esc_html( $settings['form_title'] );
    ?></h4>
			<p class="stockbeacon-description"><?php 
    echo esc_html( $settings['form_description'] );
    ?></p>
		</div>
		<div class="stockbeacon-input-group">
			<input type="email" class="stockbeacon-email" placeholder="<?php 
    echo esc_attr( $settings['form_placeholder'] );
    ?>" value="<?php 
    echo esc_attr( $prefilled_email );
    ?>" required autocomplete="email" enterkeyhint="send" inputmode="email" />
			<button type="button" class="stockbeacon-submit button"><?php 
    echo esc_html( $settings['form_button_text'] );
    ?></button>
		</div>
		<?php 
    if ( !empty( $settings['show_consent_checkbox'] ) ) {
        ?>
		<label class="stockbeacon-consent"><input type="checkbox" class="stockbeacon-consent-checkbox" required /><span><?php 
        echo esc_html( $settings['consent_text'] );
        ?></span></label>
		<?php 
    }
    ?>
		<?php 
    if ( $stockbeacon_show_waitlist_count ) {
        ?>
		<?php 
        /* translators: %d is the number of customers waiting for the product. */
        $stockbeacon_waiting_singular = __( '%d person is waiting', 'stockbeacon-product-waitlists-for-woocommerce' );
        /* translators: %d is the number of customers waiting for the product. */
        $stockbeacon_waiting_plural = __( '%d people are waiting', 'stockbeacon-product-waitlists-for-woocommerce' );
        $stockbeacon_waiting_text = '';
        if ( $count > 0 ) {
            /* translators: %d is the number of customers waiting for the product. */
            $stockbeacon_waiting_text = sprintf( _n(
                '%d person is waiting',
                '%d people are waiting',
                $count,
                'stockbeacon-product-waitlists-for-woocommerce'
            ), $count );
        }
        ?>
		<p class="stockbeacon-count" role="status" data-singular="<?php 
        echo esc_attr( $stockbeacon_waiting_singular );
        ?>" data-plural="<?php 
        echo esc_attr( $stockbeacon_waiting_plural );
        ?>"<?php 
        echo ( $count > 0 ? '' : ' style="display:none;"' );
        ?>><span class="stockbeacon-count__dot" aria-hidden="true"></span><span class="stockbeacon-count__text"><?php 
        echo esc_html( $stockbeacon_waiting_text );
        ?></span></p>
		<?php 
    }
    ?>
		<div class="stockbeacon-message" style="display:none;"></div>
	</div>
	<div class="stockbeacon-success" style="display:none;"><p class="stockbeacon-success-message"><?php 
    echo esc_html( $settings['form_success_message'] );
    ?></p></div>
</div>
<?php 
};
$stockbeacon_render_form();