if [ -d "$HOME/.local/bin" ] ; then
    prepend_path "$HOME/.local/bin"
fi

if [ -d "$HOME/.cargo/bin" ]; then
    prepend_path "$HOME/.cargo/bin"
fi

if [ -d "/opt/zig/bin" ]; then
  prepend_path "/opt/zig/bin"
fi

ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"

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

source "${ZINIT_HOME}/zinit.zsh"

ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#928374,bg=none"
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-completions
zinit light chisui/zsh-nix-shell
zinit light Aloxaf/fzf-tab

zinit snippet OMZP::git

bindkey -v

autoload -z edit-command-line
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search

zle -N edit-command-line
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search

bindkey "^[[A" up-line-or-beginning-search
if [[ -n "${terminfo[kcuu1]}" ]]; then
  bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
fi

bindkey "^[[B" down-line-or-beginning-search
if [[ -n "${terminfo[kcud1]}" ]]; then
  bindkey "${terminfo[kcud1]}" down-line-or-beginning-search
fi

bindkey -M viins '^[[1;3C' forward-word
bindkey -M viins '^[[1;3D' backward-word

bindkey -M viins '^[e' edit-command-line
bindkey -M vicmd '^[e' edit-command-line

bindkey -M viins '^[^?' backward-delete-word

export HISTSIZE=99999
export HISTFILE=~/.zsh_history
export SAVEHIST=$HISTSIZE
export 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

test -r ~/.dircolors && eval $(dircolors ~/.dircolors)

zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'lsd --color=always --icon=always $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'lsd --color=always --icon=always $realpath'
zstyle ':fzf-tab:complete:systemctl-*:*' fzf-preview 'SYSTEMD_COLORS=1 systemctl status $word'

function files() {
    thunar $@ &>/dev/null & disown
}

function onbg() {
    $@ &>/dev/null & disown
}

function openbg() {
    xdg-open $@ &>/dev/null & disown
}

function open() {
    devour xdg-open $@
}

function yy() {
    local tmp="$(mktemp -t "yazi-cwd.XXXXXX")"

    yazi "$@" --cwd-file="$tmp"

    local cwd="$(cat -- "$tmp")"

    if [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
        cd "$cwd"
    fi

    rm -f -- "$tmp"
}

function realpath() {
    local reqp="$1"
    /usr/bin/realpath -m -L "$(eval "echo $reqp")"
}

function smrt_pd() {
    local reqp=$(realpath $1)

    while IFS= read -r entry; do
        local entryp=$(realpath `echo "$entry" | cut -f2`)
        local entryn=$(echo "$entry" | cut -f1)

        if [[ "$entryp" = "$reqp" ]]; then
            pd +$entryn
            return 0
        fi
    done < <(dirs -p -v -l)

    pd $reqp
}

function errcho() {
    >&2 echo "$@"
}

function zmatch() {
    local matches="$(zoxide query $@)"

    if [[ -z "$matches" ]]; then
        errcho "Not found: $dir"
        return 1
    fi

    echo "$matches"
    return 0
}

function zpd() {
    local dir="$@"

    if [[ -z "$dir" ]]; then
        local matches="$(zmatch -i 2>/dev/null)"

        if [[ -z "$matches" ]]; then
            errcho "Not found: $dir"
            return 0
        fi

        smrt_pd "$matches"

        return 0
    fi

    case "$dir" in
        -)
            zpd "$OLDPWD"
        ;;
        /*|~*)
            smrt_pd "$dir"
            return 0
        ;;
        *)
            if [[ -d "$dir" ]]; then
                smrt_pd "$dir"
                return 0
            fi

            local matches="$(zmatch $dir 2>/dev/null)"

            if [[ -z "$matches" ]]; then
                errcho "Not found: $dir"
                return 1
            fi

            if [[ $(echo "$matches" | wc -l) -eq 1 ]]; then
                smrt_pd "$matches"
                return 0;
            fi

            matches="$(echo $matches | fzf)"

            if [[ -z "$matches" ]]; then
                errcho "Not found: $dir"
                return 1
            fi

            smrt_pd "$matches"
        ;;
    esac

    return 0
}

function pd() {
    pushd "$@" &>/dev/null && ds
}

function ppd() {
    popd "$@" &>/dev/null && ds
}

function pkill() {
    kill "$@" `psf`
}

function cd() {
    local reqp

    if [[ -z "$1" ]]; then
        reqp=$HOME
    else
        reqp=$(realpath $1)
    fi

    while IFS= read -r entry; do
        local entryp=$(realpath `echo "$entry" | cut -f2`)
        local entryn=$(echo "$entry" | cut -f1)

        if [[ "$entryp" = "$reqp" ]]; then
            ppd &>/dev/null
            pd +$(($entryn - 1))
            return 0
        fi
    done < <(dirs -p -v -l | tail -n +2)

    builtin cd $1 &>/dev/null

    local success="$?"

    if [[ $success -eq 0 ]]; then
      if [[ "$PWD" != "$(realpath $PWD)" ]]; then
        builtin cd `realpath $PWD` &>/dev/null
      fi

      ds
    else
      errcho "failed to cd into $reqp"
    fi
}

function ds() {
  dirs -p -v $@
}

function rwhich() {
    readlink -f `which "$@"`
}

alias ..='cd ..'
alias ...='cd ../..'

alias z='cd'

alias l='lsd -Al'
alias ls='lsd'

alias tree='lsd --tree'

alias df='duf'
alias du='dust'

alias psf="ps -ef | fzf | awk '{ print \$2 }'"

alias cat='bat -p -P'
alias clip='xclip -sel clip'

alias mv='mv -i'
alias rm='rip'

alias su='sudo \su -'

alias icat="wezterm imgcat"

alias ffetch="fastfetch"

WORDCHARS=''

autoload -Uz compinit && compinit
zinit cdreplay -q

eval "$(fzf --zsh)"
eval "$(zoxide init zsh --cmd zxd)"
eval "$(starship init zsh)"

fastfetch
