// source --> https://medicarama.com/wp-content/plugins/edd-recurring/assets/js/edd-frontend-recurring.js?ver=2.13.11 
var edd_scripts;
jQuery(document).ready(function($) {
	$('.edd_subscription_cancel').on('click',function(e) {
		if( confirm( edd_recurring_vars.confirm_cancel ) ) {
			return true;
		}
		return false;
	});

	// Force subscription terms to behave for Custom Prices
	$('.edd_download_purchase_form').each(function() {

		var form = $(this);

		if ( form.find( '.edd-cp-container' ).length && form.find( '.edd_price_options' ).length ) {

			var terms = form.find('.eddr-custom-terms-notice');
			var signup_fee = form.find('.eddr-custom-signup-fee-notice');
			terms.prev().append(terms);
			signup_fee.prev().append(signup_fee);
			terms.show();
			signup_fee.show();

		} else if ( form.find( '.edd-cp-container' ).length ) {

			form.find('.edd_cp_price').keyup(function() {
				form.find('.eddr-terms-notice,.eddr-signup-fee-notice').hide();
				form.find('.eddr-custom-terms-notice,.eddr-custom-signup-fee-notice').show();
			});

		}

	});

	// Look to see if the customer has purchased a free trial after email is entered on checkout
	$('#edd_purchase_form').on( 'focusout', '#edd-email', function() {

		if( 'undefined' == edd_scripts ) {
			return;
		}

		// We don't need to make this AJAX call, if there isn't a trial in the cart.
		if ( ! edd_recurring_vars.has_trial ) {
			return;
		}

		var email = $(this).val();
		var product_ids = [];

		$('body').find('.edd_cart_item').each(function() {
			product_ids.push( $(this).data( 'download-id' ) );
		});

		 $.ajax({
			type: "POST",
			data: {
				action: 'edd_recurring_check_repeat_trial',
				email: email,
				downloads: product_ids
			},
			dataType: "json",
			url: edd_scripts.ajaxurl,
			xhrFields: {
				withCredentials: true
			},
			success: function (response) {

				if( response.message ) {
					$('<div class="edd_errors"><p class="edd_error">' + response.message + '</p></div>').insertBefore( '#edd_purchase_submit' );
				}

			}
		}).fail(function (response) {
			if ( window.console && window.console.log ) {
				console.log( response );
			}
		}).done(function (response) {

		});

	} );

	$( document.body ).on( 'edd_discount_applied', updateNotices );
	$( document.body ).on( 'edd_discount_removed', updateNotices );

	/**
	 * Update cart item notices when a discount is added to or removed from the cart.
	 */
	function updateNotices ( e, data ) {
		let trial = $( '.edd_recurring_total_after_trial' );
		if ( trial.length && data.recurring_total ) {
			trial.text( data.recurring_total );
		}
		if ( data.recurring_sl ) {
			for ( var key in data.recurring_sl ) {
				if ( data.recurring_sl.hasOwnProperty( key ) ) {
					let amount = $( '.' + key );
					if ( amount.length ) {
						amount.text( data.recurring_sl[ key ] );
					}
				}
			}
		}
	}

	$( document.body ).on( 'edd_taxes_recalculated', function ( e, response ) {
		if ( !edd_recurring_vars.has_trial ) {
			return;
		}
		let selector = $( '.edd_recurring_trial_total_note' );
		if ( selector && $( '.edd_recurring_total_after_trial' ).length ) {
			selector.text( edd_recurring_vars.trial_message );
		}
	} );

	if ( edd_recurring_vars.has_trial ) {
		setTrialTotal();

		$( document.body ).on( 'edd_discount_applied', setTrialTotal );
		$( document.body ).on( 'edd_discount_removed', setTrialTotal );
		$( document.body ).on( 'edd_taxes_recalculated', setTrialTotal );
	}

	/**
	 * Sets the total order amount in the UI for a trial. (`0.00` in the store currency)
	 *
	 * @since 2.11
	 */
	function setTrialTotal ( e, data ) {
		// This sets the amount due today.
		$( '.edd_cart_amount' ).html( edd_recurring_vars.total );

		// This sets the recurring amount (after a trial).
		if ( 'undefined' !== typeof data && data.response && data.response.total ) {
			$( 'body' ).find( '.edd_recurring_total_after_trial' ).each( function () {
				$( this ).html( data.response.total );
			} );
		}
	}
} );