import Foundation
import Combine

let isConsoleLoggingEnabled = false

#if DEBUG
    func loggerMiddleware() -> Middleware<AppState, AppAction> {
        { state, action in
            let timeStr = dateFormatter.string(from: Date())
            var actionStr = "\(action)"
            actionStr = String(actionStr.prefix(200)) + " ..."
            var stateStr = "\(state)"
            stateStr = String(stateStr.prefix(200)) + " ..."

            let str = "\(timeStr) ➡️ \(actionStr)\n\(timeStr) ✅ \(stateStr)\n"
            print(str)
            if isConsoleLoggingEnabled {
                NSLog(str)
            }
            return Empty().eraseToAnyPublisher()
        }
    }
#else
    func loggerMiddleware() -> Middleware<AppState, AppAction> {
        { _, _ in
            Empty().eraseToAnyPublisher()
        }
    }
#endif

private var dateFormatter: DateFormatter {
    let formatter = DateFormatter()
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
    formatter.dateFormat = "MM-dd HH:mm:ss.SSS"
    return formatter
}
