package server

import (
	"context"
	"log"
	"net/http"

	"github.com/a-h/templ"
)

func renderTempl(w http.ResponseWriter, status int, t templ.Component) error {
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	w.WriteHeader(status)

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

	return nil
}

func hxRedirect(w http.ResponseWriter, statusCode int, url string) error {
	w.Header().Add("HX-Redirect", url)
	w.WriteHeader(statusCode)
	return nil
}

func hxRequest(r *http.Request) bool {
	return r.Header.Get("Hx-Request") == "true"
}

func hxBoosted(r *http.Request) bool {
	return r.Header.Get("Hx-Boosted") == "true"
}
