import { AxiosRequestConfig, AxiosResponse } from "axios";
import { count, from, map, Observable,reduce,tap } from "rxjs";
import { initialization } from "./axios";
import {chunk as _chunk} from 'lodash';
export const baseURL = import.meta.env.VITE_FETCH_URL
export const axiosRequestConfiguration: AxiosRequestConfig = {
    baseURL,
    responseType: 'json',
    headers: {
        'Content-Type': 'application/json',
    },
};

const req = initialization(axiosRequestConfiguration)

export const fetchProduct =async (productId:string) =>{
   const res=await req.get(`products/${productId}`)
   if(res&&res.status==200){
       return res.data
   }else{
       throw new Error('network went wrong')
   }
}

export const getJobs =  async(department?: string)=>{
    let res
    if(department){
       res = await req.get(`jobs?department=${department}`)
    }else{
        res = await req.get(`jobs`)
    }
    if(res&&res.status==200){
        return res.data
    }else{
        throw new Error('network went wrong')
    }
}


export const getEvents = (index:number)=>{
//    const res = await req.get('/events')
   const observable = from(req.get('/events')).pipe(map(res =>res.data),map((v,i) =>{ 
        const l = v.length
        // const group = Math.ceil(l/index)
        const output= _chunk(v,index)
        console.log(output);
        
        return output
   }));
   return observable
}

