import { virtual } from '@guidepup/virtual-screen-reader'

export async function spokenPhraseUntil(
  container: HTMLElement,
  match: (phrase: string) => boolean,
  maxSteps = 20,
): Promise<string> {
  await virtual.start({ container })
  try {
    let phrase = ''
    for (let i = 0; i < maxSteps; i++) {
      await virtual.next()
      phrase = await virtual.lastSpokenPhrase()
      if (match(phrase)) {
        return phrase
      }
    }

    return phrase
  } finally {
    await virtual.stop()
  }
}

