import { isDesktop } from './platform'; export interface StorageAdapter { get(key: string): Promise; set(key: string, value: string): Promise; remove(key: string): Promise; clear(): Promise; } class BrowserStorageAdapter implements StorageAdapter { async get(key: string): Promise { return localStorage.getItem(key); } async set(key: string, value: string): Promise { localStorage.setItem(key, value); } async remove(key: string): Promise { localStorage.removeItem(key); } async clear(): Promise { localStorage.clear(); } } interface TauriAPI { core: { invoke: (cmd: string, args?: Record) => Promise; }; } function getTauriAPI(): TauriAPI | null { if (typeof window !== 'undefined' && window.__TAURI__ !== undefined) { const tauri = window.__TAURI__ as TauriAPI; if (tauri?.core?.invoke) { return tauri; } } return null; } class SecureStorageAdapter implements StorageAdapter { private async tauriInvoke(cmd: string, args?: Record): Promise { const tauri = getTauriAPI(); if (tauri) { try { return await tauri.core.invoke(cmd, args); } catch { return null; } } return null; } async get(key: string): Promise { const result = await this.tauriInvoke('get_secure_value', { key }); if (result !== null) return result; return localStorage.getItem(key); } async set(key: string, value: string): Promise { const result = await this.tauriInvoke('set_secure_value', { key, value }); if (result === undefined && typeof window !== 'undefined' && window.__TAURI__) return; localStorage.setItem(key, value); } async remove(key: string): Promise { const result = await this.tauriInvoke('remove_secure_value', { key }); if (result === undefined && typeof window !== 'undefined' && window.__TAURI__) return; localStorage.removeItem(key); } async clear(): Promise { localStorage.clear(); } } function createStorageAdapter(): StorageAdapter { if (isDesktop()) { return new SecureStorageAdapter(); } return new BrowserStorageAdapter(); } export const storage = createStorageAdapter(); export function useStorage() { return storage; }