package bus

import (
	"config-server/conf"
	"config-server/service"

	"infish.cn/comm"
)

var NatsCenter *comm.NatsBus

func NewNatsBus(app *conf.AppConf) (*comm.NatsBus, map[string]*comm.NatsBus) {

	// 适配器总线连接
	replyers := []*comm.NatsMsgReplyer{}
	replyers = append(replyers, service.NewConfigReplyer(app), service.NetDevConfigReplyer(app))
	localBus, _ := comm.NewNatsBus3(app.Nats.Url, app.Nats.MaxReconnect, app.Nats.ReconnDelaySecond,
		[]*comm.NatsStreamWather{
			// NewTestStream(),
		},
		replyers...,
	)

	proxies := service.RegisterProxyBus(app)
	proxyReplyers, err := comm.CreateBusProxyReplyers(proxies, localBus)
	if err != nil {
		panic(err)
	}

	localBus.AddReplyers(proxyReplyers...)

	NatsCenter = localBus

	if localBus == nil {
		panic("create bus error")
	}

	return NatsCenter, proxies
}
