/**
 * Skip Cart Functionality
 * Automatically redirects to checkout when products are added to cart
 */

(function($) {
    'use strict';

    // Check if Skip Cart is enabled
    function isSkipCartEnabled() {
        // Prefer localized data, fall back to inline globals
        if (typeof window.ckfylt25SkipCartData !== 'undefined') {
            return !!window.ckfylt25SkipCartData.enabled;
        }
        return window.ckfylt25SkipCartEnabled === true;
    }

    // Get checkout URL
    function getCheckoutUrl() {
        if (typeof window.ckfylt25SkipCartData !== 'undefined' && window.ckfylt25SkipCartData.checkoutUrl) {
            return window.ckfylt25SkipCartData.checkoutUrl;
        }
        return window.ckfylt25CheckoutUrl || '/checkout/';
    }

    // Redirect to checkout
    function redirectToCheckout() {
        if (isSkipCartEnabled()) {
            window.location.href = getCheckoutUrl();
        }
    }

    // Handle AJAX add to cart
    function handleAjaxAddToCart() {
        $(document.body).on('added_to_cart', function(event, fragments, cart_hash, button) {
            // Small delay to ensure cart is updated
            setTimeout(function() {
                redirectToCheckout();
            }, 500);
        });
    }

    // Handle form submission add to cart
    function handleFormAddToCart() {
        $(document.body).on('submit', 'form.cart', function(e) {
            if (isSkipCartEnabled()) {
                // Let the form submit normally, but we'll redirect after
                setTimeout(function() {
                    redirectToCheckout();
                }, 1000);
            }
        });
    }

    // Handle "Add to Cart" button clicks
    function handleAddToCartButtons() {
        $(document.body).on('click', '.single_add_to_cart_button, .add_to_cart_button, .ajax_add_to_cart', function(e) {
            if (isSkipCartEnabled()) {
                // Let the button work normally, but we'll redirect after
                setTimeout(function() {
                    redirectToCheckout();
                }, 1000);
            }
        });
    }

    // Handle "Add to Cart" links
    function handleAddToCartLinks() {
        $(document.body).on('click', 'a.add_to_cart, a.ajax_add_to_cart', function(e) {
            if (isSkipCartEnabled()) {
                // Let the link work normally, but we'll redirect after
                setTimeout(function() {
                    redirectToCheckout();
                }, 1000);
            }
        });
    }

    // Initialize Skip Cart functionality
    function initSkipCart() {
        if (isSkipCartEnabled()) {
            handleAjaxAddToCart();
            handleFormAddToCart();
            handleAddToCartButtons();
            handleAddToCartLinks();
            
            console.log('Skip Cart enabled - customers will be redirected to checkout');
        }
    }

    // Initialize when document is ready
    $(document).ready(function() {
        initSkipCart();
    });

    // Also initialize on WooCommerce ready
    $(document.body).on('wc_fragment_refresh', function() {
        initSkipCart();
    });

})(jQuery);