@famgia/omnify-react-sso 2.2.0 → 2.2.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 (33) hide show
  1. package/README.md +404 -113
  2. package/dist/@omnify-base/package.json +16 -0
  3. package/dist/@omnify-base/schemas/Branch.ts +131 -0
  4. package/dist/@omnify-base/schemas/BranchCache.ts +131 -0
  5. package/dist/@omnify-base/schemas/OrganizationCache.ts +117 -0
  6. package/dist/@omnify-base/schemas/Permission.ts +114 -0
  7. package/dist/@omnify-base/schemas/Role.ts +121 -0
  8. package/dist/@omnify-base/schemas/RolePermission.ts +97 -0
  9. package/dist/@omnify-base/schemas/Team.ts +110 -0
  10. package/dist/@omnify-base/schemas/TeamCache.ts +110 -0
  11. package/dist/@omnify-base/schemas/TeamPermission.ts +109 -0
  12. package/dist/@omnify-base/schemas/User.ts +135 -0
  13. package/dist/@omnify-base/schemas/UserCache.ts +135 -0
  14. package/dist/@omnify-base/schemas/common.ts +47 -0
  15. package/dist/@omnify-base/schemas/i18n.ts +118 -0
  16. package/dist/index.cjs +1200 -381
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +245 -7
  19. package/dist/index.d.ts +245 -7
  20. package/dist/index.js +1158 -355
  21. package/dist/index.js.map +1 -1
  22. package/dist/schemas/index.cjs +201 -133
  23. package/dist/schemas/index.cjs.map +1 -1
  24. package/dist/schemas/index.d.cts +61 -28
  25. package/dist/schemas/index.d.ts +61 -28
  26. package/dist/schemas/index.js +173 -112
  27. package/dist/schemas/index.js.map +1 -1
  28. package/dist/testing/index.d.cts +1 -1
  29. package/dist/testing/index.d.ts +1 -1
  30. package/dist/{types-BCBSfJJr.d.cts → types-CJmA2a9r.d.cts} +102 -1
  31. package/dist/{types-BCBSfJJr.d.ts → types-CJmA2a9r.d.ts} +102 -1
  32. package/package.json +10 -3
  33. package/scripts/postinstall.cjs +43 -0
package/dist/index.cjs CHANGED
@@ -30,16 +30,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
+ BRANCH_HEADERS: () => BRANCH_HEADERS,
34
+ BranchContext: () => BranchContext,
35
+ BranchGate: () => BranchGate,
36
+ BranchProvider: () => BranchProvider,
37
+ OrgBranchSelectorModal: () => OrgBranchSelectorModal,
33
38
  OrganizationSwitcher: () => OrganizationSwitcher,
34
39
  ProtectedRoute: () => ProtectedRoute,
35
40
  SsoCallback: () => SsoCallback,
36
41
  SsoContext: () => SsoContext,
37
42
  SsoProvider: () => SsoProvider,
38
- branchCreateSchema: () => branchCreateSchema,
39
- branchI18n: () => branchI18n,
40
- branchSchemas: () => branchSchemas,
41
- branchUpdateSchema: () => branchUpdateSchema,
43
+ branchCacheCreateSchema: () => branchCacheCreateSchema,
44
+ branchCacheI18n: () => branchCacheI18n,
45
+ branchCacheSchemas: () => branchCacheSchemas,
46
+ branchCacheUpdateSchema: () => branchCacheUpdateSchema,
42
47
  createAuthService: () => createAuthService,
48
+ createBranchHeaderSetter: () => createBranchHeaderSetter,
43
49
  createBranchService: () => createBranchService,
44
50
  createPermissionService: () => createPermissionService,
45
51
  createRoleService: () => createRoleService,
