export const ACCENT_KEY = 'themeAccent';

export const themeAccents = [
  { key: 'default', label: 'پیش‌فرض', color: '#343a40' },
  { key: 'charcoal', label: 'ذغالی', color: '#111827' },
  { key: 'blue', label: 'آبی', color: '#2563eb' },
  { key: 'purple', label: 'بنفش', color: '#7c3aed' },
  { key: 'green', label: 'سبز', color: '#16a34a' },
  { key: 'amber', label: 'کهربایی', color: '#d97706' },
  { key: 'roseGold', label: 'رزگلد', color: '#be5f6d' },
];

const legacyAccentMap = {
  gray: 'default',
  dark: 'charcoal',
  slate: 'charcoal',
  yellow: 'amber',
  red: 'roseGold',
};

function normalizeAccent(value) {
  const normalized = legacyAccentMap[value] || value;
  return themeAccents.some((item) => item.key === normalized) ? normalized : 'default';
}

export function getThemeAccent() {
  return normalizeAccent(localStorage.getItem(ACCENT_KEY));
}

export function applyThemeAccent(accent) {
  const nextAccent = normalizeAccent(accent);
  document.documentElement.dataset.accent = nextAccent;
  localStorage.setItem(ACCENT_KEY, nextAccent);
  window.dispatchEvent(new CustomEvent('theme-accent-change', { detail: { accent: nextAccent } }));
  return nextAccent;
}
