import SwiftUI
import Combine
import XMPPSwift

let appState = AppState()
let store = AppStore.init(
    initialState: appState,
    reducer: AppState.reducer,
    middlewares: [
        loggerMiddleware(),
        AccountsMiddleware.shared.middleware,
        RegistrationMiddleware.shared.middleware
    ]
)

let database = Database()
let xmpp = XMPPService()

@main
struct SnikketApp: App {
    @State private var bag = Set<AnyCancellable>()

    var body: some Scene {
        WindowGroup {
            BaseNavigationView()
                .environmentObject(store)
                .onLoad {
                    xmpp.feed
                        .receive(on: DispatchQueue.global())
                        .sink(receiveValue: { event in
                            store.processXmppEvent(event)
                        })
                        .store(in: &bag)
                }
        }
    }
}
