local source_mapping = {
	buffer = "[Buffer]",
	nvim_lsp = "[LSP]",
	nvim_lua = "[Lua]",
	luasnip = "[Snip]",
	path = "[Path]",
}
return {
	"hrsh7th/nvim-cmp",
	dependencies = {
		"hrsh7th/cmp-nvim-lsp",
		"hrsh7th/cmp-buffer",
		"hrsh7th/cmp-path",
		"saadparwaiz1/cmp_luasnip",
		{
			"L3MON4D3/LuaSnip",
			lazy = true,
			version = "v2.*",
			build = "make install_jsregexp",
			opts = {
				history = true,
				delete_check_events = "TextChanged",
			},
			dependencies = { "rafamadriz/friendly-snippets" },
			config = function()
				require("plugins.snippets")
			end,
			keys = {
				{
					"<C-K>",
					function()
						require("luasnip").expand()
					end,
					silent = true,
				},
				{
					"<C-L>",
					function()
						require("luasnip").jump(1)
					end,
					silent = true,
				},
				{
					"<C-J>",
					function()
						require("luasnip").jump(-1)
					end,
					silent = true,
				},
			},
		},
	},
	config = function()
		require("cmp").setup({
			preselect = require("cmp").PreselectMode.None,
			experimental = {
				ghost_text = false,
			},

			confirmation = {
				default_behavior = require("cmp").ConfirmBehavior.Replace,
			},
			completion = {
				completeopt = "menu,menuone,noinsert,noselect",
				keyword_length = 2,
			},
			mapping = require("cmp").mapping.preset.insert({
				["<C-b>"] = require("cmp").mapping.scroll_docs(-4),
				["<C-f>"] = require("cmp").mapping.scroll_docs(4),
				["<C-Space>"] = require("cmp").mapping.complete(),
				["<C-e>"] = require("cmp").mapping.abort(),
				["<CR>"] = require("cmp").mapping.confirm({ select = false }),
				["<C-y>"] = require("cmp").mapping.confirm({ select = true }),
			}),
			snippet = {
				expand = function(args)
					require("luasnip").lsp_expand(args.body)
				end,
			},
			formatting = {
				format = function(entry, vim_item)
					vim_item.menu = source_mapping[entry.source.name] or "[Unknown]"
					return vim_item
				end,
			},
			sources = require("cmp").config.sources({
				{ name = "nvim_lsp", priority = 1000 },
				{ name = "luasnip", priority = 750 },
				{ name = "path", priority = 500 },
			}, {
				{ name = "buffer" },
			}),
		})
	end,
}
