package eventconsumer

import "net/url"

type Kind string

const (
	KindKnot    Kind = "knot"
	KindSpindle Kind = "spindle"
)

type Source struct {
	Kind Kind
	Host string
}

func NewKnotSource(host string) Source    { return Source{Kind: KindKnot, Host: host} }
func NewSpindleSource(host string) Source { return Source{Kind: KindSpindle, Host: host} }

func (s Source) Key() string { return string(s.Kind) + ":" + s.Host }

func DefaultURL(dev bool) func(Source, string) (*url.URL, error) {
	scheme := "wss"
	if dev {
		scheme = "ws"
	}
	return func(s Source, cursor string) (*url.URL, error) {
		u, err := url.Parse(scheme + "://" + s.Host + "/events")
		if err != nil {
			return nil, err
		}
		if cursor != "" {
			q := url.Values{}
			q.Add("cursor", cursor)
			u.RawQuery = q.Encode()
		}
		return u, nil
	}
}
