// Terminal color schemes: KKTerm's original curated set plus the generated
// TerminalColors catalog. The scheme applies per Pane renderer: the global default
// lives in Terminal Settings (`terminalSettings.colorScheme`) and each
// terminal-type Connection may override it (`connection.terminalColorScheme`,
// set from the Pane actions menu). Scheme names are proper nouns and are not
// translated.

import { TERMINAL_COLORS_CATALOG_SCHEMES } from "./terminalColorsCatalog.generated";

export interface TerminalColorSchemePalette {
  background: string;
  foreground: string;
  cursor?: string;
  selectionBackground?: string;
  black: string;
  red: string;
  green: string;
  yellow: string;
  blue: string;
  magenta: string;
  cyan: string;
  white: string;
  brightBlack: string;
  brightRed: string;
  brightGreen: string;
  brightYellow: string;
  brightBlue: string;
  brightMagenta: string;
  brightCyan: string;
  brightWhite: string;
}

export interface TerminalColorScheme {
  id: string;
  name: string;
  palette: TerminalColorSchemePalette;
}

export const DEFAULT_TERMINAL_COLOR_SCHEME_ID = "kkterm";

// The built-in KKTerm look. Background/foreground/selection/scrollbar values
// mirror the pre-scheme hard-coded renderer theme; the ANSI ramp follows the
// same muted blue-slate direction.
const KKTERM_SCHEME: TerminalColorScheme = {
  id: DEFAULT_TERMINAL_COLOR_SCHEME_ID,
  name: "KKTerm",
  palette: {
    background: "#0c1219",
    foreground: "#d9e2ef",
    cursor: "#d9e2ef",
    selectionBackground: "#305f95",
    black: "#1c2733",
    red: "#e06c75",
    green: "#98c379",
    yellow: "#e5c07b",
    blue: "#61afef",
    magenta: "#c678dd",
    cyan: "#56b6c2",
    white: "#d9e2ef",
    brightBlack: "#5a6b7f",
    brightRed: "#ef8088",
    brightGreen: "#a9d38d",
    brightYellow: "#f0cf8f",
    brightBlue: "#79c0ff",
    brightMagenta: "#d58ff0",
    brightCyan: "#6cc9d6",
    brightWhite: "#f2f6fb",
  },
};

