package templer

import (
	"context"
)

// GetHeader returns a slice of strings that should be added to the html head.
// Because strings are immutable a slice of strings is used.
func GetHeader(ctx context.Context) []string {
	v := ctx.Value(headerKey)
	if v == nil {
		return []string{}
	}

	header, ok := v.([]string)
	if !ok {
		return []string{}
	}
	return header
}

// AppendHeader lets you add lines to the header.
func AppendHeader(ctx context.Context, v ...string) context.Context {
	header := GetHeader(ctx)

	header = append(header, v...)

	return context.WithValue(ctx, headerKey, header)
}

// AppendHeader lets you add lines to the header.
func PageTitle(ctx context.Context, title string) context.Context {
	return context.WithValue(ctx, titleKey, title)
}

// AppendHeader lets you add lines to the header.
func GetTitle(ctx context.Context, main string) string {
	v := ctx.Value(titleKey)
	if v == nil {
		return main
	}

	title, ok := v.(string)
	if !ok {
		return main
	}
	return title + " | " + main
}
