<?php

/**
 * baty functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package WordPress
 * @subpackage baty
 * @since baty 1.0
 */

// Theme setup
require_once get_template_directory() . '/assets/class-tgm-plugin-activation.php';
require_once get_template_directory() . '/assets/required-plugins.php';
function baty_enqueue_aos()
{

    // CSS
    wp_enqueue_style(
        'aos',
        get_template_directory_uri() . '/assets/aos/aos.css',
        array(),
        '2.3.4'
    );

    // JS
    wp_enqueue_script(
        'aos',
        get_template_directory_uri() . '/assets/aos/aos.js',
        array(),
        '2.3.4',
        true
    );
}

add_action('wp_enqueue_scripts', 'baty_enqueue_aos');

// dashicons
function baty_load_dashicons_front_end()
{
    wp_enqueue_style('dashicons');
}
add_action('wp_enqueue_scripts', 'baty_load_dashicons_front_end');


function baty_enqueue_aos_init() {

    wp_enqueue_script(
        'baty-aos-init',
        get_template_directory_uri() . '/assets/js/aos-init.js',
        ['aos'],
        '1.0',
        true
    );
}
add_action('wp_enqueue_scripts', 'baty_enqueue_aos_init');
function baty_register_pattern_categories()
{

    if (function_exists('register_block_pattern_category')) {

        register_block_pattern_category(
            'baty_patterns',
            array(
                'label' => __('Baty Patterns', 'baty'),
            )
        );
    }
}
function baty_enqueue_preloader_assets() {

    // CSS
    wp_enqueue_style(
        'baty-preloader',
        get_template_directory_uri() . '/assets/css/preloader.css',
        array(),
        wp_get_theme()->get('Version')
    );

    // JS
    wp_enqueue_script(
        'baty-preloader',
        get_template_directory_uri() . '/assets/js/preloader.js',
        array(), 
        wp_get_theme()->get('Version'),
        true  
    );
}
add_action('wp_enqueue_scripts', 'baty_enqueue_preloader_assets');

add_action('init', 'baty_register_pattern_categories');
if (!function_exists('baty_theme_setup')):
    function baty_theme_setup()
    {
        // Enable WooCommerce block features
        add_theme_support('woocommerce', array(
            'product_grid' => array(
                'min_columns' => 2,
                'max_columns' => 4,
            ),
        ));

        // Add support for product gallery features
        add_theme_support('wc-product-gallery-zoom');
        add_theme_support('wc-product-gallery-lightbox');
        add_theme_support('wc-product-gallery-slider');

        // Post formats
        add_theme_support('post-formats', array('aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video'));
    }
endif;
add_action('after_setup_theme', 'baty_theme_setup');

// Editor style
if (!function_exists('baty_editor_style')):
    function baty_editor_style()
    {
        add_editor_style('assets/css/editor-style.css');
    }
endif;
add_action('after_setup_theme', 'baty_editor_style');

