import { experimental_createQueryPersister } from '@tanstack/query-persist-client-core'
import { createMMKV } from 'react-native-mmkv'

const queryCache = createMMKV({ id: 'react-query-cache' })

export const queryPersister = experimental_createQueryPersister({
  storage: {
    getItem: (key: string) => queryCache.getString(key) ?? null,
    setItem: (key: string, value: string) => queryCache.set(key, value),
    removeItem: (key: string) => {
      queryCache.remove(key)
    },
  },
  maxAge: 1000 * 60 * 60 * 12, // 12 hours
  prefix: 'rq',
})
