local wezterm = require("wezterm")
local config = wezterm.config_builder()
local act = wezterm.action

-- General
config.term = "wezterm"
config.color_scheme = "Invero Day"
config.use_ime = false

-- Font
config.font = wezterm.font({ family = "Maple Mono NF", weight = "Medium" })
config.font_size = 13
config.harfbuzz_features = { "calt=0", "clig=0", "liga=0" } -- disables alternates and ligatures
config.underline_position = -4
config.underline_thickness = 3

-- Appearance
config.bold_brightens_ansi_colors = false
config.window_padding = { left = "0.5cell", right = "0.5cell", top = 6, bottom = 0 }
config.window_content_alignment = { horizontal = "Center", vertical = "Top" }
config.cell_width = 0.9
config.line_height = 0.9

-- Tabs
config.use_fancy_tab_bar = false
config.show_new_tab_button_in_tab_bar = false
config.hide_tab_bar_if_only_one_tab = true

-- Events
wezterm.on("toggle-tabbar", function(window, _)
	local overrides = window:get_config_overrides() or {}
	if overrides.enable_tab_bar == false then
		wezterm.log_info("tab bar shown")
		overrides.enable_tab_bar = true
	else
		wezterm.log_info("tab bar hidden")
		overrides.enable_tab_bar = false
	end
	window:set_config_overrides(overrides)
end)

-- Keybindings
config.keys = {
	{ mods = "OPT", key = "LeftArrow", action = act.SendString("\x1bb") }, -- Jump back one word
	{ mods = "OPT", key = "RightArrow", action = act.SendString("\x1bf") }, -- Jump forward one word
	{ mods = "CMD", key = "LeftArrow", action = act.SendString("\x01") }, -- Move to start of line
	{ mods = "CMD", key = "RightArrow", action = act.SendString("\x05") }, -- Move to end of line
	{ mods = "OPT", key = "Backspace", action = act.SendString("\x1b\x7f") }, -- Delete previous word
	{ mods = "CMD", key = "Backspace", action = act.SendString("\x15") }, -- Delete previous line
	{ mods = "OPT", key = "Delete", action = act.SendString("\x1bd") }, -- Delete next word
	{ mods = "CMD", key = "Delete", action = act.SendString("\x0b") }, -- Delete next line
	{ mods = "CMD", key = "n", action = act.SpawnWindow }, -- New window
	{ mods = "CMD", key = "t", action = act.SpawnCommandInNewTab({ cwd = wezterm.home_dir }) }, -- New tab
	{ mods = "SUPER|SHIFT", key = "LeftArrow", action = act({ MoveTabRelative = -1 }) }, -- Move tab left
	{ mods = "SUPER|SHIFT", key = "RightArrow", action = act({ MoveTabRelative = 1 }) }, -- Move tab right
	{ mods = "SUPER|SHIFT", key = "b", action = act.EmitEvent("toggle-tabbar") },
	{ mods = "SUPER|SHIFT", key = "o", action = wezterm.action.ShowTabNavigator },
	{
		mods = "SUPER|SHIFT",
		key = "r",
		action = wezterm.action.PromptInputLine({
			description = "Enter new tab title",
			action = wezterm.action_callback(function(window, pane, line)
				if line then
					window:active_tab():set_title(line)
				end
			end),
		}),
	},
}

return config