@@ -49,12 +55,15 @@ __export(src_exports, {
49
55
  createUserRoleService: () => createUserRoleService,
50
56
  defaultLocale: () => defaultLocale,
51
57
  fallbackLocale: () => fallbackLocale,
52
- getBranchFieldLabel: () => getBranchFieldLabel,
53
- getBranchFieldPlaceholder: () => getBranchFieldPlaceholder,
54
- getBranchLabel: () => getBranchLabel,
58
+ getBranchCacheFieldLabel: () => getBranchCacheFieldLabel,
59
+ getBranchCacheFieldPlaceholder: () => getBranchCacheFieldPlaceholder,
60
+ getBranchCacheLabel: () => getBranchCacheLabel,
55
61
  getEffectivePermissions: () => getEffectivePermissions,
56
62
  getMessage: () => getMessage,
57
63
  getMessages: () => getMessages,
64
+ getOrganizationCacheFieldLabel: () => getOrganizationCacheFieldLabel,
65
+ getOrganizationCacheFieldPlaceholder: () => getOrganizationCacheFieldPlaceholder,
66
+ getOrganizationCacheLabel: () => getOrganizationCacheLabel,
58
67
  getPermissionFieldLabel: () => getPermissionFieldLabel,
59
68
  getPermissionFieldPlaceholder: () => getPermissionFieldPlaceholder,
60
69
  getPermissionLabel: () => getPermissionLabel,
@@ -65,15 +74,19 @@ __export(src_exports, {
65
74
  getRolePermissionFieldPlaceholder: () => getRolePermissionFieldPlaceholder,
66
75
  getRolePermissionLabel: () => getRolePermissionLabel,
67
76
  getScopeLabel: () => getScopeLabel,
68
- getTeamFieldLabel: () => getTeamFieldLabel,
69
- getTeamFieldPlaceholder: () => getTeamFieldPlaceholder,
70
- getTeamLabel: () => getTeamLabel,
77
+ getTeamCacheFieldLabel: () => getTeamCacheFieldLabel,
78
+ getTeamCacheFieldPlaceholder: () => getTeamCacheFieldPlaceholder,
79
+ getTeamCacheLabel: () => getTeamCacheLabel,
71
80
  getTeamPermissionFieldLabel: () => getTeamPermissionFieldLabel,
72
81
  getTeamPermissionFieldPlaceholder: () => getTeamPermissionFieldPlaceholder,
73
82
  getTeamPermissionLabel: () => getTeamPermissionLabel,
74
- getUserFieldLabel: () => getUserFieldLabel,
75
- getUserFieldPlaceholder: () => getUserFieldPlaceholder,
76
- getUserLabel: () => getUserLabel,
83
+ getUserCacheFieldLabel: () => getUserCacheFieldLabel,
84
+ getUserCacheFieldPlaceholder: () => getUserCacheFieldPlaceholder,
85
+ getUserCacheLabel: () => getUserCacheLabel,
86
+ organizationCacheCreateSchema: () => organizationCacheCreateSchema,
87
+ organizationCacheI18n: () => organizationCacheI18n,
88
+ organizationCacheSchemas: () => organizationCacheSchemas,
89
+ organizationCacheUpdateSchema: () => organizationCacheUpdateSchema,
77
90
  permissionCreateSchema: () => permissionCreateSchema,
78
91
  permissionI18n: () => permissionI18n,
79
92
  permissionSchemas: () => permissionSchemas,
@@ -86,23 +99,26 @@ __export(src_exports, {
86
99
  rolePermissionUpdateSchema: () => rolePermissionUpdateSchema,
87
100
  roleSchemas: () => roleSchemas,
88
101
  roleUpdateSchema: () => roleUpdateSchema,
102
+ setBranchHeaders: () => setBranchHeaders,
89
103
  ssoQueryKeys: () => ssoQueryKeys,
90
104
  supportedLocales: () => supportedLocales,
91
- teamCreateSchema: () => teamCreateSchema,
92
- teamI18n: () => teamI18n,
105
+ teamCacheCreateSchema: () => teamCacheCreateSchema,
106
+ teamCacheI18n: () => teamCacheI18n,
107
+ teamCacheSchemas: () => teamCacheSchemas,
108
+ teamCacheUpdateSchema: () => teamCacheUpdateSchema,
93
109
  teamPermissionCreateSchema: () => teamPermissionCreateSchema,
94
110
  teamPermissionI18n: () => teamPermissionI18n,
95
111
  teamPermissionSchemas: () => teamPermissionSchemas,
96
112
  teamPermissionUpdateSchema: () => teamPermissionUpdateSchema,
97
- teamSchemas: () => teamSchemas,
98
- teamUpdateSchema: () => teamUpdateSchema,
99
113
  useAuth: () => useAuth,
114
+ useBranch: () => useBranch,
115
+ useBranchGate: () => useBranchGate,
100
116
  useOrganization: () => useOrganization,
101
117
  useSso: () => useSso,
102
- userCreateSchema: () => userCreateSchema,
103
- userI18n: () => userI18n,
104
- userSchemas: () => userSchemas,
105
- userUpdateSchema: () => userUpdateSchema,
118
+ userCacheCreateSchema: () => userCacheCreateSchema,
119
+ userCacheI18n: () => userCacheI18n,
120
+ userCacheSchemas: () => userCacheSchemas,
121
+ userCacheUpdateSchema: () => userCacheUpdateSchema,
106
122
  validationMessages: () => validationMessages
107
123
  });
108
124
  module.exports = __toCommonJS(src_exports);
@@ -162,11 +178,11 @@ function getMessages(locale) {
162
178
  return result;
163
179
  }
164
180
 
165
- // node_modules/@omnify-base/schemas/Branch.ts
181
+ // node_modules/@omnify-base/schemas/BranchCache.ts
166
182
  var import_zod = require("zod");
167
- var branchI18n = {
183
+ var branchCacheI18n = {
168
184
  /** Model display name */
169
- label: { "en": "Branch" },
185
+ label: { "en": "Branch Cache" },
170
186
  /** Field labels and placeholders */
171
187
  fields: {
172
188
  console_branch_id: {
@@ -189,7 +205,7 @@ var branchI18n = {
189
205
  }
190
206
  }
191
207
  };
192
- var baseBranchSchemas = {
208
+ var baseBranchCacheSchemas = {
193
209
  console_branch_id: import_zod.z.string(),
194
210
  console_org_id: import_zod.z.string(),
195
211
  code: import_zod.z.string().min(1).max(20),
@@ -197,37 +213,91 @@ var baseBranchSchemas = {
197
213
  is_headquarters: import_zod.z.boolean(),
198
214
  is_active: import_zod.z.boolean()
199
215
  };
200
- var baseBranchCreateSchema = import_zod.z.object({
201
- console_branch_id: baseBranchSchemas.console_branch_id,
202
- console_org_id: baseBranchSchemas.console_org_id,
203
- code: baseBranchSchemas.code,
204
- name: baseBranchSchemas.name,
205
- is_headquarters: baseBranchSchemas.is_headquarters,
206
- is_active: baseBranchSchemas.is_active
216
+ var baseBranchCacheCreateSchema = import_zod.z.object({
217
+ console_branch_id: baseBranchCacheSchemas.console_branch_id,
218
+ console_org_id: baseBranchCacheSchemas.console_org_id,
219
+ code: baseBranchCacheSchemas.code,
220
+ name: baseBranchCacheSchemas.name,
221
+ is_headquarters: baseBranchCacheSchemas.is_headquarters,
222
+ is_active: baseBranchCacheSchemas.is_active
207
223
  });
208
- var baseBranchUpdateSchema = baseBranchCreateSchema.partial();
209
- function getBranchLabel(locale) {
210
- return branchI18n.label[locale] ?? branchI18n.label["en"] ?? "Branch";
224
+ var baseBranchCacheUpdateSchema = baseBranchCacheCreateSchema.partial();
225
+ function getBranchCacheLabel(locale) {
226
+ return branchCacheI18n.label[locale] ?? branchCacheI18n.label["en"] ?? "BranchCache";
211
227
  }
212
- function getBranchFieldLabel(field, locale) {
213
- const fieldI18n = branchI18n.fields[field];
228
+ function getBranchCacheFieldLabel(field, locale) {
229
+ const fieldI18n = branchCacheI18n.fields[field];
214
230
  if (!fieldI18n) return field;
215
231
  return fieldI18n.label[locale] ?? fieldI18n.label["en"] ?? field;
216
232
  }
217
- function getBranchFieldPlaceholder(field, locale) {
218
- const fieldI18n = branchI18n.fields[field];
233
+ function getBranchCacheFieldPlaceholder(field, locale) {
234
+ const fieldI18n = branchCacheI18n.fields[field];
219
235
  if (!fieldI18n || !("placeholder" in fieldI18n)) return "";
220
236
  const placeholder = fieldI18n.placeholder;
221
237
  return placeholder[locale] ?? placeholder["en"] ?? "";
222
238
  }
223
239
 
224
- // src/schemas/Branch.ts
225
- var branchSchemas = { ...baseBranchSchemas };
226
- var branchCreateSchema = baseBranchCreateSchema;
227
- var branchUpdateSchema = baseBranchUpdateSchema;
240
+ // src/schemas/BranchCache.ts
241
+ var branchCacheSchemas = { ...baseBranchCacheSchemas };
242
+ var branchCacheCreateSchema = baseBranchCacheCreateSchema;
243
+ var branchCacheUpdateSchema = baseBranchCacheUpdateSchema;
228
244
 
229
- // node_modules/@omnify-base/schemas/Permission.ts
245
+ // node_modules/@omnify-base/schemas/OrganizationCache.ts
230
246
  var import_zod2 = require("zod");
247
+ var organizationCacheI18n = {
248
+ /** Model display name */
249
+ label: { "en": "Organization Cache" },
250
+ /** Field labels and placeholders */
251
+ fields: {
252
+ console_org_id: {
253
+ label: { "en": "Console Organization ID" }
254
+ },
255
+ name: {
256
+ label: { "en": "Organization Name" }
257
+ },
258
+ code: {
259
+ label: { "en": "Organization Code" }
260
+ },
261
+ is_active: {
262
+ label: { "en": "Active" }
263
+ }
264
+ }
265
+ };
266
+ var baseOrganizationCacheSchemas = {
267
+ console_org_id: import_zod2.z.string(),
268
+ name: import_zod2.z.string().min(1).max(100),
269
+ code: import_zod2.z.string().min(1).max(20),
270
+ is_active: import_zod2.z.boolean()
271
+ };
272
+ var baseOrganizationCacheCreateSchema = import_zod2.z.object({
273
+ console_org_id: baseOrganizationCacheSchemas.console_org_id,
274
+ name: baseOrganizationCacheSchemas.name,
275
+ code: baseOrganizationCacheSchemas.code,
276
+ is_active: baseOrganizationCacheSchemas.is_active
277
+ });
278
+ var baseOrganizationCacheUpdateSchema = baseOrganizationCacheCreateSchema.partial();
279
+ function getOrganizationCacheLabel(locale) {
280
+ return organizationCacheI18n.label[locale] ?? organizationCacheI18n.label["en"] ?? "OrganizationCache";
281
+ }
282
+ function getOrganizationCacheFieldLabel(field, locale) {
283
+ const fieldI18n = organizationCacheI18n.fields[field];
284
+ if (!fieldI18n) return field;
285
+ return fieldI18n.label[locale] ?? fieldI18n.label["en"] ?? field;
286
+ }
287
+ function getOrganizationCacheFieldPlaceholder(field, locale) {
288
+ const fieldI18n = organizationCacheI18n.fields[field];
289
+ if (!fieldI18n || !("placeholder" in fieldI18n)) return "";
290
+ const placeholder = fieldI18n.placeholder;
291
+ return placeholder[locale] ?? placeholder["en"] ?? "";
292
+ }
293
+
294
+ // src/schemas/OrganizationCache.ts
295
+ var organizationCacheSchemas = { ...baseOrganizationCacheSchemas };
296
+ var organizationCacheCreateSchema = baseOrganizationCacheCreateSchema;
297
+ var organizationCacheUpdateSchema = baseOrganizationCacheUpdateSchema;
298
+
299
+ // node_modules/@omnify-base/schemas/Permission.ts
300
+ var import_zod3 = require("zod");
231
301
  var permissionI18n = {
232
302
  /** Model display name */
233
303
  label: { "en": "Permission" },
@@ -248,11 +318,11 @@ var permissionI18n = {
248
318
  }
249
319
  };
250
320
  var basePermissionSchemas = {
251
- name: import_zod2.z.string().min(1).max(100),
252
- slug: import_zod2.z.string().min(1).max(100),
253
- group: import_zod2.z.string().max(50).optional().nullable()
321
+ name: import_zod3.z.string().min(1).max(100),
322
+ slug: import_zod3.z.string().min(1).max(100),
323
+ group: import_zod3.z.string().max(50).optional().nullable()
254
324
  };
255
- var basePermissionCreateSchema = import_zod2.z.object({
325
+ var basePermissionCreateSchema = import_zod3.z.object({
256
326
  name: basePermissionSchemas.name,
257
327
  slug: basePermissionSchemas.slug,
258
328
  group: basePermissionSchemas.group
@@ -279,7 +349,7 @@ var permissionCreateSchema = basePermissionCreateSchema;
279
349
  var permissionUpdateSchema = basePermissionUpdateSchema;
280
350
 
281
351
  // node_modules/@omnify-base/schemas/Role.ts
282
- var import_zod3 = require("zod");
352
+ var import_zod4 = require("zod");
283
353
  var roleI18n = {
284
354
  /** Model display name */
285
355
  label: { "en": "Role" },
@@ -303,12 +373,12 @@ var roleI18n = {
303
373
  }
304
374
  };
305
375
  var baseRoleSchemas = {
306
- name: import_zod3.z.string().min(1).max(100),
307
- slug: import_zod3.z.string().min(1).max(100),
308
- description: import_zod3.z.string().optional().nullable(),
309
- level: import_zod3.z.number().int()
376
+ name: import_zod4.z.string().min(1).max(100),
377
+ slug: import_zod4.z.string().min(1).max(100),
378
+ description: import_zod4.z.string().optional().nullable(),
379
+ level: import_zod4.z.number().int()
310
380
  };
311
- var baseRoleCreateSchema = import_zod3.z.object({
381
+ var baseRoleCreateSchema = import_zod4.z.object({
312
382
  name: baseRoleSchemas.name,
313
383
  slug: baseRoleSchemas.slug,
314
384
  description: baseRoleSchemas.description,
@@ -336,7 +406,7 @@ var roleCreateSchema = baseRoleCreateSchema;
336
406
  var roleUpdateSchema = baseRoleUpdateSchema;
337
407
 
338
408
  // node_modules/@omnify-base/schemas/RolePermission.ts
339
- var import_zod4 = require("zod");
409
+ var import_zod5 = require("zod");
340
410
  var rolePermissionI18n = {
341
411
  /** Model display name */
342
412
  label: { "en": "Role Permission" },
@@ -351,7 +421,7 @@ var rolePermissionI18n = {
351
421
  }
352
422
  };
353
423
  var baseRolePermissionSchemas = {};
354
- var baseRolePermissionCreateSchema = import_zod4.z.object({});
424
+ var baseRolePermissionCreateSchema = import_zod5.z.object({});
355
425
  var baseRolePermissionUpdateSchema = baseRolePermissionCreateSchema.partial();
356
426
  function getRolePermissionLabel(locale) {
357
427
  return rolePermissionI18n.label[locale] ?? rolePermissionI18n.label["en"] ?? "RolePermission";
@@ -373,11 +443,11 @@ var rolePermissionSchemas = { ...baseRolePermissionSchemas };
373
443
  var rolePermissionCreateSchema = baseRolePermissionCreateSchema;
374
444
  var rolePermissionUpdateSchema = baseRolePermissionUpdateSchema;
375
445
 
376
- // node_modules/@omnify-base/schemas/Team.ts
377
- var import_zod5 = require("zod");
378
- var teamI18n = {
446
+ // node_modules/@omnify-base/schemas/TeamCache.ts
447
+ var import_zod6 = require("zod");
448
+ var teamCacheI18n = {
379
449
  /** Model display name */
380
- label: { "en": "Team" },
450
+ label: { "en": "Team Cache" },
381
451
  /** Field labels and placeholders */
382
452
  fields: {
383
453
  console_team_id: {
@@ -391,39 +461,39 @@ var teamI18n = {
391
461
  }
392
462
  }
393
463
  };
394
- var baseTeamSchemas = {
395
- console_team_id: import_zod5.z.string(),
396
- console_org_id: import_zod5.z.string(),
397
- name: import_zod5.z.string().min(1).max(100)
464
+ var baseTeamCacheSchemas = {
465
+ console_team_id: import_zod6.z.string(),
466
+ console_org_id: import_zod6.z.string(),
467
+ name: import_zod6.z.string().min(1).max(100)
398
468
  };
399
- var baseTeamCreateSchema = import_zod5.z.object({
400
- console_team_id: baseTeamSchemas.console_team_id,
401
- console_org_id: baseTeamSchemas.console_org_id,
402
- name: baseTeamSchemas.name
469
+ var baseTeamCacheCreateSchema = import_zod6.z.object({
470
+ console_team_id: baseTeamCacheSchemas.console_team_id,
471
+ console_org_id: baseTeamCacheSchemas.console_org_id,
472
+ name: baseTeamCacheSchemas.name
403
473
  });
404
- var baseTeamUpdateSchema = baseTeamCreateSchema.partial();
405
- function getTeamLabel(locale) {
406
- return teamI18n.label[locale] ?? teamI18n.label["en"] ?? "Team";
474
+ var baseTeamCacheUpdateSchema = baseTeamCacheCreateSchema.partial();
475
+ function getTeamCacheLabel(locale) {
476
+ return teamCacheI18n.label[locale] ?? teamCacheI18n.label["en"] ?? "TeamCache";
407
477
  }
408
- function getTeamFieldLabel(field, locale) {
409
- const fieldI18n = teamI18n.fields[field];
478
+ function getTeamCacheFieldLabel(field, locale) {
479
+ const fieldI18n = teamCacheI18n.fields[field];
410
480
  if (!fieldI18n) return field;
411
481
  return fieldI18n.label[locale] ?? fieldI18n.label["en"] ?? field;
412
482
  }
413
- function getTeamFieldPlaceholder(field, locale) {
414
- const fieldI18n = teamI18n.fields[field];
483
+ function getTeamCacheFieldPlaceholder(field, locale) {
484
+ const fieldI18n = teamCacheI18n.fields[field];
415
485
  if (!fieldI18n || !("placeholder" in fieldI18n)) return "";
416
486
  const placeholder = fieldI18n.placeholder;
417
487
  return placeholder[locale] ?? placeholder["en"] ?? "";
418
488
  }
419
489
 
420
- // src/schemas/Team.ts
421
- var teamSchemas = { ...baseTeamSchemas };
422
- var teamCreateSchema = baseTeamCreateSchema;
423
- var teamUpdateSchema = baseTeamUpdateSchema;
490
+ // src/schemas/TeamCache.ts
491
+ var teamCacheSchemas = { ...baseTeamCacheSchemas };
492
+ var teamCacheCreateSchema = baseTeamCacheCreateSchema;
493
+ var teamCacheUpdateSchema = baseTeamCacheUpdateSchema;
424
494
 
425
495
  // node_modules/@omnify-base/schemas/TeamPermission.ts
426
- var import_zod6 = require("zod");
496
+ var import_zod7 = require("zod");
427
497
  var teamPermissionI18n = {
428
498
  /** Model display name */
429
499
  label: { "en": "Team Permission" },
@@ -441,10 +511,10 @@ var teamPermissionI18n = {
441
511
  }
442
512
  };
443
513
  var baseTeamPermissionSchemas = {
444
- console_org_id: import_zod6.z.string(),
445
- console_team_id: import_zod6.z.string()
514
+ console_org_id: import_zod7.z.string(),
515
+ console_team_id: import_zod7.z.string()
446
516
  };
447
- var baseTeamPermissionCreateSchema = import_zod6.z.object({
517
+ var baseTeamPermissionCreateSchema = import_zod7.z.object({
448
518
  console_org_id: baseTeamPermissionSchemas.console_org_id,
449
519
  console_team_id: baseTeamPermissionSchemas.console_team_id
450
520
  });
@@ -469,11 +539,11 @@ var teamPermissionSchemas = { ...baseTeamPermissionSchemas };
469
539
  var teamPermissionCreateSchema = baseTeamPermissionCreateSchema;
470
540
  var teamPermissionUpdateSchema = baseTeamPermissionUpdateSchema;
471
541
 
472
- // node_modules/@omnify-base/schemas/User.ts
473
- var import_zod7 = require("zod");
474
- var userI18n = {
542
+ // node_modules/@omnify-base/schemas/UserCache.ts
543
+ var import_zod8 = require("zod");
544
+ var userCacheI18n = {
475
545
  /** Model display name */
476
- label: { "en": "User" },
546
+ label: { "en": "User Cache" },
477
547
  /** Field labels and placeholders */
478
548
  fields: {
479
549
  name: {
@@ -499,42 +569,42 @@ var userI18n = {
499
569
  }
500
570
  }
501
571
  };
502
- var baseUserSchemas = {
503
- name: import_zod7.z.string().min(1),
504
- email: import_zod7.z.string().min(1),
505
- console_user_id: import_zod7.z.string().optional().nullable(),
506
- console_access_token: import_zod7.z.string().optional().nullable(),
507
- console_refresh_token: import_zod7.z.string().optional().nullable(),
508
- console_token_expires_at: import_zod7.z.string().datetime({ offset: true }).optional().nullable()
572
+ var baseUserCacheSchemas = {
573
+ name: import_zod8.z.string().min(1),
574
+ email: import_zod8.z.string().min(1),
575
+ console_user_id: import_zod8.z.string().optional().nullable(),
576
+ console_access_token: import_zod8.z.string().optional().nullable(),
577
+ console_refresh_token: import_zod8.z.string().optional().nullable(),
578
+ console_token_expires_at: import_zod8.z.string().datetime({ offset: true }).optional().nullable()
509
579
  };
510
- var baseUserCreateSchema = import_zod7.z.object({
511
- name: baseUserSchemas.name,
512
- email: baseUserSchemas.email,
513
- console_user_id: baseUserSchemas.console_user_id,
514
- console_access_token: baseUserSchemas.console_access_token,
515
- console_refresh_token: baseUserSchemas.console_refresh_token,
516
- console_token_expires_at: baseUserSchemas.console_token_expires_at
580
+ var baseUserCacheCreateSchema = import_zod8.z.object({
581
+ name: baseUserCacheSchemas.name,
582
+ email: baseUserCacheSchemas.email,
583
+ console_user_id: baseUserCacheSchemas.console_user_id,
584
+ console_access_token: baseUserCacheSchemas.console_access_token,
585
+ console_refresh_token: baseUserCacheSchemas.console_refresh_token,
586
+ console_token_expires_at: baseUserCacheSchemas.console_token_expires_at
517
587
  });
518
- var baseUserUpdateSchema = baseUserCreateSchema.partial();
519
- function getUserLabel(locale) {
520
- return userI18n.label[locale] ?? userI18n.label["en"] ?? "User";
588
+ var baseUserCacheUpdateSchema = baseUserCacheCreateSchema.partial();
589
+ function getUserCacheLabel(locale) {
590
+ return userCacheI18n.label[locale] ?? userCacheI18n.label["en"] ?? "UserCache";
521
591
  }
522
- function getUserFieldLabel(field, locale) {
523
- const fieldI18n = userI18n.fields[field];
592
+ function getUserCacheFieldLabel(field, locale) {
593
+ const fieldI18n = userCacheI18n.fields[field];
524
594
  if (!fieldI18n) return field;
525
595
  return fieldI18n.label[locale] ?? fieldI18n.label["en"] ?? field;
526
596
  }
527
- function getUserFieldPlaceholder(field, locale) {
528
- const fieldI18n = userI18n.fields[field];
597
+ function getUserCacheFieldPlaceholder(field, locale) {
598
+ const fieldI18n = userCacheI18n.fields[field];
529
599
  if (!fieldI18n || !("placeholder" in fieldI18n)) return "";
530
600
  const placeholder = fieldI18n.placeholder;
531
601
  return placeholder[locale] ?? placeholder["en"] ?? "";
532
602
  }
533
603
 
534
- // src/schemas/User.ts
535
- var userSchemas = { ...baseUserSchemas };
536
- var userCreateSchema = baseUserCreateSchema;
537
- var userUpdateSchema = baseUserUpdateSchema;
604
+ // src/schemas/UserCache.ts
605
+ var userCacheSchemas = { ...baseUserCacheSchemas };
606
+ var userCacheCreateSchema = baseUserCacheCreateSchema;
607
+ var userCacheUpdateSchema = baseUserCacheUpdateSchema;
538
608
 
539
609
  // src/context/SsoContext.tsx
540
610
  var import_react = require("react");
@@ -760,20 +830,347 @@ function SsoProvider({ children, config, onAuthChange }) {
760
830
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SsoContext.Provider, { value, children });
761
831
  }
762
832
 
763
- // src/hooks/useAuth.ts
833
+ // src/context/BranchContext.tsx
764
834
  var import_react3 = require("react");
835
+ var BranchContext = (0, import_react3.createContext)(null);
836
+ function useBranchContext() {
837
+ const context = (0, import_react3.useContext)(BranchContext);
838
+ if (!context) {
839
+ throw new Error("useBranchContext must be used within a BranchProvider");
840
+ }
841
+ return context;
842
+ }
843
+
844
+ // src/context/BranchProvider.tsx
845
+ var import_react4 = require("react");
846
+ var import_react_query = require("@tanstack/react-query");
847
+
848
+ // src/services/utils.ts
849
+ function getXsrfToken2() {
850
+ if (typeof document === "undefined") return void 0;
851
+ return document.cookie.split("; ").find((row) => row.startsWith("XSRF-TOKEN="))?.split("=")[1];
852
+ }
853
+ function buildHeaders(orgSlug) {
854
+ const headers = {
855
+ "Content-Type": "application/json",
856
+ Accept: "application/json"
857
+ };
858
+ const xsrfToken = getXsrfToken2();
859
+ if (xsrfToken) {
860
+ headers["X-XSRF-TOKEN"] = decodeURIComponent(xsrfToken);
861
+ }
862
+ if (orgSlug) {
863
+ headers["X-Org-Slug"] = orgSlug;
864
+ }
865
+ return headers;
866
+ }
867
+ async function csrf(apiUrl) {
868
+ await fetch(`${apiUrl}/sanctum/csrf-cookie`, {
869
+ credentials: "include"
870
+ });
871
+ }
872
+ async function request(apiUrl, path, options = {}) {
873
+ const response = await fetch(`${apiUrl}${path}`, {
874
+ ...options,
875
+ credentials: "include"
876
+ });
877
+ if (!response.ok) {
878
+ const error = await response.json().catch(() => ({}));
879
+ throw new Error(error.message || `HTTP ${response.status}`);
880
+ }
881
+ if (response.status === 204) {
882
+ return void 0;
883
+ }
884
+ return response.json();
885
+ }
886
+
887
+ // src/services/branchService.ts
888
+ function createBranchService(config) {
889
+ const { apiUrl } = config;
890
+ return {
891
+ /**
892
+ * Get branches for current user in organization
893
+ * GET /api/sso/branches
894
+ */
895
+ list: async (orgSlug) => {
896
+ const params = orgSlug ? `?organization_slug=${orgSlug}` : "";
897
+ return request(apiUrl, `/api/sso/branches${params}`, {
898
+ headers: buildHeaders()
899
+ });
900
+ },
901
+ /**
902
+ * Get a specific branch by ID
903
+ * GET /api/sso/branches/{id}
904
+ */
905
+ get: async (branchId) => {
906
+ const response = await request(
907
+ apiUrl,
908
+ `/api/sso/branches/${branchId}`,
909
+ { headers: buildHeaders() }
910
+ );
911
+ return "data" in response ? response.data : response;
912
+ },
913
+ /**
914
+ * Get headquarters branch for organization
915
+ */
916
+ getHeadquarters: async (orgSlug) => {
917
+ try {
918
+ const params = orgSlug ? `?organization_slug=${orgSlug}` : "";
919
+ const data = await request(
920
+ apiUrl,
921
+ `/api/sso/branches${params}`,
922
+ { headers: buildHeaders() }
923
+ );
924
+ return data.branches.find((b) => b.is_headquarters) ?? null;
925
+ } catch {
926
+ return null;
927
+ }
928
+ },
929
+ /**
930
+ * Get primary branch for current user
931
+ */
932
+ getPrimary: async (orgSlug) => {
933
+ try {
934
+ const params = orgSlug ? `?organization_slug=${orgSlug}` : "";
935
+ const data = await request(
936
+ apiUrl,
937
+ `/api/sso/branches${params}`,
938
+ { headers: buildHeaders() }
939
+ );
940
+ if (data.primary_branch_id) {
941
+ return data.branches.find((b) => b.id === data.primary_branch_id) ?? null;
942
+ }
943
+ return null;
944
+ } catch {
945
+ return null;
946
+ }
947
+ }
948
+ };
949
+ }
950
+
951
+ // src/queryKeys.ts
952
+ var ssoQueryKeys = {
953
+ all: ["sso"],
954
+ // =========================================================================
955
+ // Auth (authService)
956
+ // =========================================================================
957
+ auth: {
958
+ all: () => [...ssoQueryKeys.all, "auth"],
959
+ user: () => [...ssoQueryKeys.auth.all(), "user"],
960
+ globalLogoutUrl: (redirectUri) => [...ssoQueryKeys.auth.all(), "global-logout-url", redirectUri]
961
+ },
962
+ // =========================================================================
963
+ // Tokens (tokenService)
964
+ // =========================================================================
965
+ tokens: {
966
+ all: () => [...ssoQueryKeys.all, "tokens"],
967
+ list: () => [...ssoQueryKeys.tokens.all(), "list"]
968
+ },
969
+ // =========================================================================
970
+ // Roles (roleService)
971
+ // =========================================================================
972
+ roles: {
973
+ all: () => [...ssoQueryKeys.all, "roles"],
974
+ list: () => [...ssoQueryKeys.roles.all(), "list"],
975
+ detail: (id) => [...ssoQueryKeys.roles.all(), "detail", id],
976
+ permissions: (id) => [...ssoQueryKeys.roles.all(), id, "permissions"]
977
+ },
978
+ // =========================================================================
979
+ // Permissions (permissionService)
980
+ // =========================================================================
981
+ permissions: {
982
+ all: () => [...ssoQueryKeys.all, "permissions"],
983
+ list: (params) => [...ssoQueryKeys.permissions.all(), "list", params],
984
+ detail: (id) => [...ssoQueryKeys.permissions.all(), "detail", id],
985
+ matrix: () => [...ssoQueryKeys.permissions.all(), "matrix"]
986
+ },
987
+ // =========================================================================
988
+ // Teams (teamService)
989
+ // =========================================================================
990
+ teams: {
991
+ all: () => [...ssoQueryKeys.all, "teams"],
992
+ list: () => [...ssoQueryKeys.teams.all(), "list"],
993
+ permissions: (teamId) => [...ssoQueryKeys.teams.all(), teamId, "permissions"],
994
+ orphaned: () => [...ssoQueryKeys.teams.all(), "orphaned"]
995
+ },
996
+ // =========================================================================
997
+ // User Roles (userRoleService) - Scoped Role Assignments
998
+ // =========================================================================
999
+ userRoles: {
1000
+ all: () => [...ssoQueryKeys.all, "user-roles"],
1001
+ list: (userId) => [...ssoQueryKeys.userRoles.all(), userId],
1002
+ byBranch: (userId, orgId, branchId) => [...ssoQueryKeys.userRoles.all(), userId, orgId, branchId]
1003
+ },
1004
+ // =========================================================================
1005
+ // Branches (branchService)
1006
+ // =========================================================================
1007
+ branches: {
1008
+ all: () => [...ssoQueryKeys.all, "branches"],
1009
+ list: (orgSlug) => [...ssoQueryKeys.branches.all(), "list", orgSlug],
1010
+ detail: (branchId) => [...ssoQueryKeys.branches.all(), "detail", branchId],
1011
+ headquarters: (orgSlug) => [...ssoQueryKeys.branches.all(), "headquarters", orgSlug],
1012
+ primary: (orgSlug) => [...ssoQueryKeys.branches.all(), "primary", orgSlug]
1013
+ },
1014
+ // =========================================================================
1015
+ // Admin variants (with org context)
1016
+ // =========================================================================
1017
+ admin: {
1018
+ roles: {
1019
+ all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "roles"],
1020
+ list: (orgSlug) => [...ssoQueryKeys.admin.roles.all(orgSlug), "list"],
1021
+ detail: (orgSlug, id) => [...ssoQueryKeys.admin.roles.all(orgSlug), "detail", id],
1022
+ permissions: (orgSlug, id) => [...ssoQueryKeys.admin.roles.all(orgSlug), id, "permissions"]
1023
+ },
1024
+ permissions: {
1025
+ all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "permissions"],
1026
+ list: (orgSlug, params) => [...ssoQueryKeys.admin.permissions.all(orgSlug), "list", params],
1027
+ detail: (orgSlug, id) => [...ssoQueryKeys.admin.permissions.all(orgSlug), "detail", id],
1028
+ matrix: (orgSlug) => [...ssoQueryKeys.admin.permissions.all(orgSlug), "matrix"]
1029
+ },
1030
+ teams: {
1031
+ all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "teams"],
1032
+ list: (orgSlug) => [...ssoQueryKeys.admin.teams.all(orgSlug), "list"],
1033
+ permissions: (orgSlug, teamId) => [...ssoQueryKeys.admin.teams.all(orgSlug), teamId, "permissions"],
1034
+ orphaned: (orgSlug) => [...ssoQueryKeys.admin.teams.all(orgSlug), "orphaned"]
1035
+ },
1036
+ userRoles: {
1037
+ all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "user-roles"],
1038
+ list: (orgSlug, userId) => [...ssoQueryKeys.admin.userRoles.all(orgSlug), userId],
1039
+ byBranch: (orgSlug, userId, orgId, branchId) => [...ssoQueryKeys.admin.userRoles.all(orgSlug), userId, orgId, branchId]
1040
+ }
1041
+ }
1042
+ };
1043
+
1044
+ // src/context/BranchProvider.tsx
1045
+ var import_jsx_runtime2 = require("react/jsx-runtime");
1046
+ var DEFAULT_STORAGE_KEY = "omnify_selected_branch";
1047
+ function BranchProvider({
1048
+ children,
1049
+ storage = "localStorage",
1050
+ storageKey = DEFAULT_STORAGE_KEY,
1051
+ onBranchChange
1052
+ }) {
1053
+ const { config, currentOrg, isAuthenticated } = useSsoContext();
1054
+ const queryClient = (0, import_react_query.useQueryClient)();
1055
+ const branchService = (0, import_react4.useMemo)(
1056
+ () => createBranchService({ apiUrl: config.apiUrl }),
1057
+ [config.apiUrl]
1058
+ );
1059
+ const getSavedBranchId = (0, import_react4.useCallback)(() => {
1060
+ if (typeof window === "undefined") return null;
1061
+ const storageObj = storage === "localStorage" ? localStorage : sessionStorage;
1062
+ const saved = storageObj.getItem(`${storageKey}_${currentOrg?.slug}`);
1063
+ return saved ? parseInt(saved, 10) : null;
1064
+ }, [storage, storageKey, currentOrg?.slug]);
1065
+ const saveBranchId = (0, import_react4.useCallback)((branchId) => {
1066
+ if (typeof window === "undefined" || !currentOrg?.slug) return;
1067
+ const storageObj = storage === "localStorage" ? localStorage : sessionStorage;
1068
+ if (branchId) {
1069
+ storageObj.setItem(`${storageKey}_${currentOrg.slug}`, String(branchId));
1070
+ } else {
1071
+ storageObj.removeItem(`${storageKey}_${currentOrg.slug}`);
1072
+ }
1073
+ }, [storage, storageKey, currentOrg?.slug]);
1074
+ const [selectedBranchId, setSelectedBranchId] = (0, import_react4.useState)(() => getSavedBranchId());
1075
+ const {
1076
+ data: branchesData,
1077
+ isLoading,
1078
+ error,
1079
+ refetch
1080
+ } = (0, import_react_query.useQuery)({
1081
+ queryKey: ssoQueryKeys.branches.list(currentOrg?.slug),
1082
+ queryFn: () => branchService.list(currentOrg?.slug),
1083
+ enabled: isAuthenticated && !!currentOrg,
1084
+ staleTime: 5 * 60 * 1e3
1085
+ // 5 minutes
1086
+ });
1087
+ const branches = branchesData?.branches ?? [];
1088
+ const allBranchesAccess = branchesData?.all_branches_access ?? false;
1089
+ const primaryBranchId = branchesData?.primary_branch_id ?? null;
1090
+ const hasMultipleBranches = branches.length > 1;
1091
+ const currentBranch = (0, import_react4.useMemo)(() => {
1092
+ if (!branches.length) return null;
1093
+ if (branches.length === 1) {
1094
+ return branches[0];
1095
+ }
1096
+ if (selectedBranchId) {
1097
+ const found = branches.find((b) => b.id === selectedBranchId);
1098
+ if (found) return found;
1099
+ }
1100
+ if (primaryBranchId) {
1101
+ const primary = branches.find((b) => b.id === primaryBranchId);
1102
+ if (primary) return primary;
1103
+ }
1104
+ const hq = branches.find((b) => b.is_headquarters);
1105
+ if (hq) return hq;
1106
+ return branches[0];
1107
+ }, [branches, selectedBranchId, primaryBranchId]);
1108
+ (0, import_react4.useEffect)(() => {
1109
+ if (currentBranch && currentBranch.id !== selectedBranchId) {
1110
+ setSelectedBranchId(currentBranch.id);
1111
+ saveBranchId(currentBranch.id);
1112
+ onBranchChange?.(currentBranch);
1113
+ }
1114
+ }, [currentBranch, selectedBranchId, saveBranchId, onBranchChange]);
1115
+ (0, import_react4.useEffect)(() => {
1116
+ const savedId = getSavedBranchId();
1117
+ setSelectedBranchId(savedId);
1118
+ }, [currentOrg?.slug, getSavedBranchId]);
1119
+ const switchBranch = (0, import_react4.useCallback)((branchId) => {
1120
+ const branch = branches.find((b) => b.id === branchId);
1121
+ if (branch) {
1122
+ setSelectedBranchId(branchId);
1123
+ saveBranchId(branchId);
1124
+ onBranchChange?.(branch);
1125
+ }
1126
+ }, [branches, saveBranchId, onBranchChange]);
1127
+ const refreshBranches = (0, import_react4.useCallback)(async () => {
1128
+ await queryClient.invalidateQueries({
1129
+ queryKey: ssoQueryKeys.branches.list(currentOrg?.slug)
1130
+ });
1131
+ await refetch();
1132
+ }, [queryClient, currentOrg?.slug, refetch]);
1133
+ const contextValue = (0, import_react4.useMemo)(
1134
+ () => ({
1135
+ branches,
1136
+ currentBranch,
1137
+ allBranchesAccess,
1138
+ primaryBranchId,
1139
+ isLoading,
1140
+ error,
1141
+ hasMultipleBranches,
1142
+ switchBranch,
1143
+ refreshBranches
1144
+ }),
1145
+ [
1146
+ branches,
1147
+ currentBranch,
1148
+ allBranchesAccess,
1149
+ primaryBranchId,
1150
+ isLoading,
1151
+ error,
1152
+ hasMultipleBranches,
1153
+ switchBranch,
1154
+ refreshBranches
1155
+ ]
1156
+ );
1157
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(BranchContext.Provider, { value: contextValue, children });
1158
+ }
1159
+
1160
+ // src/hooks/useAuth.ts
1161
+ var import_react5 = require("react");
765
1162
  function useAuth() {
766
1163
  const { user, isLoading, isAuthenticated, login, logout, globalLogout, refreshUser } = useSsoContext();
767
- const handleLogin = (0, import_react3.useCallback)(
1164
+ const handleLogin = (0, import_react5.useCallback)(
768
1165
  (redirectTo) => {
769
1166
  login(redirectTo);
770
1167
  },
771
1168
  [login]
772
1169
  );
773
- const handleLogout = (0, import_react3.useCallback)(async () => {
1170
+ const handleLogout = (0, import_react5.useCallback)(async () => {
774
1171
  await logout();
775
1172
  }, [logout]);
776
- const handleGlobalLogout = (0, import_react3.useCallback)(
1173
+ const handleGlobalLogout = (0, import_react5.useCallback)(
777
1174
  (redirectTo) => {
778
1175
  globalLogout(redirectTo);
779
1176
  },
@@ -791,7 +1188,7 @@ function useAuth() {
791
1188
  }
792
1189
 
793
1190
  // src/hooks/useOrganization.ts
794
- var import_react4 = require("react");
1191
+ var import_react6 = require("react");
795
1192
  var ROLE_LEVELS = {
796
1193
  admin: 100,
797
1194
  manager: 50,
@@ -801,7 +1198,7 @@ function useOrganization() {
801
1198
  const { organizations, currentOrg, switchOrg } = useSsoContext();
802
1199
  const hasMultipleOrgs = organizations.length > 1;
803
1200
  const currentRole = currentOrg?.serviceRole ?? null;
804
- const hasRole = (0, import_react4.useCallback)(
1201
+ const hasRole = (0, import_react6.useCallback)(
805
1202
  (role) => {
806
1203
  if (!currentRole) return false;
807
1204
  const requiredLevel = ROLE_LEVELS[role] ?? 0;
@@ -810,13 +1207,13 @@ function useOrganization() {
810
1207
  },
811
1208
  [currentRole]
812
1209
  );
813
- const handleSwitchOrg = (0, import_react4.useCallback)(
1210
+ const handleSwitchOrg = (0, import_react6.useCallback)(
814
1211
  (orgSlug) => {
815
1212
  switchOrg(orgSlug);
816
1213
  },
817
1214
  [switchOrg]
818
1215
  );
819
- return (0, import_react4.useMemo)(
1216
+ return (0, import_react6.useMemo)(
820
1217
  () => ({
821
1218
  organizations,
822
1219
  currentOrg,
@@ -830,10 +1227,10 @@ function useOrganization() {
830
1227
  }
831
1228
 
832
1229
  // src/hooks/useSso.ts
833
- var import_react5 = require("react");
1230
+ var import_react7 = require("react");
834
1231
  function useSso() {
835
1232
  const context = useSsoContext();
836
- return (0, import_react5.useMemo)(
1233
+ return (0, import_react7.useMemo)(
837
1234
  () => ({
838
1235
  // Auth
839
1236
  user: context.user,
@@ -856,9 +1253,14 @@ function useSso() {
856
1253
  );
857
1254
  }
858
1255
 
1256
+ // src/hooks/useBranch.ts
1257
+ function useBranch() {
1258
+ return useBranchContext();
1259
+ }
1260
+
859
1261
  // src/components/SsoCallback.tsx
860
- var import_react6 = require("react");
861
- var import_jsx_runtime2 = require("react/jsx-runtime");
1262
+ var import_react8 = require("react");
1263
+ var import_jsx_runtime3 = require("react/jsx-runtime");
862
1264
  function transformUser2(data) {
863
1265
  return {
864
1266
  id: data.id,
@@ -877,15 +1279,15 @@ function transformOrganizations2(data) {
877
1279
  }));
878
1280
  }
879
1281
  function DefaultLoading() {
880
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: {
1282
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: {
881
1283
  display: "flex",
882
1284
  justifyContent: "center",
883
1285
  alignItems: "center",
884
1286
  minHeight: "200px"
885
- }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "Authenticating..." }) });
1287
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Authenticating..." }) });
886
1288
  }
887
1289
  function DefaultError({ error }) {
888
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: {
1290
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { style: {
889
1291
  display: "flex",
890
1292
  flexDirection: "column",
891
1293
  justifyContent: "center",
@@ -893,8 +1295,8 @@ function DefaultError({ error }) {
893
1295
  minHeight: "200px",
894
1296
  color: "red"
895
1297
  }, children: [
896
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "Authentication Error" }),
897
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "0.875rem", marginTop: "0.5rem" }, children: error.message })
1298
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Authentication Error" }),
1299
+ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { fontSize: "0.875rem", marginTop: "0.5rem" }, children: error.message })
898
1300
  ] });
899
1301
  }
900
1302
  function SsoCallback({
@@ -905,10 +1307,10 @@ function SsoCallback({
905
1307
  errorComponent
906
1308
  }) {
907
1309
  const { config, refreshUser } = useSsoContext();
908
- const [error, setError] = (0, import_react6.useState)(null);
909
- const [isProcessing, setIsProcessing] = (0, import_react6.useState)(true);
910
- const isProcessingRef = (0, import_react6.useRef)(false);
911
- (0, import_react6.useEffect)(() => {
1310
+ const [error, setError] = (0, import_react8.useState)(null);
1311
+ const [isProcessing, setIsProcessing] = (0, import_react8.useState)(true);
1312
+ const isProcessingRef = (0, import_react8.useRef)(false);
1313
+ (0, import_react8.useEffect)(() => {
912
1314
  if (isProcessingRef.current) {
913
1315
  return;
914
1316
  }
@@ -959,24 +1361,24 @@ function SsoCallback({
959
1361
  }, []);
960
1362
  if (error) {
961
1363
  if (errorComponent) {
962
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: errorComponent(error) });
1364
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: errorComponent(error) });
963
1365
  }
964
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DefaultError, { error });
1366
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DefaultError, { error });
965
1367
  }
966
1368
  if (isProcessing) {
967
1369
  if (loadingComponent) {
968
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: loadingComponent });
1370
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: loadingComponent });
969
1371
  }
970
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DefaultLoading, {});
1372
+ return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DefaultLoading, {});
971
1373
  }
972
1374
  return null;
973
1375
  }
974
1376
 
975
1377
  // src/components/OrganizationSwitcher.tsx
976
- var import_react7 = __toESM(require("react"), 1);
1378
+ var import_react9 = __toESM(require("react"), 1);
977
1379
  var import_antd = require("antd");
978
1380
  var import_icons = require("@ant-design/icons");
979
- var import_jsx_runtime3 = require("react/jsx-runtime");
1381
+ var import_jsx_runtime4 = require("react/jsx-runtime");
980
1382
  var { Text } = import_antd.Typography;
981
1383
  function OrganizationSwitcher({
982
1384
  className,
@@ -985,8 +1387,8 @@ function OrganizationSwitcher({
985
1387
  onChange
986
1388
  }) {
987
1389
  const { organizations, currentOrg, hasMultipleOrgs, switchOrg } = useOrganization();
988
- const [isOpen, setIsOpen] = import_react7.default.useState(false);
989
- const handleSelect = (0, import_react7.useCallback)(
1390
+ const [isOpen, setIsOpen] = import_react9.default.useState(false);
1391
+ const handleSelect = (0, import_react9.useCallback)(
990
1392
  (org) => {
991
1393
  switchOrg(org.slug);
992
1394
  setIsOpen(false);
@@ -994,23 +1396,23 @@ function OrganizationSwitcher({
994
1396
  },
995
1397
  [switchOrg, onChange]
996
1398
  );
997
- const menuItems = (0, import_react7.useMemo)(() => {
1399
+ const menuItems = (0, import_react9.useMemo)(() => {
998
1400
  return organizations.map((org) => {
999
1401
  const isSelected = currentOrg?.slug === org.slug;
1000
1402
  if (renderOption) {
1001
1403
  return {
1002
1404
  key: org.slug,
1003
- label: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { onClick: () => handleSelect(org), children: renderOption(org, isSelected) })
1405
+ label: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { onClick: () => handleSelect(org), children: renderOption(org, isSelected) })
1004
1406
  };
1005
1407
  }
1006
1408
  return {
1007
1409
  key: org.slug,
1008
- label: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_antd.Space, { style: { width: "100%", justifyContent: "space-between" }, children: [
1009
- /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_antd.Space, { direction: "vertical", size: 0, children: [
1010
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Text, { strong: isSelected, children: org.name }),
1011
- org.serviceRole && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Text, { type: "secondary", style: { fontSize: 12 }, children: org.serviceRole })
1410
+ label: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_antd.Space, { style: { width: "100%", justifyContent: "space-between" }, children: [
1411
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_antd.Space, { direction: "vertical", size: 0, children: [
1412
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Text, { strong: isSelected, children: org.name }),
1413
+ org.serviceRole && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Text, { type: "secondary", style: { fontSize: 12 }, children: org.serviceRole })
1012
1414
  ] }),
1013
- isSelected && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_icons.CheckOutlined, { style: { color: "#1890ff" } })
1415
+ isSelected && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_icons.CheckOutlined, { style: { color: "#1890ff" } })
1014
1416
  ] }),
1015
1417
  onClick: () => handleSelect(org)
1016
1418
  };
@@ -1020,7 +1422,7 @@ function OrganizationSwitcher({
1020
1422
  return null;
1021
1423
  }
1022
1424
  if (renderTrigger) {
1023
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1425
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1024
1426
  import_antd.Dropdown,
1025
1427
  {
1026
1428
  menu: { items: menuItems },
@@ -1028,11 +1430,11 @@ function OrganizationSwitcher({
1028
1430
  open: isOpen,
1029
1431
  onOpenChange: setIsOpen,
1030
1432
  className,
1031
- children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { style: { cursor: "pointer" }, children: renderTrigger(currentOrg, isOpen) })
1433
+ children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { cursor: "pointer" }, children: renderTrigger(currentOrg, isOpen) })
1032
1434
  }
1033
1435
  );
1034
1436
  }
1035
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
1437
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1036
1438
  import_antd.Dropdown,
1037
1439
  {
1038
1440
  menu: { items: menuItems },
@@ -1040,28 +1442,28 @@ function OrganizationSwitcher({
1040
1442
  open: isOpen,
1041
1443
  onOpenChange: setIsOpen,
1042
1444
  className,
1043
- children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_antd.Button, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_antd.Space, { children: [
1044
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_antd.Badge, { status: "success" }),
1045
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("span", { children: currentOrg?.name ?? "Select Organization" }),
1046
- /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_icons.SwapOutlined, {})
1445
+ children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_antd.Button, { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_antd.Space, { children: [
1446
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_antd.Badge, { status: "success" }),
1447
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: currentOrg?.name ?? "Select Organization" }),
1448
+ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_icons.SwapOutlined, {})
1047
1449
  ] }) })
1048
1450
  }
1049
1451
  );
1050
1452
  }
1051
1453
 
1052
1454
  // src/components/ProtectedRoute.tsx
1053
- var import_react8 = require("react");
1054
- var import_jsx_runtime4 = require("react/jsx-runtime");
1455
+ var import_react10 = require("react");
1456
+ var import_jsx_runtime5 = require("react/jsx-runtime");
1055
1457
  function DefaultLoading2() {
1056
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: {
1458
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: {
1057
1459
  display: "flex",
1058
1460
  justifyContent: "center",
1059
1461
  alignItems: "center",
1060
1462
  minHeight: "200px"
1061
- }, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: "Loading..." }) });
1463
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { children: "Loading..." }) });
1062
1464
  }
1063
1465
  function DefaultLoginFallback({ login }) {
1064
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: {
1466
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: {
1065
1467
  display: "flex",
1066
1468
  flexDirection: "column",
1067
1469
  justifyContent: "center",
@@ -1069,8 +1471,8 @@ function DefaultLoginFallback({ login }) {
1069
1471
  minHeight: "200px",
1070
1472
  gap: "1rem"
1071
1473
  }, children: [
1072
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { children: "Please log in to continue" }),
1073
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1474
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { children: "Please log in to continue" }),
1475
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1074
1476
  "button",
1075
1477
  {
1076
1478
  onClick: login,
@@ -1088,7 +1490,7 @@ function DefaultLoginFallback({ login }) {
1088
1490
  ] });
1089
1491
  }
1090
1492
  function DefaultAccessDenied({ reason }) {
1091
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { style: {
1493
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { style: {
1092
1494
  display: "flex",
1093
1495
  flexDirection: "column",
1094
1496
  justifyContent: "center",
@@ -1096,8 +1498,8 @@ function DefaultAccessDenied({ reason }) {
1096
1498
  minHeight: "200px",
1097
1499
  color: "#dc2626"
1098
1500
  }, children: [
1099
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { fontSize: "1.5rem", fontWeight: 600 }, children: "Access Denied" }),
1100
- /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { style: { marginTop: "0.5rem" }, children: reason })
1501
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { fontSize: "1.5rem", fontWeight: 600 }, children: "Access Denied" }),
1502
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { style: { marginTop: "0.5rem" }, children: reason })
1101
1503
  ] });
1102
1504
  }
1103
1505
  function ProtectedRoute({
@@ -1110,7 +1512,7 @@ function ProtectedRoute({
1110
1512
  }) {
1111
1513
  const { user, isLoading, isAuthenticated, login } = useAuth();
1112
1514
  const { hasRole, currentOrg } = useOrganization();
1113
- (0, import_react8.useEffect)(() => {
1515
+ (0, import_react10.useEffect)(() => {
1114
1516
  if (isLoading) return;
1115
1517
  if (!isAuthenticated) {
1116
1518
  onAccessDenied?.("unauthenticated");
@@ -1119,62 +1521,620 @@ function ProtectedRoute({
1119
1521
  }
1120
1522
  }, [isLoading, isAuthenticated, requiredRole, hasRole, onAccessDenied]);
1121
1523
  if (isLoading) {
1122
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: fallback ?? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DefaultLoading2, {}) });
1524
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: fallback ?? /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(DefaultLoading2, {}) });
1123
1525
  }
1124
1526
  if (!isAuthenticated) {
1125
1527
  if (loginFallback) {
1126
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: loginFallback });
1528
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: loginFallback });
1127
1529
  }
1128
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(DefaultLoginFallback, { login: () => login() });
1530
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(DefaultLoginFallback, { login: () => login() });
1129
1531
  }
1130
1532
  if (requiredRole && !hasRole(requiredRole)) {
1131
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
1533
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1132
1534
  DefaultAccessDenied,
1133
1535
  {
1134
1536
  reason: `This page requires ${requiredRole} role. Your role: ${currentOrg?.serviceRole ?? "none"}`
1135
1537
  }
1136
1538
  );
1137
1539
  }
1138
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children });
1540
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children });
1139
1541
  }
1140
1542
 
1141
- // src/services/utils.ts
1142
- function getXsrfToken2() {
1143
- if (typeof document === "undefined") return void 0;
1144
- return document.cookie.split("; ").find((row) => row.startsWith("XSRF-TOKEN="))?.split("=")[1];
1145
- }
1146
- function buildHeaders(orgSlug) {
1147
- const headers = {
1148
- "Content-Type": "application/json",
1149
- Accept: "application/json"
1150
- };
1151
- const xsrfToken = getXsrfToken2();
1152
- if (xsrfToken) {
1153
- headers["X-XSRF-TOKEN"] = decodeURIComponent(xsrfToken);
1154
- }
1155
- if (orgSlug) {
1156
- headers["X-Org-Slug"] = orgSlug;
1157
- }
1158
- return headers;
1159
- }
1160
- async function csrf(apiUrl) {
1161
- await fetch(`${apiUrl}/sanctum/csrf-cookie`, {
1162
- credentials: "include"
1543
+ // src/components/OrgBranchSelectorModal.tsx
1544
+ var import_react11 = require("react");
1545
+ var import_antd2 = require("antd");
1546
+ var import_icons2 = require("@ant-design/icons");
1547
+ var import_react_query2 = require("@tanstack/react-query");
1548
+ var import_jsx_runtime6 = require("react/jsx-runtime");
1549
+ var { Text: Text2, Title } = import_antd2.Typography;
1550
+ function OrgBranchSelectorModal({
1551
+ open,
1552
+ onClose,
1553
+ onConfirm,
1554
+ title = "Select Organization & Branch",
1555
+ requireBranch = true,
1556
+ loadingComponent
1557
+ }) {
1558
+ const { config } = useSsoContext();
1559
+ const { organizations, currentOrg, hasMultipleOrgs } = useOrganization();
1560
+ const [form] = import_antd2.Form.useForm();
1561
+ const [selectedOrgSlug, setSelectedOrgSlug] = (0, import_react11.useState)(
1562
+ currentOrg?.slug ?? null
1563
+ );
1564
+ const branchService = (0, import_react11.useMemo)(
1565
+ () => createBranchService({ apiUrl: config.apiUrl }),
1566
+ [config.apiUrl]
1567
+ );
1568
+ const {
1569
+ data: branchesData,
1570
+ isLoading: branchesLoading,
1571
+ error: branchesError
1572
+ } = (0, import_react_query2.useQuery)({
1573
+ queryKey: ssoQueryKeys.branches.list(selectedOrgSlug ?? void 0),
1574
+ queryFn: () => branchService.list(selectedOrgSlug ?? void 0),
1575
+ enabled: open && !!selectedOrgSlug
1163
1576
  });
1577
+ const branches = branchesData?.branches ?? [];
1578
+ const hasMultipleBranches = branches.length > 1;
1579
+ const selectedOrg = (0, import_react11.useMemo)(
1580
+ () => organizations.find((o) => o.slug === selectedOrgSlug) ?? null,
1581
+ [organizations, selectedOrgSlug]
1582
+ );
1583
+ (0, import_react11.useEffect)(() => {
1584
+ if (!open) return;
1585
+ if (organizations.length === 1 && !selectedOrgSlug) {
1586
+ setSelectedOrgSlug(organizations[0].slug);
1587
+ form.setFieldValue("organization_id", organizations[0].id);
1588
+ } else if (currentOrg && !selectedOrgSlug) {
1589
+ setSelectedOrgSlug(currentOrg.slug);
1590
+ form.setFieldValue("organization_id", currentOrg.id);
1591
+ }
1592
+ }, [open, organizations, currentOrg, selectedOrgSlug, form]);
1593
+ (0, import_react11.useEffect)(() => {
1594
+ if (!open || branchesLoading) return;
1595
+ if (branches.length === 1) {
1596
+ form.setFieldValue("branch_id", branches[0].id);
1597
+ } else if (branches.length > 0) {
1598
+ const primaryId = branchesData?.primary_branch_id;
1599
+ if (primaryId) {
1600
+ form.setFieldValue("branch_id", primaryId);
1601
+ } else {
1602
+ const hq = branches.find((b) => b.is_headquarters);
1603
+ if (hq) {
1604
+ form.setFieldValue("branch_id", hq.id);
1605
+ }
1606
+ }
1607
+ }
1608
+ }, [open, branches, branchesLoading, branchesData, form]);
1609
+ (0, import_react11.useEffect)(() => {
1610
+ if (!open || branchesLoading) return;
1611
+ const orgId = form.getFieldValue("organization_id");
1612
+ const branchId = form.getFieldValue("branch_id");
1613
+ if (orgId && branchId && !hasMultipleOrgs && !hasMultipleBranches) {
1614
+ const timer = setTimeout(() => {
1615
+ onConfirm(orgId, branchId);
1616
+ }, 100);
1617
+ return () => clearTimeout(timer);
1618
+ }
1619
+ }, [open, branchesLoading, hasMultipleOrgs, hasMultipleBranches, form, onConfirm]);
1620
+ const handleOrgChange = (0, import_react11.useCallback)((orgId) => {
1621
+ const org = organizations.find((o) => o.id === orgId);
1622
+ if (org) {
1623
+ setSelectedOrgSlug(org.slug);
1624
+ form.setFieldValue("branch_id", void 0);
1625
+ }
1626
+ }, [organizations, form]);
1627
+ const handleSubmit = (0, import_react11.useCallback)(() => {
1628
+ form.validateFields().then((values) => {
1629
+ onConfirm(values.organization_id, values.branch_id);
1630
+ });
1631
+ }, [form, onConfirm]);
1632
+ const handleClose = (0, import_react11.useCallback)(() => {
1633
+ form.resetFields();
1634
+ setSelectedOrgSlug(currentOrg?.slug ?? null);
1635
+ onClose();
1636
+ }, [form, currentOrg, onClose]);
1637
+ const isInitialLoading = open && (!organizations.length || selectedOrgSlug && branchesLoading && !branches.length);
1638
+ const needsSelection = hasMultipleOrgs || hasMultipleBranches;
1639
+ return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1640
+ import_antd2.Modal,
1641
+ {
1642
+ title: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { children: [
1643
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.BankOutlined, {}),
1644
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { children: title })
1645
+ ] }),
1646
+ open,
1647
+ onCancel: handleClose,
1648
+ onOk: handleSubmit,
1649
+ okText: "Confirm",
1650
+ cancelText: "Cancel",
1651
+ destroyOnHidden: true,
1652
+ width: 480,
1653
+ children: isInitialLoading ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { textAlign: "center", padding: 40 }, children: [
1654
+ loadingComponent ?? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_antd2.Spin, { size: "large" }),
1655
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("div", { style: { marginTop: 16 }, children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { type: "secondary", children: "Loading..." }) })
1656
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
1657
+ import_antd2.Form,
1658
+ {
1659
+ form,
1660
+ layout: "vertical",
1661
+ style: { marginTop: 16 },
1662
+ children: [
1663
+ branchesError && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1664
+ import_antd2.Alert,
1665
+ {
1666
+ type: "error",
1667
+ message: "Failed to load branches",
1668
+ description: branchesError.message,
1669
+ style: { marginBottom: 16 }
1670
+ }
1671
+ ),
1672
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1673
+ import_antd2.Form.Item,
1674
+ {
1675
+ name: "organization_id",
1676
+ label: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { children: [
1677
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.BankOutlined, {}),
1678
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { children: "Organization" }),
1679
+ !hasMultipleOrgs && selectedOrg && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_antd2.Badge, { status: "success", text: "Auto-selected" })
1680
+ ] }),
1681
+ rules: [{ required: true, message: "Please select an organization" }],
1682
+ children: hasMultipleOrgs ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1683
+ import_antd2.Select,
1684
+ {
1685
+ placeholder: "Select organization",
1686
+ onChange: handleOrgChange,
1687
+ size: "large",
1688
+ optionLabelProp: "label",
1689
+ children: organizations.map((org) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1690
+ import_antd2.Select.Option,
1691
+ {
1692
+ value: org.id,
1693
+ label: org.name,
1694
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { style: { width: "100%", justifyContent: "space-between" }, children: [
1695
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { children: [
1696
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { strong: true, children: org.name }),
1697
+ org.serviceRole && /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Text2, { type: "secondary", style: { marginLeft: 8, fontSize: 12 }, children: [
1698
+ "(",
1699
+ org.serviceRole,
1700
+ ")"
1701
+ ] })
1702
+ ] }),
1703
+ org.slug === currentOrg?.slug && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.CheckCircleFilled, { style: { color: "#52c41a" } })
1704
+ ] })
1705
+ },
1706
+ org.id
1707
+ ))
1708
+ }
1709
+ ) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1710
+ "div",
1711
+ {
1712
+ style: {
1713
+ padding: "8px 12px",
1714
+ background: "#f5f5f5",
1715
+ borderRadius: 6,
1716
+ border: "1px solid #d9d9d9"
1717
+ },
1718
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { children: [
1719
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.BankOutlined, { style: { color: "#1677ff" } }),
1720
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { strong: true, children: selectedOrg?.name })
1721
+ ] })
1722
+ }
1723
+ )
1724
+ }
1725
+ ),
1726
+ selectedOrgSlug && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1727
+ import_antd2.Form.Item,
1728
+ {
1729
+ name: "branch_id",
1730
+ label: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { children: [
1731
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.ApartmentOutlined, {}),
1732
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)("span", { children: "Branch" }),
1733
+ !hasMultipleBranches && branches.length === 1 && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_antd2.Badge, { status: "success", text: "Auto-selected" })
1734
+ ] }),
1735
+ rules: requireBranch ? [{ required: true, message: "Please select a branch" }] : void 0,
1736
+ children: branchesLoading ? /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { style: { padding: "8px 12px", textAlign: "center" }, children: [
1737
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_antd2.Spin, { size: "small" }),
1738
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { type: "secondary", style: { marginLeft: 8 }, children: "Loading branches..." })
1739
+ ] }) : hasMultipleBranches ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1740
+ import_antd2.Select,
1741
+ {
1742
+ placeholder: "Select branch",
1743
+ size: "large",
1744
+ optionLabelProp: "label",
1745
+ children: branches.map((branch) => /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1746
+ import_antd2.Select.Option,
1747
+ {
1748
+ value: branch.id,
1749
+ label: branch.name,
1750
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { style: { width: "100%", justifyContent: "space-between" }, children: [
1751
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)("div", { children: [
1752
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { strong: true, children: branch.name }),
1753
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { type: "secondary", style: { marginLeft: 8, fontSize: 12 }, children: branch.code })
1754
+ ] }),
1755
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { size: 4, children: [
1756
+ branch.is_headquarters && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_antd2.Badge, { color: "blue", text: "HQ" }),
1757
+ branch.is_primary && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_antd2.Badge, { color: "green", text: "Primary" })
1758
+ ] })
1759
+ ] })
1760
+ },
1761
+ branch.id
1762
+ ))
1763
+ }
1764
+ ) : branches.length === 1 ? /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1765
+ "div",
1766
+ {
1767
+ style: {
1768
+ padding: "8px 12px",
1769
+ background: "#f5f5f5",
1770
+ borderRadius: 6,
1771
+ border: "1px solid #d9d9d9"
1772
+ },
1773
+ children: /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(import_antd2.Space, { children: [
1774
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.ApartmentOutlined, { style: { color: "#1677ff" } }),
1775
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(Text2, { strong: true, children: branches[0].name }),
1776
+ /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(Text2, { type: "secondary", children: [
1777
+ "(",
1778
+ branches[0].code,
1779
+ ")"
1780
+ ] })
1781
+ ] })
1782
+ }
1783
+ ) : /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1784
+ import_antd2.Alert,
1785
+ {
1786
+ type: "warning",
1787
+ message: "No branches available",
1788
+ description: "You don't have access to any branches in this organization."
1789
+ }
1790
+ )
1791
+ }
1792
+ ),
1793
+ !needsSelection && selectedOrg && branches.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
1794
+ import_antd2.Alert,
1795
+ {
1796
+ type: "info",
1797
+ message: "Auto-selected",
1798
+ description: `Using ${selectedOrg.name} / ${branches[0]?.name ?? "Default"}`,
1799
+ icon: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_icons2.CheckCircleFilled, {}),
1800
+ showIcon: true
1801
+ }
1802
+ )
1803
+ ]
1804
+ }
1805
+ )
1806
+ }
1807
+ );
1164
1808
  }
