import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
import CONFIG from "../Core/сonfig.js";


export const lensIssuesApi = createApi({
    reducerPath: 'lensIssuesApi',
    baseQuery: fetchBaseQuery({
        baseUrl: CONFIG.BASE_URL,
        prepareHeaders: (headers) => {
            const token = localStorage.getItem('access_token');
            if (token) headers.set('Authorization', `Bearer ${token}`);
            return headers;
        }
    }),
    tagTypes: ['LensIssues'],
    endpoints: (builder) => ({
        getLensIssues: builder.query({
            query: () => '/lens_issues/',
            providesTags: ['LensIssues'],
            refetchOnMountOrArgChange: 5
        }),
        addLensIssues: builder.mutation({
            query: (lensIssues) => ({
                url: `/lens_issues/`,
                method: 'POST',
                body: lensIssues
            }),
            invalidatesTags: ['LensIssues']
        }),
    }),
});

export const {
    useGetLensIssuesQuery,
    useAddLensIssuesMutation
} = lensIssuesApi;