-- Miami Cruise for WezTerm. Generated by build.py from palette.py; edit the source, not this file.
--
-- In wezterm.lua:
--   local wezterm = require("wezterm")
--   local config = wezterm.config_builder()
--   package.path = wezterm.config_dir .. "/?.lua;" .. package.path
--   require("miami-cruise").apply(config)
--   return config
--
-- Variant: follows the system appearance unless overridden.
--   MIAMI_CRUISE=dark|light|auto   environment variable, read on every config reload
--   apply(config, { mode = "dark" })  fixed in the config file (the environment variable still wins)

local wezterm = require("wezterm")

local unpack = table.unpack or unpack -- Lua 5.4 in WezTerm, 5.1 elsewhere

local M = {}

local palettes = {
  dark = {
    ["bg"] = "#0d1d22",
    ["panel"] = "#04161a",
    ["border"] = "#253c42",
    ["hover"] = "#182c32",
    ["text"] = "#deebec",
    ["muted"] = "#9fbabf",
    ["disabled"] = "#6a8d94",
    ["accent"] = "#ff7fc0",
    ["on-accent"] = "#04161a",
    ["orchid"] = "#d7a0e3",
    selection = "#3d3142",
  },
  light = {
    ["bg"] = "#f1f8f9",
    ["panel"] = "#e4eff1",
    ["border"] = "#c9dcde",
    ["hover"] = "#dfecee",
    ["text"] = "#142a30",
    ["muted"] = "#3a5157",
    ["disabled"] = "#566e74",
    ["accent"] = "#b51c79",
    ["on-accent"] = "#f1f8f9",
    ["orchid"] = "#935ba0",
    selection = "#e5ccdf",
  },
  dark_ansi = { "#253c42", "#fd717c", "#5ed99e", "#ecd065", "#d7a0e3", "#ff7fc0", "#70d5ed", "#9fbabf", "#6a8d94", "#fd717c", "#5ed99e", "#ecd065", "#d7a0e3", "#ff7fc0", "#70d5ed", "#deebec" },
  light_ansi = { "#142a30", "#c8414c", "#008254", "#906b00", "#935ba0", "#b51c79", "#007c91", "#c9dcde", "#566e74", "#c8414c", "#008254", "#906b00", "#935ba0", "#b51c79", "#007c91", "#e4eff1" },
}

local function scheme(p, a)
  return {
    foreground = p.text,
    background = p.bg,
    cursor_bg = p.accent,
    cursor_fg = p["on-accent"],
    cursor_border = p.accent,
    selection_fg = p.text,
    selection_bg = p.selection,
    scrollbar_thumb = p.border,
    split = p.border,
    compose_cursor = p.orchid,
    visual_bell = p.hover,
    ansi = { unpack(a, 1, 8) },
    brights = { unpack(a, 9, 16) },
    copy_mode_active_highlight_bg = { Color = p.accent },
    copy_mode_active_highlight_fg = { Color = p["on-accent"] },
    copy_mode_inactive_highlight_bg = { Color = p.selection },
    copy_mode_inactive_highlight_fg = { Color = p.text },
    quick_select_label_bg = { Color = p.accent },
    quick_select_label_fg = { Color = p["on-accent"] },
    quick_select_match_bg = { Color = p.selection },
    quick_select_match_fg = { Color = p.text },
    tab_bar = {
      background = p.panel,
      inactive_tab_edge = p.border,
      active_tab = { bg_color = p.bg, fg_color = p.accent, intensity = "Bold" },
      inactive_tab = { bg_color = p.panel, fg_color = p.muted },
      inactive_tab_hover = { bg_color = p.hover, fg_color = p.text },
      new_tab = { bg_color = p.panel, fg_color = p.muted },
      new_tab_hover = { bg_color = p.hover, fg_color = p.accent },
    },
  }
end

M.schemes = {
  ["Miami Cruise"] = scheme(palettes.dark, palettes.dark_ansi),
  ["Miami Cruise Day"] = scheme(palettes.light, palettes.light_ansi),
}

-- wezterm.gui is not available to the mux server; fall back to dark there.
local function system_appearance()
  if wezterm.gui then
    return wezterm.gui.get_appearance()
  end
  return "Dark"
end

function M.variant(mode)
  local m = (os.getenv("MIAMI_CRUISE") or mode or "auto"):lower()
  if m ~= "dark" and m ~= "light" then
    m = system_appearance():find("Dark") and "dark" or "light"
  end
  return m
end

function M.apply(config, opts)
  opts = opts or {}
  config.color_schemes = config.color_schemes or {}
  for name, s in pairs(M.schemes) do
    config.color_schemes[name] = s
  end
  local variant = M.variant(opts.mode)
  local p = palettes[variant]
  config.color_scheme = variant == "light" and "Miami Cruise Day" or "Miami Cruise"
  -- GUI tab rendering reads config.colors.tab_bar directly on stable WezTerm.
  local colors = config.colors or {}
  colors.tab_bar = M.schemes[config.color_scheme].tab_bar
  config.colors = colors
  -- The fancy tab bar takes its background from window_frame, not tab_bar.
  -- Merge colours only; retain the user's font, size and border settings.
  config.window_frame = config.window_frame or {}
  for key, value in pairs({
    active_titlebar_bg = p.panel,
    inactive_titlebar_bg = p.panel,
    active_titlebar_fg = p.text,
    inactive_titlebar_fg = p.muted,
    active_titlebar_border_bottom = p.border,
    inactive_titlebar_border_bottom = p.border,
    button_bg = p.panel,
    button_fg = p.muted,
    button_hover_bg = p.hover,
    button_hover_fg = p.accent,
  }) do
    config.window_frame[key] = value
  end
  return config
end

return M
