package sep

// FilterBySEPID returns ListAvailableSEP with the specified SEPID.
func (sl ListAvailableSEP) FilterBySEPID(sepID uint64) ListAvailableSEP {
	predicate := func(sd SEPData) bool {
		return sd.SEPID == sepID
	}

	return sl.FilterFunc(predicate)
}

// FilterBySEPName returns ListAvailableSEP with the specified SEPName.
func (sl ListAvailableSEP) FilterBySEPName(SEPName string) ListAvailableSEP {
	predicate := func(sd SEPData) bool {
		return sd.SEPName == SEPName
	}

	return sl.FilterFunc(predicate)
}

// FilterBySEPType returns ListAvailableSEP with the specified SEPType.
func (sl ListAvailableSEP) FilterBySEPType(SEPType string) ListAvailableSEP {
	predicate := func(sd SEPData) bool {
		return sd.SEPType == SEPType
	}

	return sl.FilterFunc(predicate)
}

// FilterByPoolType returns ListAvailableSEP where at least one pool has the specified type.
func (sl ListAvailableSEP) FilterByPoolType(poolType string) ListAvailableSEP {
	predicate := func(sd SEPData) bool {
		for _, pool := range sd.Pools {
			for _, pt := range pool.Types {
				if pt == poolType {
					return true
				}
			}
		}
		return false
	}

	return sl.FilterFunc(predicate)
}

// FilterBySystemPool returns ListAvailableSEP where at least one pool is a system pool.
func (sl ListAvailableSEP) FilterBySystemPool(isSystem bool) ListAvailableSEP {
	predicate := func(sd SEPData) bool {
		for _, pool := range sd.Pools {
			if pool.System == isSystem {
				return true
			}
		}
		return false
	}

	return sl.FilterFunc(predicate)
}

// FilterFunc allows filtering ListAvailableSEP based on a user-defined predicate.
func (sl ListAvailableSEP) FilterFunc(predicate func(SEPData) bool) ListAvailableSEP {
	var result ListAvailableSEP

	for _, item := range sl.Data {
		if predicate(item) {
			result.Data = append(result.Data, item)
		}
	}

	result.EntryCount = uint64(len(result.Data))

	return result
}

// FindOne returns the first found SEPData.
// If nothing is found, returns an empty struct.
func (sl ListAvailableSEP) FindOne() SEPData {
	if len(sl.Data) == 0 {
		return SEPData{}
	}

	return sl.Data[0]
}
