(function () {
  var $plugins = $('[data-plugin]');
  var pluginsSet = new Set();

  function appendStyle(href) {
    if (!$('link[href="' + href + '"]').length) {
      $('<link>', {
        rel: 'stylesheet',
        href: href
      }).appendTo('head');
    }
  }

  function appendScript(src, { async = false, defer = false, innerText = '', callback = null } = {}) {
    if (!$('script[src="' + src + '"]').length) {
      var script = document.createElement('script');
      script.src = src;
      if (async) script.async = true;
      if (defer) script.defer = true;
      if (innerText) script.textContent = innerText;
      if (callback) script.onload = callback;
      script.onerror = function () { console.error('Failed to load script:', src); };
      document.body.appendChild(script);
    }
  }

  var scriptsToLoad = 0;

  function incrementScriptsLoaded() {
    if (--scriptsToLoad === 0) {
      appendScript(dataJsonBase.url_asset + 'assets/js/objet.js?v8.0.4', { defer: true });
    }
  }

  $plugins.each(function () {
    pluginsSet.add($(this).data('plugin').toLowerCase());
  });

  pluginsSet.forEach(function (plugin) {
    switch (plugin) {
      case 'swiper':
        scriptsToLoad += 1;
        appendStyle(dataJsonBase.url_asset + 'node_modules/swiper/swiper.min.css');
        appendStyle(dataJsonBase.url_asset + 'node_modules/swiper/modules/navigation/navigation.min.css');
        appendStyle(dataJsonBase.url_asset + 'node_modules/swiper/modules/pagination/pagination.min.css');
        appendScript(dataJsonBase.url_asset + 'node_modules/swiper/swiper-bundle.min.js', { defer: true, callback: incrementScriptsLoaded });
        break;
      case 'lightbox':
        scriptsToLoad += 1;
        appendStyle(dataJsonBase.url_asset + 'node_modules/simplelightbox/dist/simple-lightbox.min.css');
        appendScript(dataJsonBase.url_asset + 'node_modules/simplelightbox/dist/simple-lightbox.min.js', { defer: true, callback: incrementScriptsLoaded });
        break;
      case 'magnific-popup':
        scriptsToLoad += 1;
        appendStyle(dataJsonBase.url_asset + 'node_modules/magnific-popup/dist/magnific-popup.css');
        appendScript(dataJsonBase.url_asset + 'node_modules/magnific-popup/dist/jquery.magnific-popup.min.js', { defer: true, callback: incrementScriptsLoaded });
        break;

      case 'full-calendar':
        scriptsToLoad += 1;
        appendScript(dataJsonBase.url_asset + 'node_modules/fullcalendar/index.global.min.js', { defer: true, callback: incrementScriptsLoaded });
        break;
    }
  });

  if (scriptsToLoad === 0) {
    // If no plugins to load, directly load objet.js or perform other necessary actions
    appendScript(dataJsonBase.url_asset + 'assets/js/objet.js?v8.0.4', { defer: true });
  }
})();
