package services

import (
	"sync"

	"github.com/fiskerinc/cloud-services/pkg/mongo"
)

var (
	clientOnce sync.Once
	client     mongo.Client
)

// GetMongoClient returns singleton instance of mongo client
func GetMongoClient() (mongo.Client, error) {
	var err error
	clientOnce.Do(func() {
		client, err = initMongoClient()
	})

	return client, err
}

func initMongoClient() (mongo.Client, error) {
	var err error

	if client == nil {
		client, err = mongo.NewClient(mongo.StandardDB)
	}

	return client, err
}
