// source --> https://insperia.ru/wp-content/plugins/solid-post-likes/views/public/js/solid-post-likes-public.js?ver=1.0.6 
(function($) {
  "use strict"
  $(document).on("click", ".oacs-spl-like-button", function() {
    var button = $(this);
      var post_id = button.data("post-id"); // Use .data() for data attributes
      var security = button.data("nonce");   // Use .data() for data attributes
      var iscomment = button.data("iscomment"); // Use .data() for data attributes

      var currentbutton;

      if (iscomment == "1") {
        currentbutton = $(".oacs-spl-like-comment-button-" + post_id);
      } else {
        currentbutton = $(".oacs-spl-like-button-" + post_id);
      }


      if (post_id !== "") {
        // Immediately update the button style (optimistic update)
        var liked = currentbutton.hasClass("oacs-spl-liked");
        if (liked) {
          currentbutton.removeClass("oacs-spl-liked");
        } else {
          currentbutton.addClass("oacs-spl-liked");
        }

        // Display spinner immediately
        button.children('.spinner').addClass("spl-is-active"); // Cache this too!

        $.ajax({
          type: "POST",
          url: oacs_spl_solid_likes.ajaxurl,
          data: {
            action: "oacs_spl_process_like",
            post_id: post_id,
            nonce: security,
            is_comment: iscomment
          },
          success: function(response) {
            // define variables by accessing response object.
            var icon = response.icon;
            var count = response.count;
            var text = response.text;

            var button_output = ['<span class="spinner"></span>'];

            if (icon !== null) {
              button_output.push(icon);
            }

            if (count !== null) {
              button_output.push(count);
            }

            if (text !== null) {
              button_output.push(text);
            }

            // render button with icon, count and text.
            currentbutton.html(button_output.join('')); // Join the array

            // Correct the style if the response disagrees with the optimistic update.
            if (response.status === "unliked" && liked) { // If we *thought* it was liked, but it's not.
              // No action needed, we already removed the class
            } else if (response.status === "liked" && !liked) { //If we *thought* it was unliked, but it is.
              // No action needed, we already added the class
            }

          },
          complete: () => {  //Use complete instead of success to remove the spinner.
            button.children('.spinner').removeClass("spl-is-active"); //Remove the spinner
          },
          error: (jqXHR, textStatus, errorThrown) => {
            //Revert changes on error
            if (liked) {
              currentbutton.addClass("oacs-spl-liked");
            } else {
              currentbutton.removeClass("oacs-spl-liked");
            }

            console.error("AJAX error:", textStatus, errorThrown);
            alert("An error occurred while processing the like."); // Optional: Inform the user.
          }
        });
      }
      return false;
  });
})(jQuery);