import { DEFAULT_SETTINGS, OPEN_IN_VALUES } from "./defaults.js"; // Pure. Layers stored values over the defaults, dropping unknown keys and // falling back on wrong types or out-of-range values. A fresh or signed-out // profile returns {} from storage, which yields the defaults. export function mergeSettings(stored) { const merged = { ...DEFAULT_SETTINGS }; if (stored === null || typeof stored !== "object") { return merged; } if (typeof stored.model === "string") { merged.model = stored.model; } if (typeof stored.temporaryChat === "boolean") { merged.temporaryChat = stored.temporaryChat; } if (typeof stored.webSearch === "boolean") { merged.webSearch = stored.webSearch; } if (OPEN_IN_VALUES.includes(stored.openIn)) { merged.openIn = stored.openIn; } if (typeof stored.promptTemplate === "string") { merged.promptTemplate = stored.promptTemplate; } return merged; } // storage.sync works with no Mozilla account (it behaves as local storage and // syncs later if the user signs in), so no capability probe is needed. Any // rejection falls back to defaults rather than blocking the user's action. // Firefox sanitises all non-quota backend errors to a single generic string, so // there is nothing useful to branch on. export async function load() { try { const stored = await browser.storage.sync.get(Object.keys(DEFAULT_SETTINGS)); return mergeSettings(stored); } catch { return mergeSettings(null); } } export async function save(partial) { await browser.storage.sync.set(partial); }