import storage from "redux-persist/lib/storage";

const createStorageKey = (key) => `persist:${key}`;
export const LOGIN_KEY = createStorageKey(`login`);
export const USER_KEY = createStorageKey(`user`);
export const RANDOM_DATA_KEY = createStorageKey(`randomData`);

export const getFromStorage = async (key) => {
  const item = await storage.getItem(key);

  if (!item) {
    return Promise.reject("Item is not found!");
  }


  //Deserialization of whole item
  const itemJson = JSON.parse(item);


  //Deserialization of each property (it must be done when dealing with persistor)
  const itemProperties = Object.getOwnPropertyNames(itemJson);
  itemProperties.forEach((property) => {
    itemJson[property] = JSON.parse(itemJson[property]);
  });

  
  return Promise.resolve(itemJson);
};
