# Orca daemon zsh shell-ready wrapper
if [[ "${ORCA_SHELL_STARTUP_IDENTITY:-0}" == "1" ]]; then
  unset ORCA_SHELL_STARTUP_IDENTITY
  printf "\033]777;orca-shell-start:%s\007" "$$"
fi
# Why: capture the runtime wrapper dir before it is unset below. On WSL this
# file is generated with a Windows path but sourced via /mnt/c, so the baked
# literal is unusable there and ZDOTDIR must be restored from this value.
# Derive it from the file being sourced (%x, zsh's internal script name) rather
# than the env-imported $ZDOTDIR: zsh corrupts environment values whose UTF-8
# bytes fall in its 0x84-0x9D token range (e.g. a non-ASCII Windows username
# such as a Korean login), which would make the self-check below fail and fall
# back to the unusable baked literal, so the user's .zshrc never loads (#8003).
# %x is not subject to that corruption; keep $ZDOTDIR as a fallback for the
# rare shell where %x prompt expansion yields nothing.
_orca_wrapper_zdotdir_self="${${(%):-%x}:h}"
if [[ -z "${_orca_wrapper_zdotdir_self:-}" ]]; then
  _orca_wrapper_zdotdir_self="${ZDOTDIR:-}"
fi
while [[ "${_orca_wrapper_zdotdir_self:-}" == */ ]]; do
  _orca_wrapper_zdotdir_self="${_orca_wrapper_zdotdir_self%/}"
done
_orca_spawn_orig_zdotdir="${ORCA_ORIG_ZDOTDIR:-}"
_orca_user_zdotdir="${_orca_spawn_orig_zdotdir:-$HOME}"
_orca_zshenv_source_dir="${ORCA_ZSHENV_SOURCE_DIR:-$HOME}"
_orca_zshenv_path=""
unset ORCA_ZSHENV_SOURCE_DIR

# Normalize fallback and source roots before reading user .zshenv so nested
# Orca PTYs never source another Orca wrapper recursively.
while [[ "${_orca_user_zdotdir}" == */ ]]; do
  _orca_user_zdotdir="${_orca_user_zdotdir%/}"
done
case "${_orca_user_zdotdir}" in
  ""|*/shell-ready/zsh) _orca_user_zdotdir="$HOME" ;;
esac
while [[ "${_orca_zshenv_source_dir}" == */ ]]; do
  _orca_zshenv_source_dir="${_orca_zshenv_source_dir%/}"
done
case "${_orca_zshenv_source_dir}" in
  ""|*/shell-ready/zsh) _orca_zshenv_source_dir="$HOME" ;;
esac

# Why: source at wrapper top level, not in a function/subshell, so .zshenv
# exports, functions, path/fpath typesets, and zsh options keep normal scope.
unset ZDOTDIR
if [[ -n "${_orca_zshenv_source_dir:-}" && -f "${_orca_zshenv_source_dir}/.zshenv" ]]; then
  _orca_zshenv_path="${_orca_zshenv_source_dir}/.zshenv"
fi
if [[ -n "${_orca_zshenv_path:-}" ]]; then
  source "${_orca_zshenv_path}"
fi

_orca_discovered_zdotdir="${ZDOTDIR:-}"

while [[ "${_orca_discovered_zdotdir}" == */ ]]; do
  _orca_discovered_zdotdir="${_orca_discovered_zdotdir%/}"
done

case "${_orca_discovered_zdotdir}" in
  *[![:space:]]*) ;;
  *) _orca_discovered_zdotdir="" ;;
esac

if [[ -n "${_orca_discovered_zdotdir}" && ! -d "${_orca_discovered_zdotdir}" ]]; then
  [[ "${ORCA_DEBUG:-0}" == "1" ]] && echo "[orca-shell-ready] Discovered ZDOTDIR '${_orca_discovered_zdotdir}' does not exist, falling back" >&2
  _orca_discovered_zdotdir=""
fi

export ORCA_ORIG_ZDOTDIR="${_orca_discovered_zdotdir:-${_orca_user_zdotdir:-$HOME}}"

while [[ "${ORCA_ORIG_ZDOTDIR}" == */ ]]; do
  ORCA_ORIG_ZDOTDIR="${ORCA_ORIG_ZDOTDIR%/}"
done

case "${ORCA_ORIG_ZDOTDIR}" in
  ""|*/shell-ready/zsh) export ORCA_ORIG_ZDOTDIR="$HOME" ;;
esac

# Why: use :- after user .zshenv — a pathological unset under set -u must not
# abort the wrapper; empty falls through to the baked-literal branch.
if [[ -n "${_orca_wrapper_zdotdir_self:-}" && -f "${_orca_wrapper_zdotdir_self:-}/.zshenv" ]]; then
  export ZDOTDIR="${_orca_wrapper_zdotdir_self:-}"
else
  export ZDOTDIR='<WRAPPER_ROOT>/zsh'
fi
unset _orca_spawn_orig_zdotdir _orca_user_zdotdir _orca_zshenv_source_dir _orca_zshenv_path _orca_discovered_zdotdir _orca_wrapper_zdotdir_self
