url <- "https://raw.githubusercontent.com/nanxstats/ggsci/refs/heads/master/R/palettes-iterm.R"
source(url)

palette_names <- iterm_palettes()
escape_py_string <- function(x) encodeString(x, quote = "\"", na.encode = FALSE)
palette_names_lines <- paste(
  vapply(
    palette_names,
    function(name) paste0("    ", escape_py_string(name), ","),
    character(1)
  ),
  collapse = "\n"
)

palette_names_block <- paste0(
  "ITERM_PALETTES: Final[tuple[str, ...]] = (\n",
  palette_names_lines,
  "\n)\n\n"
)

palettes_block <- paste0(
  "PALETTES_ITERM: Final[dict[str, dict[str, list[str]]]] = ",
  jsonlite::toJSON(ggsci_db_iterm, auto_unbox = TRUE, pretty = TRUE),
  "\n"
)

header <- paste0(
  "# Generated by scripts/update_palettes_iterm_data.sh: do not edit by hand\n",
  "# Please run sh scripts/update_palettes_iterm_data.sh to regenerate this file\n\n",
  "\"\"\"\n",
  "iTerm color palettes data\n",
  "\"\"\"\n\n",
  "from typing import Final\n\n"
)

txt <- paste0(header, palette_names_block, palettes_block)

writeLines(txt, con = "src/ggsci/data_iterm.py")
