export const RULESET_LABELS: Record<string, string> = {
  wcag20a:         'WCAG 2.0 A',
  wcag20aa:        'WCAG 2.0 AA',
  wcag20aaa:       'WCAG 2.0 AAA',
  wcag21a:         'WCAG 2.1 A',
  wcag21aa:        'WCAG 2.1 AA',
  wcag21aaa:       'WCAG 2.1 AAA',
  wcag22aa:        'WCAG 2.2 AA',
  section508:      'Section 508',
  'best-practice': 'Best Practice',
};

export const RULESET_TAGS: Record<string, string[]> = {
  wcag20a:         ['wcag2a'],
  wcag20aa:        ['wcag2a', 'wcag2aa'],
  wcag20aaa:       ['wcag2a', 'wcag2aa', 'wcag2aaa'],
  wcag21a:         ['wcag2a', 'wcag21a'],
  wcag21aa:        ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'],
  wcag21aaa:       ['wcag2a', 'wcag2aa', 'wcag2aaa', 'wcag21a', 'wcag21aa'],
  wcag22aa:        ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22aa'],
  section508:      ['section508'],
  'best-practice': ['best-practice'],
};

export const ALL_RULESET_KEYS = Object.keys(RULESET_LABELS);

export function tagsForRulesets(selected: string[]): string[] {
  const tags = new Set<string>();
  selected.forEach((key) => (RULESET_TAGS[key] ?? []).forEach((tag) => tags.add(tag)));
  return [...tags];
}

export function labelsForRulesets(selected: string[]): string {
  return selected.map((key) => RULESET_LABELS[key]).filter(Boolean).join(' + ');
}

/** Dropdown preset options — atomic standards plus a few "Best Practice + X" combos. */
export interface RulesetPreset {
  value: string;
  label: string;
  standards: string[];
}

export const RULESET_PRESETS: RulesetPreset[] = [
  { value: 'wcag20a',        label: 'WCAG 2.0 A',                standards: ['wcag20a'] },
  { value: 'wcag20aa',       label: 'WCAG 2.0 AA',               standards: ['wcag20aa'] },
  { value: 'wcag20aaa',      label: 'WCAG 2.0 AAA',              standards: ['wcag20aaa'] },
  { value: 'wcag21a',        label: 'WCAG 2.1 A',                standards: ['wcag21a'] },
  { value: 'wcag21aa',       label: 'WCAG 2.1 AA',               standards: ['wcag21aa'] },
  { value: 'wcag21aaa',      label: 'WCAG 2.1 AAA',              standards: ['wcag21aaa'] },
  { value: 'wcag22aa',       label: 'WCAG 2.2 AA',               standards: ['wcag22aa'] },
  { value: 'section508',     label: 'Section 508',               standards: ['section508'] },
  { value: 'best-practice',  label: 'Best Practice',             standards: ['best-practice'] },
  { value: 'bp+wcag20aa',    label: 'Best Practice + WCAG 2.0 AA', standards: ['best-practice', 'wcag20aa'] },
  { value: 'bp+wcag21aa',    label: 'Best Practice + WCAG 2.1 AA', standards: ['best-practice', 'wcag21aa'] },
  { value: 'bp+wcag22aa',    label: 'Best Practice + WCAG 2.2 AA', standards: ['best-practice', 'wcag22aa'] },
];

export const DEFAULT_RULESET_PRESET = 'wcag22aa';

/** Find the preset whose standards match the given set exactly, falling back to the default. */
export function presetValueForStandards(standards: string[]): string {
  const set = new Set(standards);
  const match = RULESET_PRESETS.find(
    (p) => p.standards.length === set.size && p.standards.every((s) => set.has(s))
  );
  return match ? match.value : DEFAULT_RULESET_PRESET;
}

export function standardsForPresetValue(value: string): string[] {
  return RULESET_PRESETS.find((p) => p.value === value)?.standards ?? [DEFAULT_RULESET_PRESET];
}
