from fastapi import Request
from fastapi.templating import Jinja2Templates
from typing import Any
import config

templates = Jinja2Templates(directory=config.TEMPLATES_DIR)


def getUrl(name: str, **path_params: Any):
    from api import app

    try:
        url = app.url_path_for(name, **path_params)
    except Exception:
        url = app.url_path_for(name)
    return url


async def render(
    request: Request,
):
    context = {
        "request": request,
        "content": {},
        # "content": {"app_secret": config.APP_SECRET},
    }

    fileName = f"{request.scope['path']}/index.html"

    response = templates.TemplateResponse(fileName, context)

    return response


def_list = [
    getUrl,
]

for def_ in def_list:
    templates.env.globals[def_.__name__] = def_
