import Foundation

public extension StatsigClient {
    /// Gets a platform-specific dynamic config and decodes it to the specified type
    /// - Parameters:
    ///   - configName: The name of the dynamic config
    ///   - platform: The platform key (defaults to "ios")
    ///   - type: The type to decode to
    /// - Returns: The decoded config or nil if not available
    func getDynamicConfig<T: Codable>(
        _ configName: DynamicConfig,
        platform: String = "ios",
        as type: T.Type
    ) -> T? {
        let configData = getDynamicConfig(
            configName: configName.rawValue,
            defaultValue: [:] as [String: Any]
        ) as? [String: Any] ?? [:]

        guard let platformConfig = configData[platform] as? [String: Any] else {
            return nil
        }

        do {
            let jsonData = try JSONSerialization.data(withJSONObject: platformConfig)
            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            return try decoder.decode(type, from: jsonData)
        } catch {
            return nil
        }
    }
}
