<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Public rendering helpers retain established names for template compatibility.
/**
 * Reusable Close Button Component
 * 
 * This component provides a unified, accessible close button for all popup types.
 * It maintains backward compatibility with existing extensions by preserving
 * the original class names and structure.
 *
 * @package Instant_Popup_Builder
 * @since 1.0.0
 */

if (! defined('ABSPATH')) exit; // Exit if accessed directly

/**
 * Render the close button HTML
 * 
 * @param array $args {
 *     Optional. Arguments to customize the close button.
 *
 *     @type string $popup_type   The popup type: 'text', 'image', 'html', 'subscription'. Default 'text'.
 *     @type string $icon_class   Font Awesome icon class. Default 'fa-solid fa-circle-xmark'.
 *     @type string $color        Icon color (CSS color value). Default empty (uses CSS).
 *     @type string $custom_class Additional CSS classes. Default empty.
 *     @type bool   $has_image    Unused; kept for backward compatibility with callers passing the key.
 * }
 * @return void
 */
function ipb_render_close_button( $args = array() ) {
    $defaults = array(
        'popup_type'   => 'text',
        'icon_class'   => 'fa-solid fa-circle-xmark',
        'color'        => '',
        'custom_class' => '',
        'has_image'    => false,
    );
    
    $args = wp_parse_args( $args, $defaults );
    
    // Inline color only when explicitly requested; subscription uses CSS (.intant-popup-type-subscription) for default contrast.
    $icon_style = '';
    if ( ! empty( $args['color'] ) ) {
        $icon_style = 'color:' . esc_attr( $args['color'] ) . ';';
    }
    
    // Build CSS classes - maintain original class for backward compatibility
    $wrapper_classes = 'instant-cross';
    if ( ! empty( $args['custom_class'] ) ) {
        $wrapper_classes .= ' ' . esc_attr( $args['custom_class'] );
    }
    
    // Accessibility label - translatable
    $aria_label = __( 'Close popup', 'instant-popup-builder' );
    
    ?>
    <div class="<?php echo esc_attr( $wrapper_classes ); ?>" 
         role="button" 
         tabindex="0" 
         aria-label="<?php echo esc_attr( $aria_label ); ?>">
        <i class="<?php echo esc_attr( $args['icon_class'] ); ?>"<?php echo $icon_style ? ' style="' . esc_attr( $icon_style ) . '"' : ''; ?> aria-hidden="true"></i>
    </div>
    <?php
}

/**
 * Get the close button HTML as a string (for use in output buffering scenarios)
 * 
 * @param array $args Same as ipb_render_close_button()
 * @return string The close button HTML
 */
function ipb_get_close_button( $args = array() ) {
    ob_start();
    ipb_render_close_button( $args );
    return ob_get_clean();
}
