package acsgroups

// FilterByID returns AccessGroupList with specified ID.
func (agl AccessGroupList) FilterByID(id string) AccessGroupList {
	predicate := func(ia AccessGroup) bool {
		return ia.ID == id
	}

	return agl.FilterFunc(predicate)
}

// FilterByName returns AccessGroupList with specified Name.
func (agl AccessGroupList) FilterByName(name string) AccessGroupList {
	predicate := func(ia AccessGroup) bool {
		return ia.DisplayName == name
	}

	return agl.FilterFunc(predicate)
}

// FilterFunc allows filtering AccessGroupList based on a user-specified predicate.
func (agl AccessGroupList) FilterFunc(predicate func(AccessGroup) bool) AccessGroupList {
	var result AccessGroupList

	for _, acc := range agl.AccessGroups {
		if predicate(acc) {
			result.AccessGroups = append(result.AccessGroups, acc)
		}
	}

	return result
}

// FindOne returns first element.
// If none was found, returns an empty struct.
func (agl AccessGroupList) FindOne() AccessGroup {
	if len(agl.AccessGroups) == 0 {
		return AccessGroup{}
	}

	return agl.AccessGroups[0]
}
