// Code generated by astool. DO NOT EDIT.

package propertycanannounce

import (
	vocab "code.superseriousbusiness.org/activity/streams/vocab"
	"fmt"
	"net/url"
)

// GoToSocialCanAnnouncePropertyIterator is an iterator for a property. It is
// permitted to be a single nilable value type.
type GoToSocialCanAnnouncePropertyIterator struct {
	gotosocialCanAnnounceMember vocab.GoToSocialCanAnnounce
	unknown                     interface{}
	iri                         *url.URL
	alias                       string
	myIdx                       int
	parent                      vocab.GoToSocialCanAnnounceProperty
}

// NewGoToSocialCanAnnouncePropertyIterator creates a new GoToSocialCanAnnounce
// property.
func NewGoToSocialCanAnnouncePropertyIterator() *GoToSocialCanAnnouncePropertyIterator {
	return &GoToSocialCanAnnouncePropertyIterator{alias: ""}
}

// deserializeGoToSocialCanAnnouncePropertyIterator creates an iterator from an
// element that has been unmarshalled from a text or binary format.
func deserializeGoToSocialCanAnnouncePropertyIterator(i interface{}, aliasMap map[string]string) (*GoToSocialCanAnnouncePropertyIterator, error) {
	alias := ""
	if a, ok := aliasMap["https://gotosocial.org/ns"]; ok {
		alias = a
	}
	if s, ok := i.(string); ok {
		u, err := url.Parse(s)
		// If error exists, don't error out -- skip this and treat as unknown string ([]byte) at worst
		// Also, if no scheme exists, don't treat it as a URL -- net/url is greedy
		if err == nil && len(u.Scheme) > 0 {
			this := &GoToSocialCanAnnouncePropertyIterator{
				alias: alias,
				iri:   u,
			}
			return this, nil
		}
	}
	if m, ok := i.(map[string]interface{}); ok {
		if v, err := mgr.DeserializeCanAnnounceGoToSocial()(m, aliasMap); err == nil {
			this := &GoToSocialCanAnnouncePropertyIterator{
				alias:                       alias,
				gotosocialCanAnnounceMember: v,
			}
			return this, nil
		}
	}
	this := &GoToSocialCanAnnouncePropertyIterator{
		alias:   alias,
		unknown: i,
	}
	return this, nil
}

// Get returns the value of this property. When IsGoToSocialCanAnnounce returns
// false, Get will return any arbitrary value.
func (this GoToSocialCanAnnouncePropertyIterator) Get() vocab.GoToSocialCanAnnounce {
	return this.gotosocialCanAnnounceMember
}

// GetIRI returns the IRI of this property. When IsIRI returns false, GetIRI will
// return any arbitrary value.
func (this GoToSocialCanAnnouncePropertyIterator) GetIRI() *url.URL {
	return this.iri
}

// GetType returns the value in this property as a Type. Returns nil if the value
// is not an ActivityStreams type, such as an IRI or another value.
func (this GoToSocialCanAnnouncePropertyIterator) GetType() vocab.Type {
	if this.IsGoToSocialCanAnnounce() {
		return this.Get()
	}

	return nil
}

// HasAny returns true if the value or IRI is set.
func (this GoToSocialCanAnnouncePropertyIterator) HasAny() bool {
	return this.IsGoToSocialCanAnnounce() || this.iri != nil
}

// IsGoToSocialCanAnnounce returns true if this property is set and not an IRI.
func (this GoToSocialCanAnnouncePropertyIterator) IsGoToSocialCanAnnounce() bool {
	return this.gotosocialCanAnnounceMember != nil
}

// IsIRI returns true if this property is an IRI.
func (this GoToSocialCanAnnouncePropertyIterator) IsIRI() bool {
	return this.iri != nil
}

// JSONLDContext returns the JSONLD URIs required in the context string for this
// property and the specific values that are set. The value in the map is the
// alias used to import the property's value or values.
func (this GoToSocialCanAnnouncePropertyIterator) JSONLDContext() map[string]string {
	m := map[string]string{"https://gotosocial.org/ns": this.alias}
	var child map[string]string
	if this.IsGoToSocialCanAnnounce() {
		child = this.Get().JSONLDContext()
	}
	/*
	   Since the literal maps in this function are determined at
	   code-generation time, this loop should not overwrite an existing key with a
	   new value.
	*/
	for k, v := range child {
		m[k] = v
	}
	return m
}

