// Shared chrome shim — uses real Chrome APIs on extension builds, // Capacitor-backed shim on mobile builds. // Background/vault code all imports from here so storage calls work on both platforms. import { Capacitor } from "@capacitor/core"; export const PeachChrome = Capacitor.isNativePlatform() ? { storage: { local: { async get(keys: string | string[] | Record): Promise> { const { Preferences } = await import("@capacitor/preferences"); const keyArray = Array.isArray(keys) ? keys : typeof keys === "string" ? [keys] : Object.keys(keys); const result: Record = {}; for (const k of keyArray) { const { value } = await Preferences.get({ key: k }); if (value !== undefined) result[k] = value; } return result; }, async set(items: Record): Promise { const { Preferences } = await import("@capacitor/preferences"); for (const [k, v] of Object.entries(items)) { await Preferences.set({ key: k, value: String(v) }); } }, async remove(keys: string | string[]): Promise { const { Preferences } = await import("@capacitor/preferences"); const keyArray = Array.isArray(keys) ? keys : [keys]; for (const k of keyArray) await Preferences.remove({ key: k }); }, async clear(): Promise { const { Preferences } = await import("@capacitor/preferences"); await Preferences.clear(); }, }, session: { _s: new Map(), async get(keys: string | string[] | Record): Promise> { const keyArray = Array.isArray(keys) ? keys : typeof keys === "string" ? [keys] : Object.keys(keys); const result: Record = {}; for (const k of keyArray) { if (this._s.has(k)) result[k] = this._s.get(k); } return result; }, async set(items: Record): Promise { for (const [k, v] of Object.entries(items)) this._s.set(k, v); }, async remove(keys: string | string[]): Promise { const keyArray = Array.isArray(keys) ? keys : [keys]; for (const k of keyArray) this._s.delete(k); }, }, onChanged: { addListener: () => {}, removeListener: () => {} }, }, runtime: { sendMessage: (_msg: unknown, cb?: (r: unknown) => void) => { if (cb) cb({ ok: false, error: "NO_SERVICE_WORKER" }); }, onMessage: { addListener: () => {}, removeListener: () => {} }, connect: () => ({ disconnect: () => {} }), lastError: null, }, } : undefined; // Use real chrome on extension builds