<?php

declare (strict_types=1);
/**
 * This file is part of the Nette Framework (https://nette.org)
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
 */
namespace StevenBuehner\sbChurchtoolsGrouphomepage\Vendor\Nette\Mail;

use StevenBuehner\sbChurchtoolsGrouphomepage\Vendor\Nette;
/**
 * Tries multiple mailers in sequence with retries; throws FallbackMailerException only when all attempts fail.
 */
class FallbackMailer implements Mailer
{
    /** @var array<callable(self, SendException, Mailer, Message): void> */
    public array $onFailure = [];
    public function __construct(
        /** @var list<Mailer> */
        private array $mailers,
        private int $retryCount = 3,
        /** in milliseconds */
        private int $retryWaitTime = 1000
    )
    {
    }
    /**
     * Sends email.
     * @throws FallbackMailerException
     */
    public function send(Message $mail): void
    {
        if (!$this->mailers) {
            throw new \StevenBuehner\sbChurchtoolsGrouphomepage\Vendor\Nette\InvalidArgumentException('At least one mailer must be provided.');
        }
        $failures = [];
        for ($i = 0; $i < $this->retryCount; $i++) {
            if ($i > 0) {
                usleep($this->retryWaitTime * 1000);
            }
            foreach ($this->mailers as $mailer) {
                try {
                    $mailer->send($mail);
                    return;
                } catch (SendException $e) {
                    $failures[] = $e;
                    \StevenBuehner\sbChurchtoolsGrouphomepage\Vendor\Nette\Utils\Arrays::invoke($this->onFailure, $this, $e, $mailer, $mail);
                }
            }
        }
        $e = new FallbackMailerException('All mailers failed to send the message.');
        $e->failures = $failures;
        throw $e;
    }
    public function addMailer(Mailer $mailer): static
    {
        $this->mailers[] = $mailer;
        return $this;
    }
}