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


export const usersApi = createApi({
    reducerPath: 'usersApi',
    baseQuery: fetchBaseQuery({
        baseUrl: CONFIG.BASE_URL,
        prepareHeaders: (headers) => {
            const token = localStorage.getItem('access_token');
            if (token) headers.set('Authorization', `Bearer ${token}`);
            return headers;
        },
    }),
    tagTypes: ['User'],
    endpoints: (builder) => ({
        getAuthenticatedUserData: builder.query({
            query: () => '/users/my-data/',
            providesTags: ['User'],
            refetchOnMountOrArgChange: 5,
        }),
        changePassword: builder.mutation({
            query: (data) => ({
                url: "/users/change-password",
                method: "POST",
                body: data,
            }),
        }),
    }),
});

export const {useGetAuthenticatedUserDataQuery} = usersApi;