1165
- async function request(apiUrl, path, options = {}) {
1166
- const response = await fetch(`${apiUrl}${path}`, {
1167
- ...options,
1168
- credentials: "include"
1809
+
1810
+ // src/components/BranchGate.tsx
1811
+ var import_react12 = __toESM(require("react"), 1);
1812
+ var import_antd3 = require("antd");
1813
+ var import_icons3 = require("@ant-design/icons");
1814
+ var import_react_query3 = require("@tanstack/react-query");
1815
+ var import_jsx_runtime7 = require("react/jsx-runtime");
1816
+ var { Text: Text3, Title: Title2 } = import_antd3.Typography;
1817
+ var DEFAULT_STORAGE_KEY2 = "omnify_branch_gate_selection";
1818
+ function BranchGate({
1819
+ children,
1820
+ onSelectionChange,
1821
+ storageKey = DEFAULT_STORAGE_KEY2,
1822
+ loadingComponent,
1823
+ title,
1824
+ description
1825
+ }) {
1826
+ const { config, isAuthenticated } = useSsoContext();
1827
+ const { organizations, currentOrg, hasMultipleOrgs } = useOrganization();
1828
+ const [storedSelection, setStoredSelection] = (0, import_react12.useState)(null);
1829
+ const [isInitialized, setIsInitialized] = (0, import_react12.useState)(false);
1830
+ const [tempOrgId, setTempOrgId] = (0, import_react12.useState)(null);
1831
+ const [tempBranchId, setTempBranchId] = (0, import_react12.useState)(null);
1832
+ const branchService = import_react12.default.useMemo(
1833
+ () => createBranchService({ apiUrl: config.apiUrl }),
1834
+ [config.apiUrl]
1835
+ );
1836
+ const activeOrgSlug = import_react12.default.useMemo(() => {
1837
+ if (tempOrgId) {
1838
+ return organizations.find((o) => String(o.id) === tempOrgId)?.slug;
1839
+ }
1840
+ return storedSelection?.orgSlug ?? currentOrg?.slug;
1841
+ }, [tempOrgId, organizations, storedSelection, currentOrg]);
1842
+ const { data: branchesData, isLoading: branchesLoading } = (0, import_react_query3.useQuery)({
1843
+ queryKey: ssoQueryKeys.branches.list(activeOrgSlug),
1844
+ queryFn: () => branchService.list(activeOrgSlug),
1845
+ enabled: isAuthenticated && !!activeOrgSlug
1169
1846
  });
1170
- if (!response.ok) {
1171
- const error = await response.json().catch(() => ({}));
1172
- throw new Error(error.message || `HTTP ${response.status}`);
1847
+ const branches = branchesData?.branches ?? [];
1848
+ const hasMultipleBranches = branches.length > 1;
1849
+ (0, import_react12.useEffect)(() => {
1850
+ if (typeof window === "undefined") return;
1851
+ const saved = localStorage.getItem(storageKey);
1852
+ if (saved) {
1853
+ try {
1854
+ const parsed = JSON.parse(saved);
1855
+ setStoredSelection(parsed);
1856
+ onSelectionChange?.(parsed);
1857
+ } catch {
1858
+ localStorage.removeItem(storageKey);
1859
+ }
1860
+ }
1861
+ setIsInitialized(true);
1862
+ }, [storageKey, onSelectionChange]);
1863
+ (0, import_react12.useEffect)(() => {
1864
+ if (!isAuthenticated || !isInitialized || storedSelection) return;
1865
+ if (organizations.length === 1 && !tempOrgId) {
1866
+ setTempOrgId(String(organizations[0].id));
1867
+ }
1868
+ }, [isAuthenticated, isInitialized, organizations, storedSelection, tempOrgId]);
1869
+ (0, import_react12.useEffect)(() => {
1870
+ if (!tempOrgId || branchesLoading || storedSelection) return;
1871
+ if (branches.length === 1 && !tempBranchId) {
1872
+ setTempBranchId(String(branches[0].id));
1873
+ } else if (branches.length > 0 && !tempBranchId) {
1874
+ const primaryId = branchesData?.primary_branch_id;
1875
+ if (primaryId) {
1876
+ setTempBranchId(String(primaryId));
1877
+ } else {
1878
+ const hq = branches.find((b) => b.is_headquarters);
1879
+ if (hq) {
1880
+ setTempBranchId(String(hq.id));
1881
+ }
1882
+ }
1883
+ }
1884
+ }, [tempOrgId, branches, branchesLoading, branchesData, storedSelection, tempBranchId]);
1885
+ (0, import_react12.useEffect)(() => {
1886
+ if (storedSelection || !isInitialized) return;
1887
+ if (!tempOrgId || !tempBranchId || branchesLoading) return;
1888
+ if (!hasMultipleOrgs && !hasMultipleBranches) {
1889
+ confirmSelection();
1890
+ }
1891
+ }, [tempOrgId, tempBranchId, branchesLoading, hasMultipleOrgs, hasMultipleBranches, storedSelection, isInitialized]);
1892
+ const confirmSelection = import_react12.default.useCallback(() => {
1893
+ if (!tempOrgId || !tempBranchId) return;
1894
+ const org = organizations.find((o) => String(o.id) === tempOrgId);
1895
+ const branch = branches.find((b) => String(b.id) === tempBranchId);
1896
+ if (!org || !branch) return;
1897
+ const selection = {
1898
+ orgId: String(org.id),
1899
+ orgSlug: org.slug,
1900
+ orgName: org.name,
1901
+ branchId: String(branch.id),
1902
+ branchName: branch.name,
1903
+ branchCode: branch.code
1904
+ };
1905
+ localStorage.setItem(storageKey, JSON.stringify(selection));
1906
+ setStoredSelection(selection);
1907
+ onSelectionChange?.(selection);
1908
+ setTempOrgId(null);
1909
+ setTempBranchId(null);
1910
+ }, [tempOrgId, tempBranchId, organizations, branches, storageKey, onSelectionChange]);
1911
+ const clearSelection = import_react12.default.useCallback(() => {
1912
+ localStorage.removeItem(storageKey);
1913
+ setStoredSelection(null);
1914
+ onSelectionChange?.(null);
1915
+ }, [storageKey, onSelectionChange]);
1916
+ const needsSelection = isAuthenticated && isInitialized && !storedSelection && (hasMultipleOrgs || hasMultipleBranches);
1917
+ const isLoading = !isAuthenticated || !isInitialized || !!activeOrgSlug && branchesLoading && !storedSelection;
1918
+ if (isLoading && !needsSelection) {
1919
+ if (loadingComponent) {
1920
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_jsx_runtime7.Fragment, { children: loadingComponent });
1921
+ }
1922
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: {
1923
+ display: "flex",
1924
+ justifyContent: "center",
1925
+ alignItems: "center",
1926
+ minHeight: "100vh",
1927
+ background: "linear-gradient(135deg, #667eea 0%, #764ba2 100%)"
1928
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Spin, { size: "large" }) });
1173
1929
  }
1174
- if (response.status === 204) {
1175
- return void 0;
1930
+ if (needsSelection) {
1931
+ const canConfirm = tempOrgId && tempBranchId;
1932
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: {
1933
+ minHeight: "100vh",
1934
+ background: "linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
1935
+ display: "flex",
1936
+ alignItems: "center",
1937
+ justifyContent: "center",
1938
+ padding: 24
1939
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: {
1940
+ background: "#fff",
1941
+ borderRadius: 16,
1942
+ padding: 32,
1943
+ maxWidth: 480,
1944
+ width: "100%",
1945
+ boxShadow: "0 20px 60px rgba(0,0,0,0.3)"
1946
+ }, children: [
1947
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { textAlign: "center", marginBottom: 24 }, children: [
1948
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: {
1949
+ width: 64,
1950
+ height: 64,
1951
+ borderRadius: "50%",
1952
+ background: "linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
1953
+ display: "flex",
1954
+ alignItems: "center",
1955
+ justifyContent: "center",
1956
+ margin: "0 auto 16px"
1957
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icons3.BankOutlined, { style: { fontSize: 28, color: "#fff" } }) }),
1958
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Title2, { level: 3, style: { margin: 0 }, children: title ?? "Select Organization" }),
1959
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { type: "secondary", children: description ?? "Please select your organization and branch to continue" })
1960
+ ] }),
1961
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { marginBottom: 20 }, children: [
1962
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { strong: true, style: { display: "block", marginBottom: 8 }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_antd3.Space, { children: [
1963
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icons3.BankOutlined, {}),
1964
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { children: "Organization" }),
1965
+ !hasMultipleOrgs && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Badge, { status: "success", text: "Auto-selected" })
1966
+ ] }) }),
1967
+ hasMultipleOrgs ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
1968
+ import_antd3.Select,
1969
+ {
1970
+ value: tempOrgId,
1971
+ onChange: (value) => {
1972
+ setTempOrgId(String(value));
1973
+ setTempBranchId(null);
1974
+ },
1975
+ placeholder: "Select organization",
1976
+ size: "large",
1977
+ style: { width: "100%" },
1978
+ optionLabelProp: "label",
1979
+ children: organizations.map((org) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Select.Option, { value: String(org.id), label: org.name, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Space, { style: { width: "100%", justifyContent: "space-between" }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { children: [
1980
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { strong: true, children: org.name }),
1981
+ org.serviceRole && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(Text3, { type: "secondary", style: { marginLeft: 8, fontSize: 12 }, children: [
1982
+ "(",
1983
+ org.serviceRole,
1984
+ ")"
1985
+ ] })
1986
+ ] }) }) }, org.id))
1987
+ }
1988
+ ) : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: {
1989
+ padding: "8px 12px",
1990
+ background: "#f5f5f5",
1991
+ borderRadius: 6,
1992
+ border: "1px solid #d9d9d9"
1993
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_antd3.Space, { children: [
1994
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icons3.BankOutlined, { style: { color: "#1677ff" } }),
1995
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { strong: true, children: organizations[0]?.name })
1996
+ ] }) })
1997
+ ] }),
1998
+ tempOrgId && /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { marginBottom: 24 }, children: [
1999
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { strong: true, style: { display: "block", marginBottom: 8 }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_antd3.Space, { children: [
2000
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icons3.ApartmentOutlined, {}),
2001
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { children: "Branch" }),
2002
+ !hasMultipleBranches && branches.length === 1 && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Badge, { status: "success", text: "Auto-selected" })
2003
+ ] }) }),
2004
+ branchesLoading ? /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { style: { padding: "12px", textAlign: "center" }, children: [
2005
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Spin, { size: "small" }),
2006
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { type: "secondary", style: { marginLeft: 8 }, children: "Loading..." })
2007
+ ] }) : hasMultipleBranches ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2008
+ import_antd3.Select,
2009
+ {
2010
+ value: tempBranchId,
2011
+ onChange: (value) => setTempBranchId(String(value)),
2012
+ placeholder: "Select branch",
2013
+ size: "large",
2014
+ style: { width: "100%" },
2015
+ optionLabelProp: "label",
2016
+ children: branches.map((branch) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Select.Option, { value: String(branch.id), label: branch.name, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_antd3.Space, { style: { width: "100%", justifyContent: "space-between" }, children: [
2017
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("div", { children: [
2018
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { strong: true, children: branch.name }),
2019
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { type: "secondary", style: { marginLeft: 8, fontSize: 12 }, children: branch.code })
2020
+ ] }),
2021
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_antd3.Space, { size: 4, children: [
2022
+ branch.is_headquarters && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Badge, { color: "blue", text: "HQ" }),
2023
+ branch.is_primary && /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_antd3.Badge, { color: "green", text: "Primary" })
2024
+ ] })
2025
+ ] }) }, branch.id))
2026
+ }
2027
+ ) : branches.length === 1 ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { style: {
2028
+ padding: "8px 12px",
2029
+ background: "#f5f5f5",
2030
+ borderRadius: 6,
2031
+ border: "1px solid #d9d9d9"
2032
+ }, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(import_antd3.Space, { children: [
2033
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icons3.ApartmentOutlined, { style: { color: "#1677ff" } }),
2034
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(Text3, { strong: true, children: branches[0].name }),
2035
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(Text3, { type: "secondary", children: [
2036
+ "(",
2037
+ branches[0].code,
2038
+ ")"
2039
+ ] })
2040
+ ] }) }) : /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2041
+ import_antd3.Alert,
2042
+ {
2043
+ type: "warning",
2044
+ message: "No branches available",
2045
+ description: "You don't have access to any branches in this organization."
2046
+ }
2047
+ )
2048
+ ] }),
2049
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
2050
+ import_antd3.Button,
2051
+ {
2052
+ type: "primary",
2053
+ size: "large",
2054
+ block: true,
2055
+ disabled: !canConfirm,
2056
+ onClick: confirmSelection,
2057
+ icon: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_icons3.CheckCircleFilled, {}),
2058
+ style: {
2059
+ height: 48,
2060
+ fontSize: 16,
2061
+ background: canConfirm ? "linear-gradient(135deg, #667eea 0%, #764ba2 100%)" : void 0,
2062
+ border: "none"
2063
+ },
2064
+ children: "Confirm"
2065
+ }
2066
+ )
2067
+ ] }) });
1176
2068
  }
