function t(t, l) {
const a = t.labels.findIndex((t) => t.label === l)
;-1 === a ? t.labels.push({ label: l, count: 1 }) : (t.labels[a].count += 1)
}
function l(t, l) {
const a = t.labels.findIndex((t) => t.label === l)
if (-1 === a) return
const e = t.labels[a]
;((e.count -= 1), e.count > 0 || t.labels.splice(a, 1))
}
module.exports = async function (a, e, s, c, n) {
if ((s && c) || (!s && !c)) return
const o = s ? s.id : c.id,
i = (await a.get("@keet/chat-label-digests", { id: o })) || {
id: o,
total: 0,
labels: [],
editedAt: 0,
flagsClock: 0,
}
;(1 === (s || c).label &&
!c &&
((e.stats.chat.flagsClock += 1), (i.flagsClock = e.stats.chat.flagsClock)),
(i.editedAt = n),
s
? (t(i, s.label), (e.stats.chat.clock += 1), (i.total += 1))
: (l(i, c.label), (e.stats.chat.clock -= 1), (i.total -= 1)),
await Promise.all([a.insert("@keet/threads", e), a.insert("@keet/chat-label-digests", i)]))
}