package defsecpolicies

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

	return agl.FilterFunc(predicate)
}

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

	return agl.FilterFunc(predicate)
}

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

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

	return result
}

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

	return agl.Policies[0]
}
