import { createApi, fetchBaseQuery, retry, } from '@reduxjs/toolkit/query';
import { getOrganizationEndpoint } from '../platform-client.js';
/**
 * `dynamicBaseQuery` is passed to the baseQuery of the createApi,
 * but note that the baseQuery will not be used if a queryFn is provided in the createApi endpoint
 */
const dynamicBaseQuery = async (args, api, extraOptions) => {
    const state = api.getState();
    const { accessToken, environment, organizationId } = state.configuration;
    const answerConfigurationId = state.generatedAnswer.answerConfigurationId;
    const updatedArgs = {
        ...args,
        headers: {
            ...(args?.headers || {}),
            Authorization: `Bearer ${accessToken}`,
        },
    };
    try {
        const platformEndpoint = getOrganizationEndpoint(organizationId, environment);
        const data = fetchBaseQuery({
            baseUrl: `${platformEndpoint}/rest/organizations/${organizationId}/answer/v1/configs/${answerConfigurationId}`,
        })(updatedArgs, api, extraOptions);
        return { data };
    }
    catch (error) {
        return { error: error };
    }
};
export const answerSlice = createApi({
    reducerPath: 'answer',
    baseQuery: retry(dynamicBaseQuery, { maxRetries: 3 }),
    endpoints: () => ({}),
});
