package segments

// FilterByID returns ListSegment with specified ID.
func (list ListSegment) FilterByID(id string) ListSegment {
	predicate := func(segment SegmentResponse) bool {
		return segment.ID == id
	}

	return list.FilterFunc(predicate)
}

// FilterByName returns ListSegment with specified display name.
func (list ListSegment) FilterByName(name string) ListSegment {
	predicate := func(segment SegmentResponse) bool {
		return segment.DisplayName == name
	}

	return list.FilterFunc(predicate)
}

// FilterBySubnetIPv4 returns ListSegment with specified subnet IPv4.
func (list ListSegment) FilterBySubnetIPv4(IPv4 string) ListSegment {
	predicate := func(segment SegmentResponse) bool {
		return segment.SubnetV4 == IPv4
	}

	return list.FilterFunc(predicate)
}

// FilterBySubnetIPv6 returns ListSegment with specified subnet IPv4.
func (list ListSegment) FilterBySubnetIPv6(IPv6 string) ListSegment {
	predicate := func(segment SegmentResponse) bool {
		return segment.SubnetV6 == IPv6
	}

	return list.FilterFunc(predicate)
}

// FilterFunc allows filtering ListSegment based on a user-specified predicate.
func (list ListSegment) FilterFunc(predicate func(response SegmentResponse) bool) ListSegment {
	var result ListSegment

	for _, item := range list {
		if predicate(item) {
			result = append(result, item)
		}
	}

	return result
}

// FindOne returns first element.
// If none was found, returns an empty struct.
func (list ListSegment) FindOne() SegmentResponse {
	if len(list) == 0 {
		return SegmentResponse{}
	}

	return list[0]
}
