export const HOOK_EVALUATION_OPEN_TAG = '<evaluate>'
export const HOOK_EVALUATION_CLOSE_TAG = '</evaluate>'

export function parseHookEvaluationInfoText(raw: unknown): string | null {
  const text = String(raw ?? '').trim()
  if (!text)
    return null
  if (
    !text.startsWith(HOOK_EVALUATION_OPEN_TAG)
    || !text.endsWith(HOOK_EVALUATION_CLOSE_TAG)
  ) {
    return null
  }

  const start = HOOK_EVALUATION_OPEN_TAG.length
  const end = text.length - HOOK_EVALUATION_CLOSE_TAG.length
  if (end < start)
    return null
  return text.slice(start, end).trim()
}

export function formatHookEvaluationInfoText(raw: unknown): string {
  const text = String(raw ?? '').trim()
  if (!text)
    return ''
  if (parseHookEvaluationInfoText(text) != null)
    return text
  return `${HOOK_EVALUATION_OPEN_TAG}\n${text}\n${HOOK_EVALUATION_CLOSE_TAG}`
}
