@elqnt/admin 2.0.5 → 2.2.0

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.
@@ -0,0 +1,1526 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // hooks/index.ts
21
+ var hooks_exports = {};
22
+ __export(hooks_exports, {
23
+ useAnalyticsContext: () => useAnalyticsContext,
24
+ useInvitesAdmin: () => useInvitesAdmin,
25
+ useOrgAdmin: () => useOrgAdmin,
26
+ useOrgProvisioning: () => useOrgProvisioning,
27
+ useOrgSettings: () => useOrgSettings,
28
+ useProductAnalytics: () => useProductAnalytics,
29
+ useUsersAdmin: () => useUsersAdmin
30
+ });
31
+ module.exports = __toCommonJS(hooks_exports);
32
+
33
+ // hooks/use-org-admin.ts
34
+ var import_react = require("react");
35
+
36
+ // api/index.ts
37
+ var import_browser6 = require("@elqnt/api-client/browser");
38
+
39
+ // api/orgs.ts
40
+ var import_browser = require("@elqnt/api-client/browser");
41
+ async function listOrgsApi(filter, options) {
42
+ const params = new URLSearchParams();
43
+ if (filter?.product) params.set("product", filter.product);
44
+ if (filter?.status) params.set("status", filter.status);
45
+ if (filter?.type) params.set("type", filter.type);
46
+ const queryString = params.toString();
47
+ const url = queryString ? `/api/v1/admin/orgs?${queryString}` : "/api/v1/admin/orgs";
48
+ return (0, import_browser.browserApiRequest)(url, {
49
+ method: "GET",
50
+ ...options
51
+ });
52
+ }
53
+ async function createOrgApi(org, options) {
54
+ return (0, import_browser.browserApiRequest)("/api/v1/admin/orgs", {
55
+ method: "POST",
56
+ body: org,
57
+ ...options
58
+ });
59
+ }
60
+ async function getOrgApi(orgId, options) {
61
+ return (0, import_browser.browserApiRequest)(`/api/v1/admin/orgs/${orgId}`, {
62
+ method: "GET",
63
+ ...options
64
+ });
65
+ }
66
+ async function updateOrgApi(orgId, updates, options) {
67
+ return (0, import_browser.browserApiRequest)(`/api/v1/admin/orgs/${orgId}`, {
68
+ method: "PUT",
69
+ body: updates,
70
+ ...options
71
+ });
72
+ }
73
+ async function deleteOrgApi(orgId, options) {
74
+ return (0, import_browser.browserApiRequest)(`/api/v1/admin/orgs/${orgId}`, {
75
+ method: "DELETE",
76
+ ...options
77
+ });
78
+ }
79
+ async function getOrgInfoApi(orgId, options) {
80
+ return (0, import_browser.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/info`, {
81
+ method: "GET",
82
+ ...options
83
+ });
84
+ }
85
+ async function createOrgWithSchemasApi(org, schemas, options) {
86
+ return (0, import_browser.browserApiRequest)("/api/v1/admin/orgs/with-schemas", {
87
+ method: "POST",
88
+ body: { org, schemas },
89
+ ...options
90
+ });
91
+ }
92
+
93
+ // api/users.ts
94
+ var import_browser2 = require("@elqnt/api-client/browser");
95
+ async function listUsersApi(options) {
96
+ return (0, import_browser2.browserApiRequest)("/api/v1/admin/users", {
97
+ method: "GET",
98
+ ...options
99
+ });
100
+ }
101
+ async function createUserApi(user, options) {
102
+ return (0, import_browser2.browserApiRequest)("/api/v1/admin/users", {
103
+ method: "POST",
104
+ body: user,
105
+ ...options
106
+ });
107
+ }
108
+ async function getUserApi(userId, options) {
109
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/${userId}`, {
110
+ method: "GET",
111
+ ...options
112
+ });
113
+ }
114
+ async function getUserByEmailApi(email, options) {
115
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/by-email?email=${encodeURIComponent(email)}`, {
116
+ method: "GET",
117
+ ...options
118
+ });
119
+ }
120
+ async function getUserByPhoneApi(phone, options) {
121
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/by-phone?phone=${encodeURIComponent(phone)}`, {
122
+ method: "GET",
123
+ ...options
124
+ });
125
+ }
126
+ async function updateUserApi(userId, updates, options) {
127
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/${userId}`, {
128
+ method: "PUT",
129
+ body: updates,
130
+ ...options
131
+ });
132
+ }
133
+ async function deleteUserApi(userId, options) {
134
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/${userId}`, {
135
+ method: "DELETE",
136
+ ...options
137
+ });
138
+ }
139
+ async function getUserSettingsApi(userId, options) {
140
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/${userId}/settings`, {
141
+ method: "GET",
142
+ ...options
143
+ });
144
+ }
145
+ async function updateUserSettingsApi(userId, settings, options) {
146
+ return (0, import_browser2.browserApiRequest)(`/api/v1/admin/users/${userId}/settings`, {
147
+ method: "PUT",
148
+ body: settings,
149
+ ...options
150
+ });
151
+ }
152
+
153
+ // api/invites.ts
154
+ var import_browser3 = require("@elqnt/api-client/browser");
155
+ async function listInvitesApi(options) {
156
+ return (0, import_browser3.browserApiRequest)("/api/v1/admin/invites", {
157
+ method: "GET",
158
+ ...options
159
+ });
160
+ }
161
+ async function sendInvitesApi(invites, options) {
162
+ return (0, import_browser3.browserApiRequest)("/api/v1/admin/invites", {
163
+ method: "POST",
164
+ body: { invites },
165
+ ...options
166
+ });
167
+ }
168
+ async function sendInviteApi(invite, options) {
169
+ return (0, import_browser3.browserApiRequest)("/api/v1/admin/invites/single", {
170
+ method: "POST",
171
+ body: invite,
172
+ ...options
173
+ });
174
+ }
175
+ async function getInviteApi(inviteId, options) {
176
+ return (0, import_browser3.browserApiRequest)(`/api/v1/admin/invites/${inviteId}`, {
177
+ method: "GET",
178
+ ...options
179
+ });
180
+ }
181
+ async function resendInviteApi(inviteId, options) {
182
+ return (0, import_browser3.browserApiRequest)(`/api/v1/admin/invites/${inviteId}/resend`, {
183
+ method: "POST",
184
+ ...options
185
+ });
186
+ }
187
+ async function revokeInviteApi(inviteId, options) {
188
+ return (0, import_browser3.browserApiRequest)(`/api/v1/admin/invites/${inviteId}`, {
189
+ method: "DELETE",
190
+ ...options
191
+ });
192
+ }
193
+ async function acceptInviteApi(inviteId, options) {
194
+ return (0, import_browser3.browserApiRequest)(`/api/v1/admin/invites/${inviteId}/accept`, {
195
+ method: "POST",
196
+ ...options
197
+ });
198
+ }
199
+
200
+ // api/analytics.ts
201
+ var import_browser4 = require("@elqnt/api-client/browser");
202
+ function buildDateFilterParams(filter) {
203
+ if (!filter) return "";
204
+ const params = new URLSearchParams();
205
+ if (filter.from) params.set("from", filter.from);
206
+ if (filter.to) params.set("to", filter.to);
207
+ const queryString = params.toString();
208
+ return queryString ? `?${queryString}` : "";
209
+ }
210
+ async function getAnalyticsSummaryApi(filter, options) {
211
+ const queryString = buildDateFilterParams(filter);
212
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/summary${queryString}`, {
213
+ method: "GET",
214
+ ...options
215
+ });
216
+ }
217
+ async function getChatsAnalyticsApi(filter, agentId, options) {
218
+ const params = new URLSearchParams();
219
+ if (filter?.from) params.set("from", filter.from);
220
+ if (filter?.to) params.set("to", filter.to);
221
+ if (agentId) params.set("agent_id", agentId);
222
+ const queryString = params.toString();
223
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/chats${queryString ? `?${queryString}` : ""}`, {
224
+ method: "GET",
225
+ ...options
226
+ });
227
+ }
228
+ async function getAgentsAnalyticsApi(filter, options) {
229
+ const queryString = buildDateFilterParams(filter);
230
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/agents${queryString}`, {
231
+ method: "GET",
232
+ ...options
233
+ });
234
+ }
235
+ async function getUsageAnalyticsApi(filter, options) {
236
+ const queryString = buildDateFilterParams(filter);
237
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/usage${queryString}`, {
238
+ method: "GET",
239
+ ...options
240
+ });
241
+ }
242
+ async function getDailyAnalyticsApi(filter, options) {
243
+ const queryString = buildDateFilterParams(filter);
244
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/daily${queryString}`, {
245
+ method: "GET",
246
+ ...options
247
+ });
248
+ }
249
+ async function getAnalyticsEventsApi(filter, options) {
250
+ const queryString = buildDateFilterParams(filter);
251
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/events${queryString}`, {
252
+ method: "GET",
253
+ ...options
254
+ });
255
+ }
256
+ async function logAnalyticsEventApi(event, options) {
257
+ return (0, import_browser4.browserApiRequest)("/api/v1/analytics/events", {
258
+ method: "POST",
259
+ body: event,
260
+ ...options
261
+ });
262
+ }
263
+ async function getGlobalSummaryApi(filter, options) {
264
+ const queryString = buildDateFilterParams(filter);
265
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/global/summary${queryString}`, {
266
+ method: "GET",
267
+ ...options
268
+ });
269
+ }
270
+ async function getGlobalOrgsAnalyticsApi(filter, options) {
271
+ const queryString = buildDateFilterParams(filter);
272
+ return (0, import_browser4.browserApiRequest)(`/api/v1/analytics/global/orgs${queryString}`, {
273
+ method: "GET",
274
+ ...options
275
+ });
276
+ }
277
+
278
+ // api/provisioning.ts
279
+ var import_browser5 = require("@elqnt/api-client/browser");
280
+ async function createOrgWithProvisioningApi(request, options) {
281
+ return (0, import_browser5.browserApiRequest)("/api/v1/admin/provisioning/orgs", {
282
+ method: "POST",
283
+ body: request,
284
+ ...options
285
+ });
286
+ }
287
+ async function getProvisioningStatusApi(orgId, options) {
288
+ return (0, import_browser5.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/provisioning`, {
289
+ method: "GET",
290
+ ...options
291
+ });
292
+ }
293
+ async function retryProvisioningApi(orgId, request, options) {
294
+ return (0, import_browser5.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/provisioning/retry`, {
295
+ method: "POST",
296
+ body: request || {},
297
+ ...options
298
+ });
299
+ }
300
+ async function validateProvisioningApi(orgId, options) {
301
+ return (0, import_browser5.browserApiRequest)(
302
+ `/api/v1/admin/orgs/${orgId}/provisioning/validate`,
303
+ {
304
+ method: "POST",
305
+ ...options
306
+ }
307
+ );
308
+ }
309
+ async function cancelProvisioningApi(orgId, options) {
310
+ return (0, import_browser5.browserApiRequest)(
311
+ `/api/v1/admin/orgs/${orgId}/provisioning/cancel`,
312
+ {
313
+ method: "POST",
314
+ ...options
315
+ }
316
+ );
317
+ }
318
+ async function cleanupProvisioningApi(orgId, options) {
319
+ return (0, import_browser5.browserApiRequest)(`/api/v1/admin/orgs/${orgId}/provisioning`, {
320
+ method: "DELETE",
321
+ ...options
322
+ });
323
+ }
324
+ function streamProvisioningProgress(orgId, callbacks, options) {
325
+ const url = new URL(
326
+ `/api/v1/admin/orgs/${orgId}/provisioning/stream`,
327
+ options.baseUrl
328
+ );
329
+ if (options.token) {
330
+ url.searchParams.set("token", options.token);
331
+ }
332
+ const eventSource = new EventSource(url.toString());
333
+ eventSource.addEventListener("connected", (event) => {
334
+ try {
335
+ const data = JSON.parse(event.data);
336
+ callbacks.onConnected?.(data.orgId);
337
+ } catch (e) {
338
+ console.error("Failed to parse connected event", e);
339
+ }
340
+ });
341
+ eventSource.addEventListener("progress", (event) => {
342
+ try {
343
+ const progress = JSON.parse(event.data);
344
+ callbacks.onProgress?.(progress);
345
+ } catch (e) {
346
+ console.error("Failed to parse progress event", e);
347
+ }
348
+ });
349
+ eventSource.addEventListener("done", () => {
350
+ callbacks.onDone?.();
351
+ eventSource.close();
352
+ });
353
+ eventSource.addEventListener("timeout", () => {
354
+ callbacks.onTimeout?.();
355
+ eventSource.close();
356
+ });
357
+ eventSource.onerror = (error) => {
358
+ callbacks.onError?.(new Error("SSE connection error"));
359
+ eventSource.close();
360
+ };
361
+ return () => {
362
+ eventSource.close();
363
+ };
364
+ }
365
+ function calculateProgressPercentage(progress) {
366
+ if (progress.totalArtifacts === 0) return 0;
367
+ return Math.round(
368
+ progress.completedArtifacts / progress.totalArtifacts * 100
369
+ );
370
+ }
371
+ function isProvisioningComplete(progress) {
372
+ return progress.status === "completed" || progress.status === "failed" || progress.status === "partial";
373
+ }
374
+ function isProvisioningSuccessful(progress) {
375
+ return progress.status === "completed";
376
+ }
377
+ function getFailedArtifacts(progress) {
378
+ return progress.artifacts.filter((a) => a.status === "failed");
379
+ }
380
+ function hasCriticalFailures(progress) {
381
+ return progress.artifacts.some(
382
+ (a) => a.status === "failed" && a.critical
383
+ );
384
+ }
385
+
386
+ // api/index.ts
387
+ async function getOrgSettingsApi(options) {
388
+ return (0, import_browser6.browserApiRequest)("/api/v1/org/settings", {
389
+ method: "GET",
390
+ ...options
391
+ });
392
+ }
393
+ async function createOrgSettingsApi(settings, options) {
394
+ return (0, import_browser6.browserApiRequest)("/api/v1/org/settings", {
395
+ method: "POST",
396
+ body: settings,
397
+ ...options
398
+ });
399
+ }
400
+ async function updateOrgSettingsApi(settings, options) {
401
+ return (0, import_browser6.browserApiRequest)("/api/v1/org/settings", {
402
+ method: "PUT",
403
+ body: settings,
404
+ ...options
405
+ });
406
+ }
407
+
408
+ // hooks/use-org-admin.ts
409
+ function useOrgAdmin(options) {
410
+ const [loading, setLoading] = (0, import_react.useState)(false);
411
+ const [error, setError] = (0, import_react.useState)(null);
412
+ const listOrgs = (0, import_react.useCallback)(async (filter) => {
413
+ setLoading(true);
414
+ setError(null);
415
+ try {
416
+ const response = await listOrgsApi(filter, options);
417
+ if (response.error) {
418
+ setError(response.error);
419
+ return [];
420
+ }
421
+ return response.data?.orgs || [];
422
+ } catch (err) {
423
+ const message = err instanceof Error ? err.message : "Failed to list organizations";
424
+ setError(message);
425
+ return [];
426
+ } finally {
427
+ setLoading(false);
428
+ }
429
+ }, [options]);
430
+ const getOrg = (0, import_react.useCallback)(
431
+ async (orgId) => {
432
+ setLoading(true);
433
+ setError(null);
434
+ try {
435
+ const response = await getOrgApi(orgId, options);
436
+ if (response.error) {
437
+ setError(response.error);
438
+ return null;
439
+ }
440
+ return response.data?.org || null;
441
+ } catch (err) {
442
+ const message = err instanceof Error ? err.message : "Failed to get organization";
443
+ setError(message);
444
+ return null;
445
+ } finally {
446
+ setLoading(false);
447
+ }
448
+ },
449
+ [options]
450
+ );
451
+ const getOrgInfo = (0, import_react.useCallback)(
452
+ async (orgId) => {
453
+ setLoading(true);
454
+ setError(null);
455
+ try {
456
+ const response = await getOrgInfoApi(orgId, options);
457
+ if (response.error) {
458
+ setError(response.error);
459
+ return null;
460
+ }
461
+ return response.data?.orgInfo || null;
462
+ } catch (err) {
463
+ const message = err instanceof Error ? err.message : "Failed to get organization info";
464
+ setError(message);
465
+ return null;
466
+ } finally {
467
+ setLoading(false);
468
+ }
469
+ },
470
+ [options]
471
+ );
472
+ const createOrg = (0, import_react.useCallback)(
473
+ async (org) => {
474
+ setLoading(true);
475
+ setError(null);
476
+ try {
477
+ const response = await createOrgApi(org, options);
478
+ if (response.error) {
479
+ setError(response.error);
480
+ return null;
481
+ }
482
+ return response.data?.org || null;
483
+ } catch (err) {
484
+ const message = err instanceof Error ? err.message : "Failed to create organization";
485
+ setError(message);
486
+ return null;
487
+ } finally {
488
+ setLoading(false);
489
+ }
490
+ },
491
+ [options]
492
+ );
493
+ const createOrgWithSchemas = (0, import_react.useCallback)(
494
+ async (org, schemas) => {
495
+ setLoading(true);
496
+ setError(null);
497
+ try {
498
+ const response = await createOrgWithSchemasApi(org, schemas, options);
499
+ if (response.error) {
500
+ setError(response.error);
501
+ return null;
502
+ }
503
+ return response.data || null;
504
+ } catch (err) {
505
+ const message = err instanceof Error ? err.message : "Failed to create organization with schemas";
506
+ setError(message);
507
+ return null;
508
+ } finally {
509
+ setLoading(false);
510
+ }
511
+ },
512
+ [options]
513
+ );
514
+ const updateOrg = (0, import_react.useCallback)(
515
+ async (orgId, updates) => {
516
+ setLoading(true);
517
+ setError(null);
518
+ try {
519
+ const response = await updateOrgApi(orgId, updates, options);
520
+ if (response.error) {
521
+ setError(response.error);
522
+ return null;
523
+ }
524
+ return response.data?.org || null;
525
+ } catch (err) {
526
+ const message = err instanceof Error ? err.message : "Failed to update organization";
527
+ setError(message);
528
+ return null;
529
+ } finally {
530
+ setLoading(false);
531
+ }
532
+ },
533
+ [options]
534
+ );
535
+ const deleteOrg = (0, import_react.useCallback)(
536
+ async (orgId) => {
537
+ setLoading(true);
538
+ setError(null);
539
+ try {
540
+ const response = await deleteOrgApi(orgId, options);
541
+ if (response.error) {
542
+ setError(response.error);
543
+ return false;
544
+ }
545
+ return response.data?.success || false;
546
+ } catch (err) {
547
+ const message = err instanceof Error ? err.message : "Failed to delete organization";
548
+ setError(message);
549
+ return false;
550
+ } finally {
551
+ setLoading(false);
552
+ }
553
+ },
554
+ [options]
555
+ );
556
+ return {
557
+ loading,
558
+ error,
559
+ listOrgs,
560
+ getOrg,
561
+ getOrgInfo,
562
+ createOrg,
563
+ createOrgWithSchemas,
564
+ updateOrg,
565
+ deleteOrg
566
+ };
567
+ }
568
+
569
+ // hooks/use-users-admin.ts
570
+ var import_react2 = require("react");
571
+ function useUsersAdmin(options) {
572
+ const [loading, setLoading] = (0, import_react2.useState)(false);
573
+ const [error, setError] = (0, import_react2.useState)(null);
574
+ const listUsers = (0, import_react2.useCallback)(async () => {
575
+ setLoading(true);
576
+ setError(null);
577
+ try {
578
+ const response = await listUsersApi(options);
579
+ if (response.error) {
580
+ setError(response.error);
581
+ return [];
582
+ }
583
+ return response.data?.users || [];
584
+ } catch (err) {
585
+ const message = err instanceof Error ? err.message : "Failed to list users";
586
+ setError(message);
587
+ return [];
588
+ } finally {
589
+ setLoading(false);
590
+ }
591
+ }, [options]);
592
+ const getUser = (0, import_react2.useCallback)(
593
+ async (userId) => {
594
+ setLoading(true);
595
+ setError(null);
596
+ try {
597
+ const response = await getUserApi(userId, options);
598
+ if (response.error) {
599
+ setError(response.error);
600
+ return null;
601
+ }
602
+ return response.data?.user || null;
603
+ } catch (err) {
604
+ const message = err instanceof Error ? err.message : "Failed to get user";
605
+ setError(message);
606
+ return null;
607
+ } finally {
608
+ setLoading(false);
609
+ }
610
+ },
611
+ [options]
612
+ );
613
+ const getUserByEmail = (0, import_react2.useCallback)(
614
+ async (email) => {
615
+ setLoading(true);
616
+ setError(null);
617
+ try {
618
+ const response = await getUserByEmailApi(email, options);
619
+ if (response.error) {
620
+ setError(response.error);
621
+ return null;
622
+ }
623
+ return response.data?.user || null;
624
+ } catch (err) {
625
+ const message = err instanceof Error ? err.message : "Failed to get user by email";
626
+ setError(message);
627
+ return null;
628
+ } finally {
629
+ setLoading(false);
630
+ }
631
+ },
632
+ [options]
633
+ );
634
+ const getUserByPhone = (0, import_react2.useCallback)(
635
+ async (phone) => {
636
+ setLoading(true);
637
+ setError(null);
638
+ try {
639
+ const response = await getUserByPhoneApi(phone, options);
640
+ if (response.error) {
641
+ setError(response.error);
642
+ return null;
643
+ }
644
+ return response.data?.user || null;
645
+ } catch (err) {
646
+ const message = err instanceof Error ? err.message : "Failed to get user by phone";
647
+ setError(message);
648
+ return null;
649
+ } finally {
650
+ setLoading(false);
651
+ }
652
+ },
653
+ [options]
654
+ );
655
+ const createUser = (0, import_react2.useCallback)(
656
+ async (user) => {
657
+ setLoading(true);
658
+ setError(null);
659
+ try {
660
+ const response = await createUserApi(user, options);
661
+ if (response.error) {
662
+ setError(response.error);
663
+ return null;
664
+ }
665
+ return response.data?.user || null;
666
+ } catch (err) {
667
+ const message = err instanceof Error ? err.message : "Failed to create user";
668
+ setError(message);
669
+ return null;
670
+ } finally {
671
+ setLoading(false);
672
+ }
673
+ },
674
+ [options]
675
+ );
676
+ const updateUser = (0, import_react2.useCallback)(
677
+ async (userId, updates) => {
678
+ setLoading(true);
679
+ setError(null);
680
+ try {
681
+ const response = await updateUserApi(userId, updates, options);
682
+ if (response.error) {
683
+ setError(response.error);
684
+ return null;
685
+ }
686
+ return response.data?.user || null;
687
+ } catch (err) {
688
+ const message = err instanceof Error ? err.message : "Failed to update user";
689
+ setError(message);
690
+ return null;
691
+ } finally {
692
+ setLoading(false);
693
+ }
694
+ },
695
+ [options]
696
+ );
697
+ const deleteUser = (0, import_react2.useCallback)(
698
+ async (userId) => {
699
+ setLoading(true);
700
+ setError(null);
701
+ try {
702
+ const response = await deleteUserApi(userId, options);
703
+ if (response.error) {
704
+ setError(response.error);
705
+ return false;
706
+ }
707
+ return response.data?.success || false;
708
+ } catch (err) {
709
+ const message = err instanceof Error ? err.message : "Failed to delete user";
710
+ setError(message);
711
+ return false;
712
+ } finally {
713
+ setLoading(false);
714
+ }
715
+ },
716
+ [options]
717
+ );
718
+ const getUserSettings = (0, import_react2.useCallback)(
719
+ async (userId) => {
720
+ setLoading(true);
721
+ setError(null);
722
+ try {
723
+ const response = await getUserSettingsApi(userId, options);
724
+ if (response.error) {
725
+ setError(response.error);
726
+ return null;
727
+ }
728
+ return response.data || null;
729
+ } catch (err) {
730
+ const message = err instanceof Error ? err.message : "Failed to get user settings";
731
+ setError(message);
732
+ return null;
733
+ } finally {
734
+ setLoading(false);
735
+ }
736
+ },
737
+ [options]
738
+ );
739
+ const updateUserSettings = (0, import_react2.useCallback)(
740
+ async (userId, settings) => {
741
+ setLoading(true);
742
+ setError(null);
743
+ try {
744
+ const response = await updateUserSettingsApi(userId, settings, options);
745
+ if (response.error) {
746
+ setError(response.error);
747
+ return null;
748
+ }
749
+ return response.data || null;
750
+ } catch (err) {
751
+ const message = err instanceof Error ? err.message : "Failed to update user settings";
752
+ setError(message);
753
+ return null;
754
+ } finally {
755
+ setLoading(false);
756
+ }
757
+ },
758
+ [options]
759
+ );
760
+ return {
761
+ loading,
762
+ error,
763
+ listUsers,
764
+ getUser,
765
+ getUserByEmail,
766
+ getUserByPhone,
767
+ createUser,
768
+ updateUser,
769
+ deleteUser,
770
+ getUserSettings,
771
+ updateUserSettings
772
+ };
773
+ }
774
+
775
+ // hooks/use-invites-admin.ts
776
+ var import_react3 = require("react");
777
+ function useInvitesAdmin(options) {
778
+ const [loading, setLoading] = (0, import_react3.useState)(false);
779
+ const [error, setError] = (0, import_react3.useState)(null);
780
+ const listInvites = (0, import_react3.useCallback)(async () => {
781
+ setLoading(true);
782
+ setError(null);
783
+ try {
784
+ const response = await listInvitesApi(options);
785
+ if (response.error) {
786
+ setError(response.error);
787
+ return [];
788
+ }
789
+ return response.data?.invites || [];
790
+ } catch (err) {
791
+ const message = err instanceof Error ? err.message : "Failed to list invites";
792
+ setError(message);
793
+ return [];
794
+ } finally {
795
+ setLoading(false);
796
+ }
797
+ }, [options]);
798
+ const getInvite = (0, import_react3.useCallback)(
799
+ async (inviteId) => {
800
+ setLoading(true);
801
+ setError(null);
802
+ try {
803
+ const response = await getInviteApi(inviteId, options);
804
+ if (response.error) {
805
+ setError(response.error);
806
+ return null;
807
+ }
808
+ return response.data?.invite || null;
809
+ } catch (err) {
810
+ const message = err instanceof Error ? err.message : "Failed to get invite";
811
+ setError(message);
812
+ return null;
813
+ } finally {
814
+ setLoading(false);
815
+ }
816
+ },
817
+ [options]
818
+ );
819
+ const sendInvite = (0, import_react3.useCallback)(
820
+ async (invite) => {
821
+ setLoading(true);
822
+ setError(null);
823
+ try {
824
+ const response = await sendInviteApi(invite, options);
825
+ if (response.error) {
826
+ setError(response.error);
827
+ return null;
828
+ }
829
+ return response.data?.invite || null;
830
+ } catch (err) {
831
+ const message = err instanceof Error ? err.message : "Failed to send invite";
832
+ setError(message);
833
+ return null;
834
+ } finally {
835
+ setLoading(false);
836
+ }
837
+ },
838
+ [options]
839
+ );
840
+ const sendInvites = (0, import_react3.useCallback)(
841
+ async (invites) => {
842
+ setLoading(true);
843
+ setError(null);
844
+ try {
845
+ const response = await sendInvitesApi(invites, options);
846
+ if (response.error) {
847
+ setError(response.error);
848
+ return null;
849
+ }
850
+ return response.data || null;
851
+ } catch (err) {
852
+ const message = err instanceof Error ? err.message : "Failed to send invites";
853
+ setError(message);
854
+ return null;
855
+ } finally {
856
+ setLoading(false);
857
+ }
858
+ },
859
+ [options]
860
+ );
861
+ const resendInvite = (0, import_react3.useCallback)(
862
+ async (inviteId) => {
863
+ setLoading(true);
864
+ setError(null);
865
+ try {
866
+ const response = await resendInviteApi(inviteId, options);
867
+ if (response.error) {
868
+ setError(response.error);
869
+ return null;
870
+ }
871
+ return response.data?.invite || null;
872
+ } catch (err) {
873
+ const message = err instanceof Error ? err.message : "Failed to resend invite";
874
+ setError(message);
875
+ return null;
876
+ } finally {
877
+ setLoading(false);
878
+ }
879
+ },
880
+ [options]
881
+ );
882
+ const revokeInvite = (0, import_react3.useCallback)(
883
+ async (inviteId) => {
884
+ setLoading(true);
885
+ setError(null);
886
+ try {
887
+ const response = await revokeInviteApi(inviteId, options);
888
+ if (response.error) {
889
+ setError(response.error);
890
+ return false;
891
+ }
892
+ return response.data?.success || false;
893
+ } catch (err) {
894
+ const message = err instanceof Error ? err.message : "Failed to revoke invite";
895
+ setError(message);
896
+ return false;
897
+ } finally {
898
+ setLoading(false);
899
+ }
900
+ },
901
+ [options]
902
+ );
903
+ const acceptInvite = (0, import_react3.useCallback)(
904
+ async (inviteId) => {
905
+ setLoading(true);
906
+ setError(null);
907
+ try {
908
+ const response = await acceptInviteApi(inviteId, options);
909
+ if (response.error) {
910
+ setError(response.error);
911
+ return null;
912
+ }
913
+ return response.data?.invite || null;
914
+ } catch (err) {
915
+ const message = err instanceof Error ? err.message : "Failed to accept invite";
916
+ setError(message);
917
+ return null;
918
+ } finally {
919
+ setLoading(false);
920
+ }
921
+ },
922
+ [options]
923
+ );
924
+ return {
925
+ loading,
926
+ error,
927
+ listInvites,
928
+ getInvite,
929
+ sendInvite,
930
+ sendInvites,
931
+ resendInvite,
932
+ revokeInvite,
933
+ acceptInvite
934
+ };
935
+ }
936
+
937
+ // hooks/use-org-settings.ts
938
+ var import_react4 = require("react");
939
+ function useOrgSettings(options) {
940
+ const [loading, setLoading] = (0, import_react4.useState)(false);
941
+ const [error, setError] = (0, import_react4.useState)(null);
942
+ const getSettings = (0, import_react4.useCallback)(async () => {
943
+ setLoading(true);
944
+ setError(null);
945
+ try {
946
+ const response = await getOrgSettingsApi(options);
947
+ if (response.error) {
948
+ setError(response.error);
949
+ return null;
950
+ }
951
+ return response.data?.settings || null;
952
+ } catch (err) {
953
+ const message = err instanceof Error ? err.message : "Failed to get organization settings";
954
+ setError(message);
955
+ return null;
956
+ } finally {
957
+ setLoading(false);
958
+ }
959
+ }, [options]);
960
+ const createSettings = (0, import_react4.useCallback)(
961
+ async (settings) => {
962
+ setLoading(true);
963
+ setError(null);
964
+ try {
965
+ const response = await createOrgSettingsApi(settings, options);
966
+ if (response.error) {
967
+ setError(response.error);
968
+ return null;
969
+ }
970
+ return response.data?.settings || null;
971
+ } catch (err) {
972
+ const message = err instanceof Error ? err.message : "Failed to create organization settings";
973
+ setError(message);
974
+ return null;
975
+ } finally {
976
+ setLoading(false);
977
+ }
978
+ },
979
+ [options]
980
+ );
981
+ const updateSettings = (0, import_react4.useCallback)(
982
+ async (settings) => {
983
+ setLoading(true);
984
+ setError(null);
985
+ try {
986
+ const response = await updateOrgSettingsApi(settings, options);
987
+ if (response.error) {
988
+ setError(response.error);
989
+ return null;
990
+ }
991
+ return response.data?.settings || null;
992
+ } catch (err) {
993
+ const message = err instanceof Error ? err.message : "Failed to update organization settings";
994
+ setError(message);
995
+ return null;
996
+ } finally {
997
+ setLoading(false);
998
+ }
999
+ },
1000
+ [options]
1001
+ );
1002
+ return {
1003
+ loading,
1004
+ error,
1005
+ getSettings,
1006
+ createSettings,
1007
+ updateSettings
1008
+ };
1009
+ }
1010
+
1011
+ // hooks/use-product-analytics.ts
1012
+ var import_react5 = require("react");
1013
+ function useProductAnalytics(options) {
1014
+ const [state, setState] = (0, import_react5.useState)({
1015
+ summary: null,
1016
+ chats: [],
1017
+ agents: [],
1018
+ usage: [],
1019
+ daily: [],
1020
+ events: [],
1021
+ loading: false,
1022
+ error: null
1023
+ });
1024
+ const sessionIdRef = (0, import_react5.useRef)("");
1025
+ (0, import_react5.useEffect)(() => {
1026
+ if (typeof window !== "undefined") {
1027
+ let sessionId = sessionStorage.getItem("analytics_session_id");
1028
+ if (!sessionId) {
1029
+ sessionId = crypto.randomUUID();
1030
+ sessionStorage.setItem("analytics_session_id", sessionId);
1031
+ }
1032
+ sessionIdRef.current = sessionId;
1033
+ }
1034
+ }, []);
1035
+ const getSummary = (0, import_react5.useCallback)(
1036
+ async (filter) => {
1037
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1038
+ try {
1039
+ const response = await getAnalyticsSummaryApi(filter, options);
1040
+ if (response.error) {
1041
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1042
+ return null;
1043
+ }
1044
+ const summary = response.data?.data || null;
1045
+ setState((prev) => ({ ...prev, loading: false, summary }));
1046
+ return summary;
1047
+ } catch (err) {
1048
+ const message = err instanceof Error ? err.message : "Failed to get analytics summary";
1049
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1050
+ return null;
1051
+ }
1052
+ },
1053
+ [options]
1054
+ );
1055
+ const getChats = (0, import_react5.useCallback)(
1056
+ async (filter, agentId) => {
1057
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1058
+ try {
1059
+ const response = await getChatsAnalyticsApi(filter, agentId, options);
1060
+ if (response.error) {
1061
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1062
+ return [];
1063
+ }
1064
+ const chats = response.data?.data || [];
1065
+ setState((prev) => ({ ...prev, loading: false, chats }));
1066
+ return chats;
1067
+ } catch (err) {
1068
+ const message = err instanceof Error ? err.message : "Failed to get chat analytics";
1069
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1070
+ return [];
1071
+ }
1072
+ },
1073
+ [options]
1074
+ );
1075
+ const getAgents = (0, import_react5.useCallback)(
1076
+ async (filter) => {
1077
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1078
+ try {
1079
+ const response = await getAgentsAnalyticsApi(filter, options);
1080
+ if (response.error) {
1081
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1082
+ return [];
1083
+ }
1084
+ const agents = response.data?.data || [];
1085
+ setState((prev) => ({ ...prev, loading: false, agents }));
1086
+ return agents;
1087
+ } catch (err) {
1088
+ const message = err instanceof Error ? err.message : "Failed to get agent analytics";
1089
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1090
+ return [];
1091
+ }
1092
+ },
1093
+ [options]
1094
+ );
1095
+ const getUsage = (0, import_react5.useCallback)(
1096
+ async (filter) => {
1097
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1098
+ try {
1099
+ const response = await getUsageAnalyticsApi(filter, options);
1100
+ if (response.error) {
1101
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1102
+ return [];
1103
+ }
1104
+ const usage = response.data?.data || [];
1105
+ setState((prev) => ({ ...prev, loading: false, usage }));
1106
+ return usage;
1107
+ } catch (err) {
1108
+ const message = err instanceof Error ? err.message : "Failed to get usage analytics";
1109
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1110
+ return [];
1111
+ }
1112
+ },
1113
+ [options]
1114
+ );
1115
+ const getDaily = (0, import_react5.useCallback)(
1116
+ async (filter) => {
1117
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1118
+ try {
1119
+ const response = await getDailyAnalyticsApi(filter, options);
1120
+ if (response.error) {
1121
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1122
+ return [];
1123
+ }
1124
+ const daily = response.data?.data || [];
1125
+ setState((prev) => ({ ...prev, loading: false, daily }));
1126
+ return daily;
1127
+ } catch (err) {
1128
+ const message = err instanceof Error ? err.message : "Failed to get daily analytics";
1129
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1130
+ return [];
1131
+ }
1132
+ },
1133
+ [options]
1134
+ );
1135
+ const getEvents = (0, import_react5.useCallback)(
1136
+ async (filter) => {
1137
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1138
+ try {
1139
+ const response = await getAnalyticsEventsApi(filter, options);
1140
+ if (response.error) {
1141
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1142
+ return [];
1143
+ }
1144
+ const events = response.data?.data || [];
1145
+ setState((prev) => ({ ...prev, loading: false, events }));
1146
+ return events;
1147
+ } catch (err) {
1148
+ const message = err instanceof Error ? err.message : "Failed to get analytics events";
1149
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1150
+ return [];
1151
+ }
1152
+ },
1153
+ [options]
1154
+ );
1155
+ const trackEvent = (0, import_react5.useCallback)(
1156
+ async (eventType, eventCategory, properties) => {
1157
+ try {
1158
+ const event = {
1159
+ sessionId: sessionIdRef.current,
1160
+ userId: options.userId || "",
1161
+ eventType,
1162
+ eventCategory,
1163
+ pagePath: typeof window !== "undefined" ? window.location.pathname : void 0,
1164
+ properties
1165
+ };
1166
+ await logAnalyticsEventApi(event, options);
1167
+ } catch {
1168
+ console.debug("Failed to track analytics event:", eventType);
1169
+ }
1170
+ },
1171
+ [options]
1172
+ );
1173
+ const trackPageView = (0, import_react5.useCallback)(
1174
+ async (pageName) => {
1175
+ await trackEvent("page_view", "navigation", {
1176
+ pageName: pageName || (typeof window !== "undefined" ? document.title : "")
1177
+ });
1178
+ },
1179
+ [trackEvent]
1180
+ );
1181
+ const getGlobalSummary = (0, import_react5.useCallback)(
1182
+ async (filter) => {
1183
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1184
+ try {
1185
+ const response = await getGlobalSummaryApi(filter, options);
1186
+ if (response.error) {
1187
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1188
+ return null;
1189
+ }
1190
+ setState((prev) => ({ ...prev, loading: false }));
1191
+ return response.data?.data || null;
1192
+ } catch (err) {
1193
+ const message = err instanceof Error ? err.message : "Failed to get global summary";
1194
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1195
+ return null;
1196
+ }
1197
+ },
1198
+ [options]
1199
+ );
1200
+ const getGlobalOrgs = (0, import_react5.useCallback)(
1201
+ async (filter) => {
1202
+ setState((prev) => ({ ...prev, loading: true, error: null }));
1203
+ try {
1204
+ const response = await getGlobalOrgsAnalyticsApi(filter, options);
1205
+ if (response.error) {
1206
+ setState((prev) => ({ ...prev, loading: false, error: response.error || null }));
1207
+ return [];
1208
+ }
1209
+ setState((prev) => ({ ...prev, loading: false }));
1210
+ return response.data?.data || [];
1211
+ } catch (err) {
1212
+ const message = err instanceof Error ? err.message : "Failed to get org analytics";
1213
+ setState((prev) => ({ ...prev, loading: false, error: message }));
1214
+ return [];
1215
+ }
1216
+ },
1217
+ [options]
1218
+ );
1219
+ return {
1220
+ // State
1221
+ ...state,
1222
+ // Query functions
1223
+ getSummary,
1224
+ getChats,
1225
+ getAgents,
1226
+ getUsage,
1227
+ getDaily,
1228
+ getEvents,
1229
+ // Event tracking
1230
+ trackEvent,
1231
+ trackPageView,
1232
+ // Global analytics (admin)
1233
+ getGlobalSummary,
1234
+ getGlobalOrgs
1235
+ };
1236
+ }
1237
+ var useAnalyticsContext = useProductAnalytics;
1238
+
1239
+ // hooks/use-org-provisioning.ts
1240
+ var import_react6 = require("react");
1241
+ function useOrgProvisioning(options) {
1242
+ const [state, setState] = (0, import_react6.useState)({
1243
+ status: "idle",
1244
+ progress: null,
1245
+ org: null,
1246
+ error: null,
1247
+ percentage: 0
1248
+ });
1249
+ const cleanupRef = (0, import_react6.useRef)(null);
1250
+ (0, import_react6.useEffect)(() => {
1251
+ return () => {
1252
+ if (cleanupRef.current) {
1253
+ cleanupRef.current();
1254
+ }
1255
+ };
1256
+ }, []);
1257
+ const createOrgWithProvisioning = (0, import_react6.useCallback)(
1258
+ async (request) => {
1259
+ setState((prev) => ({
1260
+ ...prev,
1261
+ status: "creating",
1262
+ error: null,
1263
+ progress: null,
1264
+ percentage: 0
1265
+ }));
1266
+ try {
1267
+ const response = await createOrgWithProvisioningApi(request, options);
1268
+ if (response.error) {
1269
+ setState((prev) => ({
1270
+ ...prev,
1271
+ status: "failed",
1272
+ error: response.error || "Failed to create organization"
1273
+ }));
1274
+ return null;
1275
+ }
1276
+ const org = response.data?.organization || response.data?.org;
1277
+ if (!org) {
1278
+ setState((prev) => ({
1279
+ ...prev,
1280
+ status: "failed",
1281
+ error: "No organization returned"
1282
+ }));
1283
+ return null;
1284
+ }
1285
+ setState((prev) => ({
1286
+ ...prev,
1287
+ status: "provisioning",
1288
+ org
1289
+ }));
1290
+ const cleanup = streamProvisioningProgress(
1291
+ org.id,
1292
+ {
1293
+ onProgress: (progress) => {
1294
+ const percentage = calculateProgressPercentage(progress);
1295
+ const status = isProvisioningComplete(progress) ? isProvisioningSuccessful(progress) ? "completed" : progress.status === "partial" ? "partial" : "failed" : "provisioning";
1296
+ setState((prev) => ({
1297
+ ...prev,
1298
+ status,
1299
+ progress,
1300
+ percentage,
1301
+ error: progress.error || null
1302
+ }));
1303
+ },
1304
+ onDone: () => {
1305
+ cleanupRef.current = null;
1306
+ },
1307
+ onError: (error) => {
1308
+ setState((prev) => ({
1309
+ ...prev,
1310
+ status: "failed",
1311
+ error: error.message
1312
+ }));
1313
+ cleanupRef.current = null;
1314
+ },
1315
+ onTimeout: () => {
1316
+ setState((prev) => ({
1317
+ ...prev,
1318
+ error: "Provisioning timed out"
1319
+ }));
1320
+ cleanupRef.current = null;
1321
+ }
1322
+ },
1323
+ { baseUrl: options.baseUrl }
1324
+ );
1325
+ cleanupRef.current = cleanup;
1326
+ return org;
1327
+ } catch (err) {
1328
+ const message = err instanceof Error ? err.message : "Failed to create organization";
1329
+ setState((prev) => ({
1330
+ ...prev,
1331
+ status: "failed",
1332
+ error: message
1333
+ }));
1334
+ return null;
1335
+ }
1336
+ },
1337
+ [options]
1338
+ );
1339
+ const getProvisioningStatus = (0, import_react6.useCallback)(
1340
+ async (orgId) => {
1341
+ try {
1342
+ const response = await getProvisioningStatusApi(orgId, options);
1343
+ if (response.error) {
1344
+ setState((prev) => ({ ...prev, error: response.error || null }));
1345
+ return null;
1346
+ }
1347
+ const progress = response.data?.progress || null;
1348
+ if (progress) {
1349
+ setState((prev) => ({
1350
+ ...prev,
1351
+ progress,
1352
+ percentage: calculateProgressPercentage(progress),
1353
+ status: isProvisioningComplete(progress) ? isProvisioningSuccessful(progress) ? "completed" : progress.status === "partial" ? "partial" : "failed" : "provisioning"
1354
+ }));
1355
+ }
1356
+ return progress;
1357
+ } catch (err) {
1358
+ const message = err instanceof Error ? err.message : "Failed to get provisioning status";
1359
+ setState((prev) => ({ ...prev, error: message }));
1360
+ return null;
1361
+ }
1362
+ },
1363
+ [options]
1364
+ );
1365
+ const retryProvisioning = (0, import_react6.useCallback)(
1366
+ async (orgId, artifacts) => {
1367
+ setState((prev) => ({
1368
+ ...prev,
1369
+ status: "provisioning",
1370
+ error: null
1371
+ }));
1372
+ try {
1373
+ const request = artifacts ? { artifacts } : void 0;
1374
+ const response = await retryProvisioningApi(orgId, request, options);
1375
+ if (response.error) {
1376
+ setState((prev) => ({
1377
+ ...prev,
1378
+ status: "failed",
1379
+ error: response.error || "Failed to retry provisioning"
1380
+ }));
1381
+ return false;
1382
+ }
1383
+ const cleanup = streamProvisioningProgress(
1384
+ orgId,
1385
+ {
1386
+ onProgress: (progress) => {
1387
+ const percentage = calculateProgressPercentage(progress);
1388
+ const status = isProvisioningComplete(progress) ? isProvisioningSuccessful(progress) ? "completed" : progress.status === "partial" ? "partial" : "failed" : "provisioning";
1389
+ setState((prev) => ({
1390
+ ...prev,
1391
+ status,
1392
+ progress,
1393
+ percentage,
1394
+ error: progress.error || null
1395
+ }));
1396
+ },
1397
+ onDone: () => {
1398
+ cleanupRef.current = null;
1399
+ },
1400
+ onError: (error) => {
1401
+ setState((prev) => ({
1402
+ ...prev,
1403
+ status: "failed",
1404
+ error: error.message
1405
+ }));
1406
+ cleanupRef.current = null;
1407
+ }
1408
+ },
1409
+ { baseUrl: options.baseUrl }
1410
+ );
1411
+ cleanupRef.current = cleanup;
1412
+ return true;
1413
+ } catch (err) {
1414
+ const message = err instanceof Error ? err.message : "Failed to retry provisioning";
1415
+ setState((prev) => ({
1416
+ ...prev,
1417
+ status: "failed",
1418
+ error: message
1419
+ }));
1420
+ return false;
1421
+ }
1422
+ },
1423
+ [options]
1424
+ );
1425
+ const validateProvisioning = (0, import_react6.useCallback)(
1426
+ async (orgId) => {
1427
+ try {
1428
+ const response = await validateProvisioningApi(orgId, options);
1429
+ if (response.error) {
1430
+ setState((prev) => ({ ...prev, error: response.error || null }));
1431
+ return null;
1432
+ }
1433
+ return response.data || null;
1434
+ } catch (err) {
1435
+ const message = err instanceof Error ? err.message : "Failed to validate provisioning";
1436
+ setState((prev) => ({ ...prev, error: message }));
1437
+ return null;
1438
+ }
1439
+ },
1440
+ [options]
1441
+ );
1442
+ const cancelProvisioning = (0, import_react6.useCallback)(
1443
+ async (orgId) => {
1444
+ try {
1445
+ if (cleanupRef.current) {
1446
+ cleanupRef.current();
1447
+ cleanupRef.current = null;
1448
+ }
1449
+ const response = await cancelProvisioningApi(orgId, options);
1450
+ if (response.error) {
1451
+ setState((prev) => ({ ...prev, error: response.error || null }));
1452
+ return false;
1453
+ }
1454
+ setState((prev) => ({
1455
+ ...prev,
1456
+ status: "failed",
1457
+ error: "Provisioning cancelled"
1458
+ }));
1459
+ return true;
1460
+ } catch (err) {
1461
+ const message = err instanceof Error ? err.message : "Failed to cancel provisioning";
1462
+ setState((prev) => ({ ...prev, error: message }));
1463
+ return false;
1464
+ }
1465
+ },
1466
+ [options]
1467
+ );
1468
+ const cleanupOrg = (0, import_react6.useCallback)(
1469
+ async (orgId) => {
1470
+ try {
1471
+ const response = await cleanupProvisioningApi(orgId, options);
1472
+ if (response.error) {
1473
+ setState((prev) => ({ ...prev, error: response.error || null }));
1474
+ return false;
1475
+ }
1476
+ return true;
1477
+ } catch (err) {
1478
+ const message = err instanceof Error ? err.message : "Failed to cleanup organization";
1479
+ setState((prev) => ({ ...prev, error: message }));
1480
+ return false;
1481
+ }
1482
+ },
1483
+ [options]
1484
+ );
1485
+ const reset = (0, import_react6.useCallback)(() => {
1486
+ if (cleanupRef.current) {
1487
+ cleanupRef.current();
1488
+ cleanupRef.current = null;
1489
+ }
1490
+ setState({
1491
+ status: "idle",
1492
+ progress: null,
1493
+ org: null,
1494
+ error: null,
1495
+ percentage: 0
1496
+ });
1497
+ }, []);
1498
+ return {
1499
+ state,
1500
+ createOrgWithProvisioning,
1501
+ getProvisioningStatus,
1502
+ retryProvisioning,
1503
+ validateProvisioning,
1504
+ cancelProvisioning,
1505
+ cleanupOrg,
1506
+ reset,
1507
+ // Helper functions
1508
+ isComplete: state.status === "completed",
1509
+ isFailed: state.status === "failed",
1510
+ isPartial: state.status === "partial",
1511
+ isProvisioning: state.status === "provisioning" || state.status === "creating",
1512
+ failedArtifacts: state.progress ? getFailedArtifacts(state.progress) : [],
1513
+ hasCriticalFailures: state.progress ? hasCriticalFailures(state.progress) : false
1514
+ };
1515
+ }
1516
+ // Annotate the CommonJS export names for ESM import in node:
1517
+ 0 && (module.exports = {
1518
+ useAnalyticsContext,
1519
+ useInvitesAdmin,
1520
+ useOrgAdmin,
1521
+ useOrgProvisioning,
1522
+ useOrgSettings,
1523
+ useProductAnalytics,
1524
+ useUsersAdmin
1525
+ });
1526
+ //# sourceMappingURL=index.cjs.map