package session

// FilterByID returns ListSession with specified ID.
func (lsession ListSession) FilterByID(id string) ListSession {
	predicate := func(rsession ItemSession) bool {
		return rsession.PublicSessionID == id
	}

	return lsession.FilterFunc(predicate)
}

// FilterByName returns ListSession with specified User.
func (lsession ListSession) FilterByUser(user string) ListSession {
	predicate := func(rsession ItemSession) bool {
		return rsession.User == user
	}

	return lsession.FilterFunc(predicate)
}

// FilterByObjStatus returns ListSession with specified IP.
func (lsession ListSession) FilterByIP(ip string) ListSession {
	predicate := func(rsession ItemSession) bool {
		return rsession.IP == ip
	}

	return lsession.FilterFunc(predicate)
}

// FilterFunc allows filtering ListSession based on user-specified predicate.
func (lsession ListSession) FilterFunc(predicate func(session ItemSession) bool) ListSession {
	var result ListSession

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

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

	return result
}

// FindOne returns first found ItemSession
// If none was found, returns an empty struct.
func (lsession ListSession) FindOne() ItemSession {
	if len(lsession.Data) == 0 {
		return ItemSession{}
	}

	return lsession.Data[0]
}
