import { createTheme } from '@uiw/codemirror-themes'
import { tags } from '@lezer/highlight'

/**
 * Forge CodeMirror themes.
 *
 * `settings.*` (background / gutter / selection) use CSS variables so the
 * editor chrome follows the global theme. Token colors are hard-coded
 * (GitHub Dark / Light palette) — change here if the palette ever evolves.
 */

export const forgeDark = createTheme({
  theme: 'dark',
  settings: {
    background: 'var(--bg-surface)',
    foreground: 'var(--text-primary)',
    caret: 'var(--accent-indigo)',
    selection: 'color-mix(in oklch, var(--accent-indigo) 25%, transparent)',
    selectionMatch: 'color-mix(in oklch, var(--accent-indigo) 15%, transparent)',
    gutterBackground: 'var(--bg-surface)',
    gutterForeground: 'var(--text-muted)',
    gutterActiveForeground: 'var(--text-secondary)',
    lineHighlight: 'color-mix(in oklch, var(--accent-indigo) 6%, transparent)',
    gutterBorder: 'var(--border-subtle)',
  },
  styles: [
    { tag: tags.keyword, color: '#ff7b72' },
    { tag: tags.controlKeyword, color: '#ff7b72' },
    { tag: tags.definitionKeyword, color: '#ff7b72' },
    { tag: tags.modifier, color: '#ff7b72' },
    { tag: tags.string, color: '#a5d6ff' },
    { tag: tags.special(tags.string), color: '#a5d6ff' },
    { tag: tags.number, color: '#79c0ff' },
    { tag: tags.integer, color: '#79c0ff' },
    { tag: tags.float, color: '#79c0ff' },
    { tag: tags.bool, color: '#79c0ff' },
    { tag: tags.null, color: '#79c0ff' },
    { tag: tags.comment, color: '#8b949e', fontStyle: 'italic' },
    { tag: tags.lineComment, color: '#8b949e', fontStyle: 'italic' },
    { tag: tags.blockComment, color: '#8b949e', fontStyle: 'italic' },
    { tag: tags.function(tags.name), color: '#d2a8ff' },
    { tag: tags.function(tags.variableName), color: '#d2a8ff' },
    { tag: tags.typeName, color: '#ffa657' },
    { tag: tags.className, color: '#ffa657' },
    { tag: tags.tagName, color: '#7ee787' },
    { tag: tags.attributeName, color: '#79c0ff' },
    { tag: tags.attributeValue, color: '#a5d6ff' },
    { tag: tags.propertyName, color: '#7ee787' },
    { tag: tags.variableName, color: '#e6edf3' },
    { tag: tags.operator, color: '#ff7b72' },
    { tag: tags.punctuation, color: '#e6edf3' },
    { tag: tags.angleBracket, color: '#e6edf3' },
    { tag: tags.unit, color: '#79c0ff' },
    { tag: tags.meta, color: '#8b949e' },
    { tag: tags.labelName, color: '#ff7b72' },
    { tag: tags.namespace, color: '#ffa657' },
    { tag: tags.macroName, color: '#d2a8ff' },
    { tag: tags.special(tags.variableName), color: '#79c0ff' },
    { tag: tags.self, color: '#ff7b72' },
    { tag: tags.regexp, color: '#a5d6ff' },
    { tag: tags.escape, color: '#79c0ff' },
    { tag: tags.color, color: '#79c0ff' },
    { tag: tags.heading, color: '#d2a8ff', fontWeight: 'bold' },
    { tag: tags.heading1, color: '#d2a8ff', fontWeight: 'bold' },
    { tag: tags.heading2, color: '#d2a8ff', fontWeight: 'bold' },
    { tag: tags.heading3, color: '#d2a8ff', fontWeight: 'bold' },
    { tag: tags.link, color: '#79c0ff', textDecoration: 'underline' },
    { tag: tags.url, color: '#79c0ff', textDecoration: 'underline' },
    { tag: tags.emphasis, fontStyle: 'italic' },
    { tag: tags.strong, fontWeight: 'bold' },
    { tag: tags.strikethrough, textDecoration: 'line-through' },
    { tag: tags.inserted, color: '#7ee787' },
    { tag: tags.deleted, color: '#ff7b72' },
    { tag: tags.changed, color: '#ffa657' },
    { tag: tags.invalid, color: '#ff7b72', textDecoration: 'underline' },
    { tag: tags.contentSeparator, color: '#8b949e' },
    { tag: tags.processingInstruction, color: '#8b949e' },
  ],
})