1177
- return response.json();
2069
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(import_jsx_runtime7.Fragment, { children });
2070
+ }
2071
+ function useBranchGate(storageKey = DEFAULT_STORAGE_KEY2) {
2072
+ const [selection, setSelection] = (0, import_react12.useState)(null);
2073
+ (0, import_react12.useEffect)(() => {
2074
+ if (typeof window === "undefined") return;
2075
+ const saved = localStorage.getItem(storageKey);
2076
+ if (saved) {
2077
+ try {
2078
+ setSelection(JSON.parse(saved));
2079
+ } catch {
2080
+ }
2081
+ }
2082
+ const handleStorage = (e) => {
2083
+ if (e.key === storageKey) {
2084
+ if (e.newValue) {
2085
+ try {
2086
+ setSelection(JSON.parse(e.newValue));
2087
+ } catch {
2088
+ setSelection(null);
2089
+ }
2090
+ } else {
2091
+ setSelection(null);
2092
+ }
2093
+ }
2094
+ };
2095
+ window.addEventListener("storage", handleStorage);
2096
+ return () => window.removeEventListener("storage", handleStorage);
2097
+ }, [storageKey]);
2098
+ const clearSelection = import_react12.default.useCallback(() => {
2099
+ localStorage.removeItem(storageKey);
2100
+ setSelection(null);
2101
+ }, [storageKey]);
2102
+ return {
2103
+ selection,
2104
+ selectedOrg: selection ? { id: selection.orgId, slug: selection.orgSlug, name: selection.orgName } : null,
2105
+ selectedBranch: selection ? { id: selection.branchId, name: selection.branchName, code: selection.branchCode } : null,
2106
+ clearSelection
2107
+ };
2108
+ }
2109
+
2110
+ // src/utils/branchHeaders.ts
2111
+ var BRANCH_HEADERS = {
2112
+ BRANCH_ID: "X-Branch-Id",
2113
+ ORG_ID: "X-Org-Id",
2114
+ BRANCH_NAME: "X-Branch-Name",
2115
+ BRANCH_CODE: "X-Branch-Code"
2116
+ };
2117
+ function createBranchHeaderSetter(axiosInstance) {
2118
+ return (selection) => {
2119
+ if (selection) {
2120
+ axiosInstance.defaults.headers.common[BRANCH_HEADERS.BRANCH_ID] = selection.branchId;
2121
+ axiosInstance.defaults.headers.common[BRANCH_HEADERS.ORG_ID] = selection.orgId;
2122
+ if (selection.branchName) {
2123
+ axiosInstance.defaults.headers.common[BRANCH_HEADERS.BRANCH_NAME] = selection.branchName;
2124
+ }
2125
+ if (selection.branchCode) {
2126
+ axiosInstance.defaults.headers.common[BRANCH_HEADERS.BRANCH_CODE] = selection.branchCode;
2127
+ }
2128
+ } else {
2129
+ delete axiosInstance.defaults.headers.common[BRANCH_HEADERS.BRANCH_ID];
2130
+ delete axiosInstance.defaults.headers.common[BRANCH_HEADERS.ORG_ID];
2131
+ delete axiosInstance.defaults.headers.common[BRANCH_HEADERS.BRANCH_NAME];
2132
+ delete axiosInstance.defaults.headers.common[BRANCH_HEADERS.BRANCH_CODE];
2133
+ }
2134
+ };
2135
+ }
2136
+ function setBranchHeaders(axiosInstance, selection) {
2137
+ createBranchHeaderSetter(axiosInstance)(selection);
1178
2138
  }
