// source --> https://xn--lauthentiquedesse-ntb.com/wp-content/plugins/free-shipping-notification-woocommerce/public/js/pisol-fsnw-public.js?ver=2.2.79 
(function ($) {
	'use strict';

	jQuery(function ($) {
		var message = {
			fadeInTime: 600,
			fadeOuttime: 600,
			howLongToShow: pisol_fsnw.howLongToShow,
			showContinues: pisol_fsnw.showContinues,
			show: function () {
				message.hide_progress();
				if (jQuery('.pisol-bar-container .pisol-bar-message').text() != '') {
					jQuery('.pisol-bar-container').fadeIn(message.fadeInTime);
					jQuery('body').addClass('pisol-bar-open');
					if (!message.showContinues) {
						setTimeout(function () { message.hide(); }, message.howLongToShow);
					}
				}
			},

			hide: function () {
				message.show_progress();
				jQuery('.pisol-bar-container').fadeOut(message.fadeOuttime);
				jQuery('body').removeClass('pisol-bar-open');
			},

			close_button: function () {
				jQuery('.pisol-fsnw-close').click(function () {
					message.hide();
				});
			},

			hide_progress: function () {
				jQuery('#pi-progress-circle').fadeOut();
			},

			show_progress: function () {
				jQuery('#pi-progress-circle').fadeIn();
			},

			close_progress: function () {
				jQuery('#pi-progress-circle').on('click', function () {
					message.hide_progress();
					message.show();
				});
			},

			isJson: function (item) {
				item = typeof item !== "string"
					? JSON.stringify(item)
					: item;

				try {
					item = JSON.parse(item);
				} catch (e) {
					return false;
				}

				if (typeof item === "object" && item !== null) {
					return true;
				}

				return false;
			},

			get_notification: function () {
				var action = 'get_cart_fsnw';
				jQuery.ajax({
					url: pisol_fsnw.wc_ajax_url.toString().replace('%%endpoint%%', action),
					cache: false,
					type: 'POST',
					dataType: 'json',
					data: {
						action: action
					},
					success: function (response) {

						if (message.isJson(response) && response.min_order !== false) {
							var percent = response.percent > 100 ? 1 : (parseFloat(response.percent) / 100);

							if (jQuery('#pi-progress-circle').length) {
								jQuery('#pi-progress-circle').circleProgress('value', percent);
							}

							jQuery(".pisol-bar-message").html(response.message_bar);
							message.show();
						} else {
							jQuery(".pisol-bar-message").html("");
							message.hide();
							message.hide_progress();
						}

					}
				});
			},

			update_cart: function () {
				jQuery(document).on('added_to_cart updated_wc_div updated_checkout', function (event, data) {
					message.get_notification();
				});
			},

			onload: function () {
				jQuery(document).ready(function () {
					message.get_notification();
				});
			},

			onTrigger: function () {
				var parent = this;
				jQuery(document).on('pi_trigger_get_cart_fsnw', function () {
					parent.get_notification();
				});
			}
		};
		message.show();
		message.onload();
		message.onTrigger();
		message.update_cart();
		message.close_button();
		message.close_progress();
	});

	/**
	 * 
	 * recheck old shipping method and new shipping method 
	 * to make sure the old and new are same 
	 * if old and new shipping method are not same it reloads the shipping method 
	 */
	function shippingMethodRule() {

		$(document).on('updated_checkout', function (e, response) {
			if (response?.fragments?.old_method != undefined && response?.fragments?.new_method != undefined) {
				if (response.fragments.count == 0) return;

				if (response.fragments.old_method != response.fragments.new_method) {
					jQuery("body").trigger("update_checkout");
				}
			}
		});

	}

	jQuery(function () {
		if (pisol_fsnw.percent !== false) {
			var percent = pisol_fsnw.percent > 100 ? 1 : (parseFloat(pisol_fsnw.percent) / 100);
			jQuery('#pi-progress-circle').circleProgress({
				value: percent,
				size: 70,
				fill: {
					gradient: ["red", "orange"]
				}
			});
		}

		shippingMethodRule();
	});

})(jQuery);