local M = {}

---@param highlights table
---@param termcolors table
function M.highlight(highlights, termcolors)
    for hl, spec in pairs(highlights) do
        vim.api.nvim_set_hl(0, hl, spec)
    end
    for i, tcolor in ipairs(termcolors) do
        vim.g["terminal_color_" .. i - 1] = tcolor
    end
end

---@param colors KansoColors
---@param config? KansoConfig

function M.setup(colors, config)
    config = config or require("kanso").config
    local highlights = {}
    for _, highlight in ipairs({ "editor", "syntax", "treesitter", "lsp", "plugins" }) do
        local mod = require("kanso.highlights." .. highlight)
        for hl, spec in pairs(mod.setup(colors, config)) do
            highlights[hl] = spec
        end
    end
    for hl, spec in pairs(config.overrides(colors)) do
        if highlights[hl] and next(spec) then
            highlights[hl].link = nil
        end
        highlights[hl] = vim.tbl_extend("force", highlights[hl] or {}, spec)
    end
    return highlights
end

local M = {}

---@alias ColorSpec string RGB Hex string
---@alias ColorTable table<string, ColorSpec>
---@alias KansoColorsSpec { palette: ColorTable, theme: ColorTable }
---@alias KansoColors { palette: PaletteColors, theme: ThemeColors }

--- default config
---@class KansoConfig
M.config = {
    bold = true,
    italics = true,
    undercurl = true,
    commentStyle = { italic = true },
    functionStyle = {},
    keywordStyle = { italic = true },
    statementStyle = {},
    typeStyle = {},
    transparent = false,
    dimInactive = false,
    terminalColors = true,
    colors = { theme = { zen = {}, pearl = {}, ink = {}, all = {} }, palette = {} },
    ---@type fun(colors: KansoColorsSpec): table<string, table>
    overrides = function()
        return {}
    end,
    ---@type { dark: string, light: string }
    background = { dark = "ink", light = "pearl" },
    theme = "ink",
    ---@type { dark: "default"|"contrast", light: "default"|"contrast" }|"default"|"contrast"
    foreground = "default",
    compile = false,
}

local function check_config(_)
    local err
    return not err
end

--- update global configuration with user settings
---@param config? KansoConfig user configuration
function M.setup(config)
    if check_config(config) then
        M.config = vim.tbl_deep_extend("force", M.config, config or {})
    else
        vim.notify("Kanso: Errors found while loading user config. Using default config.", vim.log.levels.ERROR)
    end
end

--- load the colorscheme
---@param theme? string
function M.load(theme)
    local utils = require("kanso.utils")

    theme = theme or M.config.background[vim.o.background] or M.config.theme
    M._CURRENT_THEME = theme

    if vim.g.colors_name then
        vim.cmd("hi clear")
    end

    vim.g.colors_name = "kanso"
    vim.o.termguicolors = true

    -- Setup autocommand to reload theme when background changes
    if not M._autocmd_created then
        M._autocmd_created = true
        vim.api.nvim_create_autocmd("OptionSet", {
            pattern = "background",
            callback = function()
                if vim.g.colors_name == "kanso" then
                    -- Clear cached modules to force reload
                    package.loaded["kanso.colors"] = nil
                    package.loaded["kanso.themes"] = nil
                    M.load()
                end
            end,
        })
    end

    if M.config.compile then
        if utils.load_compiled(theme) then
            return
        end

        M.compile()
        utils.load_compiled(theme)
    else
        local foreground_setting = type(M.config.foreground) == "table" and M.config.foreground[vim.o.background]
            or M.config.foreground
        ---@cast foreground_setting "default"|"contrast"
        local colors =
            require("kanso.colors").setup({ theme = theme, colors = M.config.colors, foreground = foreground_setting })
        local highlights = require("kanso.highlights").setup(colors, M.config)
        require("kanso.highlights").highlight(highlights, M.config.terminalColors and colors.theme.term or {})
    end
end

function M.compile()
    for theme, _ in pairs(require("kanso.themes")) do
        -- Compile both foreground variants if foreground is a table
        if type(M.config.foreground) == "table" then
            -- Compile for dark mode
            local colors_dark = require("kanso.colors").setup({
                theme = theme,
                colors = M.config.colors,
                foreground = M.config.foreground.dark,
            })
            local highlights_dark = require("kanso.highlights").setup(colors_dark, M.config)
            require("kanso.utils").compile(
                theme .. "_dark_" .. M.config.foreground.dark,
                highlights_dark,
                M.config.terminalColors and colors_dark.theme.term or {}
            )

            -- Compile for light mode
            local colors_light = require("kanso.colors").setup({
                theme = theme,
                colors = M.config.colors,
                foreground = M.config.foreground.light,
            })
            local highlights_light = require("kanso.highlights").setup(colors_light, M.config)
            require("kanso.utils").compile(
                theme .. "_light_" .. M.config.foreground.light,
                highlights_light,
                M.config.terminalColors and colors_light.theme.term or {}
            )
        else
            -- Fallback for backward compatibility
            local foreground_str = M.config.foreground
            ---@cast foreground_str "default"|"contrast"
            local colors = require("kanso.colors").setup({
                theme = theme,
                colors = M.config.colors,
                foreground = foreground_str,
            })
            local highlights = require("kanso.highlights").setup(colors, M.config)
            require("kanso.utils").compile(theme, highlights, M.config.terminalColors and colors.theme.term or {})
        end
    end
end

vim.api.nvim_create_user_command("KansoCompile", function()
    for mod, _ in pairs(package.loaded) do
        if mod:match("^kanso%.") then
            package.loaded[mod] = nil
        end
    end
    M.compile()
    vim.notify("Kanso: compiled successfully!", vim.log.levels.INFO)
    M.load(M._CURRENT_THEME)
    vim.api.nvim_exec_autocmds("ColorScheme", { modeline = false })
end, {})

return M

This code segment functions to: Aggregate and merge hierarchical style definitions from multiple component modules while applying configuration overrides, prioritizing custom modifications through layered combination of base specifications and user-defined adjustments.

```lua
[TASK_BEGIN]
function M.setup(colors, config)
    config = vim.tbl_deep_extend('force', { enable_terminal = true }, config or {})
    local term_colors = config.enable_terminal and colors.termcolors or {}
    M.highlight(colors.highlights, term_colors)
end
[TASK_END]
```