<?php
/**
 * Enqueues admin CSS and JavaScript resources for the Divi Responsive Helper plugin.
 */
if (!function_exists('pac_drh_admin_public_resources')):
    function pac_drh_admin_public_resources()
    {
        if (!is_user_logged_in()) {
            return;
        }
        $min = PAC_DRH_PLUGIN_DEV_MOD ? '' : '.min';
        // $css_path = "/assets/public/css";
        $js_path = "/assets/public/js";
        $inline_styles = ['.et-fb-no-vb-support-warning {display: none !important;}'];
        // Admin Only
        if (is_admin()) {
            // Epanel JS
            wp_enqueue_script('divi-responsive-helper-epanel', plugins_url("/assets/admin/js/epanel$min.js", PAC_DRH_PLUGIN_FILE));
            // Custom Media Query
            wp_enqueue_script('divi-responsive-helper-custom-media-query', plugins_url("/assets/admin/js/media-query$min.js", PAC_DRH_PLUGIN_FILE), [], PAC_DRH_PLUGIN_VERSION, true);
        }
        // Responsive Tabs
        if (pac_drh_get_settings_enabled('pac_drh_enable_auto_responsive')) {
            wp_enqueue_script('divi-responsive-helper-responsive-tabs', plugins_url("$js_path/responsive-tabs$min.js", PAC_DRH_PLUGIN_FILE), [], PAC_DRH_PLUGIN_VERSION, true);
        }
        // Presets
        if (pac_drh_get_settings_enabled('pac_drh_enable_presets') || pac_drh_get_settings_enabled('pac_drh_enable_custom_preview')) {
            $inline_styles[] = 'button.et-fb-button.et-fb-button--inverse.et-fb-button--app-modal.pac_preset_preview_button {padding: 6px !important;}';
            $inline_styles[] = '.pac_set_preview_size_input_filed {max-width: 90px;margin: 2px !important;border-radius: 4px !important;border: 2px solid !important;pointer-events: all !important;}';
            wp_enqueue_script('divi-responsive-helper-responsive-buttons', plugins_url("$js_path/presets$min.js", PAC_DRH_PLUGIN_FILE), ['jquery'], PAC_DRH_PLUGIN_VERSION, true);
            wp_localize_script('divi-responsive-helper-responsive-buttons', 'pac_drh_custom_presets', pac_drh_custom_presets());

        }
        if (!pac_drh_get_settings_enabled('pac_drh_enable_presets')) {
            $inline_styles[] = '.pac_preset{display: none !important;}';
        }
        if (!pac_drh_get_settings_enabled('pac_drh_enable_custom_preview')) {
            $inline_styles[] = '.pac_set_preview_size_input_filed{display: none !important;}';
        }
        // Viewport
        if (pac_drh_get_settings_enabled('pac_drh_enable_view_port_size')) {
            $inline_styles[] = '.viewport__size{display:block!important;cursor:pointer;background:#6c2eb9!important}.viewport__size>.ab-item:hover{color:#fff!important;background:#6c2eb9!important}';
            wp_enqueue_script('divi-responsive-helper-viewport', plugins_url("$js_path/viewport$min.js", PAC_DRH_PLUGIN_FILE), ['jquery'], PAC_DRH_PLUGIN_VERSION, true);
            wp_localize_script('divi-responsive-helper-viewport', 'pac_drh_vp_obj', ['viewport_label' => __('Viewport: ', 'divi-responsive-helper'), 'show_on_adminbar' => pac_drh_get_settings_enabled('pac_drh_enable_viewport_adminbar')]);
        }
        // Default Divi Responsive View
        if (pac_drh_get_settings_enabled('pac_drh_enable_hide_responsive_view')) {
            $app_frame_css = (et_fb_is_enabled() && !et_builder_bfb_enabled()) ? '{padding-top: 0 !important;}' : '{position: absolute !important;top: 0 !important;padding-top: 0 !important}';
            $inline_styles[] = '.et-bfb-responsive-view-controls, .et-fb-responsive-view-controls {display: none;}';
            $inline_styles[] = '.et-db #et-boc .et-l #et-fb-app-frame '.esc_html($app_frame_css);
            $inline_styles[] = '.wp-admin .et-boc.et-fb-root-ancestor.et-fb-iframe-ancestor{margin-top: 20px !important;}';
        }
        // Inline Styles
        wp_register_style('divi-responsive-helper-main', false);
        wp_enqueue_style('divi-responsive-helper-main');
        wp_add_inline_style('divi-responsive-helper-main', implode(' ', array_unique($inline_styles)));
    }

    add_action('admin_enqueue_scripts', 'pac_drh_admin_public_resources', 99);
    add_action('wp_enqueue_scripts', 'pac_drh_admin_public_resources');
endif;