/**
 * Piece state transition logic
 *
 * Handles determining the next movement based on rules-based routing.
 */

import type {
  PieceMovement,
} from '../../models/types.js';

/**
 * Determine next movement using rules-based detection.
 * Returns the next movement name from the matched rule, or null if no rule matched.
 */
export function determineNextMovementByRules(
  step: PieceMovement,
  ruleIndex: number,
): string | null {
  const rule = step.rules?.[ruleIndex];
  if (!rule) {
    return null;
  }
  return rule.next ?? null;
}

/**
 * Extract user-facing prompt from blocked response.
 * Looks for common patterns like "必要な情報:", "質問:", etc.
 */
export function extractBlockedPrompt(content: string): string {
  // Try to extract specific question/info needed
  const patterns = [
    /必要な情報[:：]\s*(.+?)(?:\n|$)/i,
    /質問[:：]\s*(.+?)(?:\n|$)/i,
    /理由[:：]\s*(.+?)(?:\n|$)/i,
    /確認[:：]\s*(.+?)(?:\n|$)/i,
  ];

  for (const pattern of patterns) {
    const match = content.match(pattern);
    if (match?.[1]) {
      return match[1].trim();
    }
  }

  // Return the full content if no specific pattern found
  return content;
}
