"use client";

import { countryService } from "@/services/country";
import { Country } from "@/types/global";
import { useInfiniteQuery } from "@tanstack/react-query";
import clsx from "clsx";
import Link from "next/link";
import { useT } from "@/context/translation-map";
import { useSettings } from "@/hook/use-settings";
import { extractDataFromPagination } from "@/utils/extract-data";
import { Button } from "@/components/button";
import { ClientOnly } from "@/components/client-only/client-only";

export const FindBestSalon = () => {
  const t = useT();
  return (
    <div className="mt-14 mb-14">
      <div className="md:text-[26px] text-xl font-semibold mb-10 text-center">
        {t("best.salon.and.master")}
      </div>
      <ClientOnly
        fallback={
          <div className="grid lg:grid-cols-6 md:grid-cols-5 sm:grid-cols-3 gap-4">
            {Array.from(Array(12).keys()).map((i) => (
              <div key={i} className="h-6 bg-gray-200 rounded animate-pulse" />
            ))}
          </div>
        }
      >
        <FindBestSalonList />
      </ClientOnly>
    </div>
  );
};

const FindBestSalonList = () => {
  const t = useT();
  const { language } = useSettings();
  const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery(
    ["regions", language?.locale],
    ({ pageParam }) =>
      countryService.getAll({ lang: language?.locale, page: pageParam, perPage: 12 }),
    {
      keepPreviousData: true,
      getNextPageParam: (lastPage) => lastPage.links.next && lastPage.meta.current_page + 1,
    }
  );
  const regionList = extractDataFromPagination(data?.pages);
  return (
    <>
      <ul className="grid lg:grid-cols-6 md:grid-cols-5 sm:grid-cols-3 gap-4">
        {regionList?.map((item: Country, index: number) => (
          <li
            key={item.id}
            className={clsx("text-lg cursor-pointer", index < 6 ? "font-bold" : "")}
          >
            <Link href={`/search?countryId=${item.id}`}>{item.translation?.title}</Link>
          </li>
        ))}
      </ul>
      <div className="flex justify-center items-center mt-5">
        <Button
          size="small"
          color="black"
          className={clsx("text-lg cursor-pointer")}
          disabled={!hasNextPage}
          loading={isFetchingNextPage}
          onClick={() => fetchNextPage()}
        >
          {t("load.more")}
        </Button>
      </div>
    </>
  );
};
