function create_dir() [ ! -d "$1" ] && mkdir -p "$1"
function safe_source() [ -f "$1" ] && source "$1"
function zsh_add_plugin() {
    PLUGIN_NAME=$(echo $1 | cut -d "/" -f 2)

    [ ! -d "$HOME/.local/share/zsh/plugins/$PLUGIN_NAME" ] &&
    git clone "https://github.com/$1.git" "$HOME/.local/share/zsh/plugins/$PLUGIN_NAME"

    safe_source "$HOME/.local/share/zsh/plugins/$PLUGIN_NAME/$PLUGIN_NAME.plugin.zsh"
    safe_source "$HOME/.local/share/zsh/plugins/$PLUGIN_NAME/$PLUGIN_NAME.zsh"
}

export DOTS="$HOME/docs/repos/dots"

export VISUAL="nvim"
export EDITOR="nvim"

typeset -U path
path=("$HOME/.pyenv/versions/3.12.9/bin" $path)
path=("$HOME/bin" $path)

if (( $+commands[npm] )); then
    export npm_config_prefix="$HOME/.local/npm"
    path+="$HOME/.local/npm/bin"
fi

if (( $+commands[go] )); then
    path+="$HOME/go/bin"
fi

if (( $+commands[fzf] )); then
    source <(fzf --zsh)
fi

if (( $+commands[starship] )); then
    source <(starship init zsh)
else
    [ ! -d "$HOME/.local/share/zsh/plugins/pure" ] &&
        git clone "https://github.com/sindresorhus/pure" "$HOME/.local/share/zsh/plugins/pure"

    fpath+="$HOME/.local/share/zsh/plugins/pure"
    safe_source "$HOME/.local/share/zsh/plugins/pure/pure.zsh"
fi

zsh_add_plugin "mattmc3/ez-compinit"
zstyle ":completion:*" list-colors ${(s.:.)LS_COLORS}
zstyle ":completion:*" matcher-list "m:{a-z}={A-Za-z}"
zstyle ":completion:*" menu no
zstyle ":completion:*:descriptions" format "[%d]"
zstyle ":completion:*:git-checkout:*" sort false
zstyle ":fzf-tab:complete:cd:*" fzf-preview 'eza -1 --group-directories-first --color=always --icons=always $realpath'

create_dir "$HOME/.local/state/zsh"
HISTFILE="$HOME/.local/state/zsh/history"
HISTSIZE=65536
SAVEHIST=$HISTSIZE

setopt AUTO_CD
setopt HIST_IGNORE_DUPS
setopt HIST_VERIFY
setopt INC_APPEND_HISTORY
setopt MULTIOS
setopt NO_BEEP
setopt RM_STAR_SILENT

stty stop undef
zle_highlight=("paste:none")

zsh_add_plugin "Aloxaf/fzf-tab"
zsh_add_plugin "zdharma-continuum/fast-syntax-highlighting"
zsh_add_plugin "zsh-users/zsh-autosuggestions"
zsh_add_plugin "zsh-users/zsh-completions"

ZVM_INIT_MODE=sourcing
ZVM_VI_INSERT_ESCAPE_BINDKEY=jk
zsh_add_plugin "jeffreytse/zsh-vi-mode"
ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BLINKING_BEAM

autoload -z edit-command-line
zle -N edit-command-line
bindkey "^V" edit-command-line
bindkey "^Y" autosuggest-execute
bindkey "^F" fzf-cd-widget

###############################################################################
# ALIASES                                                                     #
###############################################################################

if [[ -f /etc/os-release ]]; then
    source /etc/os-release

    if [[ "$ID_LIKE" == *"arch"* ]]; then
        alias p="sudo pacman -S"
        alias pu="sudo pacman -Syu"
    elif [[ "$ID" == "debian" || "$ID_LIKE" == *"debian"* ]]; then
        alias p="sudo apt install"
        alias pu="sudo apt update && sudo apt upgrade"
    fi
fi

function cc() python -c "from math import *; print($*)"
alias cc="noglob cc"

alias vi="nvim"

alias l="eza -x --group-directories-first --icons always"
alias la="l -a"

alias ll="l -lbh --git --no-time"
alias lla="ll -a"

alias lt="ll --tree"
alias lta="lt -a --ignore-glob=".git""

alias s="kitty +kitten ssh"

# vim: filetype=zsh