// KindIndex computes an arbitrary value for indexing this kind of value. This is
// a leaky API detail only for folks looking to replace the go-fed
// implementation. Applications should not use this method.
func (this GoToSocialCanAnnouncePropertyIterator) KindIndex() int {
	if this.IsGoToSocialCanAnnounce() {
		return 0
	}
	if this.IsIRI() {
		return -2
	}
	return -1
}

// LessThan compares two instances of this property with an arbitrary but stable
// comparison. Applications should not use this because it is only meant to
// help alternative implementations to go-fed to be able to normalize
// nonfunctional properties.
func (this GoToSocialCanAnnouncePropertyIterator) LessThan(o vocab.GoToSocialCanAnnouncePropertyIterator) bool {
	// LessThan comparison for if either or both are IRIs.
	if this.IsIRI() && o.IsIRI() {
		return this.iri.String() < o.GetIRI().String()
	} else if this.IsIRI() {
		// IRIs are always less than other values, none, or unknowns
		return true
	} else if o.IsIRI() {
		// This other, none, or unknown value is always greater than IRIs
		return false
	}
	// LessThan comparison for the single value or unknown value.
	if !this.IsGoToSocialCanAnnounce() && !o.IsGoToSocialCanAnnounce() {
		// Both are unknowns.
		return false
	} else if this.IsGoToSocialCanAnnounce() && !o.IsGoToSocialCanAnnounce() {
		// Values are always greater than unknown values.
		return false
	} else if !this.IsGoToSocialCanAnnounce() && o.IsGoToSocialCanAnnounce() {
		// Unknowns are always less than known values.
		return true
	} else {
		// Actual comparison.
		return this.Get().LessThan(o.Get())
	}
}

// Name returns the name of this property: "GoToSocialCanAnnounce".
func (this GoToSocialCanAnnouncePropertyIterator) Name() string {
	if len(this.alias) > 0 {
		return this.alias + ":" + "GoToSocialCanAnnounce"
	} else {
		return "GoToSocialCanAnnounce"
	}
}

// Next returns the next iterator, or nil if there is no next iterator.
func (this GoToSocialCanAnnouncePropertyIterator) Next() vocab.GoToSocialCanAnnouncePropertyIterator {
	if this.myIdx+1 >= this.parent.Len() {
		return nil
	} else {
		return this.parent.At(this.myIdx + 1)
	}
}

// Prev returns the previous iterator, or nil if there is no previous iterator.
func (this GoToSocialCanAnnouncePropertyIterator) Prev() vocab.GoToSocialCanAnnouncePropertyIterator {
	if this.myIdx-1 < 0 {
		return nil
	} else {
		return this.parent.At(this.myIdx - 1)
	}
}

// Set sets the value of this property. Calling IsGoToSocialCanAnnounce afterwards
// will return true.
func (this *GoToSocialCanAnnouncePropertyIterator) Set(v vocab.GoToSocialCanAnnounce) {
	this.clear()
	this.gotosocialCanAnnounceMember = v
}

// SetIRI sets the value of this property. Calling IsIRI afterwards will return
// true.
func (this *GoToSocialCanAnnouncePropertyIterator) SetIRI(v *url.URL) {
	this.clear()
	this.iri = v
}

// SetType attempts to set the property for the arbitrary type. Returns an error
// if it is not a valid type to set on this property.
func (this *GoToSocialCanAnnouncePropertyIterator) SetType(t vocab.Type) error {
	if v, ok := t.(vocab.GoToSocialCanAnnounce); ok {
		this.Set(v)
		return nil
	}

	return fmt.Errorf("illegal type to set on GoToSocialCanAnnounce property: %T", t)
}

// clear ensures no value of this property is set. Calling IsGoToSocialCanAnnounce
// afterwards will return false.
func (this *GoToSocialCanAnnouncePropertyIterator) clear() {
	this.unknown = nil
	this.iri = nil
	this.gotosocialCanAnnounceMember = nil
}

// serialize converts this into an interface representation suitable for
// marshalling into a text or binary format. Applications should not need this
// function as most typical use cases serialize types instead of individual
// properties. It is exposed for alternatives to go-fed implementations to use.
func (this GoToSocialCanAnnouncePropertyIterator) serialize() (interface{}, error) {
	if this.IsGoToSocialCanAnnounce() {
		return this.Get().Serialize()
	} else if this.IsIRI() {
		return this.iri.String(), nil
	}
	return this.unknown, nil
}

