package audit

import "strconv"

func appendUnique(list []string, item string) []string {
	for _, x := range list {
		if x == item {
			return list
		}
	}
	return append(list, item)
}

func toBoolSet(list []string) map[string]bool {
	s := make(map[string]bool, len(list))
	for _, x := range list {
		s[x] = true
	}
	return s
}

func dedup(list []string) []string {
	seen := map[string]bool{}
	var out []string
	for _, x := range list {
		if !seen[x] {
			seen[x] = true
			out = append(out, x)
		}
	}
	return out
}

func removeOne(list []string, item string) []string {
	out := make([]string, 0, len(list))
	for _, x := range list {
		if x != item {
			out = append(out, x)
		}
	}
	return out
}

func joinFirst(list []string, n int) string {
	if len(list) <= n {
		return joinAll(list)
	}
	return joinAll(list[:n]) + ", ..."
}

func joinAll(list []string) string {
	s := ""
	for i, x := range list {
		if i > 0 {
			s += ", "
		}
		s += x
	}
	return s
}

func itoa(n int) string { return strconv.Itoa(n) }
