const DEFAULT_BASE_URL = process.env.NEXT_PUBLIC_BACKEND_HOST || '';

export default class HttpClient {
    // private static getToken() { return localStorage?.getItem('token')?.replace(/"/g, ''); }

    public static async request(endpoint: string, options: RequestInit & { baseUrl?: string } = {}) {
        const baseUrl = options.baseUrl || DEFAULT_BASE_URL;
        const url = `${baseUrl}${endpoint}`;
        // const token = this?.getToken();
        // const headers = { ...options.headers, ...(token && { 'Authorization': `Bearer ${token}` }), };
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        const { baseUrl: _, ...fetchOptions } = options;

        const headers = { ...options.headers,  };

        try {
            const response = await fetch(url, { ...fetchOptions, headers });
            if (!response.ok) throw new Error(`Failed to fetch: ${response.status}`);
            return response.json();
        } catch (error) {
            console.error(`HttpClient error: ${error}`);
            return null;
        }
    }

    public static get(endpoint: string, revalidate: number, options?: object) {
        const config = {
            method: 'GET',
            next: { revalidate },
            ...options,
        };
        return this.request(endpoint, config);
    }

    public static post(endpoint: string, body: object, options?: object) {
        const config = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(body),
            ...options,
        };
        return this.request(endpoint, config);
    }

    public static put(endpoint: string, body: object, options?: object) {
        const config = {
            method: 'PUT',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(body),
            ...options,
        };
        return this.request(endpoint, config);
    }

    public static delete(endpoint: string, options?: object) {
        const config = {
            method: 'DELETE',
            ...options,
        };
        return this.request(endpoint, config);
    }
}
