"use client"
import React from 'react';
import {Pagination, PaginationContent, PaginationItem} from "@/shared/ui/pagination";
import {Link, useRouter} from "@/shared/config/i18n/navigation";
import {Button} from "@/shared/ui/button";
import {useSearchParams} from "next/navigation";

interface MyPagionationProps {
  currentPage: number;
  totalPages: number;
}

const MyPagionation = ({currentPage, totalPages}: MyPagionationProps) => {
  const router = useRouter()
  const searchParams = useSearchParams();

  const navigateToPage = (page: number) => {
    const currentParams = new URLSearchParams(searchParams.toString());
    currentParams.set("page", String(page));

    router.push(`?${currentParams.toString()}`);
  };

  const renderPaginationItems = () => {
    return Array.from({length: totalPages}, (_, index) => {
      const page = index + 1;
      const isActive = currentPage === page;
      return (
            <Button
                key={page}
                size="lg"
                variant="outline"
                className={isActive ? "bg-primary/10" : ""}
                onClick={() => navigateToPage(page)}
                disabled={isActive}
                aria-current={isActive ? "page" : undefined}
            >
              {page}
            </Button>
      );
    });
  };

  return (
      <Pagination>
        <PaginationContent>
          {currentPage > 1 && (
              <Button size="lg" onClick={() => navigateToPage(currentPage - 1)}>
                Oldingi
              </Button>
          )}
          {renderPaginationItems()}
          {currentPage < totalPages && (
              <Button size="lg" onClick={() => navigateToPage(currentPage + 1)}>
                Keyingi
              </Button>
          )}
        </PaginationContent>
      </Pagination>
  );
};

export default MyPagionation;