jQuery(document).ready(function ($) {

  /* --------------------------------
  Legacy Swiper init (only if Swiper exists AND elements exist)
  ------------------------------ */
  if (typeof Swiper !== 'undefined') {
    if ($('.ipp-slider-thumb').length) {
      var wppThumbSwiper = new Swiper(".ipp-slider-thumb", {
        slidesPerView: 4, spaceBetween: 5, freeMode: true,
        watchSlidesVisibility: true, watchSlidesProgress: true,
      });
      new Swiper(".ipp-slider", {
        slidesPerView: 1, spaceBetween: 0,
        pagination: { el: ".swiper-pagination", dynamicBullets: true },
        thumbs: { swiper: wppThumbSwiper },
      });
    }
    if ($('.ipp-classic-slider-thumb').length) {
      var classicThumbSwiper = new Swiper(".ipp-classic-slider-thumb", {
        slidesPerView: 7, spaceBetween: 10, freeMode: true,
        watchSlidesVisibility: true, watchSlidesProgress: true,
      });
      new Swiper(".ipp-classic-slider", {
        slidesPerView: 1, spaceBetween: 0,
        navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev" },
        thumbs: { swiper: classicThumbSwiper },
      });
    }
  }

  /* --------------------------------
  Load More (legacy + new)
  ------------------------------ */
  var page = 1;

  $(document).on("click", "#ipp-load-more", function () {
    var $button = $(this);
    page++;
    $button.prop("disabled", true).html(
      '<svg class="ipp-spinner" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10" opacity="0.3"/><path d="M12 2a10 10 0 0 1 10 10"/></svg> Loading...'
    );

    $.ajax({
      url: (typeof ipp_ajax !== 'undefined' ? ipp_ajax.ajax_url : ajax_params.ajax_url),
      type: "post",
      data: { action: "ajax_load_button", page: page },
      success: function (response) {
        if (response.success) {
          var $grid = $(".ipp-grid, .ipp-archive-properties");
          $grid.append(response.data.content);
          if (page >= response.data.max_pages) {
            $button.hide();
          }
        }
      },
      complete: function () {
        $button.prop("disabled", false).html("Load More Properties");
      },
    });
  });

  /* --------------------------------
  Grid / List toggle (legacy)
  ------------------------------ */
  $(".ipp-archive-list").on("click", function () {
    $(".ipp-archive-properties").addClass("ipp-archive-layout_list");
  });
  $(".ipp-archive-grid").on("click", function () {
    $(".ipp-archive-properties").removeClass("ipp-archive-layout_list");
  });

  /* --------------------------------
  Read more toggle (legacy)
  ------------------------------ */
  $(".ipp-single-read-more_button").click(function () {
    var detailsContent = $(".ipp-details_content");
    var arrow = $(".ipp-single-read-more_arrow");
    if (detailsContent.is(":visible")) {
      detailsContent.hide();
      arrow.addClass("-rotate-180");
    } else {
      detailsContent.show();
      arrow.removeClass("-rotate-180");
    }
  });
});