1179
2139
 
1180
2140
  // src/services/authService.ts
@@ -1648,70 +2608,6 @@ var getEffectivePermissions = (roleAssignments, allRoles, orgId, branchId) => {
1648
2608
  return Array.from(permissions);
1649
2609
  };
1650
2610
 
1651
- // src/services/branchService.ts
1652
- function createBranchService(config) {
1653
- const { apiUrl } = config;
1654
- return {
1655
- /**
1656
- * Get branches for current user in organization
1657
- * GET /api/sso/branches
1658
- */
1659
- list: async (orgSlug) => {
1660
- const params = orgSlug ? `?organization_slug=${orgSlug}` : "";
1661
- return request(apiUrl, `/api/sso/branches${params}`, {
1662
- headers: buildHeaders()
1663
- });
1664
- },
1665
- /**
1666
- * Get a specific branch by ID
1667
- * GET /api/sso/branches/{id}
1668
- */
1669
- get: async (branchId) => {
1670
- const response = await request(
1671
- apiUrl,
1672
- `/api/sso/branches/${branchId}`,
1673
- { headers: buildHeaders() }
1674
- );
1675
- return "data" in response ? response.data : response;
1676
- },
1677
- /**
1678
- * Get headquarters branch for organization
1679
- */
1680
- getHeadquarters: async (orgSlug) => {
1681
- try {
1682
- const params = orgSlug ? `?organization_slug=${orgSlug}` : "";
1683
- const data = await request(
1684
- apiUrl,
1685
- `/api/sso/branches${params}`,
1686
- { headers: buildHeaders() }
1687
- );
1688
- return data.branches.find((b) => b.is_headquarters) ?? null;
1689
- } catch {
1690
- return null;
1691
- }
1692
- },
1693
- /**
1694
- * Get primary branch for current user
1695
- */
1696
- getPrimary: async (orgSlug) => {
1697
- try {
1698
- const params = orgSlug ? `?organization_slug=${orgSlug}` : "";
1699
- const data = await request(
1700
- apiUrl,
1701
- `/api/sso/branches${params}`,
1702
- { headers: buildHeaders() }
1703
- );
1704
- if (data.primary_branch_id) {
1705
- return data.branches.find((b) => b.id === data.primary_branch_id) ?? null;
1706
- }
1707
- return null;
1708
- } catch {
1709
- return null;
1710
- }
1711
- }
1712
- };
1713
- }
1714
-
1715
2611
  // src/services/ssoService.ts
