// Frontend JS for Wishlist Manager
// Handles heart button animation and AJAX wishlist actions
jQuery(document).ready(function($){
    $(document).on('click', '.wlm-button', function(e){
        e.preventDefault();
        var $btn = $(this);
        var product_id = $btn.data('product-id');
        var nonce = wlm_ajax.nonce;
        $btn.addClass('wlm-animating');
        $.post(wlm_ajax.ajax_url, {
            action: 'wlm_toggle_wishlist',
            product_id: product_id,
            nonce: nonce
        }, function(response){
            if(response.success){
                if(response.data.in_wishlist){
                    $btn.addClass('wlm-button--active');
                }else{
                    $btn.removeClass('wlm-button--active');
                }
                var text = response.data.in_wishlist ? $btn.data('text-added') : $btn.data('text-add');
                $btn.find('.wlm-button__text').text(text);
            }
            $btn.removeClass('wlm-animating');
        });
    });
    $(document).on('click', '.wlm-remove-button', function(e){
        e.preventDefault();
        var $btn = $(this);
        var product_id = $btn.data('product-id');
        var nonce = wlm_ajax.nonce;
        $.post(wlm_ajax.ajax_url, {
            action: 'wlm_remove_from_wishlist',
            product_id: product_id,
            nonce: nonce
        }, function(response){
            if(response.success){
                $btn.closest('tr').fadeOut(300, function(){ $(this).remove(); });
            }
        });
    });
});