// GoToSocialCanAnnounceProperty is the non-functional property "canAnnounce". It
// is permitted to have one or more values, and of different value types.
type GoToSocialCanAnnounceProperty struct {
	properties []*GoToSocialCanAnnouncePropertyIterator
	alias      string
}

// DeserializeCanAnnounceProperty creates a "canAnnounce" property from an
// interface representation that has been unmarshalled from a text or binary
// format.
func DeserializeCanAnnounceProperty(m map[string]interface{}, aliasMap map[string]string) (vocab.GoToSocialCanAnnounceProperty, error) {
	alias := ""
	if a, ok := aliasMap["https://gotosocial.org/ns"]; ok {
		alias = a
	}
	propName := "canAnnounce"
	if len(alias) > 0 {
		propName = fmt.Sprintf("%s:%s", alias, "canAnnounce")
	}
	i, ok := m[propName]

	if ok {
		this := &GoToSocialCanAnnounceProperty{
			alias:      alias,
			properties: []*GoToSocialCanAnnouncePropertyIterator{},
		}
		if list, ok := i.([]interface{}); ok {
			for _, iterator := range list {
				if p, err := deserializeGoToSocialCanAnnouncePropertyIterator(iterator, aliasMap); err != nil {
					return this, err
				} else if p != nil {
					this.properties = append(this.properties, p)
				}
			}
		} else {
			if p, err := deserializeGoToSocialCanAnnouncePropertyIterator(i, aliasMap); err != nil {
				return this, err
			} else if p != nil {
				this.properties = append(this.properties, p)
			}
		}
		// Set up the properties for iteration.
		for idx, ele := range this.properties {
			ele.parent = this
			ele.myIdx = idx
		}
		return this, nil
	}
	return nil, nil
}

// NewGoToSocialCanAnnounceProperty creates a new canAnnounce property.
func NewGoToSocialCanAnnounceProperty() *GoToSocialCanAnnounceProperty {
	return &GoToSocialCanAnnounceProperty{alias: ""}
}

// AppendGoToSocialCanAnnounce appends a CanAnnounce value to the back of a list
// of the property "canAnnounce". Invalidates iterators that are traversing
// using Prev.
func (this *GoToSocialCanAnnounceProperty) AppendGoToSocialCanAnnounce(v vocab.GoToSocialCanAnnounce) {
	this.properties = append(this.properties, &GoToSocialCanAnnouncePropertyIterator{
		alias:                       this.alias,
		gotosocialCanAnnounceMember: v,
		myIdx:                       this.Len(),
		parent:                      this,
	})
}

// AppendIRI appends an IRI value to the back of a list of the property
// "canAnnounce"
func (this *GoToSocialCanAnnounceProperty) AppendIRI(v *url.URL) {
	this.properties = append(this.properties, &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		iri:    v,
		myIdx:  this.Len(),
		parent: this,
	})
}

// PrependType prepends an arbitrary type value to the front of a list of the
// property "canAnnounce". Invalidates iterators that are traversing using
// Prev. Returns an error if the type is not a valid one to set for this
// property.
func (this *GoToSocialCanAnnounceProperty) AppendType(t vocab.Type) error {
	n := &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		myIdx:  this.Len(),
		parent: this,
	}
	if err := n.SetType(t); err != nil {
		return err
	}
	this.properties = append(this.properties, n)
	return nil
}

// At returns the property value for the specified index. Panics if the index is
// out of bounds.
func (this GoToSocialCanAnnounceProperty) At(index int) vocab.GoToSocialCanAnnouncePropertyIterator {
	return this.properties[index]
}

// Begin returns the first iterator, or nil if empty. Can be used with the
// iterator's Next method and this property's End method to iterate from front
// to back through all values.
func (this GoToSocialCanAnnounceProperty) Begin() vocab.GoToSocialCanAnnouncePropertyIterator {
	if this.Empty() {
		return nil
	} else {
		return this.properties[0]
	}
}

// Empty returns returns true if there are no elements.
func (this GoToSocialCanAnnounceProperty) Empty() bool {
	return this.Len() == 0
}

// End returns beyond-the-last iterator, which is nil. Can be used with the
// iterator's Next method and this property's Begin method to iterate from
// front to back through all values.
func (this GoToSocialCanAnnounceProperty) End() vocab.GoToSocialCanAnnouncePropertyIterator {
	return nil
}

