package threadlocal

import (
	"context"
	"sync"

	"github.com/go-eden/routine"
)

var (
	store sync.Map
)

func SetContext(ctx context.Context) {
	store.Store(routine.Goid(), &ctx)
}

func GetContext() context.Context {
	value, ok := store.Load(routine.Goid())
	if !ok {
		return context.Background()
	}
	ctxPtr, ok := value.(*context.Context)
	if !ok {
		return context.Background()
	}
	return *ctxPtr
}

func DeleteContext() {
	store.Delete(routine.Goid())
}
