local cmp = require("cmp")

cmp.setup({
	enabled = function()
		return vim.api.nvim_get_option_value("buftype", { buf = 0 }) ~= "prompt" or require("cmp_dap").is_dap_buffer()
	end,
	snippet = {
		-- REQUIRED - you must specify a snippet engine
		expand = function(args)
			-- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
			-- require("luasnip").lsp_expand(args.body) -- For `luasnip` users.
			-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
			require("snippy").expand_snippet(args.body) -- For `snippy` users.
		end,
	},
    preselect = cmp.PreselectMode.None,
	mapping = {
		["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
		["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
		["<C-l>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
		["<C-CR>"] = cmp.mapping(cmp.mapping.confirm({ select = true })),
		["<CR>"] = cmp.mapping(cmp.mapping.confirm({ select = false })),
		["<C-j>"] = cmp.mapping(function()
			if cmp.visible() then
				cmp.select_next_item()
			end
		end, { "i", "s" }),
		["<C-k>"] = cmp.mapping(function()
			if cmp.visible() then
				cmp.select_prev_item()
			end
		end, { "i", "s" }),
		["<C-y>"] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
		["<C-e>"] = cmp.mapping({
			i = cmp.mapping.abort(),
			c = cmp.mapping.close(),
		}),
	},
	-- completion = { autocomplete = false },
	sources = cmp.config.sources({
		{ name = "nvim_lsp" }, -- { name = 'vsnip' }, -- For vsnip users.
		{ name = "snippy" }, -- For snippy users.
		-- { name = 'luasnip' }, -- For luasnip users.
		{ name = "neorg" },
	}, {
		{ name = "buffer" },
	}),
})

-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline("/", { sources = { { name = "buffer" } } })

cmp.setup.filetype({ "dap-repl", "dapui_watches", "dapui_hover" }, {
	sources = {
		{ name = "dap" },
	},
})

cmp.setup.filetype({ 'markdown' }, {
    completion = { autocomplete = false }
})

-- require("cmp").setup.buffer({
-- 	sources = {
-- 		{ name = "buffer" },
-- 		{ name = "neorg" },
-- 	},
-- })

-- Look into adding https://github.com/rcarriga/cmp-dap
