import  apiService from '../../src/APIRequestService/APIService';
    
export const fetchMenuItems = async () => {
  console.log("applying fetch menu items");
  try {
    const response = await apiService.get('/api1/submenu1');
    console.log("response", response);

    // Check for a successful response
    if (response.status !== 200) {
      throw new Error(`Error: ${response.status}`);
    }

    // Ensure that the response has a proper 'data' field and is an array
    if (!response.data || !Array.isArray(response.data)) {
      throw new Error('Unexpected data format, expected an array');
    }

    console.log('Fetched Menu Items:', response.data);
    return response.data;  // Return the correct data
  } catch (error) {
    console.error('Error fetching menu items:', error);
    throw error;  // Re-throw the error to be caught by the calling function
  }
};

export const addMenuItem = async (formData) => {
  try {
    const response = await apiService.post('/api1/Sec_menuDet', formData);
    console.log("add response: ",response.data);
    return response.data; // Return only the data part of the response
  } catch (error) {
    throw error; // Let the error be handled by the calling component
    
  }   
}

export const updateMenuItem = async (id, formData) => {
  try {
    const response = await apiService.put(`/api1/submenu1/${id}`, formData);
    console.log("update response: ",response.data);
    return response.data;
  
  } catch (error) {
    throw error;
  }
}


export const deleteMenuItem = async (id) => {
  try {
    const response = await apiService.delete(`/api1/menu/${id}`);
    return response.data; // Return only the data part of the response
  } catch (error) {
    throw error; // Let the error be handled by the calling component
  }
}
// submenu apis

export const getSubmenuItems = async (id) => {
  try {
    const response = await apiService.get(`/api1/submenu1/${id}`);
    return response.data; // Return only the data part of the response
  } catch (error) {
    throw error; // Let the error be handled by the calling component
  }
}

// export const addSubmenuItem = async (menuId, formData) => {
//   try {
//     const response = await apiService.post(`/api1/submenu1/${menuId}`, formData);
//     console.log("add subMenu response: ",response.data);
//     return response.data; // Return only the data part of the response
//   } catch (error) {
//     throw error; // Let the error be handled by the calling component
// }
// }


export const addSubmenuItem = async (menuId, formData) => {
  try {
    console.log("adding submenu item");
    console.log("menuid", menuId);

    const response = await apiService.post(`/api1/submenu1/${menuId}`, formData);
    console.log("menuid", menuId);
    console.log("add subMenu response: ", response.data);
    return response.data; // Return only the data part of the response
  } catch (error) {
    throw error; // Let the error be handled by the calling component
  }
}

        
    
        
    







