const axios = require('axios')
import type { NextRequest } from 'next/server';

const getData = async (url: any) => {
  try {
    var response = await fetch(`${url}`);
    var result = await response.json();
    return result;
  } catch (e) {
    return {
      status: false,
      message: 'Something went wrong'
    };
  }
};

const postData = async (url: any, body: any,request:NextRequest) => {
    const token = request.cookies.get('jazz_token')?.value;
    console.log(token)
  try {
    const response = await fetch(`${url}`, {
      method: "POST",
      headers: { "Content-Type": "application/json;charset=utf-8" },
    //   headers: { "authorization": `Bearer `+ "" },
      body: JSON.stringify(body),
    });
    const result = await response.json();
    return result;
  } catch (e) {
    return {
      status: false,
      message: 'Something went wrong'
    };
  }
};

const updateData = async (url: any, body: any) => {
  try {
    const response = await fetch(`${url}`, {
      method: "PUT",
      headers: { "Content-Type": "application/json;charset=utf-8" },
      body: JSON.stringify(body),
    });
    const result = await response.json();
    return result;
  } catch (e) {
    return {
      status: false,
      message: 'Something went wrong'
    };
  }
};

const deleteData = async (url: any, body?: any) => {
  try {
    const response = await fetch(`${url}`, {
      method: "DELETE",
      headers: { "Content-Type": "application/json;charset=utf-8" },
      body: JSON.stringify(body),
    });
    const result = await response.json();
    return result;
  } catch (e) {
    return {
      status: false,
      message: 'Something went wrong'
    };
  }
};


export { getData, postData, updateData, deleteData }