<?php
/**
 * Prevents horizontal scrolling by adding inline CSS to the page head.
 */
if (!function_exists('pac_drh_prevent_horizontal_scroll_css')):
    function pac_drh_prevent_horizontal_scroll_css()
    {
        echo PHP_EOL;
        echo "<style id='divi-responsive-helper-phs-inline-css'>";
        echo "#page-container{overflow-x: hidden;}";
        echo "</style>";
        echo PHP_EOL;
    }

    if (pac_drh_get_settings_enabled('pac_drh_enable_prevent_horizontal_scroll')) {
        add_action('wp_head', 'pac_drh_prevent_horizontal_scroll_css');
    }
endif;
/**
 * Checks if the 'pac_drh_enable_remove_animation' setting is enabled, and if so, disables animations.
 */
if (!function_exists('pac_drh_disabled_animation')):
    function pac_drh_disabled_animation()
    { ?>
        <script type="text/javascript">
            if (isTouchDevice && 'mobile' === getDeviceType()) {
                if (et_animation_data) {
                    et_animation_data = '';
                }
                const disabledAnimationsStyle = document.createElement('style');
                disabledAnimationsStyle.id = 'pac-drh-disabled-animation';
                disabledAnimationsStyle.innerHTML = `.et_builder_inner_content .et_animated{opacity:1!important}.et_builder_inner_content *{-webkit-animation:none!important;-moz-animation:none!important;-o-animation:none!important;animation:none!important}`;
                document.head.appendChild(disabledAnimationsStyle);
            }
        </script>
        <?php
    }

    if (pac_drh_get_settings_enabled('pac_drh_enable_remove_animation')) {
        add_action('wp_footer', 'pac_drh_disabled_animation', 999);
    }
endif;
/**
 * Generates CSS for controlling the visibility of the back-to-top button based on the specified visibility settings.
 * @return void
 */
if (!function_exists('pac_drh_backtotop_visibility_css')):
    function pac_drh_backtotop_visibility_css()
    {
        $back_top_visibility = pac_drh_get_option('pac_drh_back_top_visibility', 'off');
        echo PHP_EOL;
        echo "<style id='divi-responsive-helper-btp-inline-css'>";
        $visibility_on_desktop = "@media (min-width: 980px) {.et_pb_scroll_top {display: none !important;}}";
        $visibility_on_tablet = "@media (min-width: 768px) and (max-width: 980px){.et_pb_scroll_top {display: none !important;}}";
        $visibility_on_phone = "@media (max-width: 767px) and (min-width: 0px) {.et_pb_scroll_top {display: none !important;}}";
        if ('desktop' === $back_top_visibility) {
            echo esc_html($visibility_on_tablet);
            echo esc_html($visibility_on_phone);
        } elseif ('tablet' === $back_top_visibility) {
            echo esc_html($visibility_on_desktop);
            echo esc_html($visibility_on_phone);
        } elseif ('phone' === $back_top_visibility) {
            echo esc_html($visibility_on_desktop);
            echo esc_html($visibility_on_tablet);
        } elseif ('desktop_tablet' === $back_top_visibility) {
            echo esc_html($visibility_on_phone);
        } elseif ('tablet_phone' === $back_top_visibility) {
            echo esc_html($visibility_on_desktop);
        } elseif ('desktop_phone' === $back_top_visibility) {
            echo esc_html($visibility_on_tablet);
        }
        echo "</style>";
        echo PHP_EOL;
    }

    if ('off' !== pac_drh_get_option('pac_drh_back_top_visibility', 'off')) {
        add_action('wp_head', 'pac_drh_backtotop_visibility_css');
    }
endif;
/**
 * Add mobile logo to the logo container.
 *
 * @param string $logo_container
 *
 * @return string
 */
if (!function_exists('pac_drh_add_mobile_logo')):
    function pac_drh_add_mobile_logo($logo_container)
    {
        if (class_exists('DOMDocument')) {
            $dom = pac_drh_create_dom($logo_container);
            $d_logo_ele = $dom->getElementsByTagName('img');
            $a_tag_ele = $dom->getElementsByTagName('a');
            if (isset($a_tag_ele->length, $d_logo_ele->length) && 0 !== $a_tag_ele->length && 0 !== $d_logo_ele->length) {
                $d_logo_item = $d_logo_ele->item(0);
                $logo_class = $d_logo_item->getAttribute('class');
                $d_logo_item->setAttribute('class', esc_attr($logo_class).' pac_drh_desktop_logo');
                // Add Mobile Logo
                $m_logo = et_get_option('pac_drh_mobile_logo');
                $m_logo_size = et_get_attachment_size_by_url($m_logo);
                $m_logo_width = (!empty($m_logo_size) && is_numeric($m_logo_size[0])) ? $m_logo_size[0] : '93';
                $m_logo_height = (!empty($m_logo_size) && is_numeric($m_logo_size[1])) ? $m_logo_size[1] : '43';
                $m_logo_ele = $dom->createElement('img');
                $m_logo_ele->setAttribute('src', esc_attr($m_logo));
                $m_logo_ele->setAttribute('id', 'logo');
                $m_logo_ele->setAttribute('class', esc_attr($logo_class).' pac_drh_mobile_logo');
                $m_logo_ele->setAttribute('alt', esc_attr(get_bloginfo('name')));
                $m_logo_ele->setAttribute('width', esc_attr($m_logo_width));
                $m_logo_ele->setAttribute('height', esc_attr($m_logo_height));
                $m_logo_ele->setAttribute('data-height-percentage', esc_attr(et_get_option('logo_height', '54')));
                $a_tag_ele->item(0)->appendChild($m_logo_ele);
            }

            return $dom->saveHTML();
        }

        return $logo_container;
    }

    if (!empty(pac_drh_get_option('pac_drh_mobile_logo'))) {
        add_filter('et_html_logo_container', 'pac_drh_add_mobile_logo');
    }
