

import { useEffect } from 'react'
import useTriggerStatus from './useTriggerStatus'
import useAxios from '../api/helper/useAxios'
import { IRequestSend } from '../interfaces/axiosInterface'


function useHitRequests() {

    const axios = useAxios()
    const isOnline = useTriggerStatus()

    useEffect(() => {
        if (isOnline) {
            
            const data = JSON.parse(localStorage.getItem("offlineApi") || "[]");
            // check if offlineApi is empty.
            if(data.length < 0) {
                return ;
            }
            // mapping on offline requests
            data.map((item: IRequestSend) => {                
                axios[item.method](item.endPoint, item.data,{headers:item?.headers})
            })
            // set response array to an empty array to clear response inside it
            
            localStorage.setItem("offlineApi", "[]");

        }

    }, [isOnline])
}

export default useHitRequests