import {
  mailBackgroundStyle,
  normalizeMailBackgroundId,
} from "@/lib/mail-settings/constants"

export function clearMailBackgroundDom(html: HTMLElement = document.documentElement) {
  delete html.dataset.mailBackground
  html.style.removeProperty("--mail-bg-layer")
  html.style.removeProperty("--mail-bg-fallback")
}

export function applyMailBackgroundDom(
  backgroundId: string,
  html: HTMLElement = document.documentElement
) {
  const normalized = normalizeMailBackgroundId(backgroundId)
  const { background, fallbackColor } = mailBackgroundStyle(normalized)

  if (normalized === "none" || background === "none") {
    clearMailBackgroundDom(html)
    return
  }

  html.dataset.mailBackground = normalized
  html.style.setProperty("--mail-bg-layer", background)
  html.style.setProperty("--mail-bg-fallback", fallbackColor)
}
