import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
import {getJwtToken} from "../Util/config";

interface IBotFileSystemRequest {
    botId: string,
    current_file_manager_path: string,
}

export const api = createApi({
    baseQuery: fetchBaseQuery({
        baseUrl: process.env.REACT_APP_BACKEND_URL + "api/v1/",
        prepareHeaders: (headers: Headers) => {
            headers.set("Content-Type", "application/json;charset=UTF-8");
            headers.set("Accept", "application/json");

            const token = getJwtToken();

            if (token) {
                headers.set("Authorization", `Bearer ${token}`);
            }

            return headers;
        },
    }),
    endpoints: (builder) => ({
        search: builder.query<string[], string>({
            // TODO type for request
            query: (request: any) => ({
                url: "/search",
                body: request,
                method: "POST",
            }),
        }),
        // TODO type for builder.query
        botFileSystem: builder.query<any, any>({
            query: (request: IBotFileSystemRequest) => ({
                url: `/bots/${request.botId}/fileManager/list`,
                method: "GET",
                params: request,
            }),
        }),
    }),
    tagTypes: ["Search", "FileSystem"],
});

export const {
    useSearchQuery,
    useBotFileSystemQuery,
} = api;
