import Foundation
import BlazeSDK

extension [String: AnyHashable]? {
    func extractVideosPlaybackConfiguration() -> BlazeVideosPlaybackConfiguration? {
        guard let dict = self else { return nil }
        
        var config = Blaze.shared.getDefaultVideosPlaybackConfiguration()
        
        if let multiAspectRatio = dict["multiAspectRatio"] as? Bool {
            config.multiAspectRatio = multiAspectRatio
        }
        if let shouldOpenInLandscape = dict["shouldOpenInLandscape"] as? Bool {
            config.shouldOpenInLandscape = shouldOpenInLandscape
        }
        
        if let pipDict = dict["pipConfiguration"] as? [String: AnyHashable] {
            if let enterOnBackground = pipDict["enterPipOnAppBackground"] as? Bool {
                config.pipConfiguration.enterPipOnAppBackground = enterOnBackground
            }
        }
        
        return config
    }
    
    func extractMomentsPlaybackConfiguration() -> BlazeMomentsPlaybackConfiguration? {
        guard let dict = self else { return nil }
        
        var config = BlazeMomentsPlaybackConfiguration.base()
        
        if let loopBehaviorDict = dict["loopBehavior"] as? [String: AnyHashable],
           let type = loopBehaviorDict["type"] as? String {
            switch type {
            case "infiniteLoop":
                config.loopBehavior = .infiniteLoop
            case "loopAndAdvance":
                let numberOfPlays = loopBehaviorDict["numberOfPlays"] as? Int ?? 1
                config.loopBehavior = .loopAndAdvance(numberOfPlays: max(1, numberOfPlays))
            default:
                break
            }
        }
        
        return config
    }
}