// Generated by Saber 0.2.0

// swiftlint:disable all

import Foundation

internal class UserContainer: UserContaining {

    private let lock = NSRecursiveLock()

    internal unowned let appContainer: AppContainer

    internal let gitHubAccessToken: GitHub.AccessToken

    private var cached_userAPI: UserAPI?

    private var cached_userSessionManagerProvider: UserSessionManagerProvider?

    internal init(appContainer: AppContainer, gitHubAccessToken: GitHub.AccessToken) {
        self.appContainer = appContainer
        self.gitHubAccessToken = gitHubAccessToken
    }

    internal var userAPI: UserAPI {
        self.lock.lock()
        defer { self.lock.unlock() }
        if let cached = self.cached_userAPI { return cached }
        let userAPI = self.makeUserAPI()
        self.cached_userAPI = userAPI
        return userAPI
    }

    internal var userSessionManagerProvider: UserSessionManagerProvider {
        self.lock.lock()
        defer { self.lock.unlock() }
        if let cached = self.cached_userSessionManagerProvider { return cached }
        let userSessionManagerProvider = self.makeUserSessionManagerProvider()
        self.cached_userSessionManagerProvider = userSessionManagerProvider
        return userSessionManagerProvider
    }

    internal var userSessionManager: UserSessionManager {
        let userSessionManager = self.makeUserSessionManager()
        return userSessionManager
    }

    private func makeUserAPI() -> UserAPI {
        return UserAPI(github: self.appContainer.gitHub, sessionManager: self.userSessionManager)
    }

    private func makeUserSessionManagerProvider() -> UserSessionManagerProvider {
        return UserSessionManagerProvider(github: self.appContainer.gitHub, accessToken: self.gitHubAccessToken)
    }

    private func makeUserSessionManager() -> UserSessionManager {
        let provider = self.userSessionManagerProvider
        return provider.provide()
    }

}