const menuToggle = document.querySelector(".menu-toggle");
const navLinks = document.querySelector(".nav-links");
const navAnchors = document.querySelectorAll(".nav-links a");
const slidesTrack = document.querySelector(".slides");
const prevButton = document.querySelector(".slider-arrow.prev");
const nextButton = document.querySelector(".slider-arrow.next");
const mobileBreakpoint = window.matchMedia("(max-width: 900px)");
const slideImages = [
  { src: "assets/pc_anasayfa.png", alt: "Golder Kuyumcu pc anasayfa ekrani" },
  { src: "assets/pc_al\u0131\u015F.png", alt: "Golder Kuyumcu pc alis ekrani" },
  { src: "assets/pc_ayarlar.png", alt: "Golder Kuyumcu pc ayarlar ekrani" },
  { src: "assets/pc_bedelli.png", alt: "Golder Kuyumcu pc bedelli ekrani" },
  { src: "assets/pc_cari.png", alt: "Golder Kuyumcu pc cari ekrani" },
  { src: "assets/pc_cihazlar.png", alt: "Golder Kuyumcu pc cihazlar ekrani" },
  { src: "assets/pc_hizmetler.png", alt: "Golder Kuyumcu pc hizmetler ekrani" },
  { src: "assets/pc_hizmetler2.png", alt: "Golder Kuyumcu pc hizmetler iki ekrani" },
  { src: "assets/pc_para.png", alt: "Golder Kuyumcu pc para ekrani" },
  { src: "assets/pc_rapor.png", alt: "Golder Kuyumcu pc rapor ekrani" },
  { src: "assets/pc_stok.png", alt: "Golder Kuyumcu pc stok ekrani" },
  { src: "assets/pc_tamir.png", alt: "Golder Kuyumcu pc tamir ekrani" },
  { src: "assets/pc_vergi.png", alt: "Golder Kuyumcu pc vergi ekrani" },
];
let slides = [];
let dots = [];
const dotsContainer = document.querySelector(".slider-dots");
let activeSlide = 0;
let autoPlayId;
let touchStartX = null;
let touchDeltaX = 0;
const swipeThreshold = 48;
function buildSlides() {
  slidesTrack.innerHTML = "";
  slideImages.forEach(({ src, alt }, index) => {
    const slide = document.createElement("article");
    const image = document.createElement("img");
    slide.className = `slide${index === 0 ? " active" : ""}`;
    image.className = "slide-media";
    image.src = src;
    image.alt = alt;
    slide.appendChild(image);
    slidesTrack.appendChild(slide);
  });
  slides = Array.from(slidesTrack.querySelectorAll(".slide"));
}
function buildDots() {
  dotsContainer.innerHTML = "";
  slideImages.forEach((_, index) => {
    const dot = document.createElement("button");
    dot.type = "button";
    dot.className = `slider-dot${index === 0 ? " active" : ""}`;
    dot.setAttribute("aria-label", `${index + 1}. slayta git`);
    dot.addEventListener("click", () => {
      showSlide(index);
      restartAutoPlay();
    });
    dotsContainer.appendChild(dot);
  });
  dots = Array.from(dotsContainer.querySelectorAll(".slider-dot"));
}
function setMenuState(isOpen) {
  menuToggle.setAttribute("aria-expanded", String(isOpen));
  navLinks.classList.toggle("open", isOpen);
}
function showSlide(index) {
  activeSlide = (index + slides.length) % slides.length;
  slides.forEach((slide, slideIndex) => {
    slide.classList.toggle("active", slideIndex === activeSlide);
  });
  dots.forEach((dot, dotIndex) => {
    dot.classList.toggle("active", dotIndex === activeSlide);
  });
}
function restartAutoPlay() {
  window.clearInterval(autoPlayId);
  autoPlayId = window.setInterval(() => {
    showSlide(activeSlide + 1);
  }, 5000);
}
menuToggle.addEventListener("click", () => {
  const isOpen = menuToggle.getAttribute("aria-expanded") === "true";
  setMenuState(!isOpen);
});
navAnchors.forEach((anchor) => {
  anchor.addEventListener("click", () => {
    if (mobileBreakpoint.matches) {
      setMenuState(false);
    }
  });
});
mobileBreakpoint.addEventListener("change", (event) => {
  if (!event.matches) {
    setMenuState(false);
  }
});
prevButton.addEventListener("click", () => {
  showSlide(activeSlide - 1);
  restartAutoPlay();
});
nextButton.addEventListener("click", () => {
  showSlide(activeSlide + 1);
  restartAutoPlay();
});
slidesTrack.addEventListener("touchstart", (event) => {
  if (event.touches.length !== 1) {
    return;
  }
  touchStartX = event.touches[0].clientX;
  touchDeltaX = 0;
}, { passive: true });
slidesTrack.addEventListener("touchmove", (event) => {
  if (touchStartX === null) {
    return;
  }
  touchDeltaX = event.touches[0].clientX - touchStartX;
}, { passive: true });
slidesTrack.addEventListener("touchend", () => {
  if (Math.abs(touchDeltaX) < swipeThreshold) {
    touchStartX = null;
    touchDeltaX = 0;
    return;
  }
  showSlide(touchDeltaX > 0 ? activeSlide - 1 : activeSlide + 1);
  restartAutoPlay();
  touchStartX = null;
  touchDeltaX = 0;
});
slidesTrack.addEventListener("touchcancel", () => {
  touchStartX = null;
  touchDeltaX = 0;
});
buildSlides();
buildDots();
showSlide(0);
restartAutoPlay();


