import type { Settings, SettingsPatch } from "@/lib/types"; const SECTIONS = [ "upstream", "dns", "blocking", "cache", "web", "doh_server", "dot_server", "edns", "logging", "disk", "blocklist_update", ] as const; /** * Minimal partial patch for PUT /api/settings: only fields whose edited value * differs from the original, grouped by section. The derived `web.auth_enabled` * is never emitted. A non-empty `password` passes through as `web.password`. * Returns null when nothing changed and no password was given. */ export function buildSettingsPatch(original: Settings, edited: Settings, password?: string): SettingsPatch | null { const patch: Record> = {}; for (const section of SECTIONS) { const before = original[section] as Record; const after = edited[section] as Record; let changed: Record | undefined; for (const key of Object.keys(after)) { if (section === "web" && key === "auth_enabled") continue; if (before[key] !== after[key]) (changed ??= {})[key] = after[key]; } if (changed !== undefined) patch[section] = changed; } if (password !== undefined && password !== "") { patch["web"] = { ...patch["web"], password }; } return Object.keys(patch).length === 0 ? null : (patch as SettingsPatch); }