// Autogenerated from Pigeon (v26.0.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon

import Foundation

#if os(iOS)
  import Flutter
#elseif os(macOS)
  import FlutterMacOS
#else
  #error("Unsupported platform.")
#endif

/// Error class for passing custom error details to Dart side.
final class PigeonError: Error {
  let code: String
  let message: String?
  let details: Sendable?

  init(code: String, message: String?, details: Sendable?) {
    self.code = code
    self.message = message
    self.details = details
  }

  var localizedDescription: String {
    return
      "PigeonError(code: \(code), message: \(message ?? "<nil>"), details: \(details ?? "<nil>")"
  }
}

private func wrapResult(_ result: Any?) -> [Any?] {
  return [result]
}

private func wrapError(_ error: Any) -> [Any?] {
  if let pigeonError = error as? PigeonError {
    return [
      pigeonError.code,
      pigeonError.message,
      pigeonError.details,
    ]
  }
  if let flutterError = error as? FlutterError {
    return [
      flutterError.code,
      flutterError.message,
      flutterError.details,
    ]
  }
  return [
    "\(error)",
    "\(type(of: error))",
    "Stacktrace: \(Thread.callStackSymbols)",
  ]
}

private func isNullish(_ value: Any?) -> Bool {
  return value is NSNull || value == nil
}

private func nilOrValue<T>(_ value: Any?) -> T? {
  if value is NSNull { return nil }
  return value as! T?
}


private class CoreApiPigeonCodecReader: FlutterStandardReader {
}

private class CoreApiPigeonCodecWriter: FlutterStandardWriter {
}

private class CoreApiPigeonCodecReaderWriter: FlutterStandardReaderWriter {
  override func reader(with data: Data) -> FlutterStandardReader {
    return CoreApiPigeonCodecReader(data: data)
  }

  override func writer(with data: NSMutableData) -> FlutterStandardWriter {
    return CoreApiPigeonCodecWriter(data: data)
  }
}

class CoreApiPigeonCodec: FlutterStandardMessageCodec, @unchecked Sendable {
  static let shared = CoreApiPigeonCodec(readerWriter: CoreApiPigeonCodecReaderWriter())
}

var coreApiPigeonMethodCodec = FlutterStandardMethodCodec(readerWriter: CoreApiPigeonCodecReaderWriter());


/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
protocol CoreApi {
  func getApps(completion: @escaping (Result<String?, Error>) -> Void)
  func getSystemLocale() throws -> String?
  func connect(sessionId: String, socksPort: Int64, tunnelConfig: String, configJson: String, remainTime: Int64, isCountdown: Bool, allowVpnApps: [String], disallowVpnApps: [String], accessToken: String, aesKey: String, aesIv: String, locationId: Int64, locationCode: String, baseUrls: [String], params: String, peekTimeInterval: Int64) throws -> Bool?
  func disconnect() throws -> Bool?
  func getRemoteIp() throws -> String?
  func getAdvertisingId() throws -> String?
  func moveTaskToBack() throws -> Bool?
  func isConnected() throws -> Bool?
  func getSimInfo() throws -> String?
  func getChannel() throws -> String?
  func openPackage(packageName: String) throws
}

