#!/bin/bash
# Bridge Codex hook events into the existing VisualHUD Claude hook engine.

set -euo pipefail

INPUT=$(cat)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
ENGINE="${VISUALHUD_ENGINE:-$REPO_ROOT/engine.sh}"
JSON_HELPER="${VISUALHUD_JSON_HELPER:-$REPO_ROOT/scripts/visualhud-json.js}"

if [ ! -f "$ENGINE" ]; then
  exit 0
fi

export VISUALHUD_DEFAULT_THEME="${VISUALHUD_DEFAULT_THEME:-tmnt}"
export VISUALHUD_REAPPLY_DELAY="${VISUALHUD_REAPPLY_DELAY:-0.12}"

PAYLOAD=$(printf '%s' "$INPUT" | node "$JSON_HELPER" codex-payload 2>/dev/null || true)

if [ -z "$PAYLOAD" ]; then
  exit 0
fi

# Codex consumes hook stdout as protocol output. The engine writes terminal
# control sequences to /dev/tty, so keep stdout empty for Codex.
printf '%s' "$PAYLOAD" | bash "$ENGINE" >/dev/null 2>/dev/null || true
