/* eslint-disable @typescript-eslint/no-explicit-any */
import { usePage } from '@inertiajs/react';

const useApiClient = () => {
    const { token } = usePage().props;

    const apiClient = {
        get: async (url: string, params?: Record<string, any>) => {
            const response = await fetch(url + new URLSearchParams(params || {}), {
                method: 'GET',
                headers: {
                    Accept: 'application/json',
                    Authorization: `Bearer ${token}`,
                },
            });
            return response.json();
        },
        post: async (url: string, data: Record<string, any>) => {
            const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    Accept: 'application/json',
                    Authorization: `Bearer ${token}`,
                },
                body: JSON.stringify(data),
            });
            return response.json();
        },
        put: async (url: string, data: Record<string, any>) => {
            const response = await fetch(url, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json',
                    Accept: 'application/json',
                    Authorization: `Bearer ${token}`,
                },
                body: JSON.stringify(data),
            });
            return response.json();
        },
        delete: async (url: string) => {
            const response = await fetch(url, {
                method: 'DELETE',
                headers: {
                    Accept: 'application/json',
                    Authorization: `Bearer ${token}`,
                },
            });
            return response.json();
        },
    };

    return apiClient;
};

export default useApiClient;
