try {
  $ = jQuery;
} catch (err) {}
$(document).ready(function () {
  $(".deleteDiamond").click(function () {
    let pType = $(this).attr("data-type");
    toggle_kirb_loader(true);
    $.get(
      KIRB_GLOBALS.ajax_url,
      {
        action: "kirb_del_prod",
        type: pType,
        nonce: KIRB_GLOBALS.nonce,
      },
      (resp) => {
        if (resp.status == "success") {
          window.location.href = resp.redirect_url;
        }
      }
    );
  });

  $(".kirb-byor-reset-btn").click(function () {
    toggle_kirb_loader(true);
    $.get(
      KIRB_GLOBALS.ajax_url,
      {
        action: "kirb_byor_restart",
        nonce: KIRB_GLOBALS.nonce,
      },
      (resp) => {
        if (resp.status == "success") {
          window.location.href = resp.redirect_url;
        }
      }
    );
  });

  $(".kirb-show-dropdown-byor").each(function () {
    var $dropdown = $(this);
    var tooltipClass = $dropdown.hasClass("byor-dropdown-diamond")
      ? "tooltip-content-byor-dropdown-diamond"
      : $dropdown.hasClass("byor-dropdown-setting")
      ? "tooltip-byor-dropdown-setting"
      : "tooltip-byor-dropdown-ring";

    var $tooltip = $("." + tooltipClass);

    if ($tooltip.length) {
      // Desktop hover functionality
      $dropdown.on("mouseenter", function () {
        if ($(window).width() > 768) {
          $tooltip.css("display", "flex");
        }
      });

      $dropdown.on("mouseleave", function () {
        if ($(window).width() > 768) {
          $tooltip.css("display", "none");
        }
      });

      $tooltip.on("mouseenter", function () {
        if ($(window).width() > 768) {
          $tooltip.css("display", "flex");
        }
      });

      $tooltip.on("mouseleave", function () {
        if ($(window).width() > 768) {
          $tooltip.css("display", "none");
        }
      });

      // Mobile view toggle functionality
      $dropdown.on("click", function (event) {
        if ($(window).width() <= 768) {
          event.stopPropagation();

          // Close all other tooltips
          $(".kirb-show-dropdown-byor").each(function () {
            var $otherDropdown = $(this);
            if ($otherDropdown.get(0) !== $dropdown.get(0)) {
              var otherTooltipClass = $otherDropdown.hasClass(
                "byor-dropdown-diamond"
              )
                ? "tooltip-content-byor-dropdown-diamond"
                : $otherDropdown.hasClass("byor-dropdown-setting")
                ? "tooltip-byor-dropdown-setting"
                : "tooltip-byor-dropdown-ring";
              var $otherTooltip = $("." + otherTooltipClass);
              if ($otherTooltip.length) {
                $otherTooltip.css("display", "none");
              }
            }
          });

          // Toggle the clicked tooltip
          if ($tooltip.css("display") === "flex") {
            $tooltip.css("display", "none");
          } else {
            $tooltip.css("display", "flex");
          }
        }
      });

      // Close tooltip when clicking outside in mobile view
      $(document).on("click", function (event) {
        if (
          $(window).width() <= 768 &&
          !$dropdown.is(event.target) &&
          !$tooltip.is(event.target) &&
          $dropdown.has(event.target).length === 0 &&
          $tooltip.has(event.target).length === 0
        ) {
          $tooltip.css("display", "none");
        }
      });

      $(".kirb-btn-byor-view, .kirb-btn-byor-change").click(function () {
        const url = $(this).attr("data-url");
        if(url && url.length > 1) {
          document.location.href = url;
        }
      });
    }
  });
// added on click handler for copy-button
  $(".copy-button").click(function(){
      let text = $(this).data("copy");
      navigator.clipboard.writeText(text);

      let btn = $(this);

      btn.attr("title", "Copied");
  });

  $(".kirb-link-btn").click(function(){
    if($(this).attr("data-url") && $(this).attr("data-url").length > 1) {
      document.location.href = $(this).attr("data-url");
    }
  });

  $(".kirb-item-byor").click(function(){
    if(!($(this).hasClass("kirb-highlighted")) && $(this).attr("data-url") && $(this).attr("data-url").length > 1) {
      document.location.href = $(this).attr("data-url");
    }
  });
});
