<?php

namespace html2pdf;

require_once WD . '/modules/html2pdf/html2pdf_templates_i.php';
require_once WD . '/modules/html2pdf/helpers/html2pdf_template.php';

/** Template classes */
// Require the templates using glob
foreach ( glob(WD . '/modules/html2pdf/templates/*.php') as $filename ) {
    require_once $filename;
}

use Exception;
use html2pdf\helpers\html2pdf_template;

class html2pdf_templates implements html2pdf_templates_i
{
    /**
     * Get the template with the given name, and optionally pass data to it
     * @param string $templateName Template name
     * @param array|null $data Data to pass to the template
     * @return html2pdf_template Template object
     * @throws Exception If the template class does not exist
     */
    public static function getTemplate(string $templateName, array $data = null): html2pdf_template
    {
        $className = self::getTemplateClass($templateName);
        if (!class_exists($className)) {
            throw new Exception('Template class does not exist: ' . $className);
        }
        return new $className($data);
    }

    /**
     * Get the template class name based on the template name
     * @param string $templateName Template name
     * @return string Class name of the template
     */
    public static function getTemplateClass(string $templateName): string
    {
        return 'html2pdf\\templates\\' . $templateName;
    }
}