#!/usr/bin/env zsh

# Log functions [[[
function log(){ print -P "[%F{cyan}ZUNIT%f] %F{$2}$1%f"; }
function error(){ print -P "$(log "ERROR" "red") $1"; }
function info(){ print -P "$(log "INFO" "green") $1"; }
function warn(){ print -P "$(log "WARN" "yellow") $1"; }
# ]]]
# Create Zunit test environment [[[
GIT_REPO=$(git rev-parse --show-toplevel)
typeset -gAH  ZINIT;
ZINIT[HOME_DIR]="$GIT_REPO/tests/_support/zunit";    ZINIT[BIN_DIR]=$ZINIT[HOME_DIR]/zinit.git;
ZINIT[COMPLETIONS_DIR]=$ZINIT[HOME_DIR]/completions; ZINIT[SNIPPETS_DIR]=$ZINIT[HOME_DIR]/snippets;
ZINIT[ZCOMPDUMP_PATH]=$ZINIT[HOME_DIR]/zcompdump;    ZINIT[PLUGINS_DIR]=$ZINIT[HOME_DIR]/plugins;
ZPFX=$ZINIT[HOME_DIR]/polaris;

rm -rf $ZINIT[HOME_DIR]
mkdir -p $ZINIT[HOME_DIR]
git diff > $ZINIT[HOME_DIR]/unstaged.diff
info 'creating test env'
{
  git clone \
    --dissociate \
    --local \
    --no-hardlinks \
    --reference "$GIT_REPO" \
    "$GIT_REPO" \
    "$GIT_REPO"/tests/_support/zunit/zinit.git
  if [[ -s $ZINIT[HOME_DIR]/unstaged.diff ]] {
    git -C $ZINIT[BIN_DIR] apply $ZINIT[HOME_DIR]/unstaged.diff
  }
  info 'installing zinit'
  command chmod g-rwX $ZINIT[HOME_DIR]
  zcompile $ZINIT[BIN_DIR]/zinit.zsh
  info 'successfully created zunit env'
} || error 'failed to create zunit env'

source $ZINIT[BIN_DIR]/zinit.zsh \
  && autoload -Uz _zinit \
  && (( ${+_comps} )) \
  && _comps[zinit]=_zinit
# ]]]
# Install Annexes [[[
info 'installing test dependencies'
zinit depth'1' light-mode for @zdharma-continuum/zinit-annex-{'bin-gem-node','binary-symlink','default-ice'}
# ]]]

# vim:ft=zsh:sw=2:sts=2:et:foldmarker=[[[,]]]:foldmethod=marker
