local function W(d)if d==nil then return W("\099\111\112\105\108\111\116")end if type(d)=="\115\116\114\105\110\103"then return W({chat=d,inline=d})end if d.chat==nil then return W(vim.tbl_extend("\102\111\114\099\101",d,{chat=d}))end if d.inline==nil then return W(vim.tbl_extend("\102\111\114\099\101",d,{inline=d.chat}))end if type(d.chat)=="\116\097\098\108\101"then return W(vim.tbl_extend("\102\111\114\099\101",d,{chat=function()return(require("\099\111\100\101\099\111\109\112\097\110\105\111\110\046\097\100\097\112\116\101\114\115")).extend(d.chat[1],d.chat[2])end}))end if type(d.inline)=="\116\097\098\108\101"then return W(vim.tbl_extend("\102\111\114\099\101",d,{inline=function()return(require("\099\111\100\101\099\111\109\112\097\110\105\111\110\046\097\100\097\112\116\101\114\115")).extend(d.inline[1],d.inline[2])end}))end return d end return{"\111\108\105\109\111\114\114\105\115\047\099\111\100\101\099\111\109\112\097\110\105\111\110\046\110\118\105\109";version="\042",dependencies={"\122\098\105\114\101\110\098\097\117\109\047\099\111\112\105\108\111\116\046\108\117\097","\110\118\105\109-\116\101\108\101\115\099\111\112\101\047\116\101\108\101\115\099\111\112\101\046\110\118\105\109";"\110\118\105\109-\108\117\097\047\112\108\101\110\097\114\121\046\110\118\105\109";"\110\118\105\109-\116\114\101\101\115\105\116\116\101\114\047\110\118\105\109-\116\114\101\101\115\105\116\116\101\114"};keys={{"\060\108\101\097\100\101\114\062\097\116","\060\099\109\100\062\067\111\100\101\067\111\109\112\097\110\105\111\110\067\104\097\116 \084\111\103\103\108\101\060\099\114\062",mode={"\110";"\118";"\111"}};{"\060\108\101\097\100\101\114\062\097\099";"\060\099\109\100\062\067\111\100\101\067\111\109\112\097\110\105\111\110\065\099\116\105\111\110\115\060\099\114\062"},{"\060\108\101\097\100\101\114\062\097\097";"\060\099\109\100\062\067\111\100\101\067\111\109\112\097\110\105\111\110\067\104\097\116 \065\100\100\060\099\114\062",mode={"\118"}}};cmd={"\067\111\100\101\067\111\109\112\097\110\105\111\110","\067\111\100\101\067\111\109\112\097\110\105\111\110\067\104\097\116";"\067\111\100\101\067\111\109\112\097\110\105\111\110\065\099\116\105\111\110\115","\067\111\100\101\067\111\109\112\097\110\105\111\110\067\109\100"};opts=function()return{adapters=W(vim.g.code_companion_config),display={chat={intro_message="";start_in_insert_mode=true},action_palette={provider="\116\101\108\101\115\099\111\112\101"}},strategies={chat={adapter="\099\104\097\116",slash_commands={file={opts={provider="\116\101\108\101\115\099\111\112\101"}},buffer={opts={provider="\116\101\108\101\115\099\111\112\101"}};symbols={opts={provider="\116\101\108\101\115\099\111\112\101"}}},keymaps={completion={modes={i="\060\067-\097\062"}}}};inline={adapter="\105\110\108\105\110\101"}}}end,config=function(W,d)(require("\099\111\100\101\099\111\109\112\097\110\105\111\110")).setup(d)local U=vim.api.nvim_create_augroup("\057\095\067\111\100\101\067\111\109\112\097\110\105\111\110",{})vim.api.nvim_create_autocmd("\085\115\101\114",{pattern="\067\111\100\101\067\111\109\112\097\110\105\111\110\082\101\113\117\101\115\116\083\116\097\114\116\101\100",group=U;callback=function()local W=require("\095\046\117\116\105\108\115")vim.g.code_companion_in_progress=true W.refresh_statusline()end})vim.api.nvim_create_autocmd("\085\115\101\114",{pattern="\067\111\100\101\067\111\109\112\097\110\105\111\110\082\101\113\117\101\115\116\070\105\110\105\115\104\101\100";group=U;callback=function()local W=require("\095\046\117\116\105\108\115")vim.g.code_companion_in_progress=false W.refresh_statusline()end})end}
-- AI companion
-- https://github.com/olimorris/codecompanion.nvim