1716
2612
  function getXsrfToken3() {
1717
2613
  if (typeof document === "undefined") return void 0;
@@ -2089,111 +2985,24 @@ function createSsoService(config) {
2089
2985
  }
2090
2986
  };
2091
2987
  }
2092
-
2093
- // src/queryKeys.ts
2094
- var ssoQueryKeys = {
2095
- all: ["sso"],
2096
- // =========================================================================
2097
- // Auth (authService)
2098
- // =========================================================================
2099
- auth: {
2100
- all: () => [...ssoQueryKeys.all, "auth"],
2101
- user: () => [...ssoQueryKeys.auth.all(), "user"],
2102
- globalLogoutUrl: (redirectUri) => [...ssoQueryKeys.auth.all(), "global-logout-url", redirectUri]
2103
- },
2104
- // =========================================================================
2105
- // Tokens (tokenService)
2106
- // =========================================================================
2107
- tokens: {
2108
- all: () => [...ssoQueryKeys.all, "tokens"],
2109
- list: () => [...ssoQueryKeys.tokens.all(), "list"]
2110
- },
2111
- // =========================================================================
2112
- // Roles (roleService)
2113
- // =========================================================================
2114
- roles: {
2115
- all: () => [...ssoQueryKeys.all, "roles"],
2116
- list: () => [...ssoQueryKeys.roles.all(), "list"],
2117
- detail: (id) => [...ssoQueryKeys.roles.all(), "detail", id],
2118
- permissions: (id) => [...ssoQueryKeys.roles.all(), id, "permissions"]
2119
- },
2120
- // =========================================================================
2121
- // Permissions (permissionService)
2122
- // =========================================================================
2123
- permissions: {
2124
- all: () => [...ssoQueryKeys.all, "permissions"],
2125
- list: (params) => [...ssoQueryKeys.permissions.all(), "list", params],
2126
- detail: (id) => [...ssoQueryKeys.permissions.all(), "detail", id],
2127
- matrix: () => [...ssoQueryKeys.permissions.all(), "matrix"]
2128
- },
2129
- // =========================================================================
2130
- // Teams (teamService)
2131
- // =========================================================================
2132
- teams: {
2133
- all: () => [...ssoQueryKeys.all, "teams"],
2134
- list: () => [...ssoQueryKeys.teams.all(), "list"],
2135
- permissions: (teamId) => [...ssoQueryKeys.teams.all(), teamId, "permissions"],
2136
- orphaned: () => [...ssoQueryKeys.teams.all(), "orphaned"]
2137
- },
2138
- // =========================================================================
2139
- // User Roles (userRoleService) - Scoped Role Assignments
2140
- // =========================================================================
2141
- userRoles: {
2142
- all: () => [...ssoQueryKeys.all, "user-roles"],
2143
- list: (userId) => [...ssoQueryKeys.userRoles.all(), userId],
2144
- byBranch: (userId, orgId, branchId) => [...ssoQueryKeys.userRoles.all(), userId, orgId, branchId]
2145
- },
2146
- // =========================================================================
2147
- // Branches (branchService)
2148
- // =========================================================================
2149
- branches: {
2150
- all: () => [...ssoQueryKeys.all, "branches"],
2151
- list: (orgSlug) => [...ssoQueryKeys.branches.all(), "list", orgSlug],
2152
- detail: (branchId) => [...ssoQueryKeys.branches.all(), "detail", branchId],
2153
- headquarters: (orgSlug) => [...ssoQueryKeys.branches.all(), "headquarters", orgSlug],
2154
- primary: (orgSlug) => [...ssoQueryKeys.branches.all(), "primary", orgSlug]
2155
- },
2156
- // =========================================================================
2157
- // Admin variants (with org context)
2158
- // =========================================================================
2159
- admin: {
2160
- roles: {
2161
- all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "roles"],
2162
- list: (orgSlug) => [...ssoQueryKeys.admin.roles.all(orgSlug), "list"],
2163
- detail: (orgSlug, id) => [...ssoQueryKeys.admin.roles.all(orgSlug), "detail", id],
2164
- permissions: (orgSlug, id) => [...ssoQueryKeys.admin.roles.all(orgSlug), id, "permissions"]
2165
- },
2166
- permissions: {
2167
- all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "permissions"],
2168
- list: (orgSlug, params) => [...ssoQueryKeys.admin.permissions.all(orgSlug), "list", params],
2169
- detail: (orgSlug, id) => [...ssoQueryKeys.admin.permissions.all(orgSlug), "detail", id],
2170
- matrix: (orgSlug) => [...ssoQueryKeys.admin.permissions.all(orgSlug), "matrix"]
2171
- },
2172
- teams: {
2173
- all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "teams"],
2174
- list: (orgSlug) => [...ssoQueryKeys.admin.teams.all(orgSlug), "list"],
2175
- permissions: (orgSlug, teamId) => [...ssoQueryKeys.admin.teams.all(orgSlug), teamId, "permissions"],
2176
- orphaned: (orgSlug) => [...ssoQueryKeys.admin.teams.all(orgSlug), "orphaned"]
2177
- },
2178
- userRoles: {
2179
- all: (orgSlug) => [...ssoQueryKeys.all, "admin", orgSlug, "user-roles"],
2180
- list: (orgSlug, userId) => [...ssoQueryKeys.admin.userRoles.all(orgSlug), userId],
2181
- byBranch: (orgSlug, userId, orgId, branchId) => [...ssoQueryKeys.admin.userRoles.all(orgSlug), userId, orgId, branchId]
2182
- }
2183
- }
2184
- };
2185
2988
  // Annotate the CommonJS export names for ESM import in node:
