<?php

/**
 * Build shared typography CSS and optional additional CSS fragments.
 *
 * @package B3WVS\VariationSwatches\Assets
 */
namespace B3WVS\VariationSwatches\Assets;

defined( 'ABSPATH' ) || exit;
// Load the shared helpers eagerly; supplemental emitters load only when available.
require_once __DIR__ . '/TypographyRenderer/FontStackNormalizer.php';
use function B3WVS\VariationSwatches\Assets\TypographyRenderer\font_family_rule;
/**
 * TypographyRenderer Class
 */
class TypographyRenderer {
    /**
     * Extract font family from a font stack string.
     *
     * @param string $stack Font stack (e.g., "Inter, sans-serif").
     * @return string Family name or empty string.
     */
    public function extract_family_name_from_font_stack( $stack ) {
        return extract_family_name_from_font_stack( $stack );
    }

    /**
     * Build full typography and swatch CSS block.
     *
     * @param array  $settings Settings array.
     * @param string $scope_prefix CSS selector prefix.
     * @param array  $families_meta Local font metadata.
     * @param bool   $include_all_installed_local_fonts Whether to use all installed fonts.
     * @return string CSS output.
     */
    public function build_typography_styles(
        $settings,
        $scope_prefix,
        $families_meta,
        $include_all_installed_local_fonts
    ) {
        $settings = ( is_array( $settings ) ? $settings : array() );
        $scope_prefix = ( is_string( $scope_prefix ) ? $scope_prefix : '' );
        $prefix = esc_attr( $scope_prefix );
        $families_meta = ( is_array( $families_meta ) ? $families_meta : array() );
        $heading_color = ( isset( $settings['heading_font_color'] ) ? sanitize_text_field( $settings['heading_font_color'] ) : '#000000' );
        $heading_size = ( isset( $settings['heading_font_size'] ) ? absint( $settings['heading_font_size'] ) : 16 );
        $heading_weight_value = '600';
        $heading_family_value = 'inherit';
        $label_font_size = ( isset( $settings['label_font_size'] ) ? absint( $settings['label_font_size'] ) : 14 );
        $label_weight_value = '500';
        $label_text_color = ( isset( $settings['label_text_color'] ) ? sanitize_text_field( $settings['label_text_color'] ) : '' );
        $label_family_value = 'inherit';
        $css = '';
        // Fallback styles for uninitialized containers.
        if ( '' === $scope_prefix ) {
            $css .= 'html.b3-wvs-js form.variations_form:has(.b3-wvs-swatches-container:not(.b3-wvs-initialized):not(.b3-wvs-fallback)){opacity:0 !important;pointer-events:none !important;}';
            $css .= 'html.b3-wvs-js .b3-wvs-swatches-container:not(.b3-wvs-initialized):not(.b3-wvs-fallback) .b3-wvs-original-select{display:none !important;}';
            $css .= 'html.b3-wvs-js .b3-wvs-swatches-container.b3-wvs-fallback .b3-wvs-original-select{display:block !important;}';
        }
        $css .= $prefix . '.variations label, ' . $prefix . '.variations th, ' . $prefix . '.variations .label, ' . $prefix . 'table.variations label, ' . $prefix . 'table.variations th {';
        $css .= sprintf( 'color: %s !important;', esc_html( $heading_color ) );
        $css .= sprintf( 'font-size: %spx !important;', esc_html( (string) $heading_size ) );
        $css .= sprintf( 'font-weight: %s !important;', esc_html( $heading_weight_value ) );
        if ( $heading_family_value && 'inherit' !== $heading_family_value ) {
            $css .= font_family_rule( $heading_family_value );
        }
        $css .= '}';
        $css .= $prefix . '.b3-wvs-swatches-container button, ' . $prefix . '.b3-wvs-swatches-container [role="button"] {';
        $css .= sprintf( 'font-size: %spx !important;', esc_html( (string) $label_font_size ) );
        $css .= sprintf( 'font-weight: %s !important;', esc_html( $label_weight_value ) );
        if ( $label_family_value && 'inherit' !== $label_family_value ) {
            $css .= font_family_rule( $label_family_value );
        }
        $css .= '}';
        if ( '' !== trim( $label_text_color ) ) {
            $css .= $prefix . '.b3-wvs-swatches-container button:not([style*="color"]), ' . $prefix . '.b3-wvs-swatches-container [role="button"]:not([style*="color"]) {';
            $css .= sprintf( 'color: %s !important;', esc_html( $label_text_color ) );
            $css .= '}';
        }
        return $css;
    }

    /**
     * Build font-family rule while preserving quotes.
     *
     * @param string $family Stack.
     * @return string CSS snippet.
     * @deprecated Use B3WVS\VariationSwatches\Assets\TypographyRenderer\font_family_rule() instead.
     */
    private function font_family_rule( $family ) {
        return font_family_rule( $family );
    }

}
