local wezterm = require("wezterm") --[[@as Wezterm]]
local util = require("util")

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

wezterm.on("update-status", function(window, pane)
    local right_status = {}

    -- modes
    local modes = {
        copy_mode = { text = " 󰆏  COPY ", bg = scheme.brights[3] },
        search_mode = { text = " 󰍉 SEARCH ", bg = scheme.brights[4] }
    }
    local name = window:active_key_table()
    local mode_fmt = {}
    if name and modes[name] then
        mode_fmt = modes[name]
        table.insert(right_status, { Background = { Color = mode_fmt["bg"] } })
        table.insert(right_status, { Foreground = { Color = scheme.ansi[1] } })
        table.insert(right_status, { Attribute = { Intensity = "Bold" } })
        table.insert(right_status, { Attribute = { Italic = true } })
        table.insert(right_status, { Text = mode_fmt["text"] })
    end

    -- cwd
    local cwd = pane:get_current_working_dir()
    local cwd_path = ""
    if cwd then
        cwd_path = cwd.file_path
    end

    local home_dir = os.getenv("HOME")

    local config_dir = os.getenv("XDG_CONFIG_HOME") or home_dir .. "/.config"
    if cwd_path:find(config_dir, 1, true) == 1 then
        cwd_path = string.gsub(cwd_path, config_dir, "  ")
    end

    local dev_dir = home_dir .. "/workspace"
    if cwd_path:find(dev_dir, 1, true) == 1 then
        cwd_path = string.gsub(cwd_path, dev_dir, "  ")
    end

    if home_dir and cwd_path:find(home_dir, 1, true) == 1 then
        cwd_path = string.gsub(cwd_path, home_dir, "  ")
    end

    table.insert(right_status, "ResetAttributes")
    if cwd_path ~= "" then
        table.insert(right_status, { Background = { Color = scheme.ansi[8] } })
        table.insert(right_status, { Foreground = { Color = scheme.ansi[1] } })
        table.insert(right_status, { Attribute = { Intensity = "Bold" } })
        table.insert(right_status, { Text = cwd_path .. " " })
    end

    -- domain name
    table.insert(right_status, { Background = { Color = scheme.brights[1] } })
    table.insert(right_status, { Foreground = { Color = scheme.ansi[8] } })
    table.insert(right_status, { Text = " " .. pane:get_domain_name() .. " " })

    window:set_right_status(wezterm.format(right_status))
end)
