//go:build webui

package main

import (
	"fmt"
	"github.com/notherealmarco/WASAPhoto/webui"
	"io/fs"
	"net/http"
	"strings"
)

func registerWebUI(hdl http.Handler) (http.Handler, error) {
	fmt.Printf("Registering WebUI...")
	distDirectory, err := fs.Sub(webui.Dist, "dist")
	if err != nil {
		return nil, fmt.Errorf("error embedding WebUI dist/ directory: %w", err)
	}
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.RequestURI, "/dashboard/") {
			http.StripPrefix("/dashboard/", http.FileServer(http.FS(distDirectory))).ServeHTTP(w, r)
			return
		}
		hdl.ServeHTTP(w, r)
	}), nil
}
