package auth

import "sync"

type Memstore struct {
	tokens             map[string]Token
	refreshTokenLookup map[string]string
	profileTokenLookup map[string][]string
	tokenLock          sync.RWMutex

	grants    map[string]Grant
	grantLock sync.RWMutex
}

func NewMemstore() *Memstore {
	return &Memstore{
		tokens:             map[string]Token{},
		refreshTokenLookup: map[string]string{},
		profileTokenLookup: map[string][]string{},
		grants:             map[string]Grant{},
	}
}

func (m *Memstore) lookupTokenByRefresh(token string) (string, error) {
	m.tokenLock.RLock()
	defer m.tokenLock.RUnlock()
	t, ok := m.refreshTokenLookup[token]
	if !ok {
		return "", ErrTokenNotFound
	}
	return t, nil
}

func (m *Memstore) lookupTokensByProfileID(id string) ([]string, error) {
	m.tokenLock.RLock()
	defer m.tokenLock.RUnlock()
	return m.profileTokenLookup[id], nil
}
