<?php

namespace Hyperpay\Gateways\Helpers;

if (! defined('ABSPATH')) exit;

use Hyperpay\Gateways\Main;

final class View
{
    public static function render($name, $params = [])
    {
        $loader = new \Twig\Loader\FilesystemLoader(Main::ROOT_PATH . "/assets/templates");
        $twig = new \Twig\Environment($loader);

        $params = static::esc($params);

        // rendering the template HTML, JS and CSS
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo ($twig->render($name, $params));
        return;
    }

    public static function esc($value)
    {
        if (is_array($value)) {
            $esc_values = [];
            foreach ($value as $key => $nested_value) {
                $esc_values[$key] =  static::esc($nested_value);
            }
            return $esc_values;
        }

        return esc_html($value);
    }
}
