import { describe, expect, it, vi, beforeEach } from 'vitest';

// Mock @inertiajs/react - hoisted to top
const mockRouterGet = vi.fn();
vi.mock('@inertiajs/react', () => ({
    router: {
        get: (...args: any[]) => mockRouterGet(...args),
    },
}));

// Define global route function
declare global {
    function route(routeName: string): string;
}

describe('usePagination', () => {
    beforeEach(() => {
        mockRouterGet.mockClear();
        // Mock global route function
        (global as any).route = vi.fn((routeName: string) => `/api/${routeName}`);
    });

    it('should return handlePageChange function', async () => {
        const { usePagination } = await import('../use-pagination');
        const { handlePageChange } = usePagination('users.index');
        expect(typeof handlePageChange).toBe('function');
    });

    it('should call router.get with correct parameters', async () => {
        const { usePagination } = await import('../use-pagination');
        const { handlePageChange } = usePagination('users.index');
        handlePageChange(2);

        expect(mockRouterGet).toHaveBeenCalledWith(
            '/api/users.index',
            { page: 2 },
            {
                preserveState: true,
                preserveScroll: true,
            }
        );
    });

    it('should handle different page numbers', async () => {
        const { usePagination } = await import('../use-pagination');
        const { handlePageChange } = usePagination('posts.index');
        
        handlePageChange(1);
        expect(mockRouterGet).toHaveBeenCalledWith(
            '/api/posts.index',
            { page: 1 },
            expect.any(Object)
        );

        handlePageChange(10);
        expect(mockRouterGet).toHaveBeenCalledWith(
            '/api/posts.index',
            { page: 10 },
            expect.any(Object)
        );
    });
});
