package logicalports

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

	return agl.FilterFunc(predicate)
}

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

	return agl.FilterFunc(predicate)
}

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

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

	return result
}

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

	return agl.Ports[0]
}
