vim.cmd("let g:netrw_liststyle = 3")
local opt = vim.opt -- for conciseness

-- line numbers
opt.relativenumber = true -- show relative line numbers
opt.number = true -- shows absolute line number on cursor line (when relative number is on)

-- tabs & indentation
opt.tabstop = 2 -- 2 spaces for tabs (prettier default)
opt.shiftwidth = 2 -- 2 spaces for indent width
opt.expandtab = true -- expand tab to spaces
opt.autoindent = true -- copy indent from current line when starting new one

-- line wrapping
opt.wrap = false -- disable line wrapping

-- search settings
opt.ignorecase = true -- ignore case when searching
opt.smartcase = true -- if you include mixed case in your search, assumes you want case-sensitive

-- cursor line
opt.cursorline = true -- highlight the current cursor line

-- appearance

-- turn on termguicolors for nightfly colorscheme to work
-- (have to use iterm2 or any other true color terminal)
opt.termguicolors = true
opt.background = "dark" -- colorschemes that can be light or dark will be made dark
opt.signcolumn = "yes" -- show sign column so that text doesn't shift

-- backspace
opt.backspace = "indent,eol,start" -- allow backspace on indent, end of line or insert mode start position

-- clipboard
opt.clipboard:append("unnamedplus") -- use system clipboard as default register

-- split windows
opt.splitright = true -- split vertical window to the right
opt.splitbelow = true -- split horizontal window to the bottom

-- turn off swapfile
opt.swapfile = false

opt.updatetime = 50

opt.hlsearch = false
opt.incsearch = true

opt.scrolloff = 999
opt.fillchars = { eob = " " }

vim.g.python3_host_prog = "/Users/marco/.asdf/shims/python"
vim.g.node_host_prog = "/opt/homebrew/bin/node"

local function split(str, delimiter)
	local result = {}
	for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do
		table.insert(result, match)
	end
	return result
end

local function get_harpoon_indicator(harpoon_entry)
	local array = split(harpoon_entry.value, "/")
	local file = array[#array]
	return file:match("(.+)%..+$") or file
end

-- Function to get Harpoon status for statusline
_G.get_harpoon_status = function()
	local ok, harpoon = pcall(require, "harpoon")
	if not ok then
		return ""
	end

	local list = harpoon:list()
	local items = list.items or {}
	local current_file = vim.fn.expand("%:p")

	local status = {}
	for i, item in ipairs(items) do
		if i > 8 then
			break
		end -- limit to 8 items
		local file_path = item.value
		local indicator = get_harpoon_indicator(item)

		if file_path == current_file then
			table.insert(status, "[" .. indicator .. "]")
		else
			table.insert(status, indicator)
		end
	end

	if #status > 0 then
		return "♥  " .. table.concat(status, " | ") .. " "
	end
	return ""
end

-- Create Vimscript wrapper
vim.cmd([[
function! HarpoonStatus()
	return v:lua.get_harpoon_status()
endfunction
]])

opt.statusline = "%t [%{FugitiveHead()}] %m %= %{HarpoonStatus()} %l,%c %P"