function baty_disable_3d_viewer_on_product() {
    if ( ! function_exists( 'is_product' ) || ! is_product() ) {
        return;
    }

    global $wp_filter;

    if ( ! isset( $wp_filter['wp_footer'] ) ) {
        return;
    }

    foreach ( $wp_filter['wp_footer']->callbacks as $priority => $callbacks ) {
        foreach ( $callbacks as $key => $callback ) {
            if ( is_array( $callback['function'] ) &&
                is_object( $callback['function'][0] ) &&
                strpos( get_class( $callback['function'][0] ), 'BP3D' ) !== false ) {
                unset( $wp_filter['wp_footer']->callbacks[ $priority ][ $key ] );
            }
        }
    }
}
add_action( 'wp', 'baty_disable_3d_viewer_on_product' );
if (!function_exists('baty_enqueue_theme_assets')):

    function baty_enqueue_theme_assets()
    {
        // Global styles (always load)
        $suffix = SCRIPT_DEBUG ? '' : '.min';

        wp_enqueue_style(
            'baty-style',
            get_theme_file_uri( 'style' . $suffix . '.css' ),
            array(),
            wp_get_theme()->get( 'Version' )
        );

        wp_enqueue_style(
            'baty-features-product-style', // handle
            get_template_directory_uri() . '/assets/css/features_product.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );
        wp_enqueue_style(
            'baty-home-patterns', // handle
            get_template_directory_uri() . '/assets/css/home_patterns.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );
        // Swiper (if needed globally)
        wp_enqueue_style(
            'swiper',
            get_template_directory_uri() . '/assets/swiper/swiper-bundle.min.css',
            array(),
            '11.0.0'
        );
        // Owl Carousel CSS
        wp_enqueue_style(
            'owl-carousel',
            get_template_directory_uri() . '/assets/owl/owl.carousel.min.css',
            [],
            '2.3.4'
        );

        wp_enqueue_style(
            'owl-theme-default',
            get_template_directory_uri() . '/assets/owl/owl.theme.default.min.css',
            ['owl-carousel'],
            '2.3.4'
        );

        // Owl Carousel JS
        wp_enqueue_script(
            'owl-carousel',
            get_template_directory_uri() . '/assets/owl/owl.carousel.min.js',
            ['jquery'],
            '2.3.4',
            true
        );

        // Custom JS Init
        wp_enqueue_script(
            'baty-main',
            get_template_directory_uri() . '/assets/js/main.js',
            ['jquery', 'owl-carousel', 'swiper'], 
            '1.0',
            true
        );

        // Components (your main CSS file)
        
        wp_enqueue_style(
            'baty-coverflow-slider-style', // handle
            get_template_directory_uri() . '/assets/css/coverflowSlider.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );
        wp_enqueue_style(
            'baty-hero-style', // handle
            get_template_directory_uri() . '/assets/css/hero.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );
        wp_enqueue_style(
            'baty-about-style', // handle
            get_template_directory_uri() . '/assets/css/about.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );
        wp_enqueue_style(
            'baty-archive-style', // handle
            get_template_directory_uri() . '/assets/css/archive-product.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );
        wp_enqueue_style(
            'baty-single-style', // handle
            get_template_directory_uri() . '/assets/css/single-product.css', 
            array(), // dependencies
            wp_get_theme()->get('Version') // version
        );

        // Swiper JS (if needed)
        wp_enqueue_script(
            'swiper',
            get_template_directory_uri() . '/assets/swiper/swiper-bundle.min.js',
            array(),
            '11.0.0',
            true
        );

        // Custom sliders JS
        $sliders_js_path = get_template_directory() . '/assets/js/sliders.js';
        if (file_exists($sliders_js_path)) {
            wp_enqueue_script(
                'baty-sliders',
                get_template_directory_uri() . '/assets/js/sliders.js',
                array('swiper'),
                filemtime($sliders_js_path),
                true
            );
        }
    }
endif;

add_action('wp_enqueue_scripts', 'baty_enqueue_theme_assets');
// Block styles
if (!function_exists('baty_block_styles')):
    function baty_block_styles()
    {
        register_block_style('core/list', array(
            'name' => 'checkmark-list',
            'label' => __('Checkmark', 'baty'),
            'inline_style' => '
            ul.is-style-checkmark-list {
                list-style-type: "\2713";
            }
            ul.is-style-checkmark-list li {
                padding-inline-start: 1ch;
            }',
        ));
    }
endif;
add_action('init', 'baty_block_styles');

// Pattern categories
if (!function_exists('baty_patterns_categories')):
    function baty_patterns_categories()
    {
        register_block_pattern_category('baty_pages', array(
            'label' => __('Pages', 'baty'),
            'description' => __('A collection of full page layouts.', 'baty'),
        ));

        register_block_pattern_category('baty_post_formats', array(
            'label' => __('Post formats', 'baty'),
            'description' => __('A collection of post format patterns.', 'baty'),
        ));
    }
endif;
add_action('init', 'baty_patterns_categories');

// Block bindings
if (!function_exists('baty_register_block_bindings')):
    function baty_register_block_bindings()
    {
        register_block_bindings_source('baty/format', array(
            'label' => _x('Post format name', 'Label for the block binding placeholder in the editor', 'baty'),
            'get_value_callback' => 'baty_format_binding',
        ));
    }
endif;
add_action('init', 'baty_register_block_bindings');

if (!function_exists('baty_format_binding')):
    function baty_format_binding()
    {
        $post_format_slug = get_post_format();
        if ($post_format_slug && 'standard' !== $post_format_slug) {
            return get_post_format_string($post_format_slug);
        }
    }
endif;

function baty_preloader()
{
?>
    <div id="site-preloader">
        <div class="loader-container">
            <div class="loader">
                <div class="truckWrapper">
                    <div class="truckBody">
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 198 93" class="trucksvg">
                            <path stroke-width="3" stroke="#282828" fill="#F83D3D" d="M135 22.5H177.264C178.295 22.5 179.22 23.133 179.594 24.0939L192.33 56.8443C192.442 57.1332 192.5 57.4404 192.5 57.7504V89C192.5 90.3807 191.381 91.5 190 91.5H135C133.619 91.5 132.5 90.3807 132.5 89V25C132.5 23.6193 133.619 22.5 135 22.5Z"></path>
                            <path stroke-width="3" stroke="#282828" fill="#7D7C7C" d="M146 33.5H181.741C182.779 33.5 183.709 34.1415 184.078 35.112L190.538 52.112C191.16 53.748 189.951 55.5 188.201 55.5H146C144.619 55.5 143.5 54.3807 143.5 53V36C143.5 34.6193 144.619 33.5 146 33.5Z"></path>
                            <path stroke-width="2" stroke="#282828" fill="#282828" d="M150 65C150 65.39 149.763 65.8656 149.127 66.2893C148.499 66.7083 147.573 67 146.5 67C145.427 67 144.501 66.7083 143.873 66.2893C143.237 65.8656 143 65.39 143 65C143 64.61 143.237 64.1344 143.873 63.7107C144.501 63.2917 145.427 63 146.5 63C147.573 63 148.499 63.2917 149.127 63.7107C149.763 64.1344 150 64.61 150 65Z"></path>
                            <rect stroke-width="2" stroke="#282828" fill="#FFFCAB" rx="1" height="7" width="5" y="63" x="187"></rect>
                            <rect stroke-width="2" stroke="#282828" fill="#282828" rx="1" height="11" width="4" y="81" x="193"></rect>
                            <rect stroke-width="3" stroke="#282828" fill="#DFDFDF" rx="2.5" height="90" width="121" y="1.5" x="6.5"></rect>
                            <rect stroke-width="2" stroke="#282828" fill="#DFDFDF" rx="2" height="4" width="6" y="84" x="1"></rect>
                        </svg>
                    </div>
                    <div class="truckTires">
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 30 30" class="tiresvg">
                            <circle stroke-width="3" stroke="#282828" fill="#282828" r="13.5" cy="15" cx="15"></circle>
                            <circle fill="#DFDFDF" r="7" cy="15" cx="15"></circle>
                        </svg>
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 30 30" class="tiresvg">
                            <circle stroke-width="3" stroke="#282828" fill="#282828" r="13.5" cy="15" cx="15"></circle>
                            <circle fill="#DFDFDF" r="7" cy="15" cx="15"></circle>
                        </svg>
                    </div>
                    <div class="road"></div>
                    <svg xml:space="preserve" viewBox="0 0 453.459 453.459" xmlns="http://www.w3.org/2000/svg" id="Capa_1" version="1.1" fill="#000000" class="lampPost">
                        <path d="M252.882,0c-37.781,0-68.686,29.953-70.245,67.358h-6.917v8.954c-26.109,2.163-45.463,10.011-45.463,19.366h9.993 c-1.65,5.146-2.507,10.54-2.507,16.017c0,28.956,23.558,52.514,52.514,52.514c28.956,0,52.514-23.558,52.514-52.514 c0-5.478-0.856-10.872-2.506-16.017h9.992c0-9.354-19.352-17.204-45.463-19.366v-8.954h-6.149C200.189,38.779,223.924,16,252.882,16 c29.952,0,54.32,24.368,54.32,54.32c0,28.774-11.078,37.009-25.105,47.437c-17.444,12.968-37.216,27.667-37.216,78.884v113.914 h-0.797c-5.068,0-9.174,4.108-9.174,9.177c0,2.844,1.293,5.383,3.321,7.066c-3.432,27.933-26.851,95.744-8.226,115.459v11.202h45.75 v-11.202c18.625-19.715-4.794-87.527-8.227-115.459c2.029-1.683,3.322-4.223,3.322-7.066c0-5.068-4.107-9.177-9.176-9.177h-0.795 V196.641c0-43.174,14.942-54.283,30.762-66.043c14.793-10.997,31.559-23.461,31.559-60.277C323.202,31.545,291.656,0,252.882,0z M232.77,111.694c0,23.442-19.071,42.514-42.514,42.514c-23.442,0-42.514-19.072-42.514-42.514c0-5.531,1.078-10.957,3.141-16.017 h78.747C231.693,100.736,232.77,106.162,232.77,111.694z"></path>
                    </svg>
                </div>
            </div>
            <button id="skip-preloader" class="skip-btn">
                <?php esc_html_e('Skip to Content', 'baty'); ?>
            </button>
        </div>
    </div>



<?php
}
add_action('wp_body_open', 'baty_preloader');

/* --- PRELOADER SECTION END --- */

/**
 * Conditionally hide WooCommerce blocks if WooCommerce is not active.
 */
function baty_conditional_woo_blocks( $pre_render, $parsed_block ) {
    $block_name = isset( $parsed_block['blockName'] ) ? $parsed_block['blockName'] : '';

    // Hide wishlist group if Wishlist plugin is not active
    if ( 'core/group' === $block_name ) {
        $className = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] : '';
        if ( strpos( $className, 'wishlist-btn' ) !== false && ! class_exists( 'TInvWL_Public_WishlistCounter' ) ) {
            return ''; // Short-circuit rendering
        }
    }

    if ( ! class_exists( 'WooCommerce' ) ) {
        // Hide core categories block if it is querying product_cat
        if ( 'core/categories' === $block_name ) {
            $taxonomy = isset( $parsed_block['attrs']['taxonomy'] ) ? $parsed_block['attrs']['taxonomy'] : '';
            if ( 'product_cat' === $taxonomy ) {
                return ''; // Short-circuit rendering
            }
        }
        
        // Hide all WooCommerce specific blocks
        if ( strpos( $block_name, 'woocommerce/' ) === 0 ) {
            return ''; // Short-circuit rendering
        }
    }
    
    return $pre_render;
}
add_filter( 'pre_render_block', 'baty_conditional_woo_blocks', 10, 2 );

