import { createAsyncThunk } from '@reduxjs/toolkit';
import { addEntryInActionsHistory, } from '../insight-search-actions.js';
import { logFetchMoreResults } from '../insight-search-analytics-actions.js';
import { buildInsightFetchFacetValuesRequest, buildInsightFetchMoreResultsRequest, buildInsightSearchRequest, } from '../insight-search-request.js';
import { AsyncInsightSearchThunkProcessor } from './insight-search-actions-thunk-processor.js';
export async function legacyExecuteSearch(state, 
// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- <>
config, analyticsAction) {
    addEntryInActionsHistory(state);
    const processor = new AsyncInsightSearchThunkProcessor({
        ...config,
        analyticsAction,
    });
    const { analyticsClientMiddleware, preprocessRequest, logger } = config.extra;
    const { description: eventDescription } = await analyticsAction.prepare({
        getState: () => config.getState(),
        analyticsClientMiddleware,
        preprocessRequest,
        logger,
    });
    const mappedRequest = await buildInsightSearchRequest(state, eventDescription);
    const fetched = await processor.fetchFromAPI(mappedRequest);
    return await processor.process(fetched, mappedRequest);
}
export async function legacyFetchPage(state, 
// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- <>
config, analyticsAction) {
    addEntryInActionsHistory(state);
    const processor = new AsyncInsightSearchThunkProcessor({
        ...config,
        analyticsAction,
    });
    const { analyticsClientMiddleware, preprocessRequest, logger } = config.extra;
    const { description: eventDescription } = await analyticsAction.prepare({
        getState: () => config.getState(),
        analyticsClientMiddleware,
        preprocessRequest,
        logger,
    });
    const mappedRequest = await buildInsightSearchRequest(state, eventDescription);
    const fetched = await processor.fetchFromAPI(mappedRequest);
    return await processor.process(fetched, mappedRequest);
}
export async function legacyFetchMoreResults(state, 
// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- <>
config) {
    const processor = new AsyncInsightSearchThunkProcessor({
        ...config,
        analyticsAction: logFetchMoreResults,
    });
    const { analyticsClientMiddleware, preprocessRequest, logger } = config.extra;
    const { description: eventDescription } = await logFetchMoreResults().prepare({
        getState: () => config.getState(),
        analyticsClientMiddleware,
        preprocessRequest,
        logger,
    });
    const mappedRequest = await buildInsightFetchMoreResultsRequest(state, eventDescription);
    const fetched = await processor.fetchFromAPI(mappedRequest);
    return await processor.process(fetched, mappedRequest);
}
export async function legacyFetchFacetValues(state, 
// oxlint-disable-next-line @typescript-eslint/no-explicit-any -- <>
config, analyticsAction) {
    const processor = new AsyncInsightSearchThunkProcessor({
        ...config,
        analyticsAction,
    });
    const { analyticsClientMiddleware, preprocessRequest, logger } = config.extra;
    const { description: eventDescription } = await analyticsAction.prepare({
        getState: () => config.getState(),
        analyticsClientMiddleware,
        preprocessRequest,
        logger,
    });
    const mappedRequest = await buildInsightFetchFacetValuesRequest(state, eventDescription);
    const fetched = await processor.fetchFromAPI(mappedRequest);
    return await processor.process(fetched, mappedRequest);
}
export const executeSearch = createAsyncThunk('search/executeSearch', async (analyticsAction, config) => {
    const state = config.getState();
    return await legacyExecuteSearch(state, config, analyticsAction);
});
export const fetchPage = createAsyncThunk('search/fetchPage', async (analyticsAction, config) => {
    const state = config.getState();
    return await legacyFetchPage(state, config, analyticsAction);
});
export const fetchMoreResults = createAsyncThunk('search/fetchMoreResults', async (_, config) => {
    const state = config.getState();
    return await legacyFetchMoreResults(state, config);
});
export const fetchFacetValues = createAsyncThunk('search/fetchFacetValues', async (analyticsAction, config) => {
    const state = config.getState();
    return await legacyFetchFacetValues(state, config, analyticsAction);
});
