// ! function ($) {
// 	"use strict";
// 	$(document).on("click", ".pcpl-wrapper .pcpl-button", function () {
// 		var button = $(this),
// 			post_id = button.attr("data-post-id"),
// 			security = button.attr("data-nonce"),
// 			iscomment = button.attr("data-iscomment"),

// 			isGBlock = button.parents().hasClass('sp-smart-post-wrapper') ? 1 : 0,
// 			allbuttons, loader = (allbuttons = $("1" === iscomment ? ".pcpl-comment-button-" + post_id : ".pcpl-button-" + post_id)).next("#pcpl-loader");
// 		//
// 		if (isGBlock) {
// 			var $firstButton = button.parents('.sp-smart-post-wrapper').find('.pcpl-button').eq(0);
// 			security = $firstButton.length > 0 ? $firstButton.attr("data-nonce") : security;
// 		}
// 		return "" !== post_id && $.ajax({
// 			type: "POST",
// 			url: simpleLikes.ajaxurl,
// 			data: {
// 				action: "process_pcp_like",
// 				post_id: post_id,
// 				nonce: security,
// 				is_comment: iscomment,
// 				is_block: isGBlock
// 			},
// 			beforeSend: function () {
// 				loader.html('&nbsp;<div class="loader">Loading...</div>')
// 			},
// 			error: function (response) {
// 				console.log(response);
// 			},
// 			success: function (response) {
// 				var icon = response.icon,
// 					count = response.count;
// 				if (allbuttons.html(icon + count), "unliked" === response.status) {
// 					var like_text = simpleLikes.like;
// 					allbuttons.prop("title", like_text), allbuttons.removeClass("liked")
// 				} else {
// 					var unlike_text = simpleLikes.unlike;
// 					allbuttons.prop("title", unlike_text), allbuttons.addClass("liked")
// 				}
// 				loader.empty()
// 			}
// 		}), !1
// 	})
// }(jQuery);

!(function ($) {
	'use strict';
	$(document).on('click', '.pcpl-wrapper .pcpl-button', function () {
		var button = $(this),
			post_id = button.attr('data-post-id'),
			security = button.attr('data-nonce'),
			iscomment = button.attr('data-iscomment'),
			isGBlock = button.parents().hasClass('sp-smart-post-wrapper')
				? 1
				: 0,
			allbuttons,
			loader = (allbuttons = $(
				'1' === iscomment
					? '.pcpl-comment-button-' + post_id
					: '.pcpl-button-' + post_id
			)).next('#pcpl-loader');
		//
		if (isGBlock) {
			var $firstButton = button
				.parents('.sp-smart-post-wrapper')
				.find('.pcpl-button')
				.eq(0);
			security =
				$firstButton.length > 0
					? $firstButton.attr('data-nonce')
					: security;
		}

		const fromSingleTemplate = button.attr('data-single');
		return (
			'' !== post_id &&
				$.ajax({
					type: 'POST',
					url: simpleLikes.ajaxurl,
					data: {
						action: 'process_pcp_like',
						post_id: post_id,
						nonce: security,
						is_comment: iscomment,
						is_block: isGBlock,
					},
					beforeSend: function () {
						loader.html(
							'&nbsp;<div class="loader">Loading...</div>'
						);
					},
					error: function (response) {
						console.log(response);
					},
					success: function (response) {
						if (fromSingleTemplate) {
							const countHtml = response.count;
							const countMatch = countHtml.match(/>(\d+)</);
							const count = countMatch ? countMatch[1] : '0';
							const responseIcon = response.icon;
							button
								.find('.sp-smart-post-like-count-value')
								.text(count);
							button
								.find('.sp-smart-post-like-count-icon')
								?.html(responseIcon);
							button
								.parents('.sp-smart-post-wrapper')
								.find('.sp-smart-post-like-count-value')
								.text(count);
							if ( 'unliked' === response.status ) {
								button.removeClass('liked');
							} else {
								button.removeClass('liked');
								button.addClass('liked');

							}
						} else {
							var icon = response.icon,
							count = response.count;
							if (
								(allbuttons.html(icon + count),
								'unliked' === response.status)
							) {
								var like_text = simpleLikes.like;
								(allbuttons.prop('title', like_text),
									allbuttons.removeClass('liked'));
							} else {
								var unlike_text = simpleLikes.unlike;
								(allbuttons.prop('title', unlike_text),
									allbuttons.addClass('liked'));
							}
							loader.empty();
						}
					},
				}),
			!1
		);
	});
})(jQuery);

