<?php

declare(strict_types=1);

namespace Paysera\CheckoutSdk\Service\Provider;

class PaymentMethodIconResolver
{
    private const ICON_EXTENSION = '.svg';
    private const ASSETS_RELATIVE_PATH = '/../../../assets/payment-methods';

    private const SUPPORTED_KEYS = [
        'apple-pay',
        'artea',
        'card-payment',
        'cards',
        'citadele',
        'coop',
        'digiline',
        'google-pay',
        'hire-purchase',
        'ibanez',
        'inbank',
        'lhv',
        'luminor',
        'nordea',
        'pay-next-month',
        'paysera',
        'paysera-wallet',
        'revolut',
        'seb',
        'siauliu-bankas',
        'split-into-parts',
        'swedbank',
        'urbo',
        'wise',
    ];

    private string $baseUrl;

    public function __construct(string $baseUrl = '')
    {
        $this->baseUrl = $baseUrl;
    }

    public function resolve(string $key): string
    {
        if ($this->baseUrl === '') {
            return '';
        }

        if (in_array($key, self::SUPPORTED_KEYS, true) === false) {
            return '';
        }

        if (preg_match('/[\x00-\x1F\x7F]/', $this->baseUrl) === 1) {
            return '';
        }

        return rtrim($this->baseUrl, '/') . '/' . $key . self::ICON_EXTENSION;
    }

    /**
     * @return string[]
     */
    public static function getSupportedKeys(): array
    {
        return self::SUPPORTED_KEYS;
    }

    public static function getSourceDirectory(): string
    {
        $path = realpath(__DIR__ . self::ASSETS_RELATIVE_PATH);

        if ($path === false) {
            return '';
        }

        return $path;
    }
}
