//
//  Dictionary+Extension.swift
//  CZUtils
//
//  Created by Cheng Zhang on 1/29/17.
//  Copyright © 2017 Cheng Zhang. All rights reserved.
//

import Foundation

public extension Dictionary {
    /// Retrieve value from `dotedKey`, compatible with multi-dot in keyPath. e.g. "user.profile.fullName"
    public func value(forDotedKey dotedKey: String) -> Value? {
        return value(forSegmentedKey: dotedKey)
    }

    /// Retrieve value from `segmentedKey`, compatible with multi-segments separated by `splitter`. e.g. "user.profile.fullName", "user/profile/fullName"
    public func value(forSegmentedKey segmentedKey: String, splitter: String = ".") -> Value? {
        var value: Any? = nil
        var dict: Dictionary? = self

        for subkey in segmentedKey.components(separatedBy: splitter) {
            guard dict != nil, let subkey = subkey as? Key else {
                return nil
            }
            value = dict?[subkey]
            dict = value as? Dictionary
        }
        return (value is NSNull) ? nil : (value as? Value)
    }
    
    /// Insert key/value pairs with input Dictionary
    public mutating func insert(_ other: Dictionary) {
        for (key, value) in other {
            self[key] = value
        }
    }

    /// Pretty formatted description string
    public var prettyDescription: String {
        do {
            let data: Data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
            return String(data: data, encoding: .utf8).assertIfNil ?? ""
        } catch {
            assertionFailure("Failed to retrieve Dictionary pretty printed description. error - \(error.localizedDescription)")
            return ""
        }
    }

}
