import type { ParaStorageAdapter } from '@getpara/react-core';
import RNAsyncStorage from '@react-native-async-storage/async-storage';

/**
 * ParaStorageAdapter backed by React Native AsyncStorage.
 * Used by the core Zustand store for state persistence.
 */
export const asyncStorageAdapter: ParaStorageAdapter = {
  getItem: async (key: string) => {
    try {
      return await RNAsyncStorage.getItem(key);
    } catch (error) {
      console.warn('[Para] Error reading from AsyncStorage:', error);
      return null;
    }
  },
  setItem: async (key: string, value: string) => {
    try {
      await RNAsyncStorage.setItem(key, value);
    } catch (error) {
      console.warn(`[Para] Error writing to AsyncStorage (key: ${key}):`, error);
    }
  },
  removeItem: async (key: string) => {
    try {
      await RNAsyncStorage.removeItem(key);
    } catch (error) {
      console.warn(`[Para] Error removing from AsyncStorage (key: ${key}):`, error);
    }
  },
};