/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
class CoreApiSetup {
  static var codec: FlutterStandardMessageCodec { CoreApiPigeonCodec.shared }
  /// Sets up an instance of `CoreApi` to handle messages through the `binaryMessenger`.
  static func setUp(binaryMessenger: FlutterBinaryMessenger, api: CoreApi?, messageChannelSuffix: String = "") {
    let channelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""
    let getAppsChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.getApps\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      getAppsChannel.setMessageHandler { _, reply in
        api.getApps { result in
          switch result {
          case .success(let res):
            reply(wrapResult(res))
          case .failure(let error):
            reply(wrapError(error))
          }
        }
      }
    } else {
      getAppsChannel.setMessageHandler(nil)
    }
    let getSystemLocaleChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.getSystemLocale\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      getSystemLocaleChannel.setMessageHandler { _, reply in
        do {
          let result = try api.getSystemLocale()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      getSystemLocaleChannel.setMessageHandler(nil)
    }
    let connectChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.connect\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      connectChannel.setMessageHandler { message, reply in
        let args = message as! [Any?]
        let sessionIdArg = args[0] as! String
        let socksPortArg = args[1] as! Int64
        let tunnelConfigArg = args[2] as! String
        let configJsonArg = args[3] as! String
        let remainTimeArg = args[4] as! Int64
        let isCountdownArg = args[5] as! Bool
        let allowVpnAppsArg = args[6] as! [String]
        let disallowVpnAppsArg = args[7] as! [String]
        let accessTokenArg = args[8] as! String
        let aesKeyArg = args[9] as! String
        let aesIvArg = args[10] as! String
        let locationIdArg = args[11] as! Int64
        let locationCodeArg = args[12] as! String
        let baseUrlsArg = args[13] as! [String]
        let paramsArg = args[14] as! String
        let peekTimeIntervalArg = args[15] as! Int64
        do {
          let result = try api.connect(sessionId: sessionIdArg, socksPort: socksPortArg, tunnelConfig: tunnelConfigArg, configJson: configJsonArg, remainTime: remainTimeArg, isCountdown: isCountdownArg, allowVpnApps: allowVpnAppsArg, disallowVpnApps: disallowVpnAppsArg, accessToken: accessTokenArg, aesKey: aesKeyArg, aesIv: aesIvArg, locationId: locationIdArg, locationCode: locationCodeArg, baseUrls: baseUrlsArg, params: paramsArg, peekTimeInterval: peekTimeIntervalArg)
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      connectChannel.setMessageHandler(nil)
    }
    let disconnectChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.disconnect\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      disconnectChannel.setMessageHandler { _, reply in
        do {
          let result = try api.disconnect()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      disconnectChannel.setMessageHandler(nil)
    }
    let getRemoteIpChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.getRemoteIp\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      getRemoteIpChannel.setMessageHandler { _, reply in
        do {
          let result = try api.getRemoteIp()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      getRemoteIpChannel.setMessageHandler(nil)
    }
    let getAdvertisingIdChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.getAdvertisingId\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      getAdvertisingIdChannel.setMessageHandler { _, reply in
        do {
          let result = try api.getAdvertisingId()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      getAdvertisingIdChannel.setMessageHandler(nil)
    }
    let moveTaskToBackChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.moveTaskToBack\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      moveTaskToBackChannel.setMessageHandler { _, reply in
        do {
          let result = try api.moveTaskToBack()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      moveTaskToBackChannel.setMessageHandler(nil)
    }
    let isConnectedChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.isConnected\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      isConnectedChannel.setMessageHandler { _, reply in
        do {
          let result = try api.isConnected()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      isConnectedChannel.setMessageHandler(nil)
    }
    let getSimInfoChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.getSimInfo\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      getSimInfoChannel.setMessageHandler { _, reply in
        do {
          let result = try api.getSimInfo()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      getSimInfoChannel.setMessageHandler(nil)
    }
    let getChannelChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.getChannel\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      getChannelChannel.setMessageHandler { _, reply in
        do {
          let result = try api.getChannel()
          reply(wrapResult(result))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      getChannelChannel.setMessageHandler(nil)
    }
    let openPackageChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.com.flashlink.vpn.CoreApi.openPackage\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
    if let api = api {
      openPackageChannel.setMessageHandler { message, reply in
        let args = message as! [Any?]
        let packageNameArg = args[0] as! String
        do {
          try api.openPackage(packageName: packageNameArg)
          reply(wrapResult(nil))
        } catch {
          reply(wrapError(error))
        }
      }
    } else {
      openPackageChannel.setMessageHandler(nil)
    }
  }
}

private class PigeonStreamHandler<ReturnType>: NSObject, FlutterStreamHandler {
  private let wrapper: PigeonEventChannelWrapper<ReturnType>
  private var pigeonSink: PigeonEventSink<ReturnType>? = nil

  init(wrapper: PigeonEventChannelWrapper<ReturnType>) {
    self.wrapper = wrapper
  }

  func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink)
    -> FlutterError?
  {
    pigeonSink = PigeonEventSink<ReturnType>(events)
    wrapper.onListen(withArguments: arguments, sink: pigeonSink!)
    return nil
  }

  func onCancel(withArguments arguments: Any?) -> FlutterError? {
    pigeonSink = nil
    wrapper.onCancel(withArguments: arguments)
    return nil
  }
}

class PigeonEventChannelWrapper<ReturnType> {
  func onListen(withArguments arguments: Any?, sink: PigeonEventSink<ReturnType>) {}
  func onCancel(withArguments arguments: Any?) {}
}

class PigeonEventSink<ReturnType> {
  private let sink: FlutterEventSink

  init(_ sink: @escaping FlutterEventSink) {
    self.sink = sink
  }

  func success(_ value: ReturnType) {
    sink(value)
  }

  func error(code: String, message: String?, details: Any?) {
    sink(FlutterError(code: code, message: message, details: details))
  }

  func endOfStream() {
    sink(FlutterEndOfEventStream)
  }

}

class OnEventChangeStreamHandler: PigeonEventChannelWrapper<String> {
  static func register(with messenger: FlutterBinaryMessenger,
                      instanceName: String = "",
                      streamHandler: OnEventChangeStreamHandler) {
    var channelName = "dev.flutter.pigeon.com.flashlink.vpn.CoreChangeEventApi.onEventChange"
    if !instanceName.isEmpty {
      channelName += ".\(instanceName)"
    }
    let internalStreamHandler = PigeonStreamHandler<String>(wrapper: streamHandler)
    let channel = FlutterEventChannel(name: channelName, binaryMessenger: messenger, codec: coreApiPigeonMethodCodec)
    channel.setStreamHandler(internalStreamHandler)
  }
}
      
