<?php

/*
 * This file is part of the Behat.
 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Behat\Behat\Output\Node\EventListener\Flow;

use Behat\Testwork\Event\Event;
use Behat\Testwork\Output\Formatter;
use Behat\Testwork\Output\Node\EventListener\EventListener;

/**
 * Behat fire only siblings listener.
 *
 * This listener catches all events, but proxies them to further listeners only if they
 * live inside specific event lifecycle (between BEFORE and AFTER events).
 *
 * @author Konstantin Kudryashov <ever.zet@gmail.com>
 */
class FireOnlySiblingsListener implements EventListener
{
    /**
     * @var bool
     */
    private $inContext = false;

    /**
     * Initializes listener.
     *
     * @param string        $beforeEventName
     * @param string        $afterEventName
     */
    public function __construct(
        private $beforeEventName,
        private $afterEventName,
        private readonly EventListener $descendant,
    ) {
    }

    public function listenEvent(Formatter $formatter, Event $event, $eventName)
    {
        if ($this->beforeEventName === $eventName) {
            $this->inContext = true;
        }

        if ($this->inContext) {
            $this->descendant->listenEvent($formatter, $event, $eventName);
        }

        if ($this->afterEventName === $eventName) {
            $this->inContext = false;
        }
    }
}
