package timezone

import (
	"errors"
	"strings"
	"time"
)

const Default = "UTC"

var (
	ErrEmpty   = errors.New("timezone is required")
	ErrInvalid = errors.New("timezone must be a valid IANA timezone")
)

func Normalize(name string) (string, error) {
	tz := strings.TrimSpace(name)
	if tz == "" {
		return "", ErrEmpty
	}

	loc, err := time.LoadLocation(tz)
	if err != nil {
		return "", ErrInvalid
	}
	return loc.String(), nil
}

func WithDefault(name string) string {
	tz := strings.TrimSpace(name)
	if tz == "" {
		return Default
	}
	return tz
}
