import { MMKV } from 'react-native-mmkv';

let storage: MMKV | null = null;

function getStorage() {
  if (!storage) {
    storage = new MMKV({ id: 'bytelyst-agentic-notes-mobile' });
  }

  return storage;
}

export const mmkvStorage = {
  getItem(name: string): string | null {
    try {
      return getStorage().getString(name) ?? null;
    } catch {
      return null;
    }
  },
  setItem(name: string, value: string): void {
    try {
      getStorage().set(name, value);
    } catch {
    }
  },
  removeItem(name: string): void {
    try {
      getStorage().delete(name);
    } catch {
    }
  },
};
