<?php
/**
 * Enqueues public CSS and JavaScript resources for the Divi Responsive Helper plugin.
 */
if (!function_exists('pac_drh_public_resources')):
    function pac_drh_public_resources()
    {
        $min = PAC_DRH_PLUGIN_DEV_MOD ? '' : '.min';
        $handle = 'divi-responsive-helper';
        $css_path = "/assets/public/css";
        $js_path = "/assets/public/js";
        // Inline Styles
        $public_css [] = '.pac_drh_d_none {display: none !important;}';
        if (pac_drh_get_settings_enabled('pac_drh_enable_text_sizes')) {
            $public_css [] = '.et-core-modal-content p{font-size: inherit !important;line-height:inherit !important;}';
            $public_css [] = '.et-fb-page-creation-card-text p{font-size: inherit !important;line-height:inherit !important;}';
            $public_css [] = '.et-cloud-filter p{font-size: inherit !important;line-height:inherit !important;}';
            $public_css [] = '.et-cloud-app__upsell-description{font-size: inherit !important;}';
            $public_css [] = '.et-core-modal p{font-size: inherit !important;line-height:inherit !important;}';
            $public_css [] = '.et-common-prompt__modal p{font-size: inherit !important;line-height:inherit !important;}';
            $public_css [] = '.et-fb-main-settings p{font-size: inherit !important;line-height:inherit !important;}';
            $public_css [] = '.et-fb-module-settings p{font-size: inherit !important;line-height:inherit !important;}';
        }
        if (pac_drh_get_settings_enabled('pac_drh_enable_tabs_layout_settings')) {
            $public_css [] = '@media (min-width:768px) and (max-width:980px){.pac_drh_tabs_vertical_tab .et_pb_tabs_controls li{float:none;border-right:none;border-bottom:1px solid #d9d9d9;display:block}}';
            $public_css [] = '@media (min-width:300px) and (max-width:767px){.pac_drh_tabs_horizontal_phone ul.et_pb_tabs_controls{display:flex;flex-wrap:wrap}.pac_drh_tabs_horizontal_phone .et_pb_tabs_controls li{flex-grow:1}.pac_drh_tabs_horizontal_phone .et_pb_tabs_controls li a{display:block}}';
        }
        if (pac_drh_get_settings_enabled('pac_drh_enable_show_hide_menu_item')) {
            $public_css [] = '@media (min-width:981px){.pac_drh_hide_menu_item_desktop{display:none!important;}}';
            $public_css [] = '@media (min-width:768px) and (max-width:980px){.pac_drh_hide_menu_item_tablet{display:none!important;}}';
            $public_css [] = '@media (min-width:300px) and (max-width:767px){.pac_drh_hide_menu_item_mobile{display:none!important;}}';
        }
        if (pac_drh_get_settings_enabled('pac_drh_enable_menu_module_settings')) {
            $public_css [] = '@media only screen and (min-width:981px){[data-drh-menu-device=phone],[data-drh-menu-device=tablet]{display:none!important}}@media only screen and (min-width:768px) and (max-width:980px){[data-drh-menu-device=desktop],[data-drh-menu-device=phone]{display:none!important}}@media only screen and (max-width:767px){[data-drh-menu-device=desktop],[data-drh-menu-device=tablet]{display:none!important}}';
        }
        wp_register_style("$handle-public", false);
        wp_enqueue_style("$handle-public");
        $public_css = apply_filters('pac_drh_public_styles', $public_css);
        wp_add_inline_style("$handle-public", implode(' ', array_unique($public_css)));
        // Inline Scripts
        $public_js[] = 'function getDeviceType() { if (window.matchMedia("(max-width: 767px)").matches) { return "mobile"; } else if (window.matchMedia("(min-width: 768px) and (max-width: 980px)").matches) {return "tablet";} else {return "desktop";}}';
        $public_js[] = 'let isTouchDevice = "ontouchstart" in window || navigator.msMaxTouchPoints;';
        wp_register_script("$handle-public", false, ['jquery'], false, true);
        wp_enqueue_script("$handle-public");
        wp_add_inline_script("$handle-public", implode(' ', array_unique($public_js)));
        wp_localize_script("$handle-public", 'pac_drh_js_params', [
            'isExcludeEnabled' => et_get_option('pac_drh_exclude_widow_fixer'),
            'isCollapseSubMenus' => pac_drh_get_settings_enabled('pac_drh_enable_collapse_other_submenus')
        ]);
        // Widow Fixer
        if (pac_drh_get_settings_enabled('pac_drh_enable_widow_fixer')) {
            $resources['js'][] = "$js_path/widow-fixer$min.js";
        }
        // Mobile Parallax
        if (pac_drh_get_settings_enabled('pac_drh_enable_mobile_parallax')) {
            $resources['js'][] = "$js_path/mobile-parallax$min.js";
        }
        // Misc
        $resources['js'][] = "$js_path/misc$min.js";
        // Column Numbers
        if (pac_drh_get_settings_enabled('pac_drh_enable_number_of_columns')) {
            $resources['css'][] = "$css_path/column-numbers$min.css";
        }
        // Column Stacking
        if (pac_drh_get_settings_enabled('pac_drh_enable_col_stacking')) {
            $resources['css'][] = "$css_path/column-stacking$min.css";
        }
        // Blog Column Numbers
        if (pac_drh_get_settings_enabled('pac_drh_enable_blog_number_of_columns')) {
            $resources['css'][] = "$css_path/divi-blog-module$min.css";
        }
        // Portfolio Column Numbers
        if (pac_drh_get_settings_enabled('pac_drh_enable_portfolio_number_of_columns')) {
            $resources['css'][] = "$css_path/divi-portfolio-module$min.css";
        }
        // Gallery Column Numbers
        if (pac_drh_get_settings_enabled('pac_drh_enable_gallery_number_of_columns')) {
            $resources['css'][] = "$css_path/divi-gallery-module$min.css";
        }
        // Shop Column Numbers
        if (pac_drh_get_settings_enabled('pac_drh_enable_shop_number_of_columns')) {
            $resources['css'][] = "$css_path/divi-shop-module$min.css";
        }
        if (!empty($resources)) {
            pac_drh_load_scripts($resources, $handle);
        }
    }

    add_action('wp_enqueue_scripts', 'pac_drh_public_resources');
