<?php
/**
 * BookiFlex Payment Extension
 *
 * @package    BookiFlex
 * @subpackage Payment
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */


declare(strict_types=1);
namespace BookiFlex\Extensions\Payment\Service\Processor;

use BookiFlex\Core\Contract\Payment\PaymentProcessorInterface;

defined( 'ABSPATH' ) || exit;

/**
 * Resolves the right payment processor for a gateway name.
 *
 * Additional processors (e.g. WooCommerce) register themselves via
 * {@see addProcessor()} and are consulted before the default one.
 */
class PaymentProcessorResolver
{
    /** @var PaymentProcessorInterface[] */
    private array $processors = [];

    public function __construct(
        private readonly PaymentProcessorInterface $default,
    ) {
    }

    public function addProcessor(PaymentProcessorInterface $processor): void
    {
        $this->processors[] = $processor;
    }

    public function resolve(string $gatewayName): PaymentProcessorInterface
    {
        foreach ($this->processors as $processor) {
            if ($processor->supports($gatewayName)) {
                return $processor;
            }
        }

        return $this->default;
    }
}