/**
 * Shared `contacts.jsonc` mutations — denylist + preferred.
 *
 * Pure logic with cloud I/O injected, so the desktop service
 * (`mailx-service`, Node) and the Android service (`mailx-store-web`, in the
 * WebView) run ONE implementation instead of each keeping its own copy.
 * `web-service.ts` used to stub `addToDenylist` / `addPreferredContact` as
 * `notImpl()`, which is why the phone's ⊘ / ★ buttons did nothing.
 *
 * Nothing here is platform-specific: it's a `cloudRead` → mutate → `cloudWrite`
 * round-trip. Each caller passes its own platform's cloud functions.
 */
/** Loose-parse a JSONC blob. The file mailx writes is plain JSON
 *  (`JSON.stringify`), but a hand-edit may add `//` comments or trailing
 *  commas — strip those and retry rather than losing the whole file. */
function parseContactsConfig(raw) {
    if (!raw)
        return {};
    try {
        return JSON.parse(raw) || {};
    }
    catch { /* fall through to loose */ }
    try {
        const stripped = raw
            .replace(/^\s*\/\/.*$/gm, "") // line comments
            .replace(/,(\s*[}\]])/g, "$1"); // trailing commas
        return JSON.parse(stripped) || {};
    }
    catch {
        return {};
    }
}
/** Append an email to `contacts.jsonc#denylist[]` — idempotent and
 *  case-insensitive. Writes only when the entry is genuinely new. */
export async function addContactsDenylistEntry(email, cloudRead, cloudWrite) {
    const lower = (email || "").trim().toLowerCase();
    if (!lower)
        return;
    const cfg = parseContactsConfig(await cloudRead("contacts.jsonc"));
    if (!Array.isArray(cfg.denylist))
        cfg.denylist = [];
    if (cfg.denylist.some((e) => (e || "").toLowerCase() === lower))
        return;
    cfg.denylist.push(email);
    await cloudWrite("contacts.jsonc", JSON.stringify(cfg, null, 2));
}
/** Append a preferred contact to `contacts.jsonc#preferred[]` — dedup by
 *  source|email|name. Writes only when the entry is genuinely new. */
export async function addContactsPreferredEntry(entry, cloudRead, cloudWrite) {
    if (!entry?.email)
        return;
    const cfg = parseContactsConfig(await cloudRead("contacts.jsonc"));
    if (!Array.isArray(cfg.preferred))
        cfg.preferred = [];
    const key = (s, e, n) => `${(s || "preferred").toLowerCase()}|${(e || "").toLowerCase()}|${(n || "").toLowerCase()}`;
    const dupKey = key(entry.source || "", entry.email, entry.name || "");
    if (cfg.preferred.some((e) => key(e?.source || "", e?.email || "", e?.name || "") === dupKey))
        return;
    const row = { name: entry.name || "", email: entry.email };
    if (entry.source)
        row.source = entry.source;
    if (entry.organization)
        row.organization = entry.organization;
    cfg.preferred.push(row);
    await cloudWrite("contacts.jsonc", JSON.stringify(cfg, null, 2));
}
//# sourceMappingURL=contacts-config.js.map