 import Foundation
 import CoreLocation
 import AdSupport
+import CryptoSwift
 
 class TrackingManager: NSObject {
   static let shared = TrackingManager()

      payload["ip"] = ip
    }
    if let idfa = IDFA.shared.identifier {
      payload["user_id"] = ["type": "idfa", "id": idfa]
    }
    
    guard
      let data = try? JSONSerialization.data(withJSONObject: payload, options: .prettyPrinted),
      let body = String(data: data, encoding: .utf8)
    else { return }
    events.removeAll()
    print(body)
  }
  var sharedPayload: [String: Any] = [
    "sdk_version": Bundle.init(for: AdsManager.self).infoDictionary?["CFBundleShortVersionString"] as? String ?? "",

    return ASIdentifierManager.shared().advertisingIdentifier.uuidString
  }
}

extension Array {
  func sample() -> Element {
    let index = Int(arc4random_uniform(UInt32(self.count)))
    return self[index]
  }
}

extension String {
  func index(from: Int) -> Index {
    return self.index(startIndex, offsetBy: from)
  }
  
  func substring(from: Int) -> String {
    let fromIndex = index(from: from)
    return substring(from: fromIndex)
  }
  
  func substring(to: Int) -> String {
    let toIndex = index(from: to)
    return substring(to: toIndex)
  }
  
  func substring(with r: Range<Int>) -> String {
    let startIndex = index(from: r.lowerBound)
    let endIndex = index(from: r.upperBound)
    return substring(with: startIndex..<endIndex)
  }
}
