@telnyx/react-voice-commons-sdk 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +483 -0
  2. package/TelnyxVoiceCommons.podspec +31 -31
  3. package/ios/CallKitBridge.m +43 -43
  4. package/ios/CallKitBridge.swift +879 -879
  5. package/ios/VoicePnBridge.m +30 -30
  6. package/ios/VoicePnBridge.swift +86 -86
  7. package/lib/callkit/callkit-coordinator.d.ts +117 -113
  8. package/lib/callkit/callkit-coordinator.js +727 -681
  9. package/lib/callkit/callkit.d.ts +41 -41
  10. package/lib/callkit/callkit.js +242 -252
  11. package/lib/callkit/index.js +47 -15
  12. package/lib/callkit/use-callkit.d.ts +19 -19
  13. package/lib/callkit/use-callkit.js +310 -270
  14. package/lib/context/TelnyxVoiceContext.d.ts +9 -9
  15. package/lib/context/TelnyxVoiceContext.js +13 -10
  16. package/lib/hooks/use-callkit-coordinator.d.ts +17 -9
  17. package/lib/hooks/use-callkit-coordinator.js +50 -45
  18. package/lib/hooks/useAppReadyNotifier.js +15 -13
  19. package/lib/hooks/useAppStateHandler.d.ts +11 -6
  20. package/lib/hooks/useAppStateHandler.js +110 -95
  21. package/lib/index.d.ts +21 -3
  22. package/lib/index.js +201 -50
  23. package/lib/internal/CallKitHandler.d.ts +6 -6
  24. package/lib/internal/CallKitHandler.js +104 -96
  25. package/lib/internal/callkit-manager.d.ts +57 -57
  26. package/lib/internal/callkit-manager.js +316 -299
  27. package/lib/internal/calls/call-state-controller.d.ts +86 -81
  28. package/lib/internal/calls/call-state-controller.js +307 -269
  29. package/lib/internal/session/session-manager.d.ts +75 -75
  30. package/lib/internal/session/session-manager.js +424 -350
  31. package/lib/internal/user-defaults-helpers.js +39 -49
  32. package/lib/internal/voice-pn-bridge.d.ts +11 -11
  33. package/lib/internal/voice-pn-bridge.js +3 -3
  34. package/lib/models/call-state.d.ts +44 -44
  35. package/lib/models/call-state.js +68 -66
  36. package/lib/models/call.d.ts +133 -133
  37. package/lib/models/call.js +382 -354
  38. package/lib/models/config.d.ts +18 -11
  39. package/lib/models/config.js +35 -37
  40. package/lib/models/connection-state.d.ts +10 -10
  41. package/lib/models/connection-state.js +16 -16
  42. package/lib/telnyx-voice-app.d.ts +28 -28
  43. package/lib/telnyx-voice-app.js +482 -424
  44. package/lib/telnyx-voip-client.d.ts +167 -155
  45. package/lib/telnyx-voip-client.js +392 -331
  46. package/package.json +1 -1
  47. package/src/telnyx-voip-client.ts +64 -0
@@ -1,11 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.CallStateController = void 0;
4
- const rxjs_1 = require("rxjs");
5
- const operators_1 = require("rxjs/operators");
6
- const call_1 = require("../../models/call");
7
- const call_state_1 = require("../../models/call-state");
8
- const callkit_coordinator_1 = require("../../callkit/callkit-coordinator");
4
+ const rxjs_1 = require('rxjs');
5
+ const operators_1 = require('rxjs/operators');
6
+ const call_1 = require('../../models/call');
7
+ const call_state_1 = require('../../models/call-state');
8
+ const callkit_coordinator_1 = require('../../callkit/callkit-coordinator');
9
9
  /**
10
10
  * Central state machine for call management.
11
11
  *
@@ -13,282 +13,320 @@ const callkit_coordinator_1 = require("../../callkit/callkit-coordinator");
13
13
  * and provides reactive streams for call-related state changes.
14
14
  */