export const forgeLight = createTheme({
  theme: 'light',
  settings: {
    background: 'var(--bg-surface)',
    foreground: 'var(--text-primary)',
    caret: 'var(--accent-indigo)',
    selection: 'color-mix(in oklch, var(--accent-indigo) 18%, transparent)',
    selectionMatch: 'color-mix(in oklch, var(--accent-indigo) 10%, transparent)',
    gutterBackground: 'var(--bg-surface)',
    gutterForeground: 'var(--text-muted)',
    gutterActiveForeground: 'var(--text-secondary)',
    lineHighlight: 'color-mix(in oklch, var(--accent-indigo) 4%, transparent)',
    gutterBorder: 'var(--border-subtle)',
  },
  styles: [
    { tag: tags.keyword, color: '#cf222e' },
    { tag: tags.controlKeyword, color: '#cf222e' },
    { tag: tags.definitionKeyword, color: '#cf222e' },
    { tag: tags.modifier, color: '#cf222e' },
    { tag: tags.string, color: '#0a3069' },
    { tag: tags.special(tags.string), color: '#0a3069' },
    { tag: tags.number, color: '#0550ae' },
    { tag: tags.integer, color: '#0550ae' },
    { tag: tags.float, color: '#0550ae' },
    { tag: tags.bool, color: '#0550ae' },
    { tag: tags.null, color: '#0550ae' },
    { tag: tags.comment, color: '#6e7781', fontStyle: 'italic' },
    { tag: tags.lineComment, color: '#6e7781', fontStyle: 'italic' },
    { tag: tags.blockComment, color: '#6e7781', fontStyle: 'italic' },
    { tag: tags.function(tags.name), color: '#8250df' },
    { tag: tags.function(tags.variableName), color: '#8250df' },
    { tag: tags.typeName, color: '#953800' },
    { tag: tags.className, color: '#953800' },
    { tag: tags.tagName, color: '#116329' },
    { tag: tags.attributeName, color: '#0550ae' },
    { tag: tags.attributeValue, color: '#0a3069' },
    { tag: tags.propertyName, color: '#116329' },
    { tag: tags.variableName, color: '#1f2328' },
    { tag: tags.operator, color: '#cf222e' },
    { tag: tags.punctuation, color: '#1f2328' },
    { tag: tags.angleBracket, color: '#1f2328' },
    { tag: tags.unit, color: '#0550ae' },
    { tag: tags.meta, color: '#6e7781' },
    { tag: tags.labelName, color: '#cf222e' },
    { tag: tags.namespace, color: '#953800' },
    { tag: tags.macroName, color: '#8250df' },
    { tag: tags.special(tags.variableName), color: '#0550ae' },
    { tag: tags.self, color: '#cf222e' },
    { tag: tags.regexp, color: '#0a3069' },
    { tag: tags.escape, color: '#0550ae' },
    { tag: tags.color, color: '#0550ae' },
    { tag: tags.heading, color: '#8250df', fontWeight: 'bold' },
    { tag: tags.heading1, color: '#8250df', fontWeight: 'bold' },
    { tag: tags.heading2, color: '#8250df', fontWeight: 'bold' },
    { tag: tags.heading3, color: '#8250df', fontWeight: 'bold' },
    { tag: tags.link, color: '#0550ae', textDecoration: 'underline' },
    { tag: tags.url, color: '#0550ae', textDecoration: 'underline' },
    { tag: tags.emphasis, fontStyle: 'italic' },
    { tag: tags.strong, fontWeight: 'bold' },
    { tag: tags.strikethrough, textDecoration: 'line-through' },
    { tag: tags.inserted, color: '#116329' },
    { tag: tags.deleted, color: '#cf222e' },
    { tag: tags.changed, color: '#953800' },
    { tag: tags.invalid, color: '#cf222e', textDecoration: 'underline' },
    { tag: tags.contentSeparator, color: '#6e7781' },
    { tag: tags.processingInstruction, color: '#6e7781' },
  ],
})
