(($) => {
  "use strict";

  $(document).ready(function () {
    const { ajaxURL, welcomeNonce, redirectURL } = ajaxObj;

    const $dashboard = $("#monocle-dashboard");

    /* Welcome notice script */
    $("#monocle-welcome-notice").on("click", ".notice-dismiss", function () {
      $.ajax({
        url: ajaxURL,
        method: "POST",
        data: {
          action: "monocle_dismissble_notice",
          nonce: welcomeNonce,
        },
        success: function (response) {
          if (response.success) {
            console.log("Notice dismissed successfully.");
            $("#monocle-welcome-notice").fadeOut(); // Hide the notice
          } else {
            console.log("Failed to dismiss notice!");
          }
        },
        error: function (jqXHR, textStatus, errorThrown) {
          console.log("Error:", textStatus, errorThrown);
        },
      });
    });

    // FAQ Accordion
    $dashboard.find(".accordion-header").on("click", function () {
      var $item = $(this).closest(".accordion-item");
      var isActive = $item.hasClass("active");

      // close all others (remove this block if you want multiple open at once)
      $(".accordion-item").not($item).removeClass("active");

      $item.toggleClass("active", !isActive);
    });

    /* Plugin Installation */
    // Cozy Blocks installation
    $(".cozy-addons-install").click(function () {
      const $this = $(this);
      const $spinner = $this.find("#monocle-admin-spinner");

      $spinner.removeClass("monocle-display-none");
      $this.addClass("monocle-disabled");

      $.post(
        ajaxURL,
        {
          action: "monocle_install_and_activate_plugins",
          plugins: JSON.stringify(["cozy-addons"]),
          nonce: welcomeNonce,
        },
        function (response) {
          // alert(response);
          var checkJSON = /{.*}/; // Regular expression to match the JSON portion
          var match = checkJSON.exec(response);

          if (match) {
            var jsonResponse = match[0]; // Extracted JSON portion
            try {
              var responseObj = JSON.parse(jsonResponse); // Parse the extracted JSON

              if (responseObj.success === true) {
                // window.location.href = response.data.redirect_url;
                console.log("Plugin installed");
              } else {
                console.log("Error!");
              }
            } catch (error) {
              console.log("Error parsing JSON!");
            }
          }

          window.location.href = redirectURL;

          $spinner.addClass("monocle-display-none");
          $this.removeClass("monocle-disabled");
        },
      );
    });

    // Install Cozy Essential Addons/Advanced Import
    $("#install-required-plugins").click(function () {
      const $this = $(this);
      const $spinner = $this.find("#monocle-admin-spinner");

      $spinner.removeClass("monocle-display-none");
      $this.addClass("monocle-disabled");

      $.post(
        ajaxURL,
        {
          action: "monocle_install_and_activate_plugins",
          plugins: JSON.stringify(["cozy-addons"]),
          nonce: welcomeNonce,
        },
        function (response) {
          // alert(response);
          var checkJSON = /{.*}/; // Regular expression to match the JSON portion
          var match = checkJSON.exec(response);

          if (match) {
            var jsonResponse = match[0]; // Extracted JSON portion
            try {
              var responseObj = JSON.parse(jsonResponse); // Parse the extracted JSON

              if (responseObj.success === true) {
                // window.location.href = response.data.redirect_url;
                console.log("Plugin installed");
              } else {
                console.log("Error!");
              }
            } catch (error) {
              console.log("Error parsing JSON!");
            }
          }

          window.location.href = redirectURL;

          $spinner.addClass("monocle-display-none");
          $this.removeClass("monocle-disabled");
        },
      );
    });
  });
})(jQuery);
