import { createEvent, OdpEvent, sendEvent } from "@insite/client-framework/OdpEvents/OdpEvent";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";

export const sendAutocompleteEvent = (
    {
        attributionToken,
        searchQuery,
        searchPosition,
    }: { attributionToken?: string; searchQuery?: string; searchPosition?: number },
    getState: () => ApplicationState,
    customize?: (odpEvent: OdpEvent) => void,
) => {
    let state = getState();
    let odpEvent = createEvent("navigation", state);
    odpEvent.properties["action"] = "autocomplete";

    if (typeof attributionToken !== "undefined") {
        odpEvent.properties["search_attribution_token"] = attributionToken;
    }
    if (typeof searchQuery !== "undefined") {
        odpEvent.properties["search_selected_suggestion"] = searchQuery;
    }
    if (typeof searchPosition !== "undefined") {
        odpEvent.properties["search_selected_suggestion_position"] = searchPosition;
    }

    customize?.(odpEvent);

    sendEvent(odpEvent, state);
};
