package dataext

import "sync"

type SyncStringSet struct {
	data map[string]bool
	lock sync.Mutex
}

func (s *SyncStringSet) Add(value string) bool {
	s.lock.Lock()
	defer s.lock.Unlock()

	if s.data == nil {
		s.data = make(map[string]bool)
	}

	_, ok := s.data[value]
	s.data[value] = true

	return !ok
}

func (s *SyncStringSet) AddAll(values []string) {
	s.lock.Lock()
	defer s.lock.Unlock()

	if s.data == nil {
		s.data = make(map[string]bool)
	}

	for _, value := range values {
		s.data[value] = true
	}
}

func (s *SyncStringSet) Contains(value string) bool {
	s.lock.Lock()
	defer s.lock.Unlock()

	if s.data == nil {
		s.data = make(map[string]bool)
	}

	_, ok := s.data[value]

	return ok
}

func (s *SyncStringSet) Get() []string {
	s.lock.Lock()
	defer s.lock.Unlock()

	if s.data == nil {
		s.data = make(map[string]bool)
	}

	r := make([]string, 0, len(s.data))

	for k := range s.data {
		r = append(r, k)
	}

	return r
}