endif;
/**
 * Change the mobile header color by adding the theme color meta-tag to the head section.
 * This function outputs a meta-tag with the theme color specified in the theme options.
 * If the theme color is not empty, it adds the meta-tag to the head section of the document.
 */
if (!function_exists('pac_drh_change_mobile_header_color')):
    function pac_drh_change_mobile_header_color()
    {
        echo PHP_EOL;
        echo sprintf('<meta name="theme-color" content="%s" />', esc_attr(pac_drh_get_option('pac_drh_mobile_header_color')));
        echo PHP_EOL;
    }

    if ('' !== pac_drh_get_option('pac_drh_mobile_header_color')) {
        add_action('wp_head', 'pac_drh_change_mobile_header_color');
    }
endif;
/**
 * Checks if the function pac_drh_mobile_pinch_zoom exists.
 * If not, define it.
 * This function adds a meta-viewport tag to enable pinch zoom on mobile devices.
 */
if (!function_exists('pac_drh_mobile_pinch_zoom')):
    function pac_drh_mobile_pinch_zoom()
    {
        echo PHP_EOL;
        echo '<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0, minimum-scale=0.1, maximum-scale=10.0">';
        echo PHP_EOL;
    }

    if (pac_drh_get_settings_enabled('pac_drh_enable_mobile_pinch_zoom')) {
        remove_action('wp_head', 'et_add_viewport_meta');
        add_action('wp_head', 'pac_drh_mobile_pinch_zoom');
    }
endif;
/**
 * Disable hover effects function.
 * Checks if hover effects are disabled in settings and remove hover effects accordingly.
 */
if (!function_exists('pac_drh_disabled_hover_effects')):
    function pac_drh_disabled_hover_effects()
    { ?>
        <script type="text/javascript" id="pac-drh-disabled-hover-effects">
            jQuery(document).ready(function () {
                // Function to remove hover effects
                function removeHoverEffects() {
                    const mViewHoverElements = $('.et_multi_view__hover_selector');
                    if (mViewHoverElements.length > 0) {
                        mViewHoverElements.removeClass('et_multi_view__hover_selector');
                    }
                }

                // Function to process stylesheet rules
                function processStylesheets() {
                    for (let si = 0; si < document.styleSheets.length; si++) {
                        const styleSheet = document.styleSheets[si];
                        // Try to access the cssRules, and skip the stylesheet if access is denied
                        let rules;
                        try {
                            rules = styleSheet.cssRules || styleSheet.rules;
                        } catch (e) {
                            console.warn(`Cannot access cssRules for stylesheet: ${styleSheet.href}`, e);
                            continue;
                        }
                        if (!rules) continue;
                        for (let ri = rules.length - 1; ri >= 0; ri--) {
                            const rule = rules[ri];
                            if (!rule.selectorText) continue;
                            // Loop through each rule in the stylesheet
                            if (rule.selectorText.includes(':hover')) {
                                const exceptions = ['.nav li.et-touch-hover > ul', '.nav li:hover > ul', 'et_pb_button'];
                                // Check if any of the exceptions are present in the rule
                                const hasException = exceptions.some(exception => rule.selectorText.includes(exception));
                                if (!hasException) {
                                    styleSheet.deleteRule(ri);
                                } else {
                                    rule.selectorText = rule.selectorText.replace(/:hover/g, '.pac_drh_responsive_hover_effect:hover');
                                }
                            }
                        }
                        // Insert a CSS rule to remove text decoration from non-button links
                        try {
                            styleSheet.insertRule('a:where(:not(.wp-element-button)) { text-decoration: none !important; }', 0);
                        } catch (e) {
                            console.error('Failed to insert rule:', e);
                        }
                    }
                }

                // Detect if the device supports touch
                const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
                if (isTouchDevice) {
                    // Remove hover effects on touch devices
                    removeHoverEffects();
                    window.addEventListener('touchstart', removeHoverEffects, {passive: false});
                    // Set a timeout to process stylesheets after a short delay
                    setTimeout(processStylesheets, 500);
                }
            });
        </script>
        <?php
    }

    if (pac_drh_get_settings_enabled('pac_drh_disabled_hover_effects')) {
        add_action('wp_footer', 'pac_drh_disabled_hover_effects');
    }
endif;
/**
 * Override hover CSS function.
 *
 * @param array $data_resources
 * @param string $context
 * @param string $page_resource
 *
 * @return array
 */
if (!function_exists('pac_drh_overide_hover_css')):
    function pac_drh_overide_hover_css($data_resources, $context, $page_resource)
    {
        $detect_mobile = pac_drh_detect_mobile();
        if (!empty($data_resources) && $detect_mobile->isMobile()) {
            $data_resources = pac_drh_search_replace_array($data_resources, ':hover', '.pac_drh_responsive_hover_effect:hover');
        }

        return $data_resources;
    }
endif;
if (pac_drh_get_settings_enabled('pac_drh_disabled_hover_effects')) {
    add_filter('et_core_page_resource_get_data', 'pac_drh_overide_hover_css', 10, 3);
}