export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"

# Source the nvm shell function.
# Handles: manual install ($HOME/.nvm), Homebrew on macOS, and WSL.
# On Windows with nvm-windows, nvm is already an executable in PATH — no sourcing needed.
if [ -s "$NVM_DIR/nvm.sh" ]; then
  . "$NVM_DIR/nvm.sh"
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] && command -v brew &>/dev/null; then
  _nvm_sh="$(brew --prefix nvm 2>/dev/null)/nvm.sh"
  [ -s "$_nvm_sh" ] && . "$_nvm_sh"
  unset _nvm_sh
fi

if [ -f ".nvmrc" ]; then
  _node_version="$(tr -d '[:space:]' < .nvmrc)"
  nvm install "$_node_version"
  nvm use "$_node_version"
  unset _node_version
fi

# Ensure the pnpm shim declared in package.json#packageManager is active via Corepack.
# `corepack enable` is idempotent — safe to run on every cd.
if command -v corepack &>/dev/null; then
  corepack enable pnpm
fi