export const TERMINAL_COLOR_SCHEMES: readonly TerminalColorScheme[] = [
  KKTERM_SCHEME,
  {
    id: "ayu-dark",
    name: "Ayu Dark",
    palette: {
      background: "#0f1419",
      foreground: "#e6e1cf",
      black: "#000000",
      red: "#ff3333",
      green: "#b8cc52",
      yellow: "#e7c547",
      blue: "#36a3d9",
      magenta: "#f07178",
      cyan: "#95e6cb",
      white: "#ffffff",
      brightBlack: "#323232",
      brightRed: "#ff6565",
      brightGreen: "#eafe84",
      brightYellow: "#fff779",
      brightBlue: "#68d5ff",
      brightMagenta: "#ffa3aa",
      brightCyan: "#c7fffd",
      brightWhite: "#ffffff",
    },
  },
  {
    id: "catppuccin-latte",
    name: "Catppuccin Latte",
    palette: {
      background: "#eff1f5",
      foreground: "#4c4f69",
      cursor: "#dc8a78",
      selectionBackground: "#ccd0da",
      black: "#5c5f77",
      red: "#d20f39",
      green: "#40a02b",
      yellow: "#df8e1d",
      blue: "#1e66f5",
      magenta: "#ea76cb",
      cyan: "#179299",
      white: "#acb0be",
      brightBlack: "#6c6f85",
      brightRed: "#de293e",
      brightGreen: "#49af3d",
      brightYellow: "#eea02d",
      brightBlue: "#456eff",
      brightMagenta: "#fe85d8",
      brightCyan: "#2d9fa8",
      brightWhite: "#bcc0cc",
    },
  },
  {
    id: "catppuccin-mocha",
    name: "Catppuccin Mocha",
    palette: {
      background: "#1e1e2e",
      foreground: "#cdd6f4",
      cursor: "#f5e0dc",
      selectionBackground: "#585b70",
      black: "#45475a",
      red: "#f38ba8",
      green: "#a6e3a1",
      yellow: "#f9e2af",
      blue: "#89b4fa",
      magenta: "#f5c2e7",
      cyan: "#94e2d5",
      white: "#bac2de",
      brightBlack: "#585b70",
      brightRed: "#f37799",
      brightGreen: "#89d88b",
      brightYellow: "#ebd391",
      brightBlue: "#74a8fc",
      brightMagenta: "#f2aede",
      brightCyan: "#6bd7ca",
      brightWhite: "#a6adc8",
    },
  },
  {
    id: "cobalt2",
    name: "Cobalt2",
    palette: {
      background: "#132738",
      foreground: "#ffffff",
      cursor: "#f0cc09",
      black: "#000000",
      red: "#ff0000",
      green: "#38de21",
      yellow: "#ffe50a",
      blue: "#1460d2",
      magenta: "#ff005d",
      cyan: "#00bbbb",
      white: "#bbbbbb",
      brightBlack: "#555555",
      brightRed: "#f40e17",
      brightGreen: "#3bd01d",
      brightYellow: "#edc809",
      brightBlue: "#5555ff",
      brightMagenta: "#ff55ff",
      brightCyan: "#6ae3fa",
      brightWhite: "#ffffff",
    },
  },
  {
    id: "dracula",
    name: "Dracula",
    palette: {
      background: "#282a36",
      foreground: "#f8f8f2",
      selectionBackground: "#44475a",
      black: "#21222c",
      red: "#ff5555",
      green: "#50fa7b",
      yellow: "#f1fa8c",
      blue: "#bd93f9",
      magenta: "#ff79c6",
      cyan: "#8be9fd",
      white: "#f8f8f2",
      brightBlack: "#6272a4",
      brightRed: "#ff6e6e",
      brightGreen: "#69ff94",
      brightYellow: "#ffffa5",
      brightBlue: "#d6acff",
      brightMagenta: "#ff92df",
      brightCyan: "#a4ffff",
      brightWhite: "#ffffff",
    },
  },
  {
    id: "everforest-dark",
    name: "Everforest Dark",
    palette: {
      background: "#2d353b",
      foreground: "#d3c6aa",
      selectionBackground: "#475258",
      black: "#475258",
      red: "#e67e80",
      green: "#a7c080",
      yellow: "#dbbc7f",
      blue: "#7fbbb3",
      magenta: "#d699b6",
      cyan: "#83c092",
      white: "#d3c6aa",
      brightBlack: "#5c6a72",
      brightRed: "#e67e80",
      brightGreen: "#a7c080",
      brightYellow: "#dbbc7f",
      brightBlue: "#7fbbb3",
      brightMagenta: "#d699b6",
      brightCyan: "#83c092",
      brightWhite: "#d3c6aa",
    },
  },
  {
    id: "github-dark",
    name: "GitHub Dark",
    palette: {
      background: "#0d1117",
      foreground: "#c9d1d9",
      selectionBackground: "#264f78",
      black: "#484f58",
      red: "#ff7b72",
      green: "#3fb950",
      yellow: "#d29922",
      blue: "#58a6ff",
      magenta: "#bc8cff",
      cyan: "#39c5cf",
      white: "#b1bac4",
      brightBlack: "#6e7681",
      brightRed: "#ffa198",
      brightGreen: "#56d364",
      brightYellow: "#e3b341",
      brightBlue: "#79c0ff",
      brightMagenta: "#d2a8ff",
      brightCyan: "#56d4dd",
      brightWhite: "#f0f6fc",
    },
  },
  {
    id: "gruvbox-dark",
    name: "Gruvbox Dark",
    palette: {
      background: "#282828",
      foreground: "#ebdbb2",
      selectionBackground: "#504945",
      black: "#282828",
      red: "#cc241d",
      green: "#98971a",
      yellow: "#d79921",
      blue: "#458588",
      magenta: "#b16286",
      cyan: "#689d6a",
      white: "#a89984",
      brightBlack: "#928374",
      brightRed: "#fb4934",
      brightGreen: "#b8bb26",
      brightYellow: "#fabd2f",
      brightBlue: "#83a598",
      brightMagenta: "#d3869b",
      brightCyan: "#8ec07c",
      brightWhite: "#ebdbb2",
    },
  },
  {
    id: "gruvbox-light",
    name: "Gruvbox Light",
    palette: {
      background: "#fbf1c7",
      foreground: "#3c3836",
      selectionBackground: "#d5c4a1",
      black: "#fbf1c7",
      red: "#cc241d",
      green: "#98971a",
      yellow: "#d79921",
      blue: "#458588",
      magenta: "#b16286",
      cyan: "#689d6a",
      white: "#7c6f64",
      brightBlack: "#928374",
      brightRed: "#9d0006",
      brightGreen: "#79740e",
      brightYellow: "#b57614",
      brightBlue: "#076678",
      brightMagenta: "#8f3f71",
      brightCyan: "#427b58",
      brightWhite: "#3c3836",
    },
  },
  {
    id: "kanagawa",
    name: "Kanagawa",
    palette: {
      background: "#1f1f28",
      foreground: "#dcd7ba",
      selectionBackground: "#2d4f67",
      black: "#090618",
      red: "#c34043",
      green: "#76946a",
      yellow: "#c0a36e",
      blue: "#7e9cd8",
      magenta: "#957fb8",
      cyan: "#6a9589",
      white: "#c8c093",
      brightBlack: "#727169",
      brightRed: "#e82424",
      brightGreen: "#98bb6c",
      brightYellow: "#e6c384",
      brightBlue: "#7fb4ca",
      brightMagenta: "#938aa9",
      brightCyan: "#7aa89f",
      brightWhite: "#dcd7ba",
    },
  },
  {
    id: "monokai",
    name: "Monokai",
    palette: {
      background: "#272822",
      foreground: "#f8f8f2",
      selectionBackground: "#49483e",
      black: "#272822",
      red: "#f92672",
      green: "#a6e22e",
      yellow: "#e6db74",
      blue: "#66d9ef",
      magenta: "#ae81ff",
      cyan: "#a1efe4",
      white: "#f8f8f2",
      brightBlack: "#75715e",
      brightRed: "#f92672",
      brightGreen: "#a6e22e",
      brightYellow: "#e6db74",
      brightBlue: "#66d9ef",
      brightMagenta: "#ae81ff",
      brightCyan: "#a1efe4",
      brightWhite: "#f9f8f5",
    },
  },
  {
    id: "nord",
    name: "Nord",
    palette: {
      background: "#2e3440",
      foreground: "#d8dee9",
      selectionBackground: "#434c5e",
      black: "#3b4252",
      red: "#bf616a",
      green: "#a3be8c",
      yellow: "#ebcb8b",
      blue: "#81a1c1",
      magenta: "#b48ead",
      cyan: "#88c0d0",
      white: "#e5e9f0",
      brightBlack: "#4c566a",
      brightRed: "#bf616a",
      brightGreen: "#a3be8c",
      brightYellow: "#ebcb8b",
      brightBlue: "#81a1c1",
      brightMagenta: "#b48ead",
      brightCyan: "#8fbcbb",
      brightWhite: "#eceff4",
    },
  },
  {
    id: "one-dark",
    name: "One Dark",
    palette: {
      background: "#282c34",
      foreground: "#abb2bf",
      selectionBackground: "#3e4451",
      black: "#282c34",
      red: "#e06c75",
      green: "#98c379",
      yellow: "#e5c07b",
      blue: "#61afef",
      magenta: "#c678dd",
      cyan: "#56b6c2",
      white: "#abb2bf",
      brightBlack: "#5c6370",
      brightRed: "#e06c75",
      brightGreen: "#98c379",
      brightYellow: "#e5c07b",
      brightBlue: "#61afef",
      brightMagenta: "#c678dd",
      brightCyan: "#56b6c2",
      brightWhite: "#ffffff",
    },
  },
  {
    id: "one-light",
    name: "One Light",
    palette: {
      background: "#fafafa",
      foreground: "#383a42",
      selectionBackground: "#e5e5e6",
      black: "#383a42",
      red: "#e45649",
      green: "#50a14f",
      yellow: "#c18401",
      blue: "#4078f2",
      magenta: "#a626a4",
      cyan: "#0184bc",
      white: "#a0a1a7",
      brightBlack: "#4f525e",
      brightRed: "#e45649",
      brightGreen: "#50a14f",
      brightYellow: "#c18401",
      brightBlue: "#4078f2",
      brightMagenta: "#a626a4",
      brightCyan: "#0184bc",
      brightWhite: "#ffffff",
    },
  },
  {
    id: "rose-pine",
    name: "Rosé Pine",
    palette: {
      background: "#191724",
      foreground: "#e0def4",
      selectionBackground: "#403d52",
      black: "#26233a",
      red: "#eb6f92",
      green: "#31748f",
      yellow: "#f6c177",
      blue: "#9ccfd8",
      magenta: "#c4a7e7",
      cyan: "#ebbcba",
      white: "#e0def4",
      brightBlack: "#6e6a86",
      brightRed: "#eb6f92",
      brightGreen: "#31748f",
      brightYellow: "#f6c177",
      brightBlue: "#9ccfd8",
      brightMagenta: "#c4a7e7",
      brightCyan: "#ebbcba",
      brightWhite: "#e0def4",
    },
  },
  {
    id: "snazzy",
    name: "Snazzy",
    palette: {
      background: "#282a36",
      foreground: "#eff0eb",
      selectionBackground: "#3e404a",
      black: "#282a36",
      red: "#ff5c57",
      green: "#5af78e",
      yellow: "#f3f99d",
      blue: "#57c7ff",
      magenta: "#ff6ac1",
      cyan: "#9aedfe",
      white: "#f1f1f0",
      brightBlack: "#686868",
      brightRed: "#ff5c57",
      brightGreen: "#5af78e",
      brightYellow: "#f3f99d",
      brightBlue: "#57c7ff",
      brightMagenta: "#ff6ac1",
      brightCyan: "#9aedfe",
      brightWhite: "#eff0eb",
    },
  },
  {
    id: "solarized-dark",
    name: "Solarized Dark",
    palette: {
      background: "#002b36",
      foreground: "#839496",
      selectionBackground: "#073642",
      black: "#073642",
      red: "#dc322f",
      green: "#859900",
      yellow: "#b58900",
      blue: "#268bd2",
      magenta: "#d33682",
      cyan: "#2aa198",
      white: "#eee8d5",
      brightBlack: "#002b36",
      brightRed: "#cb4b16",
      brightGreen: "#586e75",
      brightYellow: "#657b83",
      brightBlue: "#839496",
      brightMagenta: "#6c71c4",
      brightCyan: "#93a1a1",
      brightWhite: "#fdf6e3",
    },
  },
  {
    id: "solarized-light",
    name: "Solarized Light",
    palette: {
      background: "#fdf6e3",
      foreground: "#657b83",
      selectionBackground: "#eee8d5",
      black: "#073642",
      red: "#dc322f",
      green: "#859900",
      yellow: "#b58900",
      blue: "#268bd2",
      magenta: "#d33682",
      cyan: "#2aa198",
      white: "#eee8d5",
      brightBlack: "#002b36",
      brightRed: "#cb4b16",
      brightGreen: "#586e75",
      brightYellow: "#657b83",
      brightBlue: "#839496",
      brightMagenta: "#6c71c4",
      brightCyan: "#93a1a1",
      brightWhite: "#fdf6e3",
    },
  },
  {
    id: "tokyo-night",
    name: "Tokyo Night",
    palette: {
      background: "#1a1b26",
      foreground: "#c0caf5",
      selectionBackground: "#33467c",
      black: "#15161e",
      red: "#f7768e",
      green: "#9ece6a",
      yellow: "#e0af68",
      blue: "#7aa2f7",
      magenta: "#bb9af7",
      cyan: "#7dcfff",
      white: "#a9b1d6",
      brightBlack: "#414868",
      brightRed: "#f7768e",
      brightGreen: "#9ece6a",
      brightYellow: "#e0af68",
      brightBlue: "#7aa2f7",
      brightMagenta: "#bb9af7",
      brightCyan: "#7dcfff",
      brightWhite: "#c0caf5",
    },
  },
  {
    id: "ubuntu",
    name: "Ubuntu",
    palette: {
      background: "#300a24",
      foreground: "#eeeeec",
      black: "#2e3436",
      red: "#cc0000",
      green: "#4e9a06",
      yellow: "#c4a000",
      blue: "#3465a4",
      magenta: "#75507b",
      cyan: "#06989a",
      white: "#d3d7cf",
      brightBlack: "#555753",
      brightRed: "#ef2929",
      brightGreen: "#8ae234",
      brightYellow: "#fce94f",
      brightBlue: "#729fcf",
      brightMagenta: "#ad7fa8",
      brightCyan: "#34e2e2",
      brightWhite: "#eeeeec",
    },
  },
  {
    id: "zenburn",
    name: "Zenburn",
    palette: {
      background: "#3f3f3f",
      foreground: "#dcdccc",
      black: "#4d4d4d",
      red: "#705050",
      green: "#60b48a",
      yellow: "#dfaf8f",
      blue: "#506070",
      magenta: "#dc8cc3",
      cyan: "#8cd0d3",
      white: "#dcdccc",
      brightBlack: "#709080",
      brightRed: "#cc9393",
      brightGreen: "#7f9f7f",
      brightYellow: "#f0dfaf",
      brightBlue: "#94bff3",
      brightMagenta: "#ec93d3",
      brightCyan: "#93e0e3",
      brightWhite: "#ffffff",
    },
  },
  ...TERMINAL_COLORS_CATALOG_SCHEMES,
];

export function resolveTerminalColorScheme(schemeId: string | null | undefined): TerminalColorScheme {
  if (!schemeId) {
    return KKTERM_SCHEME;
  }
  return TERMINAL_COLOR_SCHEMES.find((scheme) => scheme.id === schemeId) ?? KKTERM_SCHEME;
}

/** `#rrggbb` → `rgba(r, g, b, alpha)`. Falls back to the input on malformed hex. */
export function hexColorWithAlpha(hex: string, alpha: number) {
  const normalized = hex.trim();
  const match = /^#([0-9a-fA-F]{6})$/.exec(normalized);
  if (!match) {
    return normalized;
  }
  const value = Number.parseInt(match[1], 16);
  const clampedAlpha = Math.min(Math.max(alpha, 0), 1);
  const r = (value >> 16) & 0xff;
  const g = (value >> 8) & 0xff;
  const b = value & 0xff;
  return `rgba(${r}, ${g}, ${b}, ${clampedAlpha})`;
}
