/**
 * File: contactUsThunks.ts
 * Purpose: This file contains asynchronous thunk actions for managing contact us-related workflows 
 *          such as contact us, and more.
 *          
 * Description:
 * - Utilizes `redux-thunk` to create async actions that handle API requests and responses.
 * - Handles contact us API calls by dispatching appropriate Redux actions based on the API response.
 * - Displays error notifications using the `Toast` component in case of API failures.
*/

import apiClient from "@/src/interceptor/apiClient";
import { contactUsActions } from "../../slices/ContactUs/contact-us";
import { Toast } from "@/src/components/Toast/index";
import { API_ENDPOINTS } from "@/src/utils/commonVariables";
import { ContactUsPayload } from "@/redux/types/contactUsType";


/**
 * Thunk action for sending a contact us message.
 * @param payload - The payload containing the contact us message details.
 * @param callback - Optional callback function that receives the API response.
 * @returns A thunk function that dispatches actions based on the API response.
 */
export const sendContactUsMessage = (payload: ContactUsPayload, callback?: (data: any) => void) => {
    return async (dispatch: any) => {
        dispatch(contactUsActions.RequestContactUs());
        try {
            const response = await apiClient.post(API_ENDPOINTS.CONTACT_US, payload);
            dispatch(contactUsActions.SuccessContactUs(response.data));
            callback && callback(response?.data);
            // Toast('success', response.data.meta.message);
        } catch (error) {
            Toast('error', error?.message);
            dispatch(contactUsActions.ErrorContactUs());
        }
    }
}

