import { answerGenerationApi } from '../../answer-generation-api.js';
import { initialAnswerGenerationServerState, } from '../../answer-generation-api-state.js';
import { streamAnswerWithStrategy } from '../../streaming/answer-streaming-runner.js';
import { streamingStrategyCreators } from '../../streaming/strategies/streaming-strategy-creators.js';
import { buildAnswerEndpointUrl } from './url-builders/endpoint-url-builder.js';
/**
 * RTK Query endpoint for streaming answer generation.
 */
export const answerEndpoint = answerGenerationApi.injectEndpoints({
    overrideExisting: true,
    endpoints: (builder) => ({
        generateAnswer: builder.query({
            queryFn: () => {
                return {
                    data: initialAnswerGenerationServerState(),
                };
            },
            async onQueryStarted(args, { getState, updateCachedData, dispatch }) {
                const { strategyKey, ...params } = args;
                const endpointUrl = buildAnswerEndpointUrl(getState());
                await streamAnswerWithStrategy(endpointUrl, params, {
                    getState: getState,
                    updateCachedData,
                    dispatch,
                }, streamingStrategyCreators[strategyKey]?.());
            },
        }),
    }),
});
/**
 * Initiates an answer generation query with the specified strategy and parameters.
 */
export const initiateAnswerEndpoint = (args) => {
    return answerEndpoint.endpoints.generateAnswer.initiate(args);
};
