<?php

namespace Autogedal\TbiBank\Plugin;

use Autogedal\TbiBank\Model\Environment;
use TbiBank\FusionPay\Helper\Data;

class HelperConfig
{
    private Environment $environment;

    public function __construct(Environment $environment)
    {
        $this->environment = $environment;
    }

    public function afterGetConfigValue(Data $subject, $result, $value)
    {
        $path = (string) $value;
        if ($this->environment->getEffectivePath($path) === $path) {
            if ($path === 'tbi_settings/general_settings/encryption_key') {
                return $this->environment->decodeKeyValue((string) $result);
            }
            return $result;
        }
        // Shared presentation labels are not financing limits or credentials.
        if (in_array($path, [
            'tbi_settings/label_settings/payment_name',
            'tbi_settings/label_settings/payment_description',
        ], true)) {
            return $result;
        }
        if ($path === 'tbi_settings/general_settings/encryption_key') {
            $this->environment->assertUatCredentials();
        }
        return $this->environment->getUatValue($path);
    }
}
