(function (jQuery, elementor) {
  "use strict";

  var MEDITEST = {
    init: function () {
      var widgets = {
        "posts-grid-one-col.default": MEDITEST.Posts_medium_list_load,
      };

      jQuery.each(widgets, function (widget, callback) {
        elementorFrontend.hooks.addAction(
          "frontend/element_ready/" + widget,
          callback
        );
      });
    },

    /* ----------------------------------------------------------- */
    /*   Post grid ajax load
         /* ----------------------------------------------------------- */

    Posts_medium_list_load: function (jQueryscope) {
      var jQuerycontainer = jQueryscope.find(".meditest-load-more-btn");

      if (jQuerycontainer.length > 0) {
        jQuerycontainer.on("click", function (event) {
          event.preventDefault();
          if (jQuery.active > 0) {
            return;
          }
          var jQuerythat = jQuery(this);
          var ajaxjsondata = jQuerythat.data("json_grid_meta");
          var meditest_json_data = Object(ajaxjsondata);

          var contentwrap = jQueryscope.find(".grid-loadmore-content"), // item contentwrap
            postperpage = parseInt(meditest_json_data.posts_per_page), // post per page number
            showallposts = parseInt(meditest_json_data.total_post); // total posts count

          var items = contentwrap.find(".grid-item"),
            totalpostnumber = parseInt(items.length),
            paged = parseInt(totalpostnumber / postperpage) + 1; // paged number

          jQuery
            .ajax({
              url: meditest_ajax.ajax_url,
              type: "POST",
              data: {
                action: "meditest_post_ajax_loading",
                ajax_json_data: ajaxjsondata,
                paged: paged,
              },
              beforeSend: function () {
                jQuery(
                  '<i class="ts-icon ts-icon-spinner fa-spin" style="margin-left:10px"></i>'
                )
                  .appendTo("#meditest-load-more-btn")
                  .fadeIn(100);
              },
              complete: function () {
                jQueryscope
                  .find(".meditest-load-more-btn .fa-spinner ")
                  .remove();
              },
            })

            .done(function (data) {
              var jQuerypstitems = jQuery(data);
              jQueryscope.find(".grid-loadmore-content").append(jQuerypstitems);
              var newLenght = contentwrap.find(".grid-item").length;

              if (showallposts <= newLenght) {
                jQueryscope
                  .find(".meditest-load-more-btn")
                  .fadeOut(300, function () {
                    jQueryscope.find(".meditest-load-more-btn").remove();
                  });
              }
            })

            .fail(function () {
              jQueryscope.find(".meditest-load-more-btn").remove();
            });
        });
      }
    },
  };

  jQuery(window).on("elementor/frontend/init", MEDITEST.init);
})(jQuery, window.elementorFrontend);
