-- This file contains the built-in components. Each componment is a function
-- that takes the following arguments:
--      config: A table containing the configuration provided by the user
--              when declaring this component in their renderer config.
--      node:   A NuiNode object for the currently focused node.
--      state:  The current state of the source providing the items.
--
-- The function should return either a table, or a list of tables, each of which
-- contains the following keys:
--    text:      The text to display for this item.
--    highlight: The highlight group to apply to this text.

local highlights = require("neo-tree.ui.highlights")
local common = require("neo-tree.sources.common.components")

---@alias neotree.Component.GitStatus._Key
---|"name"

---@class neotree.Component.GitStatus
---@field [1] neotree.Component.GitStatus._Key|neotree.Component.Common._Key

---@type table<neotree.Component.GitStatus._Key, neotree.Renderer>
local M = {}

---@class (exact) neotree.Component.GitStatus.Name : neotree.Component.Common.Name
---@field [1] "current_filter"?
---@field use_git_status_colors boolean?

---@param config neotree.Component.GitStatus.Name
M.name = function(config, node, state)
  local highlight = config.highlight or highlights.FILE_NAME_OPENED
  local name = node.name
  if node.type == "directory" then
    if node:get_depth() == 1 then
      highlight = highlights.ROOT_NAME
      if node:has_children() then
        name = "GIT STATUS for " .. name
      else
        name = "GIT STATUS (working tree clean) for " .. name
      end
    else
      highlight = highlights.DIRECTORY_NAME
    end
  elseif config.use_git_status_colors then
    local git_status = state.components.git_status({}, node, state)
    if git_status and git_status.highlight then
      highlight = git_status.highlight
    end
  end
  return {
    text = name,
    highlight = highlight,
  }
end

return vim.tbl_deep_extend("force", common, M)
