# Path to your oh-my-zsh installation.
export DOTFILES=${DOTFILES:-"$HOME/.dotfiles"}
export ZSH="$DOTFILES/zsh/ohmyzsh"
export ZSH_CUSTOM="$DOTFILES/zsh/custom"

ZSH_THEME="agnoster_dx"

zstyle ':omz:update' mode reminder  # just remind me to update when it's time
# zstyle ':omz:update' frequency 13

# CASE_SENSITIVE="true"
# HYPHEN_INSENSITIVE="true"
# DISABLE_MAGIC_FUNCTIONS="true"
# DISABLE_AUTO_TITLE="true"
# ENABLE_CORRECTION="true"
COMPLETION_WAITING_DOTS="true"
ZSH_DISABLE_COMPFIX="true"
ZOXIDE_CMD_OVERRIDE="cd"

plugins=(
	git
	zsh-autosuggestions
	# Disabled because the highlighting conflicts with autosuggestions
	# history-substring-search
	zsh-vi-mode
	colored-man-pages
	command-not-found
	zsh-autopair
	fast-syntax-highlighting
	zoxide
	mise
	gpg-agent
)

bindkey -M vicmd "k" up-line-or-beginning-search
bindkey -M vicmd "j" down-line-or-beginning-search

HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND="fg=yellow"
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND="fg=red"
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_TIMEOUT=3

ZSH_AUTOSUGGEST_STRATEGY=(history completion)

# Default to insert mode
ZVM_LINE_INIT_MODE=i
# Enable system clipboard
ZVM_SYSTEM_CLIPBOARD_ENABLED=true
# Make p and P use the system clipboard, instead of CUTBUFFER,
# without this you need gp and gP
function zvm_after_lazy_keybindings() {
  bindkey -M vicmd 'p' zvm_paste_clipboard_after
  bindkey -M vicmd 'P' zvm_paste_clipboard_before
  bindkey -M visual 'p' zvm_visual_paste_clipboard
  bindkey -M visual 'P' zvm_visual_paste_clipboard
}

# Fix autopair and zsh-vi-mode compatibility
AUTOPAIR_INHIBIT_INIT=1
zvm_after_init_commands+=('autopair-init')

# Might cause issues?
KEYTIMEOUT=1

fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/completions
fpath+=${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions/src
autoload -Uz compinit && compinit
source "$ZSH/oh-my-zsh.sh"

less_termcap[so]="${fg_bold[black]}${bg[white]}"
export LESS="-F -R -i --incsearch -M"

export PATH="${HOME}/.local/bin:${HOME}/.cargo/bin:$PATH"
export EDITOR=nvim
# export GREP_COLORS="mt=01;32"

alias cl="clear"

if (( $+commands[bat] )); then
	export BAT_THEME="gruvbox-dark"
	alias cat=bat
fi

if (( $+commands[devcontainer] )); then
	dc() {
	  ORIG_DIR="$PWD"
	  while [[ "$PWD" != / ]] ; do
		if find "$PWD"/ -maxdepth 1 -type d -name ".devcontainer" | grep -q ".devcontainer"; then
		  WORKSPACE_FOLDER="$PWD"
		  builtin cd "$ORIG_DIR"

		  if [[ "$1" == "nvim" ]]; then
			  tmux set-option -pt 0 @pane-is-vim 1
			  devcontainer exec --workspace-folder "$WORKSPACE_FOLDER" bash -c "$@"
			  tmux set-option -pt 0 @pane-is-vim 0
		  else
			  devcontainer exec --workspace-folder "$WORKSPACE_FOLDER" $@
		  fi

		  return 0
		else
		  builtin cd ..
		fi
	  done
	  builtin cd "$ORIG_DIR"
	  echo "Unable to find workspace folder"
	  return 1
	}
fi

export PNPM_HOME="/home/tim/.local/share/pnpm"
if [ -d "$PNPM_HOME" ]; then
	case ":$PATH:" in
	  *":$PNPM_HOME:"*) ;;
	  *) export PATH="$PNPM_HOME:$PATH" ;;
	esac
else
	unset PNPM_HOME
fi
if (( $+commands[pnpm] )); then
	source <(pnpm completion zsh)
fi

export FZF_DEFAULT_OPTS='--tmux'
export FZF_DEFAULT_OPTS=$FZF_DEFAULT_OPTS'
  --color=fg:-1,fg+:#83a598,bg:-1,bg+:#262626
  --color=hl:bold:#8ec07c,hl+:#8ec07c,marker:#cc241d
  --color=prompt:#fb4934,spinner:#af5fff,pointer:#83a598,header:#87afaf
  --color=border:#7c6f64,label:#7c6f64
  --border="sharp" --border-label-pos="0" --preview-window="border-sharp"
  --prompt="> " --marker="◆" --pointer=">" --separator="─"
  --scrollbar="│" --info="right"
  --layout="default"'

if (( $+commands[fd] )); then
	export FZF_DEFAULT_COMMAND="fd --type f --strip-cwd-prefix"
	export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
fi

if (( $+commands[fzf] )); then
	# Fix fzf keybind after zsh-vi-mode has loaded
	zvm_after_init_commands+=('source <(fzf --zsh)')

	if (( $+commands[paru] )); then
		function pi {
			paru -Slq | fzf -q "$1" --border-label=" Install package " --multi --preview 'paru -Si {1} --color=always' | xargs -ro paru -S
		}
	fi
fi

ZSH_TMUX_AUTOSTART=true
if (( $+commands[tmux] )); then
	if [[ -z "$TMUX" && "$ZSH_TMUX_AUTOSTART" == "true" && -z "$INSIDE_EMACS" && -z "$EMACS" && -z "$VIM" && -z "$INTELLIJ_ENVIRONMENT_READER" && -z "$ZED_TERM" ]]; then
		if (( $+commands[sesh] )) && (( $+commands[sesh-select] )); then
			sesh-select
		else
			tmux new-session
		fi
		exit
	fi
fi