// InsertGoToSocialCanAnnounce inserts a CanAnnounce value at the specified index
// for a property "canAnnounce". Existing elements at that index and higher
// are shifted back once. Invalidates all iterators.
func (this *GoToSocialCanAnnounceProperty) InsertGoToSocialCanAnnounce(idx int, v vocab.GoToSocialCanAnnounce) {
	this.properties = append(this.properties, nil)
	copy(this.properties[idx+1:], this.properties[idx:])
	this.properties[idx] = &GoToSocialCanAnnouncePropertyIterator{
		alias:                       this.alias,
		gotosocialCanAnnounceMember: v,
		myIdx:                       idx,
		parent:                      this,
	}
	for i := idx; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
}

// Insert inserts an IRI value at the specified index for a property
// "canAnnounce". Existing elements at that index and higher are shifted back
// once. Invalidates all iterators.
func (this *GoToSocialCanAnnounceProperty) InsertIRI(idx int, v *url.URL) {
	this.properties = append(this.properties, nil)
	copy(this.properties[idx+1:], this.properties[idx:])
	this.properties[idx] = &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		iri:    v,
		myIdx:  idx,
		parent: this,
	}
	for i := idx; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
}

// PrependType prepends an arbitrary type value to the front of a list of the
// property "canAnnounce". Invalidates all iterators. Returns an error if the
// type is not a valid one to set for this property.
func (this *GoToSocialCanAnnounceProperty) InsertType(idx int, t vocab.Type) error {
	n := &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		myIdx:  idx,
		parent: this,
	}
	if err := n.SetType(t); err != nil {
		return err
	}
	this.properties = append(this.properties, nil)
	copy(this.properties[idx+1:], this.properties[idx:])
	this.properties[idx] = n
	for i := idx; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
	return nil
}

// JSONLDContext returns the JSONLD URIs required in the context string for this
// property and the specific values that are set. The value in the map is the
// alias used to import the property's value or values.
func (this GoToSocialCanAnnounceProperty) JSONLDContext() map[string]string {
	m := map[string]string{"https://gotosocial.org/ns": this.alias}
	for _, elem := range this.properties {
		child := elem.JSONLDContext()
		/*
		   Since the literal maps in this function are determined at
		   code-generation time, this loop should not overwrite an existing key with a
		   new value.
		*/
		for k, v := range child {
			m[k] = v
		}
	}
	return m
}

// KindIndex computes an arbitrary value for indexing this kind of value. This is
// a leaky API method specifically needed only for alternate implementations
// for go-fed. Applications should not use this method. Panics if the index is
// out of bounds.
func (this GoToSocialCanAnnounceProperty) KindIndex(idx int) int {
	return this.properties[idx].KindIndex()
}

// Len returns the number of values that exist for the "canAnnounce" property.
func (this GoToSocialCanAnnounceProperty) Len() (length int) {
	return len(this.properties)
}

// Less computes whether another property is less than this one. Mixing types
// results in a consistent but arbitrary ordering
func (this GoToSocialCanAnnounceProperty) Less(i, j int) bool {
	idx1 := this.KindIndex(i)
	idx2 := this.KindIndex(j)
	if idx1 < idx2 {
		return true
	} else if idx1 == idx2 {
		if idx1 == 0 {
			lhs := this.properties[i].Get()
			rhs := this.properties[j].Get()
			return lhs.LessThan(rhs)
		} else if idx1 == -2 {
			lhs := this.properties[i].GetIRI()
			rhs := this.properties[j].GetIRI()
			return lhs.String() < rhs.String()
		}
	}
	return false
}

// LessThan compares two instances of this property with an arbitrary but stable
// comparison. Applications should not use this because it is only meant to
// help alternative implementations to go-fed to be able to normalize
// nonfunctional properties.
func (this GoToSocialCanAnnounceProperty) LessThan(o vocab.GoToSocialCanAnnounceProperty) bool {
	l1 := this.Len()
	l2 := o.Len()
	l := l1
	if l2 < l1 {
		l = l2
	}
	for i := 0; i < l; i++ {
		if this.properties[i].LessThan(o.At(i)) {
			return true
		} else if o.At(i).LessThan(this.properties[i]) {
			return false
		}
	}
	return l1 < l2
}

// Name returns the name of this property ("canAnnounce") with any alias.
func (this GoToSocialCanAnnounceProperty) Name() string {
	if len(this.alias) > 0 {
		return this.alias + ":" + "canAnnounce"
	} else {
		return "canAnnounce"
	}
}

