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

const storageMMKV = new MMKV();

const get = (key: string, type: StoreType) => {
  switch (type) {
    case StoreType.STRING:
      return storageMMKV.getString(key);
    case StoreType.BOOLEAN:
      return storageMMKV.getBoolean(key);
    case StoreType.NUMBER:
      return storageMMKV.getNumber(key);
  }
};

const set = (key: string, value: string | boolean | number) => {
  storageMMKV.set(key, value);
};

const remove = (key: string) => {
  storageMMKV.delete(key);
};

export const storage = { get, set, remove };

export enum StoreType {
  STRING = 'string',
  NUMBER = 'number',
  BOOLEAN = 'boolean'
}

export function loadData<T>(nameKey: string, key: string): T | null {
  const namespacedKey = `${nameKey}:${key}`;
  const jsonData = storage.get(namespacedKey, StoreType.STRING) as string;

  return jsonData ? JSON.parse(jsonData) : null;
}
