import * as SecureStore from "expo-secure-store";

export const TokenCache = {
  getToken: async (key: string) => {
    try {
      const item = await SecureStore.getItemAsync(key);
      if (item) {
        console.log(`${key} was used 🔐`);
      } else {
        console.log("No values stored under key:", key);
      }
      return item;
    } catch (error) {
      console.error("Secure Store getItem error:", error);
      await SecureStore.deleteItemAsync(key); // Delete potentially corrupted value
      return null;
    }
  },
  saveToken: async (key: string, token: string) => {
    try {
      await SecureStore.setItemAsync(key, token);
      console.log(`${key} has been saved successfully ✅`);
    } catch (error) {
      console.error("Secure Store saveToken error:", error);
    }
  },
};
