# set the directory we want to store zinit and plugins
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.locale/share}/zinit/zinit.git"

# Download Zinit
if [ ! -d "$ZINIT_HOME" ]; then
  mkdir -p "$(dirname $ZINIT_HOME)"
  git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
fi

ZVM_INIT_MODE=sourcing
# Fix zsh-vi-mode conflicts with fzf
function zvm_after_init() {
  source <(fzf --zsh)
}

# Source zsh, alias
source "${ZINIT_HOME}/zinit.zsh"
source ~/.aliases.zsh
source ~/.plugins.zsh
source ~/.export.zsh
# https://github.com/jungunn/fzf-git.sh
# source ~/scripts/fzf-git.sh
# you should unse
# source /usr/share/zsh/plugins/zsh-you-should-use/you-should-use.plugin.zsh
# Set up fzf key bindings and fuzzy completion
## hydroxide
#source ~/scripts/hydroxide.sh

# env aur helper
if command -v paru &> /dev/null; then
  export AUR_HELPER="paru"
elif command -v yay &> /dev/null; then
  export AUR_HELPER="yay"
else
  export AUR_HELPER="pacman"  # Fallback to pacman
fi
#export AUR_HELPER="paru"

# Load completions
autoload -U compinit && compinit

## zinit cdreplay -q
eval "$(oh-my-posh init zsh --config $HOME/.config/ohmyposh/zen.toml)"

#eval "$(ssh-agent -s)"
#if ! pgrep -u "$USER" ssh-agent > /dev/null; then
#    ssh-agent > ~/.ssh-agent.env
#fi
#if [[ -z "$SSH_AUTH_SOCK" ]]; then
#    source ~/.ssh-agent.env > /dev/null
#fi

# pyenv
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"

# Bindkey
# bindkey -e
# bindkey '^p' history-search-backward
# bindkey '^n' history-search-forward

# History
HISTSIZE=5000
HISTFILE=~/.zsh_history
SAVEHIST=$HISTSIZE
HISTDUP=erase
setopt appendhistory
setopt sharehistory
setopt hist_ignore_space
setopt hist_ignore_all_dups
setopt hist_save_no_dups
setopt hist_ignore_dups
setopt hist_find_no_dups

# Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'

# Shell integration
eval "$(fzf --zsh)"
eval "$(zoxide init --cmd cd zsh)"
eval "$(thefuck --alias)"
eval "$(navi widget zsh)"
# pyenv https://github.com/pyenv/pyenv-virtualenv
# eval "$(pyenv init --path)"
eval "$(pyenv init -)"
# eval "$(pyenv virtualenv-init -)"
# starship
# eval "$(starship init zsh)"

_fzf_comprun() {
  local command=$1
  shift

  case "$command" in
    cd)             fzf --preview 'eza --tree --color=always {} | head -200' "$@" ;;
    export|unset)   fzf --preview "eval 'echo \$' {}"   "$@" ;;
    ssh)            fzf --preview 'dig {}'              "$@" ;;
    *)              fzf --preview "--preview 'bat -n --color=always --line-range :10 {}'" "$@" ;;
  esac
}

function y() {
  local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
  yazi "$@" --cwd-file="$tmp"
  if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
    builtin cd -- "$cwd"
  fi
  rm -f -- "$tmp"
}

# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \
  zdharma-continuum/zinit-annex-as-monitor \
  zdharma-continuum/zinit-annex-bin-gem-node \
  zdharma-continuum/zinit-annex-patch-dl \
  zdharma-continuum/zinit-annex-rust

### End of Zinit's installer chunk

#  . "$HOME/.local/bin/env"
# In .zshrc, after zinit loads
function zvm_after_init() {
  # Re-bind ctrl+space for autosuggestions if needed
  bindkey '^@' autosuggest-accept
}
export PATH="$HOME/.local/bin:$PATH"

# opencode
export PATH=/home/liph/.opencode/bin:$PATH
