"use client";

import { useEffect, useState } from "react";
import Image from "next/image";
import { useMediaQuery } from "usehooks-ts";
import { useSliderBanner } from "@/hooks/use-slider";
import Loader from "../ui/Loader";
import { useLocale } from "next-intl";

export const SliderClient = () => {
  const isTab = useMediaQuery("(min-width: 1024px)");
  const isMd = useMediaQuery("(min-width: 700px)");

  const [data, setData] = useState<any>(null);
  const [loading, setLoading] = useState(true);
  const bannerType = useSliderBanner(isTab, isMd);
  const locale = useLocale();

  const fetchData = async () => {
    try {
      setLoading(true);
      const res = await fetch(`/api/banners?bannerType=${bannerType}`, {
        headers: {
          "Accept-Language": locale,
        },
      });
      const json = await res.json();
      setData(json);
      setLoading(false);
    } catch (error) {
      console.error(error);
    }
  };

  useEffect(() => {
    fetchData();
  }, [bannerType, locale]);

  if (loading) return <Loader className="h-[600px] min-h-[320px]" />;

  return (
    <Image
      src={data.data.banner_items?.[0]?.image || ""}
      alt="Баннер"
      width={1920}
      height={600}
      className="object-cover max-h-[600px] min-h-[320px] size-full object-center"
    />
  );
};
