jQuery(function ($) {
	'use strict';

	// Function to check if browser is online
	function isOnline() {
		return navigator.onLine;
	}
	// Function to show offline error message for refund/void operations
	function showOfflineError(operation) {
		var messages = {
			'refund': 'Unable to process refund: No internet connection detected. Please check your network connection and try again.',
			'void': 'Unable to void transaction: No internet connection detected. Please check your network connection and try again.'
		};
		alert(messages[operation] || 'Unable to process transaction: No internet connection detected. Please check your network connection and try again.');
	}
	// Bind offline check directly to refund buttons when they exist
	jQuery(document).ready(function ($) {
		// Check for refund buttons and bind offline check
		setTimeout(function () {
			$('.button.refund-items, .button.do-api-refund').off('click.offline').on('click.offline', function (e) {
				if (!isOnline()) {
					// Check order status to determine if it's a void or refund operation
					var operation = 'refund'; 
					if (typeof anet_admin_order !== 'undefined') {
						if (anet_admin_order.anet_order_status_hold === anet_admin_order.order_status) {
							operation = 'void'; 
						}
					}
					showOfflineError(operation);
					return false;
				}
			});
		}, 500); // Small delay to ensure buttons are loaded
		// Add global AJAX error handler for refund operations
		$(document).ajaxError(function (event, xhr, settings, error) {
			// Check if this is a WooCommerce refund AJAX call
			if (settings.url && settings.url.includes('admin-ajax.php') &&
				settings.data && (settings.data.includes('woocommerce_refund_line_items') ||
					settings.data.includes('woocommerce_do_ajax_refund'))) {
				// Check if error is due to network disconnection
				if (!navigator.onLine || xhr.status === 0 || error === 'timeout') {
					var operation = 'refund'; 
					if (typeof anet_admin_order !== 'undefined') {
						if (anet_admin_order.anet_order_status_hold === anet_admin_order.order_status) {
							operation = 'void';
						}
					}
					var message = operation === 'void'
						? 'Unable to void transaction: Network connection lost during the operation. Please check your internet connection and try again.'
						: 'Unable to process refund: Network connection lost during the operation. Please check your internet connection and try again.';
					alert(message);
					$('#woocommerce-order-items').unblock();
				}
			}
		});
	});
	if (anet_admin_order.anet_val_undefined !== typeof anet_admin_order) {
		if (anet_admin_order.anet_credit_card_id == anet_admin_order.gateway_id || anet_admin_order.anet_echeck_id == anet_admin_order.gateway_id) {
			jQuery(document).ready(function ($) {
				$('.woocommerce_order_items .edit-order-item, .woocommerce_order_items .delete-order-item').hide();
			});
			// Conditionally show refund button.
			const refundButton = document.querySelector('.button.refund-items');
			if (refundButton) {
				const status = anet_admin_order.order_status;
				if (anet_admin_order.anet_order_status_hold === status || anet_admin_order.anet_order_status_processing === status) {
					refundButton.style.display = 'inline-block';
				} else {
					refundButton.style.display = 'none';
				}
			}
			var manual_refund_button = document.querySelector('.button.button-primary.do-manual-refund');
			if (manual_refund_button) {
				manual_refund_button.style.display = 'none';
			}
			var button = document.querySelector('button.button-primary.do-api-refund');
			// Hide button if no amount is left.
			if (anet_admin_order.total_refund_amount <= 0) {
				if (button) {
					button.style.display = 'none';
				}
			}
			if (button) {
				button.disabled = true;
			}
			if (anet_admin_order.anet_order_status_hold === anet_admin_order.order_status) {
				jQuery('input[name=refund_amount]').on('change', function () {
					var total_amount = parseFloat(anet_admin_order.total_refund_amount);
					var refund_amount = jQuery('#refund_amount').val();
					refund_amount = Number(refund_amount);
					if (refund_amount == total_amount) {
						if (button) {
							button.disabled = false;
						}
					} else {
						if (button) {
							button.disabled = true;
						}
					}
				});
				jQuery('input[name=refund_amount]').on('input', function () {
					var total_amount = parseFloat(anet_admin_order.total_refund_amount);
					var refund_amount = jQuery('#refund_amount').val();
					refund_amount = Number(refund_amount);
					if (refund_amount == total_amount) {
						button.disabled = false;
					} else {
						button.disabled = true;
					}
				});
			}
			if (anet_admin_order.anet_order_status_processing === anet_admin_order.order_status || anet_admin_order.anet_order_status_completed === anet_admin_order.order_status) {
				jQuery('input[name=refund_amount]').on('change', function () {
					var total_amount = parseFloat(anet_admin_order.total_refund_amount);
					var refund_amount = jQuery('#refund_amount').val();
					refund_amount = Number(refund_amount);
					if (refund_amount <= total_amount && refund_amount > 0) {
						if (button) {
							button.disabled = false;
						}
					} else {
						if (button) {
							button.disabled = true;
						}
					}
				});
				jQuery('input[name=refund_amount]').on('input', function () {
					var total_amount = parseFloat(anet_admin_order.total_refund_amount);
					var refund_amount = jQuery('#refund_amount').val();
					refund_amount = Number(refund_amount);
					if (refund_amount <= total_amount && refund_amount > 0) {
						button.disabled = false;
					} else {
						button.disabled = true;
					}
				});
			}
		}
	}
});
jQuery(function ($) {
	const hideButtons = () => {
		if ((anet_admin_order.anet_order_status_refunded === anet_admin_order.order_status) ||
			(anet_admin_order.anet_order_status_cancelled === anet_admin_order.order_status)) {
			$('.button.refund-items').prop('disabled', true);
		}
		$('.button.add-line-item').hide();
		$('.button.add-coupon').hide();
		$('.button.button-primary.calculate-action').hide();
		$('.woocommerce_order_items .edit-order-item').hide();
		$('.button.button-primary.do-manual-refund').hide();
		$('.woocommerce_order_items .delete-order-item').hide();
		// Conditionally hide refund button
		if (typeof anet_admin_order !== 'undefined') {
			if (!anet_admin_order.refund_button_visibility) {
				$('.button.refund-items').hide();
			} else {
				$('.button.refund-items').show();
			}
		}
	};
	// Initial hide
	hideButtons();
	// DOM changes
	const observer = new MutationObserver(() => {
		hideButtons();
	});
	observer.observe(document.body, {
		childList: true,
		subtree: true
	});
});
