import { Preferences } from "@capacitor/preferences";
import { isNativeApp } from "./server-config";
/**
* Unified storage adapter.
* Uses Capacitor Preferences in native builds, localStorage in web builds.
* All methods are async to keep the interface uniform.
*/
export const storage = {
/**
* Read a string value by key. Returns null if missing or on error.
* @param {string} key
* @returns {Promise<string|null>}
*/
async get(key) {
if (isNativeApp()) {
try {
const { value } = await Preferences.get({ key });
return value;
} catch {
return null;
}
}
try {
return localStorage.getItem(key);
} catch {
return null;
}
},
/**
* Write a string value by key.
* @param {string} key
* @param {string} value
* @returns {Promise<void>}
*/
async set(key, value) {
if (isNativeApp()) {
try {
await Preferences.set({ key, value });
} catch {
// ignore
}
return;
}
try {
localStorage.setItem(key, value);
} catch {
// ignore (private mode, quota exceeded, etc.)
}
},
/**
* Remove a key.
* @param {string} key
* @returns {Promise<void>}
*/
async remove(key) {
if (isNativeApp()) {
try {
await Preferences.remove({ key });
} catch {
// ignore
}
return;
}
try {
localStorage.removeItem(key);
} catch {
// ignore
}
},
};