package utils

import (
	"bytes"
	"net/http"

	"github.com/a-h/templ"
	"github.com/labstack/echo/v4"
)

func ViewMulti(c echo.Context, cmp []templ.Component) error {
	var stringHtml string = ""
	buf := new(bytes.Buffer)

	var err error
	for _, x := range cmp {
		err = x.Render(c.Request().Context(), buf)
		if err != nil {
			return err
		}
		stringHtml += buf.String()
	}

	c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)

	return c.String(http.StatusOK, stringHtml)

}

func View(c echo.Context, cmp templ.Component) error {
	c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)

	return cmp.Render(c.Request().Context(), c.Response().Writer)
}