15
15
  class CallStateController {
16
- constructor(_sessionManager) {
17
- this._sessionManager = _sessionManager;
18
- this._calls = new rxjs_1.BehaviorSubject([]);
19
- this._callMap = new Map();
20
- this._disposed = false;
21
- // Don't set up client listeners here - client doesn't exist yet
22
- // Will be called when client is available
16
+ constructor(_sessionManager) {
17
+ this._sessionManager = _sessionManager;
18
+ this._calls = new rxjs_1.BehaviorSubject([]);
19
+ this._callMap = new Map();
20
+ this._disposed = false;
21
+ // Don't set up client listeners here - client doesn't exist yet
22
+ // Will be called when client is available
23
+ }
24
+ /**
25
+ * Observable stream of all current calls
26
+ */
27
+ get calls$() {
28
+ return this._calls.asObservable().pipe((0, operators_1.distinctUntilChanged)());
29
+ }
30
+ /**
31
+ * Observable stream of the currently active call
32
+ */
33
+ get activeCall$() {
34
+ return this.calls$.pipe(
35
+ (0, operators_1.map)((calls) => {
36
+ // Find the first call that is not terminated (includes RINGING, CONNECTING, ACTIVE, HELD)
37
+ return (
38
+ calls.find(
39
+ (call) =>
40
+ call.currentState === call_state_1.TelnyxCallState.RINGING ||
41
+ call.currentState === call_state_1.TelnyxCallState.CONNECTING ||
42
+ call.currentState === call_state_1.TelnyxCallState.ACTIVE ||
43
+ call.currentState === call_state_1.TelnyxCallState.HELD
44
+ ) || null
45
+ );
46
+ }),
47
+ (0, operators_1.distinctUntilChanged)()
48
+ );
49
+ }
50
+ /**
51
+ * Current list of calls (synchronous access)
52
+ */
53
+ get currentCalls() {
54
+ return this._calls.value;
55
+ }
56
+ /**
57
+ * Current active call (synchronous access)
58
+ */
59
+ get currentActiveCall() {
60
+ const calls = this.currentCalls;
61
+ return (
62
+ calls.find(
63
+ (call) =>
64
+ call.currentState === call_state_1.TelnyxCallState.RINGING ||
65
+ call.currentState === call_state_1.TelnyxCallState.CONNECTING ||
66
+ call.currentState === call_state_1.TelnyxCallState.ACTIVE ||
67
+ call.currentState === call_state_1.TelnyxCallState.HELD
68
+ ) || null
69
+ );
70
+ }
71
+ /**
72
+ * Set a call to connecting state (used for push notification calls when answered via CallKit)
73
+ * @param callId The ID of the call to set to connecting state
74
+ */
75
+ setCallConnecting(callId) {
76
+ const call = this._callMap.get(callId);
77
+ if (call) {
78
+ console.log('CallStateController: Setting call to connecting state:', callId);
79
+ call.setConnecting();
80
+ } else {
81
+ console.warn('CallStateController: Could not find call to set connecting:', callId);
23
82
  }
24
- /**
25
- * Observable stream of all current calls
26
- */
27
- get calls$() {
28
- return this._calls.asObservable().pipe((0, operators_1.distinctUntilChanged)());
83
+ }
84
+ /**
85
+ * Find a call by its underlying Telnyx call ID
86
+ * @param telnyxCall The Telnyx call object to find
87
+ */
88
+ findCallByTelnyxCall(telnyxCall) {
89
+ for (const call of this._callMap.values()) {
90
+ if (call.telnyxCall === telnyxCall || call.telnyxCall.callId === telnyxCall.callId) {
91
+ return call;
92
+ }
29
93
  }
30
- /**
31
- * Observable stream of the currently active call
32
- */
33
- get activeCall$() {
34
- return this.calls$.pipe((0, operators_1.map)((calls) => {
35
- // Find the first call that is not terminated (includes RINGING, CONNECTING, ACTIVE, HELD)
36
- return (calls.find((call) => call.currentState === call_state_1.TelnyxCallState.RINGING ||
37
- call.currentState === call_state_1.TelnyxCallState.CONNECTING ||
38
- call.currentState === call_state_1.TelnyxCallState.ACTIVE ||
39
- call.currentState === call_state_1.TelnyxCallState.HELD) || null);
40
- }), (0, operators_1.distinctUntilChanged)());
94
+ return null;
95
+ }
96
+ /**
97
+ * Initialize client listeners when the Telnyx client becomes available
98
+ * This should be called by the session manager after client creation
99
+ */
100
+ initializeClientListeners() {
101
+ console.log('🔧 CallStateController: initializeClientListeners called');
102
+ this._setupClientListeners();
103
+ // CallKit integration now handled by CallKitCoordinator
104
+ console.log('🔧 CallStateController: Using CallKitCoordinator for CallKit integration');
105
+ }
106
+ /**
107
+ * Initiate a new outgoing call
108
+ */
109
+ async newCall(destination, callerName, callerNumber, debug = false) {
110
+ if (this._disposed) {
111
+ throw new Error('CallStateController has been disposed');
41
112
  }
42
- /**
43
- * Current list of calls (synchronous access)
44
- */
45
- get currentCalls() {
46
- return this._calls.value;
113
+ if (!this._sessionManager.telnyxClient) {
114
+ throw new Error('Telnyx client not available');
47
115
  }
48
- /**
49
- * Current active call (synchronous access)
50
- */
51
- get currentActiveCall() {
52
- const calls = this.currentCalls;
53
- return (calls.find((call) => call.currentState === call_state_1.TelnyxCallState.RINGING ||
54
- call.currentState === call_state_1.TelnyxCallState.CONNECTING ||
55
- call.currentState === call_state_1.TelnyxCallState.ACTIVE ||
56
- call.currentState === call_state_1.TelnyxCallState.HELD) || null);
116
+ try {
117
+ // Create the call using the Telnyx SDK
118
+ const callOptions = {
119
+ destinationNumber: destination,
120
+ callerIdName: callerName,
121
+ callerIdNumber: callerNumber,
122
+ };
123
+ const telnyxCall = await this._sessionManager.telnyxClient.newCall(callOptions);
124
+ // Create our wrapper Call object
125
+ const call = new call_1.Call(
126
+ telnyxCall,
127
+ telnyxCall.callId || this._generateCallId(),
128
+ destination,
129
+ false // outgoing call
130
+ );
131
+ // Add to our call tracking
132
+ this._addCall(call);
133
+ return call;
134
+ } catch (error) {
135
+ console.error('Failed to create new call:', error);
136
+ throw error;
57
137
  }
58
- /**
59
- * Set a call to connecting state (used for push notification calls when answered via CallKit)
60
- * @param callId The ID of the call to set to connecting state
61
- */
62
- setCallConnecting(callId) {
63
- const call = this._callMap.get(callId);
64
- if (call) {
65
- console.log('CallStateController: Setting call to connecting state:', callId);
66
- call.setConnecting();
67
- }
68
- else {
69
- console.warn('CallStateController: Could not find call to set connecting:', callId);
70
- }
138
+ }
139
+ /**
140
+ * Set callbacks for waiting for invite logic (used for push notifications)
141
+ */
142
+ setWaitingForInviteCallbacks(callbacks) {
143
+ this._isWaitingForInvite = callbacks.isWaitingForInvite;
144
+ this._onInviteAutoAccepted = callbacks.onInviteAutoAccepted;
145
+ }
146
+ /**
147
+ * Dispose of the controller and clean up resources
148
+ */
149
+ dispose() {
150
+ if (this._disposed) {
151
+ return;
71
152
  }
72
- /**
73
- * Find a call by its underlying Telnyx call ID
74
- * @param telnyxCall The Telnyx call object to find
75
- */
76
- findCallByTelnyxCall(telnyxCall) {
77
- for (const call of this._callMap.values()) {
78
- if (call.telnyxCall === telnyxCall || call.telnyxCall.callId === telnyxCall.callId) {
79
- return call;
80
- }
81
- }
82
- return null;
153
+ this._disposed = true;
154
+ // Dispose of all calls
155
+ this.currentCalls.forEach((call) => call.dispose());
156
+ this._callMap.clear();
157
+ // CallKit cleanup is now handled by CallKitCoordinator automatically
158
+ this._calls.complete();
159
+ }
160
+ /**
161
+ * Set up event listeners for the Telnyx client
162
+ */
163
+ _setupClientListeners() {
164
+ console.log('🔧 CallStateController: Setting up client listeners...');
165
+ if (!this._sessionManager.telnyxClient) {
166
+ console.log('🔧 CallStateController: No telnyxClient available yet, skipping listener setup');
167
+ return;
83
168
  }
84
- /**
85
- * Initialize client listeners when the Telnyx client becomes available
86
- * This should be called by the session manager after client creation
87
- */
88
- initializeClientListeners() {
89
- console.log('🔧 CallStateController: initializeClientListeners called');
90
- this._setupClientListeners();
91
- // CallKit integration now handled by CallKitCoordinator
92
- console.log('🔧 CallStateController: Using CallKitCoordinator for CallKit integration');
169
+ console.log('🔧 CallStateController: TelnyxClient found, setting up incoming call listener');
170
+ // Listen for incoming calls
171
+ this._sessionManager.telnyxClient.on('telnyx.call.incoming', (telnyxCall, msg) => {
172
+ console.log('📞 CallStateController: Incoming call received:', telnyxCall.callId);
173
+ this._handleIncomingCall(telnyxCall, msg);
174
+ });
175
+ // Listen for other call events if needed
176
+ // this._sessionManager.telnyxClient.on('telnyx.call.stateChange', this._handleCallStateChange.bind(this));
177
+ console.log('🔧 CallStateController: Client listeners set up successfully');
178
+ }
179
+ /**
180
+ * Handle incoming call
181
+ */
182
+ _handleIncomingCall(telnyxCall, inviteMsg) {
183
+ const callId = telnyxCall.callId || this._generateCallId();
184
+ console.log('📞 CallStateController: Handling incoming call:', callId);
185
+ console.log('📞 CallStateController: TelnyxCall object:', telnyxCall);
186
+ console.log('📞 CallStateController: Invite message:', inviteMsg);
187
+ // Check if we already have this call
188
+ if (this._callMap.has(callId)) {
189
+ console.log('Call already exists:', callId);
190
+ return;
93
191
  }
94
- /**
95
- * Initiate a new outgoing call
96
- */
97
- async newCall(destination, callerName, callerNumber, debug = false) {
98
- if (this._disposed) {
99
- throw new Error('CallStateController has been disposed');
100
- }
101
- if (!this._sessionManager.telnyxClient) {
102
- throw new Error('Telnyx client not available');
103
- }
104
- try {
105
- // Create the call using the Telnyx SDK
106
- const callOptions = {
107
- destinationNumber: destination,
108
- callerIdName: callerName,
109
- callerIdNumber: callerNumber,
110
- };
111
- const telnyxCall = await this._sessionManager.telnyxClient.newCall(callOptions);
112
- // Create our wrapper Call object
113
- const call = new call_1.Call(telnyxCall, telnyxCall.callId || this._generateCallId(), destination, false // outgoing call
114
- );
115
- // Add to our call tracking
116
- this._addCall(call);
117
- return call;
118
- }
119
- catch (error) {
120
- console.error('Failed to create new call:', error);
121
- throw error;
122
- }
192
+ // Get caller information from the invite message (preferred) or fallback to TelnyxCall
193
+ let callerNumber = 'Unknown';
194
+ let callerName = 'Unknown';
195
+ if (inviteMsg && inviteMsg.params) {
196
+ callerNumber = inviteMsg.params.caller_id_number || 'Unknown';
197
+ callerName = inviteMsg.params.caller_id_name || callerNumber;
198
+ console.log(
199
+ '📞 CallStateController: Extracted caller info from invite - Number:',
200
+ callerNumber,
201
+ 'Name:',
202
+ callerName
203
+ );
204
+ } else {
205
+ // Fallback to TelnyxCall properties
206
+ callerNumber = telnyxCall.remoteCallerIdNumber || 'Unknown';
207
+ callerName = telnyxCall.remoteCallerIdName || callerNumber;
208
+ console.log(
209
+ '📞 CallStateController: Extracted caller info from TelnyxCall - Number:',
210
+ callerNumber,
211
+ 'Name:',
212
+ callerName
213
+ );
123
214
  }
124
- /**
125
- * Set callbacks for waiting for invite logic (used for push notifications)
126
- */
127
- setWaitingForInviteCallbacks(callbacks) {
128
- this._isWaitingForInvite = callbacks.isWaitingForInvite;
129
- this._onInviteAutoAccepted = callbacks.onInviteAutoAccepted;
215
+ // Create our wrapper Call object
216
+ const call = new call_1.Call(
217
+ telnyxCall,
218
+ callId,
219
+ callerNumber, // Use caller number as destination for incoming calls
220
+ true // incoming call
221
+ );
222
+ // Check if we're waiting for an invite (push notification scenario)
223
+ if (this._isWaitingForInvite && this._isWaitingForInvite()) {
224
+ console.log('Auto-accepting call from push notification');
225
+ call.answer().catch((error) => {
226
+ console.error('Failed to auto-accept call:', error);
227
+ });
228
+ if (this._onInviteAutoAccepted) {
229
+ this._onInviteAutoAccepted();
230
+ }
130
231
  }
131
- /**
132
- * Dispose of the controller and clean up resources
133
- */
134
- dispose() {
135
- if (this._disposed) {
136
- return;
137
- }
138
- this._disposed = true;
139
- // Dispose of all calls
140
- this.currentCalls.forEach((call) => call.dispose());
141
- this._callMap.clear();
142
- // CallKit cleanup is now handled by CallKitCoordinator automatically
143
- this._calls.complete();
232
+ // Add to our call tracking - CallKit integration happens in _addCall
233
+ this._addCall(call);
234
+ }
235
+ /**
236
+ * Handle call state changes from the Telnyx client
237
+ */
238
+ _handleCallStateChange(event) {
239
+ const callId = event.callId || event.id;
240
+ const call = this._callMap.get(callId);
241
+ if (call) {
242
+ // The Call object will handle its own state updates through its listeners
243
+ console.log(`Call ${callId} state changed to ${event.state}`);
244
+ } else {
245
+ console.warn(`Received state change for unknown call: ${callId}`);
144
246
  }
145
- /**
146
- * Set up event listeners for the Telnyx client
147
- */
148
- _setupClientListeners() {
149
- console.log('🔧 CallStateController: Setting up client listeners...');
150
- if (!this._sessionManager.telnyxClient) {
151
- console.log('🔧 CallStateController: No telnyxClient available yet, skipping listener setup');
152
- return;
153
- }
154
- console.log('🔧 CallStateController: TelnyxClient found, setting up incoming call listener');
155
- // Listen for incoming calls
156
- this._sessionManager.telnyxClient.on('telnyx.call.incoming', (telnyxCall, msg) => {
157
- console.log('📞 CallStateController: Incoming call received:', telnyxCall.callId);
158
- this._handleIncomingCall(telnyxCall, msg);
159
- });
160
- // Listen for other call events if needed
161
- // this._sessionManager.telnyxClient.on('telnyx.call.stateChange', this._handleCallStateChange.bind(this));
162
- console.log('🔧 CallStateController: Client listeners set up successfully');
247
+ }
248
+ /**
249
+ * Handle call updates from notifications
250
+ */
251
+ _handleCallUpdate(callData) {
252
+ const callId = callData.id;
253
+ const call = this._callMap.get(callId);
254
+ if (call) {
255
+ // Update call state based on the notification
256
+ console.log(`Call ${callId} updated:`, callData);
257
+ } else {
258
+ console.warn(`Received update for unknown call: ${callId}`);
163
259
  }
164
- /**
165
- * Handle incoming call
166
- */
167
- _handleIncomingCall(telnyxCall, inviteMsg) {
168
- const callId = telnyxCall.callId || this._generateCallId();
169
- console.log('📞 CallStateController: Handling incoming call:', callId);
170
- console.log('📞 CallStateController: TelnyxCall object:', telnyxCall);
171
- console.log('📞 CallStateController: Invite message:', inviteMsg);
172
- // Check if we already have this call
173
- if (this._callMap.has(callId)) {
174
- console.log('Call already exists:', callId);
175
- return;
176
- }
177
- // Get caller information from the invite message (preferred) or fallback to TelnyxCall
178
- let callerNumber = 'Unknown';
179
- let callerName = 'Unknown';
180
- if (inviteMsg && inviteMsg.params) {
181
- callerNumber = inviteMsg.params.caller_id_number || 'Unknown';
182
- callerName = inviteMsg.params.caller_id_name || callerNumber;
183
- console.log('📞 CallStateController: Extracted caller info from invite - Number:', callerNumber, 'Name:', callerName);
184
- }
185
- else {
186
- // Fallback to TelnyxCall properties
187
- callerNumber = telnyxCall.remoteCallerIdNumber || 'Unknown';
188
- callerName = telnyxCall.remoteCallerIdName || callerNumber;
189
- console.log('📞 CallStateController: Extracted caller info from TelnyxCall - Number:', callerNumber, 'Name:', callerName);
190
- }
191
- // Create our wrapper Call object
192
- const call = new call_1.Call(telnyxCall, callId, callerNumber, // Use caller number as destination for incoming calls
193
- true // incoming call
260
+ }
261
+ /**
262
+ * Add a call to our tracking
263
+ */
264
+ _addCall(call) {
265
+ this._callMap.set(call.callId, call);
266
+ const currentCalls = this.currentCalls;
267
+ currentCalls.push(call);
268
+ this._calls.next([...currentCalls]);
269
+ // Integrate with CallKit using CallKitCoordinator
270
+ if (callkit_coordinator_1.callKitCoordinator.isAvailable()) {
271
+ // Get the underlying TelnyxCall for CallKitCoordinator
272
+ const telnyxCall = call.telnyxCall;
273
+ // Check if this call already has CallKit integration (e.g., from push notification)
274
+ const existingCallKitUUID =
275
+ callkit_coordinator_1.callKitCoordinator.getCallKitUUID(telnyxCall);
276
+ if (existingCallKitUUID) {
277
+ console.log(
278
+ 'CallStateController: Call already has CallKit integration, skipping duplicate report:',
279
+ existingCallKitUUID
194
280
  );
195
- // Check if we're waiting for an invite (push notification scenario)
196
- if (this._isWaitingForInvite && this._isWaitingForInvite()) {
197
- console.log('Auto-accepting call from push notification');
198
- call.answer().catch((error) => {
199
- console.error('Failed to auto-accept call:', error);
200
- });
201
- if (this._onInviteAutoAccepted) {
202
- this._onInviteAutoAccepted();
203
- }
204
- }
205
- // Add to our call tracking - CallKit integration happens in _addCall
206
- this._addCall(call);
207
- }
208
- /**
209
- * Handle call state changes from the Telnyx client
210
- */
211
- _handleCallStateChange(event) {
212
- const callId = event.callId || event.id;
213
- const call = this._callMap.get(callId);
214
- if (call) {
215
- // The Call object will handle its own state updates through its listeners
216
- console.log(`Call ${callId} state changed to ${event.state}`);
217
- }
218
- else {
219
- console.warn(`Received state change for unknown call: ${callId}`);
220
- }
221
- }
222
- /**
223
- * Handle call updates from notifications
224
- */
225
- _handleCallUpdate(callData) {
226
- const callId = callData.id;
227
- const call = this._callMap.get(callId);
228
- if (call) {
229
- // Update call state based on the notification
230
- console.log(`Call ${callId} updated:`, callData);
231
- }
232
- else {
233
- console.warn(`Received update for unknown call: ${callId}`);
234
- }
235
- }
236
- /**
237
- * Add a call to our tracking
238
- */
239
- _addCall(call) {
240
- this._callMap.set(call.callId, call);
241
- const currentCalls = this.currentCalls;
242
- currentCalls.push(call);
243
- this._calls.next([...currentCalls]);
244
- // Integrate with CallKit using CallKitCoordinator
245
- if (callkit_coordinator_1.callKitCoordinator.isAvailable()) {
246
- // Get the underlying TelnyxCall for CallKitCoordinator
247
- const telnyxCall = call.telnyxCall;
248
- // Check if this call already has CallKit integration (e.g., from push notification)
249
- const existingCallKitUUID = callkit_coordinator_1.callKitCoordinator.getCallKitUUID(telnyxCall);
250
- if (existingCallKitUUID) {
251
- console.log('CallStateController: Call already has CallKit integration, skipping duplicate report:', existingCallKitUUID);
252
- }
253
- else if (call.isIncoming) {
254
- // Handle incoming call with CallKit (only if not already integrated)
255
- console.log('CallStateController: Reporting incoming call to CallKitCoordinator');
256
- callkit_coordinator_1.callKitCoordinator.reportIncomingCall(telnyxCall, call.destination, call.destination);
257
- }
258
- else {
259
- // Handle outgoing call with CallKit
260
- console.log('CallStateController: Starting outgoing call with CallKitCoordinator');
261
- callkit_coordinator_1.callKitCoordinator.startOutgoingCall(telnyxCall, call.destination, call.destination);
262
- }
263
- }
264
- // Listen for call state changes - CallKitCoordinator handles this automatically
265
- call.callState$.subscribe((state) => {
266
- // CallKitCoordinator automatically updates CallKit via setupWebRTCCallListeners
267
- console.log('CallStateController: Call state changed to:', state);
268
- // Clean up when call ends
269
- if (state === call_state_1.TelnyxCallState.ENDED || state === call_state_1.TelnyxCallState.FAILED) {
270
- this._removeCall(call.callId);
271
- }
272
- });
273
- }
274
- /**
275
- * Remove a call from our tracking
276
- */
277
- _removeCall(callId) {
278
- const call = this._callMap.get(callId);
279
- if (call) {
280
- // CallKit cleanup is handled automatically by CallKitCoordinator
281
- call.dispose();
282
- this._callMap.delete(callId);
283
- const currentCalls = this.currentCalls.filter((c) => c.callId !== callId);
284
- this._calls.next(currentCalls);
285
- }
281
+ } else if (call.isIncoming) {
282
+ // Handle incoming call with CallKit (only if not already integrated)
283
+ console.log('CallStateController: Reporting incoming call to CallKitCoordinator');
284
+ callkit_coordinator_1.callKitCoordinator.reportIncomingCall(
285
+ telnyxCall,
286
+ call.destination,
287
+ call.destination
288
+ );
289
+ } else {
290
+ // Handle outgoing call with CallKit
291
+ console.log('CallStateController: Starting outgoing call with CallKitCoordinator');
292
+ callkit_coordinator_1.callKitCoordinator.startOutgoingCall(
293
+ telnyxCall,
294
+ call.destination,
295
+ call.destination
296
+ );
297
+ }
286
298
  }
287
- /**
288
- * Generate a unique call ID
289
- */
290
- _generateCallId() {
291
- return `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
299
+ // Listen for call state changes - CallKitCoordinator handles this automatically
300
+ call.callState$.subscribe((state) => {
301
+ // CallKitCoordinator automatically updates CallKit via setupWebRTCCallListeners
302
+ console.log('CallStateController: Call state changed to:', state);
303
+ // Clean up when call ends
304
+ if (
305
+ state === call_state_1.TelnyxCallState.ENDED ||
306
+ state === call_state_1.TelnyxCallState.FAILED
307
+ ) {
308
+ this._removeCall(call.callId);
309
+ }
310
+ });
311
+ }
312
+ /**
313
+ * Remove a call from our tracking
314
+ */
315
+ _removeCall(callId) {
316
+ const call = this._callMap.get(callId);
317
+ if (call) {
318
+ // CallKit cleanup is handled automatically by CallKitCoordinator
319
+ call.dispose();
320
+ this._callMap.delete(callId);
321
+ const currentCalls = this.currentCalls.filter((c) => c.callId !== callId);
322
+ this._calls.next(currentCalls);
292
323
  }
324
+ }
325
+ /**
326
+ * Generate a unique call ID
327
+ */
328
+ _generateCallId() {
329
+ return `call_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
330
+ }
293
331
  }
294
332
  exports.CallStateController = CallStateController;