// PrependGoToSocialCanAnnounce prepends a CanAnnounce value to the front of a
// list of the property "canAnnounce". Invalidates all iterators.
func (this *GoToSocialCanAnnounceProperty) PrependGoToSocialCanAnnounce(v vocab.GoToSocialCanAnnounce) {
	this.properties = append([]*GoToSocialCanAnnouncePropertyIterator{{
		alias:                       this.alias,
		gotosocialCanAnnounceMember: v,
		myIdx:                       0,
		parent:                      this,
	}}, this.properties...)
	for i := 1; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
}

// PrependIRI prepends an IRI value to the front of a list of the property
// "canAnnounce".
func (this *GoToSocialCanAnnounceProperty) PrependIRI(v *url.URL) {
	this.properties = append([]*GoToSocialCanAnnouncePropertyIterator{{
		alias:  this.alias,
		iri:    v,
		myIdx:  0,
		parent: this,
	}}, this.properties...)
	for i := 1; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
}

// PrependType prepends an arbitrary type value to the front of a list of the
// property "canAnnounce". Invalidates all iterators. Returns an error if the
// type is not a valid one to set for this property.
func (this *GoToSocialCanAnnounceProperty) PrependType(t vocab.Type) error {
	n := &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		myIdx:  0,
		parent: this,
	}
	if err := n.SetType(t); err != nil {
		return err
	}
	this.properties = append([]*GoToSocialCanAnnouncePropertyIterator{n}, this.properties...)
	for i := 1; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
	return nil
}

// Remove deletes an element at the specified index from a list of the property
// "canAnnounce", regardless of its type. Panics if the index is out of
// bounds. Invalidates all iterators.
func (this *GoToSocialCanAnnounceProperty) Remove(idx int) {
	(this.properties)[idx].parent = nil
	copy((this.properties)[idx:], (this.properties)[idx+1:])
	(this.properties)[len(this.properties)-1] = &GoToSocialCanAnnouncePropertyIterator{}
	this.properties = (this.properties)[:len(this.properties)-1]
	for i := idx; i < this.Len(); i++ {
		(this.properties)[i].myIdx = i
	}
}

// Serialize converts this into an interface representation suitable for
// marshalling into a text or binary format. Applications should not need this
// function as most typical use cases serialize types instead of individual
// properties. It is exposed for alternatives to go-fed implementations to use.
func (this GoToSocialCanAnnounceProperty) Serialize() (interface{}, error) {
	s := make([]interface{}, 0, len(this.properties))
	for _, iterator := range this.properties {
		if b, err := iterator.serialize(); err != nil {
			return s, err
		} else {
			s = append(s, b)
		}
	}
	// Shortcut: if serializing one value, don't return an array -- pretty sure other Fediverse software would choke on a "type" value with array, for example.
	if len(s) == 1 {
		return s[0], nil
	}
	return s, nil
}

// Set sets a CanAnnounce value to be at the specified index for the property
// "canAnnounce". Panics if the index is out of bounds. Invalidates all
// iterators.
func (this *GoToSocialCanAnnounceProperty) Set(idx int, v vocab.GoToSocialCanAnnounce) {
	(this.properties)[idx].parent = nil
	(this.properties)[idx] = &GoToSocialCanAnnouncePropertyIterator{
		alias:                       this.alias,
		gotosocialCanAnnounceMember: v,
		myIdx:                       idx,
		parent:                      this,
	}
}

// SetIRI sets an IRI value to be at the specified index for the property
// "canAnnounce". Panics if the index is out of bounds.
func (this *GoToSocialCanAnnounceProperty) SetIRI(idx int, v *url.URL) {
	(this.properties)[idx].parent = nil
	(this.properties)[idx] = &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		iri:    v,
		myIdx:  idx,
		parent: this,
	}
}

// SetType sets an arbitrary type value to the specified index of the property
// "canAnnounce". Invalidates all iterators. Returns an error if the type is
// not a valid one to set for this property. Panics if the index is out of
// bounds.
func (this *GoToSocialCanAnnounceProperty) SetType(idx int, t vocab.Type) error {
	n := &GoToSocialCanAnnouncePropertyIterator{
		alias:  this.alias,
		myIdx:  idx,
		parent: this,
	}
	if err := n.SetType(t); err != nil {
		return err
	}
	(this.properties)[idx] = n
	return nil
}

// Swap swaps the location of values at two indices for the "canAnnounce" property.
func (this GoToSocialCanAnnounceProperty) Swap(i, j int) {
	this.properties[i], this.properties[j] = this.properties[j], this.properties[i]
}
