<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Retry;

use PpPhp\Core\Platform\Event\Retry\Contracts\RetryPolicyInterface;
use PpPhp\Core\Platform\Event\Retry\Policy\ExponentialBackoffPolicy;
use PpPhp\Core\Platform\Event\Retry\Policy\FixedDelayRetryPolicy;
use PpPhp\Core\Platform\Event\Retry\Policy\ImmediateRetryPolicy;
use PpPhp\Core\Platform\Event\Retry\Policy\LinearRetryPolicy;

final class RetryPolicyRegistry
{
    private array $policies = [];

    public function __construct()
    {
        $this->register(new ImmediateRetryPolicy());
        $this->register(new FixedDelayRetryPolicy());
        $this->register(new LinearRetryPolicy());
        $this->register(new ExponentialBackoffPolicy());
    }

    public function register(RetryPolicyInterface $policy): void { $this->policies[$policy->name()] = $policy; }
    public function get(string $name): ?RetryPolicyInterface { return $this->policies[$name] ?? null; }
    public function default(): RetryPolicyInterface { return $this->policies['immediate']; }
    public function names(): array { return array_keys($this->policies); }
}
