package mo

import (
	"context"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

const (
	DefaultTimout = 10 * time.Second
)

func Dial(address string) (*Client, error) {
	return DialOptions(options.Client().ApplyURI(address))
}

func DialOptions(opts *options.ClientOptions) (*Client, error) {
	if opts.Timeout == nil {
		opts.SetConnectTimeout(DefaultTimout)
	}
	if opts.ConnectTimeout == nil {
		opts.SetConnectTimeout(DefaultTimout / 2)
	}
	if opts.AppName == nil {
		opts.SetAppName("golib/v3")
	}
	return mongo.Connect(context.Background(), opts)
}
