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

  const bodyTag = $(document.body);

  const swiperCarouselDashboard = $(".swiper-carousel-dashboard");

  if (swiperCarouselDashboard.length) {
    new Swiper(".swiper-carousel-dashboard", {
      slidesPerView: 1,
      spaceBetween: 0,
      pagination: {
        el: ".swiper-pagination",
        type: "bullets",
        clickable: true,
      },
      effect: "fade",
      fadeEffect: {
        crossFade: true,
      },
      loop: true,
    });
  }

  const sidebar = $(".dashboard-kaveh-sidebar");
  const sidebarContent = $(".dashboard-kaveh-sidebar-content");

  // Toggle Side Bar
  $(
    ".dashboard-kaveh-sidebar-toggle, .dashboard-kaveh-header-open-sidebar"
  ).click(() => {
    if (window.innerWidth >= 1200) {
      sidebar.toggleClass("toggled");
    } else {
      sidebar.toggleClass("open");
      sidebarContent[0].scrollTo({ behavior: "smooth", top: 0 });
    }
  });

  $(".dashboard-kaveh-sidebar-backdrop").click(() => {
    sidebar.removeClass("open");
    sidebarContent[0].scrollTo({ behavior: "smooth", top: 0 });
  });

  // Set Height Bg sidebar Mobile
  const setHeightBgSidebarMobile = () => {
    if (window.innerWidth < 1200) {
      const sidebar = $(".dashboard-kaveh-sidebar-content");

      if (sidebar.length) {
        sidebar.css({ "--h-bg": sidebar[0].scrollHeight + "px" });
      }
    }
  };

  setHeightBgSidebarMobile();

  $(window).resize(setHeightBgSidebarMobile());

  // Select2
  const iconFromValue = (e) => {
    if (e.element) {
      const { icon } = e.element.dataset;

      if (e.text && icon) {
        return $(`<span class="d-flex align-items-center">
          <img src="${icon}" alt="icon" width="29" height="29" class="rounded-circle">
          <span>${e.text}</span>
          </span>`);
      } else {
        return e.text;
      }
    }
  };

  if ($(".select-field").length > 0)
    $(".select-field").select2({
      templateResult: iconFromValue,
      templateSelection: iconFromValue,
      minimumResultsForSearch: -1,
      dropdownAutoWidth: true,
    });

  // dropdown
  $('[data-toggle="dropdown"]').click((ev) => {
    const el = $(ev.currentTarget),
      dropdownOpened = $(".dropdown.open");

    if (el[0].tagName === "INPUT" && !el.parent().hasClass("open")) {
      el.parent().toggleClass("open");
    } else if (el[0].tagName !== "INPUT") {
      el.parent().toggleClass("open");
      dropdownOpened.removeClass("open");
    }
  });

  $(document).click((ev) => {
    const el = ev.target,
      dropdown = $(".dropdown.open");

    if ((dropdown[0] && el.contains(dropdown[0])) || !el.closest(".dropdown")) {
      dropdown.removeClass("open");
    }
  });

  // Set Height Latest Products Purchased
  const latestProductsPurchased = $(
    ".dashboard-kaveh-latest-products-purchased-products"
  );

  if (latestProductsPurchased.length) {
    const products = latestProductsPurchased.children();
    const productHeight = products[0].scrollHeight;
    const productsHeight = Math.ceil(productHeight * 2.3);

    if (products.length > 2) {
      latestProductsPurchased.height(productsHeight);
      latestProductsPurchased.css({ paddingLeft: "8px" });
    }

    latestProductsPurchased.scroll((ev) => {
      if (
        latestProductsPurchased[0].scrollHeight - productHeight <=
        latestProductsPurchased[0].scrollTop + productsHeight
      ) {
        latestProductsPurchased.addClass("ended");
      } else {
        latestProductsPurchased.removeClass("ended");
      }
    });
  }

  // Dark Mode
  if (
    localStorage.getItem("color-theme") === "dark" ||
    (!("color-theme" in localStorage) &&
      window.matchMedia("(prefers-color-scheme: dark)").matches)
  ) {
    bodyTag.addClass("dashboard-kaveh-dark");
  } else if (localStorage.getItem("color-theme") === "dark-2") {
    bodyTag.addClass("dashboard-kaveh-dark-two");
  } else {
    bodyTag.removeClass("dashboard-kaveh-dark");
  }

  const elToggleMode = $("#toggle-mode");

  if (
    ["dark", 'dark-2'].includes(localStorage.getItem("color-theme")) ||
    (!("color-theme" in localStorage) &&
      window.matchMedia("(prefers-color-scheme: dark)").matches)
  ) {
    elToggleMode.addClass("light");
  } else {
    elToggleMode.addClass("dark");
  }

  elToggleMode.click(() => {
    // toggle icons inside button

    // if set via local storage previously
    if (localStorage.getItem("color-theme")) {
      if (localStorage.getItem("color-theme") === "light") {
        elToggleMode.addClass("light");
        elToggleMode.removeClass("dark");
        bodyTag.addClass("dashboard-kaveh-dark");
        localStorage.setItem("color-theme", "dark");
      } else if (localStorage.getItem("color-theme") === "dark") {
        elToggleMode.addClass("light");
        elToggleMode.removeClass("dark");
        bodyTag.addClass("dashboard-kaveh-dark-two");
        bodyTag.removeClass("dashboard-kaveh-dark");
        localStorage.setItem("color-theme", "dark-2");
      } else {
        elToggleMode.removeClass("light");
        elToggleMode.addClass("dark");
        bodyTag.removeClass("dashboard-kaveh-dark-two");
        localStorage.setItem("color-theme", "light");
      }

      // if NOT set via local storage previously
    } else {
      if (document.documentElement.classList.contains("dark")) {
        bodyTag.removeClass("dashboard-kaveh-dark");
        localStorage.setItem("color-theme", "light");
      } else {
        bodyTag.addClass("dashboard-kaveh-dark");
        localStorage.setItem("color-theme", "dark");
      }
    }
  });
});

