//
//  Glider
//  Fast, Lightweight yet powerful logging system for Swift.
//
//  Created by Daniele Margutti
//  Email: hello@danielemargutti.com
//  Web: http://www.danielemargutti.com
//
//  Copyright ©2021 Daniele Margutti. All rights reserved.
//  Licensed under MIT License.
//

import Foundation

extension LogScope {
    
    // MARK: - Internal Functions
    
    /// Get the value for a specific field.
    /// - Parameters:
    ///   - field: field.
    ///   - isScopeEntered: `true` if scope is entered
    /// - Returns: String?
    public func value(forField field: FormatterField, entered isScopeEntered: Bool) -> LoggableValue? {
        switch field {
        case .kind:
            return "scope"
        case .timestamp(let style):
            return DateFormatter.formatDate(timestamp, style: style)
        case .callingThread(let style):
            return ProcessIdentification.formatCallingThreadID(context.threadID, style: style)
        case .message:
            let prefix = (isScopeEntered ? "┌" : "└")
            let suffix = (isScopeEntered ? "" : "(\(duration.seconds) s)")
            return "\(prefix) \(name) \(suffix)"
        case .processID:
            return String(ProcessIdentification.current.processID)
        case .processName:
            return ProcessIdentification.current.processName
        case .delimiter(let style):
            return style.value
        case .callSite:
            return LogContext.formatCallSite(context)
        case .stackFrame:
            return context.functionName
        case .literal(let value):
            return value
        case .scopeName:
            return name
        case .scopeLevel:
            return String(scopeLevel)
        default:
            return ""
        }
    }
    
}
