<commit_before>import Foundation
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}



class PluginRTCDataChannel : NSObject, RTCDataChannelDelegate {
	var rtcDataChannel: RTCDataChannel?
	var eventListener: ((_ data: NSDictionary) -> Void)?
	var eventListenerForBinaryMessage: ((_ data: Data) -> Void)?
	var lostStates = Array<String>()
	var lostMessages = Array<RTCDataBuffer>()


	/**
	 * Constructor for pc.createDataChannel().
	 */
	init(
		rtcPeerConnection: RTCPeerConnection,
		label: String,
		options: NSDictionary?,
		eventListener: @escaping (_ data: NSDictionary) -> Void,
		eventListenerForBinaryMessage: @escaping (_ data: Data) -> Void
	) {
		NSLog("PluginRTCDataChannel#init()")

		self.eventListener = eventListener
		self.eventListenerForBinaryMessage = eventListenerForBinaryMessage

		let rtcDataChannelInit = RTCDataChannelConfiguration.init()

		if options?.object(forKey: "ordered") != nil {
			rtcDataChannelInit.isOrdered = options!.object(forKey: "ordered") as! Bool
		}

		if options?.object(forKey: "maxPacketLifeTime") != nil {
			// TODO: rtcDataChannel.maxRetransmitTime always reports 0.
			rtcDataChannelInit.maxRetransmitTimeMs = options!.object(forKey: "maxPacketLifeTime") as! Int
		}

		if options?.object(forKey: "maxRetransmits") != nil {
			rtcDataChannelInit.maxRetransmits = options!.object(forKey: "maxRetransmits") as! Int32
		}

		// TODO: error: expected member name following '.'
		//   https://code.google.com/p/webrtc/issues/detail?id=4614
		// if options?.objectForKey("protocol") != nil {
			// rtcDataChannelInit.protocol = options!.objectForKey("protocol") as! String
		// }
		if options?.object(forKey: "protocol") != nil {
			rtcDataChannelInit.`protocol` = options!.object(forKey: "protocol") as! String
		}

		if options?.object(forKey: "negotiated") != nil {
			rtcDataChannelInit.isNegotiated = options!.object(forKey: "negotiated") as! Bool
		}

		if options?.object(forKey: "id") != nil {
			rtcDataChannelInit.channelId = options!.object(forKey: "id") as! Int32
		}

		self.rtcDataChannel = rtcPeerConnection.dataChannel(forLabel: label, configuration: rtcDataChannelInit)

		if self.rtcDataChannel == nil {
			NSLog("PluginRTCDataChannel#init() | rtcPeerConnection.createDataChannelWithLabel() failed")
			return
		}

		// Report definitive data to update the JS instance.
		self.eventListener!([
			"type": "new",
			"channel": [
				"ordered": self.rtcDataChannel!.isOrdered,
				"maxPacketLifeTime": self.rtcDataChannel!.maxRetransmitTime,
				"maxRetransmits": self.rtcDataChannel!.maxRetransmits,
				"protocol": self.rtcDataChannel!.`protocol`,
				"negotiated": self.rtcDataChannel!.isNegotiated,
				"id": self.rtcDataChannel!.streamId,
				"readyState": self.rtcDataChannel!.readyState,
				"bufferedAmount": self.rtcDataChannel!.bufferedAmount
			]
		])
	}


	deinit {
		NSLog("PluginRTCDataChannel#deinit()")
	}


	/**
	 * Constructor for pc.ondatachannel event.
	 */
	init(rtcDataChannel: RTCDataChannel) {
		NSLog("PluginRTCDataChannel#init()")

		self.rtcDataChannel = rtcDataChannel
	}


