/**
 * Maps axe-core rule IDs to human-readable issue titles.
 * Used when rendering stored scan results in the dashboard so that
 * issues stored before the sanitize_issue_text() fix (2026-07-21)
 * show the correct title instead of the garbled version that resulted
 * from sanitize_text_field() stripping HTML tag tokens.
 *
 * If a rule ID is present here, this title takes precedence over the
 * stored value. Rules not in this map fall back to the stored title.
 */
export const RULE_TITLES: Record<string, string> = {
  // WCAG 1.1.1 — Alt text
  'image-alt':       'Images missing alt text attribute',
  'input-image-alt': 'Images missing alt text attribute',
  'area-alt':        'Images missing alt text attribute',
  // WCAG 4.1.2 — Form labels / button names
  'label':            'Form inputs missing label association',
  'label-title-only': 'Form inputs missing label association',
  'button-name':      'Form inputs missing label association',
  // WCAG 2.1.1 — Keyboard accessibility
  'keyboard':        'Interactive element not keyboard accessible',
  // WCAG 3.1.1 — Language
  'html-has-lang':   'HTML lang attribute missing',
  'html-lang-valid': 'HTML lang attribute missing',
  // WCAG 2.4.7 — Focus visible
  'focus-visible':   'Focus indicator removed via CSS',
  // WCAG 1.4.3 — Colour contrast
  'color-contrast':           'Potentially insufficient colour contrast',
  'color-contrast-enhanced':  'Potentially insufficient colour contrast',
  // WCAG 2.4.6 — Headings
  'empty-heading':   'Empty heading elements found',
  'heading-order':   'Heading levels are skipped',
  // WCAG 2.4.4 — Link text
  'link-name':       'Links with non-descriptive text',
  // WCAG 4.1.2 — ARIA
  'aria-allowed-role':  'Invalid ARIA role attributes',
  'aria-roles':         'Invalid ARIA role attributes',
  // WCAG 1.3.1 — Tables
  'scope-attr-valid':     'Data tables missing headers or caption',
  'td-headers-attr':      'Data tables missing headers or caption',
  'th-has-data-cells':    'Data tables missing headers or caption',
  'table-duplicate-name': 'Data tables missing headers or caption',
  // WCAG 1.3.6 / 1.3.1 — Landmarks
  'landmark-one-main':                    'Page missing landmark elements: <main>',
  'landmark-main-is-top-level':           'Page missing landmark elements: <main>',
  'landmark-no-duplicate-main':           'Page missing landmark elements: <main>',
  'landmark-banner-is-top-level':         'Page missing landmark elements: <nav>',
  'landmark-complementary-is-top-level':  'Page missing landmark elements: <nav>',
  'landmark-contentinfo-is-top-level':    'Page missing landmark elements: <nav>',
  'landmark-no-duplicate-banner':         'Page missing landmark elements: <nav>',
  'landmark-no-duplicate-contentinfo':    'Page missing landmark elements: <nav>',
  'region':                               'Page missing landmark elements: <main>',
  // WCAG 1.3.1 — Lists
  'list':            'List elements must only contain valid child elements',
  'listitem':        'List item elements must be contained in a list element',
  'definition-list': 'Definition list elements must contain valid child elements',
  'dlitem':          'Definition list items must be contained in a definition list',
};

/** Returns the canonical display title for an issue, preferring the
 *  shared map over the stored value so garbled legacy data is corrected. */
export function issueTitle(rule: string, storedTitle: string): string {
  return RULE_TITLES[rule] ?? storedTitle;
}