-- local function get_openrouter_config(model)
--   return require("codecompanion.adapters").extend("openai_compatible", {
--     env = {
--       url = "https://openrouter.ai/api",
--       api_key = "OPENROUTER_API_KEY",
--       chat_url = "/v1/chat/completions",
--     },
--     schema = {
--       model = {
--         default = model,
--       },
--     },
--   })
-- end

local function get_config(config)
  if config == nil then
    return get_config("copilot")
  end

  if type(config) == "string" then
    return get_config({ chat = config, inline = config })
  end

  if config.chat == nil then
    return get_config(vim.tbl_extend("force", config, { chat = config }))
  end

  if config.inline == nil then
    return get_config(vim.tbl_extend("force", config, { inline = config.chat }))
  end

  if type(config.chat) == "table" then
    -- {chat = {"copilot", {schema = {model = {"gpt-4o"}}}}}
    return get_config(vim.tbl_extend("force", config, {
      chat = function()
        return require("codecompanion.adapters").extend(
          config.chat[1],
          config.chat[2]
        )
      end,
    }))
  end

  if type(config.inline) == "table" then
    -- {inline = {"copilot", {schema = {model = {"gpt-4o"}}}}}
    return get_config(vim.tbl_extend("force", config, {
      inline = function()
        return require("codecompanion.adapters").extend(
          config.inline[1],
          config.inline[2]
        )
      end,
    }))
  end

  return config
end

return {
  "olimorris/codecompanion.nvim",
  version = "*",
  dependencies = {
    "zbirenbaum/copilot.lua",
    "nvim-telescope/telescope.nvim",
    "nvim-lua/plenary.nvim",
    "nvim-treesitter/nvim-treesitter",
  },
  keys = {
    {
      "<leader>at",
      "<cmd>CodeCompanionChat Toggle<cr>",
      mode = { "n", "v", "o" },
    },
    {
      "<leader>ac",
      "<cmd>CodeCompanionActions<cr>",
    },
    {
      "<leader>aa",
      "<cmd>CodeCompanionChat Add<cr>",
      mode = { "v" },
    },
  },
  cmd = {
    "CodeCompanion",
    "CodeCompanionChat",
    "CodeCompanionActions",
    "CodeCompanionCmd",
  },

  opts = function()
    return {
      adapters = get_config(vim.g.code_companion_config),

      display = {
        chat = {
          intro_message = "",
          start_in_insert_mode = true,
        },
        action_palette = {
          provider = "telescope",
        },
      },

      strategies = {
        chat = {
          adapter = "chat",

          slash_commands = {
            ["file"] = {
              opts = {
                provider = "telescope",
              },
            },
            ["buffer"] = {
              opts = {
                provider = "telescope",
              },
            },
            ["symbols"] = {
              opts = {
                provider = "telescope",
              },
            },
          },

          keymaps = {
            completion = {
              modes = {
                i = "<C-a>",
              },
            },
          },
        },
        inline = {
          adapter = "inline",
        },
      },
    }
  end,

  config = function(_, opts)
    require("codecompanion").setup(opts)

    local group = vim.api.nvim_create_augroup("9_CodeCompanion", {})

    vim.api.nvim_create_autocmd("User", {
      pattern = "CodeCompanionRequestStarted",
      group = group,
      callback = function()
        local utils = require("_.utils")

        vim.g.code_companion_in_progress = true
        utils.refresh_statusline()
      end,
    })

    vim.api.nvim_create_autocmd("User", {
      pattern = "CodeCompanionRequestFinished",
      group = group,
      callback = function()
        local utils = require("_.utils")

        vim.g.code_companion_in_progress = false
        utils.refresh_statusline()
      end,
    })
  end,
}
