#!/usr/bin/env bash
# tags: [discovery, session-start, governance]
# description: SessionStart hook. Reads the developer's conversation-language preference (~/.bewith/config.json, resolved via bewith-config.sh) and injects a directive to converse in that language — while keeping ALL written artifacts in English regardless. Mirrors the role hook. Fail-open.
#
# .claude/hooks/conversation-language.sh
#
# The language twin of the capability-awareness (role) hook. Where role filters
# which capabilities get surfaced, this sets the language Claude converses in.
#
# Resolution is delegated to scripts/bewith-config.sh (the single deterministic
# config core — session override → config.language → config.defaultLanguage →
# "en"), so there is exactly one implementation of the fallback chain. If the
# script can't be found, the hook defaults to English and injects nothing
# special — English is the artifact default, so that's the safe no-op.
#
# Hard rule (reinforces feedback_artifact_language_english): the conversation
# may be Hebrew, but every written artifact stays English.
#
# Distribution: registered in .claude-plugin/hooks/hooks.json + .claude/settings.json.
#
# Exit codes: 0 always (fail-open; never blocks).

set -uo pipefail

HOOK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || echo "")"
CONFIG_SH=""
for c in "${CLAUDE_PLUGIN_ROOT:-}/scripts/bewith-config.sh" "${HOOK_DIR}/../../scripts/bewith-config.sh"; do
  [[ -n "$c" && -f "$c" ]] && { CONFIG_SH="$c"; break; }
done

LANG_CODE="en"
[[ -n "$CONFIG_SH" ]] && LANG_CODE="$(bash "$CONFIG_SH" resolve language 2>/dev/null || echo en)"
[[ "$LANG_CODE" == "he" || "$LANG_CODE" == "en" ]] || LANG_CODE="en"

case "$LANG_CODE" in
  he) LANG_NAME="Hebrew";;
  *)  LANG_NAME="English";;
esac

if [[ "$LANG_CODE" == "he" ]]; then
  read -r -d '' CTX <<EOF || true
[bewith conversation language]

Converse with the developer in ${LANG_NAME} (their configured preference). This
governs CHAT ONLY.

ALL written artifacts remain in English regardless of the conversation language:
code, comments, commit messages, PR titles/descriptions, docs, memory files,
tickets, Slack/ClickUp content. When the developer explicitly asks for a Hebrew
phrasing or a translation, provide it — that is chat they will copy elsewhere,
not an artifact being written into a system.

Change with /lang en|he (or /settings · /role for the role filter).
EOF
else
  read -r -d '' CTX <<EOF || true
[bewith conversation language]

Conversation language: English (configured preference). Written artifacts are
English too — no change. Switch the conversation to Hebrew with /lang he; all
artifacts stay English regardless.
EOF
fi

jq -n --arg ctx "$CTX" '{
  hookSpecificOutput: {
    hookEventName: "SessionStart",
    additionalContext: $ctx
  }
}'

exit 0
