import {
  DEFAULT_BUSINESS_PRIMARY_COLOR_ID,
  getBusinessPrimaryColorTokens,
  type BusinessPrimaryColorId,
} from './businessPrimaryColors'

const CSS_VAR_DEFAULTS: Record<string, string> = {
  '--primary': '#3b82f6',
  '--primary-glow': '#3b82f6',
  '--primary-light': '#dbeafe',
  '--primary-dark': '#2563eb',
  '--primary-rgb': '59 130 246',
  '--primary-dark-rgb': '37 99 235',
  '--chart-accent': '#06b6d4',
  '--chart-accent-dark': '#0891b2',
  '--chart-accent-rgb': '6 182 212',
  '--chart-accent-dark-rgb': '8 145 178',
}

export function applyBusinessPrimaryColor(colorId?: BusinessPrimaryColorId | null) {
  const root = document.documentElement
  const tokens = getBusinessPrimaryColorTokens(colorId ?? DEFAULT_BUSINESS_PRIMARY_COLOR_ID)

  root.style.setProperty('--primary', tokens.primary)
  root.style.setProperty('--primary-glow', tokens.primaryGlow)
  root.style.setProperty('--primary-light', tokens.primaryLight)
  root.style.setProperty('--primary-dark', tokens.primaryDark)
  root.style.setProperty('--primary-rgb', tokens.primaryRgb)
  root.style.setProperty('--primary-dark-rgb', tokens.primaryDarkRgb)
  root.style.setProperty('--chart-accent', tokens.chartAccent)
  root.style.setProperty('--chart-accent-dark', tokens.chartAccentDark)
  root.style.setProperty('--chart-accent-rgb', tokens.chartAccentRgb)
  root.style.setProperty('--chart-accent-dark-rgb', tokens.chartAccentDarkRgb)
}

export function resetBusinessPrimaryColor() {
  const root = document.documentElement

  for (const [name, value] of Object.entries(CSS_VAR_DEFAULTS)) {
    root.style.setProperty(name, value)
  }
}
