import type { ConsentCategory } from '../categories.js' import type { CookieFinding, LocalizedText } from './types.js' /** The editable values of a review entry; `purpose` / `retention` per locale. */ export type CookieReviewValues = { category?: ConsentCategory | null domain?: null | string purpose?: Record retention?: Record setBeforeConsent?: boolean | null vendor?: null | string } const SCALARS = ['category', 'domain', 'setBeforeConsent', 'vendor'] as const const TEXTS = ['purpose', 'retention'] as const /** Text for one locale: that language, else English, else the first one given. */ export function textFor(text: LocalizedText | undefined, locale: string): string | undefined { if (text === undefined) {return undefined} if (typeof text === 'string') {return text} return text[locale] ?? text.en ?? Object.values(text)[0] } /** What a scan suggests for an entry, resolved for the project's locales (default locale first). */ export function suggestionFor(finding: CookieFinding, locales: string[]): CookieReviewValues { const perLocale = (text: LocalizedText | undefined) => { if (text === undefined) {return undefined} // A locale without its own text gets the default locale's (locales[0]), then English, then any. const fallback = typeof text === 'string' ? text : (text[locales[0]] ?? undefined) return Object.fromEntries(locales.map((l) => [l, (typeof text === 'string' ? text : text[l]) ?? fallback ?? textFor(text, l) ?? null])) } const out: CookieReviewValues = {} if (finding.category) {out.category = finding.category} if (finding.domain) {out.domain = finding.domain} if (finding.vendor) {out.vendor = finding.vendor} if (typeof finding.setBeforeConsent === 'boolean') {out.setBeforeConsent = finding.setBeforeConsent} const purpose = perLocale(finding.purpose) const retention = perLocale(finding.retention) if (purpose) {out.purpose = purpose} if (retention) {out.retention = retention} return out } const isEmpty = (v: unknown) => v === undefined || v === null || v === '' /** * New values for an entry after a scan. A value takes the new suggestion only * while it is empty or still equal to what the previous scan suggested — once an * editor changed it, it is theirs and stays. Text is compared per locale. */ export function mergeCookieReview( current: CookieReviewValues, previous: CookieReviewValues | null | undefined, next: CookieReviewValues, ): { changed: boolean; values: CookieReviewValues } { const prev = previous ?? {} const values: CookieReviewValues = { ...current } let changed = false const pick = (cur: V, before: undefined | V, suggested: undefined | V): V => { if (suggested === undefined || isEmpty(suggested)) {return cur} if (isEmpty(cur) || cur === before) {return suggested} return cur } for (const key of SCALARS) { const v = pick(current[key], prev[key], next[key]) if (v !== current[key]) { ;(values as Record)[key] = v changed = true } } for (const key of TEXTS) { const cur = current[key] ?? {} const merged = { ...cur } for (const [locale, suggested] of Object.entries(next[key] ?? {})) { const v = pick(cur[locale], prev[key]?.[locale], suggested) if (v !== cur[locale]) { merged[locale] = v ?? null changed = true } } if (Object.keys(merged).length) {values[key] = merged} } return { changed, values } }