/** Must match backend `internal/game/adventure_log_markers.go` */
export const AH_ENC_PREFIX = '__AH_ENC__';
export const AH_BAT_PREFIX = '__AH_BAT__';

export type ParsedAdventureLine =
  | { type: 'encounter'; title: string }
  | { type: 'battle'; text: string }
  | { type: 'plain'; text: string };

export function parseAdventureLogLine(raw: string): ParsedAdventureLine {
  if (raw.startsWith(AH_ENC_PREFIX)) {
    return { type: 'encounter', title: raw.slice(AH_ENC_PREFIX.length) };
  }
  if (raw.startsWith(AH_BAT_PREFIX)) {
    return { type: 'battle', text: raw.slice(AH_BAT_PREFIX.length) };
  }
  return { type: 'plain', text: raw };
}

export function shouldSuppressThoughtBubble(raw: string): boolean {
  return raw.startsWith(AH_ENC_PREFIX) || raw.startsWith(AH_BAT_PREFIX);
}

/** Thought bubble: hide for combat grouping lines; show roadside thoughts and plain social lines. */
export function isAdventureLogEncounterCode(code: string | undefined): boolean {
  return code === 'encountered_enemy' || code === 'log.encountered_enemy';
}

export function isAdventureLogCombatCode(code: string | undefined): boolean {
  if (!code) return false;
  if (code === 'combat_swing') return true;
  return code.startsWith('log.combat.');
}

/** Meet chat is shown only via `hero_meet_line` → meet bubbles; skip duplicate thought bubble. */
export function isAdventureLogHeroMeetCode(code: string | undefined): boolean {
  return code === 'log.hero_meet.player_said' || code === 'log.hero_meet.scripted_line';
}

export function shouldSuppressThoughtBubblePayload(p: {
  message?: string;
  event?: { code?: string };
}): boolean {
  const msg = p.message ?? '';
  if (msg.startsWith(AH_ENC_PREFIX) || msg.startsWith(AH_BAT_PREFIX)) return true;
  if (isAdventureLogEncounterCode(p.event?.code)) return true;
  if (isAdventureLogCombatCode(p.event?.code)) return true;
  if (isAdventureLogHeroMeetCode(p.event?.code)) return true;
  return false;
}