2186
2989
  0 && (module.exports = {
2990
+ BRANCH_HEADERS,
2991
+ BranchContext,
2992
+ BranchGate,
2993
+ BranchProvider,
2994
+ OrgBranchSelectorModal,
2187
2995
  OrganizationSwitcher,
2188
2996
  ProtectedRoute,
2189
2997
  SsoCallback,
2190
2998
  SsoContext,
2191
2999
  SsoProvider,
2192
- branchCreateSchema,
2193
- branchI18n,
2194
- branchSchemas,
2195
- branchUpdateSchema,
3000
+ branchCacheCreateSchema,
3001
+ branchCacheI18n,
3002
+ branchCacheSchemas,
3003
+ branchCacheUpdateSchema,
2196
3004
  createAuthService,
3005
+ createBranchHeaderSetter,
2197
3006
  createBranchService,
2198
3007
  createPermissionService,
2199
3008
  createRoleService,
@@ -2203,12 +3012,15 @@ var ssoQueryKeys = {
2203
3012
  createUserRoleService,
2204
3013
  defaultLocale,
2205
3014
  fallbackLocale,
2206
- getBranchFieldLabel,
2207
- getBranchFieldPlaceholder,
2208
- getBranchLabel,
3015
+ getBranchCacheFieldLabel,
3016
+ getBranchCacheFieldPlaceholder,
3017
+ getBranchCacheLabel,
2209
3018
  getEffectivePermissions,
2210
3019
  getMessage,
2211
3020
  getMessages,
3021
+ getOrganizationCacheFieldLabel,
3022
+ getOrganizationCacheFieldPlaceholder,
3023
+ getOrganizationCacheLabel,
2212
3024
  getPermissionFieldLabel,
2213
3025
  getPermissionFieldPlaceholder,
2214
3026
  getPermissionLabel,
@@ -2219,15 +3031,19 @@ var ssoQueryKeys = {
2219
3031
  getRolePermissionFieldPlaceholder,
2220
3032
  getRolePermissionLabel,
2221
3033
  getScopeLabel,
2222
- getTeamFieldLabel,
2223
- getTeamFieldPlaceholder,
2224
- getTeamLabel,
3034
+ getTeamCacheFieldLabel,
3035
+ getTeamCacheFieldPlaceholder,
3036
+ getTeamCacheLabel,
2225
3037
  getTeamPermissionFieldLabel,
2226
3038
  getTeamPermissionFieldPlaceholder,
2227
3039
  getTeamPermissionLabel,
2228
- getUserFieldLabel,
2229
- getUserFieldPlaceholder,
2230
- getUserLabel,
3040
+ getUserCacheFieldLabel,
3041
+ getUserCacheFieldPlaceholder,
3042
+ getUserCacheLabel,
3043
+ organizationCacheCreateSchema,
3044
+ organizationCacheI18n,
3045
+ organizationCacheSchemas,
3046
+ organizationCacheUpdateSchema,
2231
3047
  permissionCreateSchema,
2232
3048
  permissionI18n,
2233
3049
  permissionSchemas,
@@ -2240,23 +3056,26 @@ var ssoQueryKeys = {
2240
3056
  rolePermissionUpdateSchema,
2241
3057
  roleSchemas,
2242
3058
  roleUpdateSchema,
3059
+ setBranchHeaders,
2243
3060
  ssoQueryKeys,
2244
3061
  supportedLocales,
2245
- teamCreateSchema,
2246
- teamI18n,
3062
+ teamCacheCreateSchema,
3063
+ teamCacheI18n,
3064
+ teamCacheSchemas,
3065
+ teamCacheUpdateSchema,
2247
3066
  teamPermissionCreateSchema,
2248
3067
  teamPermissionI18n,
2249
3068
  teamPermissionSchemas,
2250
3069
  teamPermissionUpdateSchema,
2251
- teamSchemas,
2252
- teamUpdateSchema,
2253
3070
  useAuth,
3071
+ useBranch,
3072
+ useBranchGate,
2254
3073
  useOrganization,
2255
3074
  useSso,
2256
- userCreateSchema,
2257
- userI18n,
2258
- userSchemas,
2259
- userUpdateSchema,
3075
+ userCacheCreateSchema,
3076
+ userCacheI18n,
3077
+ userCacheSchemas,
3078
+ userCacheUpdateSchema,
2260
3079
  validationMessages
2261
3080
  });
2262
3081
  //# sourceMappingURL=index.cjs.map