// Package slicer contains utility classes for handling slices
package slicer

// Imports
import "strings"

// _SLICERNAME_ handles slices of _TYPE_
type _SLICERNAME_ struct {
	slice []_TYPE_
}

// _TYPETITLE_ creates a new _SLICERNAME_
func _TYPETITLE_(slice ...[]_TYPE_) *_SLICERNAME_ {
	if len(slice) > 0 {
		return &_SLICERNAME_{slice: slice[0]}
	}
	return &_SLICERNAME_{}
}

// Add a _TYPE_ value to the slicer
func (s *_SLICERNAME_) Add(value _TYPE_, additional ..._TYPE_) {
	s.slice = append(s.slice, value)
	s.slice = append(s.slice, additional...)
}

// AddUnique adds a _TYPE_ value to the slicer if it does not already exist
func (s *_SLICERNAME_) AddUnique(value _TYPE_, additional ..._TYPE_) {

	if !s.Contains(value) {
		s.slice = append(s.slice, value)
	}

	// Add additional values
	for _, value := range additional {
		if !s.Contains(value) {
			s.slice = append(s.slice, value)
		}
	}
}

// AddSlice adds a _TYPE_ slice to the slicer
func (s *_SLICERNAME_) AddSlice(value []_TYPE_) {
	s.slice = append(s.slice, value...)
}

// AsSlice returns the slice
func (s *_SLICERNAME_) AsSlice() []_TYPE_ {
	return s.slice
}

// AddSlicer appends a _SLICERNAME_ to the slicer
func (s *_SLICERNAME_) AddSlicer(value *_SLICERNAME_) {
	s.slice = append(s.slice, value.AsSlice()...)
}

// Filter the slice based on the given function
func (s *_SLICERNAME_) Filter(fn func(_TYPE_) bool) *_SLICERNAME_ {
	result := &_SLICERNAME_{}
	for _, elem := range s.slice {
		if fn(elem) {
			result.Add(elem)
		}
	}
	return result
}

// Each runs a function on every element of the slice
func (s *_SLICERNAME_) Each(fn func(_TYPE_)) {
	for _, elem := range s.slice {
		fn(elem)
	}
}

// Contains indicates if the given value is in the slice
func (s *_SLICERNAME_) Contains(matcher _TYPE_) bool {
	result := false
	for _, elem := range s.slice {
		if elem == matcher {
			result = true
		}
	}
	return result
}

// Length returns the number of elements in the slice
func (s *_SLICERNAME_) Length() int {
	return len(s.slice)
}

// Clear all elements in the slice
func (s *_SLICERNAME_) Clear() {
	s.slice = []_TYPE_{}
}

// Deduplicate removes duplicate values from the slice
func (s *_SLICERNAME_) Deduplicate() {

	result := &_SLICERNAME_{}

	for _, elem := range s.slice {
		if !result.Contains(elem) {
			result.Add(elem)
		}
	}

	s.slice = result.AsSlice()
}
