import Database from "../../../../../Components/common/DAsyncStorage";
const PIOfflineBackgroundActivity = "PIOfflineBackgroundActivity";

/*
 If once submit pi called success for X activity id
 then status will be Success, 
 if activity id status is success it should not restart in the background 
*/

const setOfflineActivity = async (activityId, status) => {
  const existingData = await Database.get(PIOfflineBackgroundActivity);
  console.log(
    "[BACKGROUND] . BackgroundDatabase . setOfflineActivity . exisitingData - ",
    existingData
  );
  if (existingData) {
    const newItem = {
      activityId: activityId,
      status: status,
    };
    const newData = [...existingData, newItem];
    console.log(
      "[BACKGROUND] . BackgroundDatabase . setOfflineActivity . existingData - present . newData - ",
      newData
    );
    await Database.set(PIOfflineBackgroundActivity);
  } else {
    const newData = [{ activityId, status }];
    console.log(
      "[BACKGROUND] . BackgroundDatabase . setOfflineActivity . existingData - null . newData - ",
      newData
    );
    await Database.set(PIOfflineBackgroundActivity);
  }
};