endif;
/**
 * Loads CSS and JavaScript resources for the specified handle.
 *
 * @param array $resources
 * @param string $handle
 *
 * @return void
 */
if (!function_exists('pac_drh_load_scripts')):
    function pac_drh_load_scripts($resources, $handle)
    {
        $load_resources_inline = 'on' === et_get_option('pac_drh_load_resources_inline');
        if (!empty($resources['css'])) {
            foreach ($resources['css'] as $resource) {
                if ($load_resources_inline) {
                    wp_add_inline_style("$handle-public", pac_drh_get_file_content($resource));
                } else {
                    $handle_css = $handle.'-'.str_replace('.min', '', pathinfo($resource, PATHINFO_FILENAME));
                    wp_enqueue_style($handle_css, plugins_url($resource, PAC_DRH_PLUGIN_FILE), [], PAC_DRH_PLUGIN_VERSION);
                }
            }
        }
        if (!empty($resources['js'])) {
            foreach ($resources['js'] as $resource) {
                if ($load_resources_inline) {
                    wp_add_inline_script("$handle-public", pac_drh_get_file_content($resource));
                } else {
                    $handle_js = $handle.'-'.str_replace('.min', '', pathinfo($resource, PATHINFO_FILENAME));
                    wp_enqueue_script($handle_js, plugins_url($resource, PAC_DRH_PLUGIN_FILE), ['jquery'], PAC_DRH_PLUGIN_VERSION, true);
                }
            }
        }
    }
endif;
/**
 * Retrieves the contents of a file from a given resource path, with a comment header.
 *
 * @param string $resource
 *
 * @return string|null
 */
if (!function_exists('pac_drh_get_file_content')):
    function pac_drh_get_file_content($resource)
    {
        $string = null;
        $resource_path = PAC_DRH_PLUGIN_DIR.$resource;
        $resoure_name = str_replace('.min', '', pathinfo($resource, PATHINFO_FILENAME));
        $comment = sprintf("/*----------------- %s Helper [Added By Divi Responsive Helper] -----------------*/", ucwords(str_replace('-', ' ', $resoure_name)));
        if (et_()->WPFS()->exists($resource_path)) {
            $string = $comment.PHP_EOL.et_()->WPFS()->get_contents($resource_path);
        }

        return $string;
    }
endif;