	func run() {
		NSLog("PluginRTCDataChannel#run()")

		self.rtcDataChannel!.delegate = self

		//if data channel is created after there is a connection,
		// we need to dispatch its current state.
		if (self.rtcDataChannel?.readyState != RTCDataChannelState.connecting) {
			dataChannelDidChangeState(self.rtcDataChannel!);<commit_msg>fix NSInvalidArgumentException using PluginRTCDataChannel.stateToString<commit_after>import Foundation
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}



class PluginRTCDataChannel : NSObject, RTCDataChannelDelegate {
	var rtcDataChannel: RTCDataChannel?
	var eventListener: ((_ data: NSDictionary) -> Void)?
	var eventListenerForBinaryMessage: ((_ data: Data) -> Void)?
	var lostStates = Array<String>()
	var lostMessages = Array<RTCDataBuffer>()


	/**
	 * Constructor for pc.createDataChannel().
	 */
	init(
		rtcPeerConnection: RTCPeerConnection,
		label: String,
		options: NSDictionary?,
		eventListener: @escaping (_ data: NSDictionary) -> Void,
		eventListenerForBinaryMessage: @escaping (_ data: Data) -> Void
	) {
		NSLog("PluginRTCDataChannel#init()")

		self.eventListener = eventListener
		self.eventListenerForBinaryMessage = eventListenerForBinaryMessage

		let rtcDataChannelInit = RTCDataChannelConfiguration.init()

		if options?.object(forKey: "ordered") != nil {
			rtcDataChannelInit.isOrdered = options!.object(forKey: "ordered") as! Bool
		}

		if options?.object(forKey: "maxPacketLifeTime") != nil {
			// TODO: rtcDataChannel.maxRetransmitTime always reports 0.
			rtcDataChannelInit.maxRetransmitTimeMs = options!.object(forKey: "maxPacketLifeTime") as! Int
		}

		if options?.object(forKey: "maxRetransmits") != nil {
			rtcDataChannelInit.maxRetransmits = options!.object(forKey: "maxRetransmits") as! Int32
		}

		// TODO: error: expected member name following '.'
		//   https://code.google.com/p/webrtc/issues/detail?id=4614
		// if options?.objectForKey("protocol") != nil {
			// rtcDataChannelInit.protocol = options!.objectForKey("protocol") as! String
		// }
		if options?.object(forKey: "protocol") != nil {
			rtcDataChannelInit.`protocol` = options!.object(forKey: "protocol") as! String
		}

		if options?.object(forKey: "negotiated") != nil {
			rtcDataChannelInit.isNegotiated = options!.object(forKey: "negotiated") as! Bool
		}

		if options?.object(forKey: "id") != nil {
			rtcDataChannelInit.channelId = options!.object(forKey: "id") as! Int32
		}

		self.rtcDataChannel = rtcPeerConnection.dataChannel(forLabel: label, configuration: rtcDataChannelInit)

		if self.rtcDataChannel == nil {
			NSLog("PluginRTCDataChannel#init() | rtcPeerConnection.createDataChannelWithLabel() failed")
			return
		}

		// Report definitive data to update the JS instance.
		self.eventListener!([
			"type": "new",
			"channel": [
				"ordered": self.rtcDataChannel!.isOrdered,
				"maxPacketLifeTime": self.rtcDataChannel!.maxPacketLifeTime,
				"maxRetransmits": self.rtcDataChannel!.maxRetransmits,
				"protocol": self.rtcDataChannel!.`protocol`,
				"negotiated": self.rtcDataChannel!.isNegotiated,
				"id": self.rtcDataChannel!.channelId,
				//"readyState": self.rtcDataChannel!.readyState,
				//"readyState": PluginRTCTypes.dataChannelStates[self.rtcDataChannel!.readyState.rawValue] as String!,
				"readyState": PluginRTCDataChannel.stateToString(state: self.rtcDataChannel!.readyState),
				"bufferedAmount": self.rtcDataChannel!.bufferedAmount
			]
		])
	}


	deinit {
		NSLog("PluginRTCDataChannel#deinit()")
	}


	/**
	 * Constructor for pc.ondatachannel event.
	 */
	init(rtcDataChannel: RTCDataChannel) {
		NSLog("PluginRTCDataChannel#init()")

		self.rtcDataChannel = rtcDataChannel
	}


	func run() {
		NSLog("PluginRTCDataChannel#run()")

		self.rtcDataChannel!.delegate = self

		//if data channel is created after there is a connection,
		// we need to dispatch its current state.
		if (self.rtcDataChannel?.readyState != RTCDataChannelState.connecting) {
			dataChannelDidChangeState(self.rtcDataChannel!);
{'fraction_non_alphanumeric': 0.12587244283995186, 'fraction_numerical': 0.0021660649819494585, 'mean_word_length': 4.478576137112722, 'pattern_counts': {'":': 22, '<': 17, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1759518', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2542, 'n_words': 593}