#!/bin/zsh

# Speed debugging
# zmodload zsh/zprof

if [[ "$(locale LC_CTYPE)" == "UTF-8" ]]; then
    setopt COMBINING_CHARS
fi

# Disable the log builtin, so we don't conflict with /usr/bin/log
disable log

# Save command history
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=99999999
SAVEHIST=$HISTSIZE

# Variables
export CASE_SENSITIVE="true"
export EDITOR=nvim
export GIT_EDITOR=nvim
export PATH="${HOME}/.local/bin/:${HOME}/go/bin:${HOME}/.cargo/bin:${PATH}"

# export DOCKER_DEFAULT_PLATFORM=linux/amd64

# Enable substitution in the prompt.
setopt prompt_subst

function git_branch_name() {
    branch=$(git symbolic-ref HEAD 2> /dev/null | sed 's/refs\/heads\///g')
  if [[ $branch != "" ]]; then
    echo "- ${branch} "
  fi
}

PROMPT='%{$fg[blue]%}%1~ %{$fg[magenta]%}$(git_branch_name)%{$fg[green]%}>%{$reset_color%} '

# Set some options if running on mac
if [[ $(uname) == 'Darwin' ]]; then
    export HOMEBREW_NO_AUTO_UPDATE=1

    export PATH="/opt/homebrew/lib/ruby/gems/3.3.0:$PATH"
    export PATH="/opt/homebrew/Cellar/ruby/3.3.0/lib/ruby/gems/3.3.0:$PATH"


    eval "$(/opt/homebrew/bin/brew shellenv)"
fi

autoload -Uz compinit
if [[ -f "${ZDOTDIR}/.zcompdump" && "${ZDOTDIR}/.zcompdump" -nt "${ZDOTDIR}/.zshrc" ]]; then
  compinit
else
  compinit -C
fi

# Auto complete case insensitive
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'

# Load colors
autoload -U colors && colors

# Automatically cd into typed directory.
setopt autocd

# Useful support for interacting with Terminal.app or other terminal programs
[ -r "/etc/zshrc_$TERM_PROGRAM" ] && . "/etc/zshrc_$TERM_PROGRAM"

# Load Git completion
zstyle ':completion:*:*:git:*' script "${ZDOTDIR}/.zsh/git-completion.bash"
fpath=(~/.zsh $fpath)

# vi mode
bindkey -v
export KEYTIMEOUT=1

# Edit line in vim with ctrl-e:
autoload edit-command-line; zle -N edit-command-line
bindkey '^e' edit-command-line

# Go up directory structures
up () {
    cd $(printf "%0.0s../" $(seq 1 $1));
}

# cd with fzf
function cdf () {
    cd $(find . -type d -print | fzf)
}

# Android dev config
export ANDROID_HOME="$HOME/Android/Sdk"
if [[ $(uname) == 'Darwin' ]]; then
    export ANDROID_HOME="$HOME/Library/Android/sdk"
    export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"
fi
export PATH="${PATH}:${ANDROID_HOME}/platform-tools:$ANDROID_HOME/cmdline-tools/latest/bin"

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"

# Load nvm on load to speed up shell init
function nvm() {
    unset -f nvm node npm npx
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    nvm "$@"
}
function node { nvm; node "$@" }
function npm { nvm; npm "$@" }
function npx { nvm; npx "$@" }

# Load external aliases
source ~/.config/aliasrc

# Source fzf for Ctrl+r
source <(fzf --zsh)

if [ -f "$HOME/.cargo/env" ]; then
    . "$HOME/.cargo/env"
fi

if [ -f "$HOME/.config/zsh/.api-keys" ]; then
    source "$HOME/.config/zsh/.api-keys"
fi

export PATH=~/.config/composer/vendor/bin:$PATH

# Speed debugging
# zprof