package network

import "strings"

// ResolveVisibility expands a stream particle's visible_to entries into the set
// of human IDs that should see (and thus be notified about) activity in that
// stream. Entries are formatted as `human:{id}` for a specific human or
// `network:{id}` to expand to every member of the surrounding network.
//
// networkMembers must contain every human currently in the network (members +
// admin). visible_to entries that point to humans no longer in the network are
// dropped — they may have been removed since the stream was created.
//
// Returns a deduped slice; ordering is not stable.
func ResolveVisibility(visibleTo []string, networkMembers []string) []string {
	memberSet := make(map[string]bool, len(networkMembers))
	for _, id := range networkMembers {
		memberSet[id] = true
	}

	result := make(map[string]bool)
	for _, entry := range visibleTo {
		switch {
		case strings.HasPrefix(entry, "human:"):
			id := strings.TrimPrefix(entry, "human:")
			if memberSet[id] {
				result[id] = true
			}
		case strings.HasPrefix(entry, "network:"):
			for id := range memberSet {
				result[id] = true
			}
		}
	}

	out := make([]string, 0, len(result))
	for id := range result {
		out = append(out, id)
	}
	return out
}
