import UIKit

/// Decodes the `preview` prop shipped from JS and constructs the right
/// `UIViewController` for the peek. Only `image` builds a custom controller (it
/// swaps the thumbnail for the cached fullsize). For `link`, returning nil lets
/// UIKit lift the RN-rendered view as-is; the live-Safari `link` morph is built
/// in the view itself, before this is reached. `video` is still a follow-up.
enum PreviewFactory {
  static func makeController(from spec: [String: Any]?) -> UIViewController? {
    guard let spec = spec,
          let type = spec["type"] as? String else { return nil }

    switch type {
    case "image":
      let uri = spec["uri"] as? String
      let thumbUri = spec["thumbUri"] as? String
      let url = uri.flatMap(URL.init(string:))
      let thumbURL = thumbUri.flatMap(URL.init(string:))
      let aspect = CGFloat((spec["aspectRatio"] as? Double) ?? 1)
      return ImagePreviewController(
        imageURL: url,
        thumbURL: thumbURL,
        aspectRatio: aspect
      )
    default:
      // nil → UIKit uses the source view itself as the peek preview.
      return nil
    }
  }
}
