// Shoptimizer ajax add to cart js.

document.addEventListener( 'DOMContentLoaded', function() {
	var cart_forms = document.querySelectorAll( '.summary form.cart' );
	cart_forms.forEach( function( cart_form ) {
		cart_form.addEventListener( 'submit', function( event ) {
			var parent_elem = cart_form.closest( '.product.type-product' );
			if ( ! parent_elem ) {
				return;
			}
			if ( parent_elem.classList.contains( 'product-type-external' ) || parent_elem.classList.contains( 'product-type-subscription' ) || parent_elem.classList.contains( 'product-type-variable-subscription' ) || parent_elem.classList.contains( 'product-type-grouped' ) ) {
				return;
			}
			if ( typeof shoptimizer_ajax_obj === 'undefined' ) {
				return;
			}
			event.preventDefault();

			var atc_elem = cart_form.querySelector( '.single_add_to_cart_button' );
			var formData = new FormData( cart_form );

			if ( atc_elem.value ) {
				formData.append( 'add-to-cart', atc_elem.value );
			}
			atc_elem.classList.remove( 'added' );
			atc_elem.classList.remove( 'not-added' );
			atc_elem.classList.add( 'loading' );

			// Trigger adding to cart event
			var wce_add_cart = new Event( 'adding_to_cart' );
			document.body.dispatchEvent( wce_add_cart );

			fetch( shoptimizer_ajax_obj.wcAjaxUrl.toString().replace( '%%endpoint%%', 'shoptimizer_pdp_ajax_atc' ), {
				method: 'POST',
				body: formData,
				credentials: 'same-origin',
				headers: {
					'X-Requested-With': 'XMLHttpRequest'
				}
			} ).then( function( resp ) {
				if ( ! resp ) {
					return;
				}
				if ( 'yes' === shoptimizer_ajax_obj.isCartRedirect ) {
					window.location = shoptimizer_ajax_obj.cartUrl;
					return;
				}
				return resp.json();
			} ).then( resp => {
				if ( ! resp ) {
					return;
				}

				var cur_page = window.location.toString();
				cur_page = cur_page.replace( 'add-to-cart', 'added-to-cart' );

				// Handle error cases
				if ( resp.error && resp.product_url ) {
					window.location = resp.product_url;
					return;
				}

				atc_elem.classList.remove( 'loading' );

				// Check for error notices in both legacy and new format
				var hasError = (resp.notices && resp.notices.indexOf( 'error' ) > -1) || 
				              (resp.fragments && resp.fragments.notices && resp.fragments.notices.indexOf( 'error' ) > -1);

				if ( hasError ) {
					document.body.insertAdjacentHTML( 'beforeend', resp.notices || resp.fragments.notices );
					atc_elem.classList.add( 'not-added' );
				} else {
					atc_elem.classList.add( 'added' );
					
					// Handle fragments for both legacy and new format
					var fragments = resp.fragments || resp;
					if (fragments) {
						jQuery.each(fragments, function(key, value) {
							jQuery(key).replaceWith(value);
						});
					}
					
					// Trigger fragment refresh
					jQuery(document.body).trigger('wc_fragments_refreshed');

					// Open cart drawer (only if it exists)
					if (document.getElementById('shoptimizerCartDrawer')) {
						document.querySelector('body').classList.add('drawer-open');
					}
					
					// Trigger added_to_cart event with appropriate data
					var cartHash = resp.cart_hash || (resp.data && resp.data.cart_hash);

					// Ensure the button carries data-product_id so plugins like
					// Google Analytics for WooCommerce can read the product ID.
					if ( ! atc_elem.dataset.product_id ) {
						var productIdInput = cart_form.querySelector( 'input[name="product_id"]' );
						var productId = productIdInput ? productIdInput.value : atc_elem.value;
						if ( productId ) {
							atc_elem.setAttribute( 'data-product_id', productId );
						}
					}

					jQuery(document.body).trigger('added_to_cart', [fragments, cartHash, jQuery(atc_elem)]);
				}
			} ).catch(error => {
				console.error('Error:', error);
				atc_elem.classList.remove('loading');
				atc_elem.classList.add('not-added');
			});
		} );
	} );
} );
