<?php
/**
 * Function to handle custom typography styles based on theme options.
 */
if (!function_exists('pac_drh_typography')):
    function pac_drh_typography()
    {
        $typography_groups = [
            'p' => [
                'elements' => 'p:not(.et_pb_text p):not(.et_pb_blurb p):not(.et_pb_blurb p):not(.et_pb_toggle p):not(.et_pb_promo p):not(.et_pb_subscribe p):not(.et_pb_newsletter p):not(.et_pb_team_member p):not(.et_pb_slider p):not(.et_pb_pricing_table p):not(.et_pb_slide p):not(.et_pb_testimonial p):not(.et_pb_tab p):not(.et_pb_circle_counter p):not(#footer-info),.et_pb_text,.et_pb_blurb,.et_pb_toggle,.et_pb_promo,.et_pb_subscribe,.et_pb_newsletter,.et_pb_team_member,.et_pb_slider,.et_pb_pricing_table,.et_pb_slide,.et_pb_testimonial,.et_pb_tab,.et_pb_circle_counter,.et_pb_column .et_quote_content blockquote cite, .et_pb_column .et_link_content a.et_link_main_url, .et_pb_blog_grid .et_quote_content blockquote cite, .et_pb_post .post-content, .et_pb_counter_title, .et_pb_counter_amount, .et_pb_slide_content, .et-menu li, .et_pb_filterable_portfolio .et_pb_portfolio_filters li, .woocommerce ul.products li.product .price, .woocommerce ul.products li.product .price del, .woocommerce ul.products li.product .price ins, .et_pb_post .post-meta, .nav-single a',
                'option_prefix' => 'pac_drh_p'
            ],
            'h1' => [
                'elements' => 'h1, .et_pb_pagebuilder_layout .page .post-content h1, .et_pb_title_container h1, .et_pb_column_1_3 h1, .et_pb_column_1_4 h1, .et_pb_column_1_5 h1, .et_pb_column_1_6 h1, .et_pb_column_2_5 h1',
                'option_prefix' => 'pac_drh_h1'
            ],
            'h2' => [
                'elements' => 'h2, .et_pb_blog_grid h2, .et_pb_slide_description .et_pb_slide_title, .woocommerce ul.products li.product .woocommerce-loop-product__title, .et_pb_portfolio_grid .et_pb_portfolio_item h2, .et_pb_filterable_portfolio_grid .et_pb_portfolio_item h2, .et_pb_column_1_3 h2, .et_pb_column_1_4 h2, .et_pb_column_1_5 h2, .et_pb_column_1_6 h2, .et_pb_column_2_5 h2',
                'option_prefix' => 'pac_drh_h2'
            ],
            'h3' => [
                'elements' => 'h3, .et_pb_circle_counter h3, .et_pb_number_counter h3, .et_pb_column_1_3 h3, .et_pb_column_1_4 h3, .et_pb_column_1_5 h3, .et_pb_column_1_6 h3, .et_pb_column_2_5 h3',
                'option_prefix' => 'pac_drh_h3'
            ],
            'h4' => [
                'elements' => 'h4, .et_pb_column_1_3 h4, .et_pb_column_1_4 h4, .et_pb_column_1_5 h4, .et_pb_column_1_6 h4, .et_pb_column_2_5 h4',
                'option_prefix' => 'pac_drh_h4'
            ],
            'h5' => [
                'elements' => 'h5, .et_pb_column_1_3 h5, .et_pb_column_1_4 h5, .et_pb_column_1_5 h5, .et_pb_column_1_6 h5, .et_pb_column_2_5 h5',
                'option_prefix' => 'pac_drh_h5'
            ],
            'h6' => [
                'elements' => 'h6, .et_pb_column_1_3 h6, .et_pb_column_1_4 h6, .et_pb_column_1_5 h6, .et_pb_column_1_6 h6, .et_pb_column_2_5 h6',
                'option_prefix' => 'pac_drh_h6'
            ]
        ];
        $inline_css = "<style id='divi-responsive-helper-typography-inline-css'>";
        foreach ($typography_groups as $group) {
            $desktop_size = et_get_option($group['option_prefix'].'_desktop');
            $tablet_size = et_get_option($group['option_prefix'].'_tablet');
            $phone_size = et_get_option($group['option_prefix'].'_phone');
            $line_height = et_get_option($group['option_prefix'].'_line_height');
            $inline_css .= generate_typography_css($group['elements'], $desktop_size, $tablet_size, $phone_size, $line_height);
        }
        $inline_css .= "</style>";
        echo $inline_css; // phpcs:ignore
    }

    // Check if Divi Builder is in Front-end editing mode and enqueue the typography function accordingly
    $hook = (isset($_GET['et_fb']) && sanitize_text_field($_GET['et_fb']) === '1') ? 'wp_head' : 'wp_footer'; // phpcs:ignore
    add_action($hook, 'pac_drh_typography');
endif;
if (!function_exists('generate_typography_css')):
    function generate_typography_css($elements, $desktop_size, $tablet_size, $phone_size, $line_height): string
    {
        return "@media (min-width: 981px) {
                    $elements {
                        font-size: $desktop_size;
                        line-height: $line_height;
                    }
                }
                @media (min-width: 481px) and (max-width: 980px) {
                    $elements {
                        font-size: $tablet_size;
                        line-height: $line_height; 
                    }
                }
                @media (max-width: 480px) {
                    $elements {
                        font-size: $phone_size;
                        line-height: $line_height; 
                    }
                }
            ";
    }
endif;
