// import Swiper core and required modules
import { Navigation, Pagination, Scrollbar, A11y } from "swiper/modules";

import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import "swiper/css/scrollbar";
import { useGetCategories } from "../../api/Categories";
import { Category } from "../../types/item";
import { useNavigate } from "react-router-dom";
import { addBaseUrlToSrc } from "../../utils/addBaseUrlToSrc";

const CategoriesSlider = () => {
  const { data } = useGetCategories();
  const categories = (data?.categories as Category[]) || ([] as []);
  const navigate = useNavigate();
  const handelNavigate = (data: Category) => {
    navigate(`/categories?category=${data?.id}`);
  };


  return (
    <div className="CategoriesSlider">
      <Swiper
        modules={[Navigation, A11y]}
        spaceBetween={20}
        slidesPerView={5}
        navigation
        breakpoints={{
          320: {
            slidesPerView: 2,
          },
          480: {
            slidesPerView: 2,
          },
          640: {
            slidesPerView: 3,
          },
          768: {
            slidesPerView: 4,
          },
          1024: {
            slidesPerView: 5,
          },
        }}

      >
        {categories?.map((item: Category, index: number) => {
          return (
            <SwiperSlide key={index}>
              <div className="CategoriesSlider_image">
                <img
                  onClick={() => handelNavigate(item)}
                  src={addBaseUrlToSrc(item?.photo)}
                  alt={`category ${index + 1}`}
                />
              </div>
            </SwiperSlide>
          );
        })}
      </Swiper>
    </div>
  );
};
export default CategoriesSlider;
