// Package route provides structures and functions to represent a route.
package route

// Version represents the API version
type Version string

const (
	V1Alpha1 = "v1alpha1"
	V1Beta1  = "v1beta1"
	V1Beta2  = "v1beta2"
	V1       = "v1"
	Default  = ""
)

// AsPrefix returns the version as a path prefix such as /v1alpha1, /v1beta1 and so on.
func (v Version) AsPrefix() string {
	return string(v)
}

// GeneratedVersions returns the generated versions that mapped to the given version of API.
// When an API promotes to a higher version, it ensures that calling a lower versioned path
// will be directed to the expected handler.
func GeneratedVersions(v Version) []Version {
	switch v {
	case V1Alpha1:
		return []Version{V1Alpha1}
	case V1Beta1:
		return []Version{V1Alpha1, V1Beta1}
	case V1Beta2:
		return []Version{V1Beta2}
	case V1:
		return []Version{V1Alpha1, V1Beta1, V1, Default}
	default:
	}
	return []Version{}
}

// GreaterThan returns true if and only if the given version is greather than the original one.
func (v Version) GreaterThan(another Version) bool {
	switch v {
	case Default, V1:
		return another == V1Beta1 || another == V1Alpha1
	case V1Beta1:
		return another == V1Alpha1
	}
	return false
}
