package server

import (
	"context"
	"log"

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

func renderTempl(c echo.Context, status int, t templ.Component) error {
	c.Response().Writer.WriteHeader(status)

	err := t.Render(context.Background(), c.Response().Writer)
	if err != nil {
		log.Printf("failed to render response template %s", err)
		return err
	}

	return nil
}

func hxRedirect(c echo.Context, statusCode int, url string) error {
	c.Response().Header().Add("HX-Redirect", url)
	return c.NoContent(statusCode)
}

func hxRequest(c echo.Context) bool {
	header, ok := c.Request().Header["Hx-Request"]
	if !ok {
		return false
	}
	if header[0] != "true" {
		return false
	}
	return true
}

func hxBoosted(c echo.Context) bool {
	header, ok := c.Request().Header["Hx-Boosted"]
	if !ok {
		return false
	}
	if header[0] != "true" {
		return false
	}
	return true
}
