local wezterm = require("wezterm")
local util = require("util")

local scheme = util.get_schemes()["Everforest Dark Soft (Gogh)"]

local function tab_title(tab_info)
    local title = tab_info.tab_title
    -- if the tab title is explicitly set, take that
    if title and #title > 0 then
        return title
    end
    -- Otherwise, use the title from the active pane
    -- in that tab
    return tab_info.active_pane.title
end

wezterm.on(
    "format-tab-title",
    function(tab, _, _, _, hover, max_width)
        local LEFT_CIRCLE = ""
        local RIGHT_CIRCLE = ""

        local edge_background = scheme.background
        local background = scheme.brights[1]
        local foreground = scheme.brights[8]

        -- ensure that the titles fit in the available space,
        -- and that we have room for the edges.
        local title = tab_title(tab)
        title = wezterm.truncate_right(title, max_width - 2)

        if tab.is_active then
            background = scheme.ansi[8]
            foreground = scheme.ansi[1]
        elseif hover then
            background = scheme.brights[1]
            foreground = scheme.brights[8]
        end

        local edge_foreground = background

        return {
            { Background = { Color = edge_background } },
            { Foreground = { Color = edge_foreground } },
            { Text = LEFT_CIRCLE },
            { Background = { Color = background } },
            { Foreground = { Color = foreground } },
            { Text = title },
            { Background = { Color = edge_background } },
            { Foreground = { Color = edge_foreground } },
            { Text = RIGHT_CIRCLE },
        }
    end
)
