//
//  AppticsInAppratingsSwift.swift
//  apptics-react-native
//
//  Created by Jaikarthick R on 03/02/26.
//






//import Foundation
//import AppticsRateUs
//import StoreKit
//import UIKit

//@objc(CustomHandler)
//
//class CustomHandler: NSObject, ARCustomHandler {
//    @objc func showAppStoreReview() {
//        if #available(iOS 10.3, *) {
//            DispatchQueue.main.async {
//                if #available(iOS 14.0, *) {
//                    if let windowScene = UIApplication.shared.connectedScenes
//                        .compactMap({ $0 as? UIWindowScene })
//                        .first(where: { $0.activationState == .foregroundActive || $0.activationState == .foregroundInactive }) {
//                        SKStoreReviewController.requestReview(in: windowScene)
//                    }
//                } else {
//                    SKStoreReviewController.requestReview()
//                }
//            }
//        } 
//    }
//
//}


import Foundation
import StoreKit
import UIKit

@available(iOS 13.0, *)
@objc(AppticsInAppratingsSwift)
public class AppticsInAppratingsSwift: NSObject {

  @objc
  public func requestReview() {
    if let scene = findActiveScene() {
      if #available(iOS 16.0, *) {
        Task { @MainActor in
          AppStore.requestReview(in: scene)
        }
      } else {
        if #available(iOS 14.0, *) {
          SKStoreReviewController.requestReview(in: scene)
        } else {
          SKStoreReviewController.requestReview()
        }
      }
//      resolve(true)
    } else {
//      reject("no_active_scene", "No active scene found", nil)
    }
  }
  
 
    private func findActiveScene() -> UIWindowScene? {
        if Thread.isMainThread {
            return UIApplication.shared.connectedScenes
                .compactMap { $0 as? UIWindowScene }
                .first { $0.activationState == .foregroundActive }
        } else {
            var scene: UIWindowScene?
            DispatchQueue.main.sync {
                scene = UIApplication.shared.connectedScenes
                    .compactMap { $0 as? UIWindowScene }
                    .first { $0.activationState == .foregroundActive }
            }
            return scene
        }
    }

    
}
