import { SyntaxStyle, RGBA } from "@opentui/core"
import type { Theme } from "./theme-types"
import { getCodeSyntaxRules } from "./theme-syntax-code"
import { getMarkupSyntaxRules } from "./theme-syntax-markup"

export function getSyntaxRules(theme: Theme) {
  return [...getCodeSyntaxRules(theme), ...getMarkupSyntaxRules(theme)]
}

export function generateSyntax(theme: Theme) {
  return SyntaxStyle.fromTheme(getSyntaxRules(theme))
}

export function generateSubtleSyntax(theme: Theme) {
  const rules = getSyntaxRules(theme)
  return SyntaxStyle.fromTheme(
    rules.map((rule) => {
      if (rule.style.foreground) {
        const fg = rule.style.foreground
        return {
          ...rule,
          style: {
            ...rule.style,
            foreground: RGBA.fromInts(
              Math.round(fg.r * 255),
              Math.round(fg.g * 255),
              Math.round(fg.b * 255),
              Math.round(theme.thinkingOpacity * 255),
            ),
          },
        }
      }
      return rule
    }),
  )
}
