package web

import (
	"html/template"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

func makePreviewer(renderer *template.Template) gin.HandlerFunc {
	const prefix = "storybook-preview"
	return func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		u := c.Request.URL
		p := strings.Replace(u.EscapedPath(), "/"+prefix+"/", "", 1)
		splitPath := strings.Split(p, "/")
		name, _ := splitPath[0], splitPath[1]
		q := u.Query()
		fl := q["flashes"]
		c.HTML(http.StatusOK, name, fl)
	}
}
