import {createApi} from '@reduxjs/toolkit/query/react'
import {baseQueryWithAuth} from "./baseQuery.js";

export const setsApi = createApi({
    reducerPath: 'setsApi',
    baseQuery: baseQueryWithAuth,
    tagTypes: ['Set'],
    endpoints: (builder) => ({
        getSets: builder.query({
            query: () => '/sets/',
            providesTags: ['Set'],
            refetchOnMountOrArgChange: 5
        }),
        addSet: builder.mutation({
            query: (set) => ({
                url: '/sets/',
                method: 'POST',
                body: set
            }),
            invalidatesTags: ['Set']
        }),
        updateSet: builder.mutation({
            query: ({id, ...set}) => ({
                url: `/sets/${id}/`,
                method: 'PUT',
                body: set
            }),
            invalidatesTags: ['Set']
        }),
        deleteSet: builder.mutation({
            query: (id) => ({
                url: `/sets/${id}/`,
                method: 'DELETE'
            }),
            invalidatesTags: ['Set']
        }),
        appendLensesFromSet: builder.mutation({
            query: (id) => ({
                url: `/sets/append_lenses/${id}/`,
                method: 'POST',
            }),
            invalidatesTags: ['Set']
        }),
    }),
});

export const {
    useGetSetsQuery,
    useAddSetMutation,
    useUpdateSetMutation,
    useDeleteSetMutation,
    useAppendLensesFromSetMutation,
} = setsApi;