@viu/emporix-sdk-react 2.11.0 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- export { E as EmporixProvider, a as EmporixProviderProps, c as EmporixTelemetryEvent, S as SiteContextValue, u as useEmporix, d as useEmporixTelemetry } from './provider-CfgMAu8B.cjs';
1
+ export { E as EmporixProvider, a as EmporixProviderProps, c as EmporixTelemetryEvent, S as SiteContextValue, u as useEmporix, d as useEmporixTelemetry } from './provider-Ddo7aSOI.cjs';
2
2
  import { EmporixStorage } from './storage.cjs';
3
3
  export { PersistedAnonymousSession, TokenStorage, createCookieStorage, createLocalStorageStorage, createMemoryStorage } from './storage.cjs';
4
- export { A as AddressMutationsApi, C as CartMutationsApi, a as CheckoutApi, b as CompanySwitcherApi, c as CouponActionVars, d as CustomerSessionApi, I as InvokeCloudFunctionVars, P as PasswordResetApi, U as UseAvailabilitiesOptions, e as UseAvailabilityOptions, g as UseMyOrdersInfiniteOptions, h as UseMyOrdersOptions, n as UseVariantChildrenOptions, u as useActiveCart, o as useAddGroupMember, p as useAddToShoppingList, q as useAddressMutations, r as useAssignContact, s as useAvailabilities, t as useAvailability, v as useCancelOrder, w as useCart, x as useCartMutations, y as useCategories, z as useCategoriesInfinite, B as useCategory, D as useCategoryTree, E as useChangePassword, F as useCheckout, G as useCloudFunction, H as useCompany, J as useCompanyContacts, K as useCompanyGroups, L as useCompanyLocations, M as useCompanySwitcher, N as useCreateCart, O as useCreateCompany, Q as useCreateLocation, R as useCreateReturn, S as useCreateShoppingList, T as useCustomerAddresses, V as useCustomerSession, W as useDefaultSite, X as useDeleteCompany, Y as useDeleteLocation, Z as useDeleteShoppingList, _ as useInvokeCloudFunction, $ as useMatchPrices, a0 as useMatchPricesChunked, a1 as useMyCompanies, a2 as useMyOrders, a3 as useMyOrdersInfinite, a4 as useMyReturns, a5 as useMyRewardPoints, a6 as useMyRewardPointsSummary, a7 as useMySegmentCategories, a8 as useMySegmentCategoriesInfinite, a9 as useMySegmentCategoryTree, aa as useMySegmentItems, ab as useMySegmentProducts, ac as useMySegmentProductsInfinite, ad as useMySegments, ae as useOrder, af as useOrderTransition, ag as usePasswordReset, ah as usePaymentModes, ai as useProduct, aj as useProductByCode, ak as useProductMedia, al as useProductNameSearch, am as useProductSearch, an as useProducts, ao as useProductsByCodes, ap as useProductsInCategory, aq as useProductsInCategoryInfinite, ar as useProductsInfinite, as as useRedeemCoupon, at as useRedeemOptions, au as useRedeemRewardPoints, av as useRemoveFromShoppingList, aw as useRemoveGroupMember, ax as useReorder, ay as useReturn, az as useSalesOrder, aA as useSetShoppingListItemQuantity, aB as useShoppingLists, aC as useSiteContext, aD as useSites, aE as useSubcategories, aF as useUnassignContact, aG as useUpdateCompany, aH as useUpdateContactAssignment, aI as useUpdateCustomer, aJ as useUpdateLocation, aK as useUpdateSalesOrder, aL as useValidateCoupon, aM as useVariantChildren } from './use-returns-DDH_HnPS.cjs';
4
+ export { A as AddressMutationsApi, C as CartMutationsApi, a as CheckoutApi, b as CompanySwitcherApi, c as CouponActionVars, d as CustomerSessionApi, I as InvokeCloudFunctionVars, P as PasswordResetApi, U as UseAvailabilitiesOptions, e as UseAvailabilityOptions, g as UseMyOrdersInfiniteOptions, h as UseMyOrdersOptions, n as UseVariantChildrenOptions, u as useActiveCart, o as useAddGroupMember, p as useAddToShoppingList, q as useAddressMutations, r as useAssignContact, s as useAvailabilities, t as useAvailability, v as useCancelOrder, w as useCart, x as useCartMutations, y as useCategories, z as useCategoriesInfinite, B as useCategory, D as useCategoryTree, E as useChangePassword, F as useCheckout, G as useCloudFunction, H as useCompany, J as useCompanyContacts, K as useCompanyGroups, L as useCompanyLocations, M as useCompanySwitcher, N as useCreateCart, O as useCreateCompany, Q as useCreateLocation, R as useCreateReturn, S as useCreateShoppingList, T as useCustomerAddresses, V as useCustomerSession, W as useDefaultSite, X as useDeleteCompany, Y as useDeleteLocation, Z as useDeleteShoppingList, _ as useInvokeCloudFunction, $ as useMatchPrices, a0 as useMatchPricesChunked, a1 as useMyCompanies, a2 as useMyOrders, a3 as useMyOrdersInfinite, a4 as useMyReturns, a5 as useMyRewardPoints, a6 as useMyRewardPointsSummary, a7 as useMySegmentCategories, a8 as useMySegmentCategoriesInfinite, a9 as useMySegmentCategoryTree, aa as useMySegmentItems, ab as useMySegmentProducts, ac as useMySegmentProductsInfinite, ad as useMySegments, ae as useOrder, af as useOrderTransition, ag as usePasswordReset, ah as usePaymentModes, ai as useProduct, aj as useProductByCode, ak as useProductMedia, al as useProductNameSearch, am as useProductSearch, an as useProducts, ao as useProductsByCodes, ap as useProductsInCategory, aq as useProductsInCategoryInfinite, ar as useProductsInfinite, as as useRedeemCoupon, at as useRedeemOptions, au as useRedeemRewardPoints, av as useRemoveFromShoppingList, aw as useRemoveGroupMember, ax as useReorder, ay as useReturn, az as useSalesOrder, aA as useSetShoppingListItemQuantity, aB as useShoppingLists, aC as useSiteContext, aD as useSites, aE as useSubcategories, aF as useUnassignContact, aG as useUpdateCompany, aH as useUpdateContactAssignment, aI as useUpdateCustomer, aJ as useUpdateLocation, aK as useUpdateSalesOrder, aL as useValidateCoupon, aM as useVariantChildren } from './use-returns-cFZZl21v.cjs';
5
5
  import * as react from 'react';
6
6
  import { Component, ReactNode, ErrorInfo } from 'react';
7
7
  import { EmporixAuthError, EmporixError, EmporixClient, LegalEntity } from '@viu/emporix-sdk';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { E as EmporixProvider, a as EmporixProviderProps, c as EmporixTelemetryEvent, S as SiteContextValue, u as useEmporix, d as useEmporixTelemetry } from './provider--y2yz3Jj.js';
1
+ export { E as EmporixProvider, a as EmporixProviderProps, c as EmporixTelemetryEvent, S as SiteContextValue, u as useEmporix, d as useEmporixTelemetry } from './provider-Bcb-tOpS.js';
2
2
  import { EmporixStorage } from './storage.js';
3
3
  export { PersistedAnonymousSession, TokenStorage, createCookieStorage, createLocalStorageStorage, createMemoryStorage } from './storage.js';
4
- export { A as AddressMutationsApi, C as CartMutationsApi, a as CheckoutApi, b as CompanySwitcherApi, c as CouponActionVars, d as CustomerSessionApi, I as InvokeCloudFunctionVars, P as PasswordResetApi, U as UseAvailabilitiesOptions, e as UseAvailabilityOptions, g as UseMyOrdersInfiniteOptions, h as UseMyOrdersOptions, n as UseVariantChildrenOptions, u as useActiveCart, o as useAddGroupMember, p as useAddToShoppingList, q as useAddressMutations, r as useAssignContact, s as useAvailabilities, t as useAvailability, v as useCancelOrder, w as useCart, x as useCartMutations, y as useCategories, z as useCategoriesInfinite, B as useCategory, D as useCategoryTree, E as useChangePassword, F as useCheckout, G as useCloudFunction, H as useCompany, J as useCompanyContacts, K as useCompanyGroups, L as useCompanyLocations, M as useCompanySwitcher, N as useCreateCart, O as useCreateCompany, Q as useCreateLocation, R as useCreateReturn, S as useCreateShoppingList, T as useCustomerAddresses, V as useCustomerSession, W as useDefaultSite, X as useDeleteCompany, Y as useDeleteLocation, Z as useDeleteShoppingList, _ as useInvokeCloudFunction, $ as useMatchPrices, a0 as useMatchPricesChunked, a1 as useMyCompanies, a2 as useMyOrders, a3 as useMyOrdersInfinite, a4 as useMyReturns, a5 as useMyRewardPoints, a6 as useMyRewardPointsSummary, a7 as useMySegmentCategories, a8 as useMySegmentCategoriesInfinite, a9 as useMySegmentCategoryTree, aa as useMySegmentItems, ab as useMySegmentProducts, ac as useMySegmentProductsInfinite, ad as useMySegments, ae as useOrder, af as useOrderTransition, ag as usePasswordReset, ah as usePaymentModes, ai as useProduct, aj as useProductByCode, ak as useProductMedia, al as useProductNameSearch, am as useProductSearch, an as useProducts, ao as useProductsByCodes, ap as useProductsInCategory, aq as useProductsInCategoryInfinite, ar as useProductsInfinite, as as useRedeemCoupon, at as useRedeemOptions, au as useRedeemRewardPoints, av as useRemoveFromShoppingList, aw as useRemoveGroupMember, ax as useReorder, ay as useReturn, az as useSalesOrder, aA as useSetShoppingListItemQuantity, aB as useShoppingLists, aC as useSiteContext, aD as useSites, aE as useSubcategories, aF as useUnassignContact, aG as useUpdateCompany, aH as useUpdateContactAssignment, aI as useUpdateCustomer, aJ as useUpdateLocation, aK as useUpdateSalesOrder, aL as useValidateCoupon, aM as useVariantChildren } from './use-returns-vxGoBH1I.js';
4
+ export { A as AddressMutationsApi, C as CartMutationsApi, a as CheckoutApi, b as CompanySwitcherApi, c as CouponActionVars, d as CustomerSessionApi, I as InvokeCloudFunctionVars, P as PasswordResetApi, U as UseAvailabilitiesOptions, e as UseAvailabilityOptions, g as UseMyOrdersInfiniteOptions, h as UseMyOrdersOptions, n as UseVariantChildrenOptions, u as useActiveCart, o as useAddGroupMember, p as useAddToShoppingList, q as useAddressMutations, r as useAssignContact, s as useAvailabilities, t as useAvailability, v as useCancelOrder, w as useCart, x as useCartMutations, y as useCategories, z as useCategoriesInfinite, B as useCategory, D as useCategoryTree, E as useChangePassword, F as useCheckout, G as useCloudFunction, H as useCompany, J as useCompanyContacts, K as useCompanyGroups, L as useCompanyLocations, M as useCompanySwitcher, N as useCreateCart, O as useCreateCompany, Q as useCreateLocation, R as useCreateReturn, S as useCreateShoppingList, T as useCustomerAddresses, V as useCustomerSession, W as useDefaultSite, X as useDeleteCompany, Y as useDeleteLocation, Z as useDeleteShoppingList, _ as useInvokeCloudFunction, $ as useMatchPrices, a0 as useMatchPricesChunked, a1 as useMyCompanies, a2 as useMyOrders, a3 as useMyOrdersInfinite, a4 as useMyReturns, a5 as useMyRewardPoints, a6 as useMyRewardPointsSummary, a7 as useMySegmentCategories, a8 as useMySegmentCategoriesInfinite, a9 as useMySegmentCategoryTree, aa as useMySegmentItems, ab as useMySegmentProducts, ac as useMySegmentProductsInfinite, ad as useMySegments, ae as useOrder, af as useOrderTransition, ag as usePasswordReset, ah as usePaymentModes, ai as useProduct, aj as useProductByCode, ak as useProductMedia, al as useProductNameSearch, am as useProductSearch, an as useProducts, ao as useProductsByCodes, ap as useProductsInCategory, aq as useProductsInCategoryInfinite, ar as useProductsInfinite, as as useRedeemCoupon, at as useRedeemOptions, au as useRedeemRewardPoints, av as useRemoveFromShoppingList, aw as useRemoveGroupMember, ax as useReorder, ay as useReturn, az as useSalesOrder, aA as useSetShoppingListItemQuantity, aB as useShoppingLists, aC as useSiteContext, aD as useSites, aE as useSubcategories, aF as useUnassignContact, aG as useUpdateCompany, aH as useUpdateContactAssignment, aI as useUpdateCustomer, aJ as useUpdateLocation, aK as useUpdateSalesOrder, aL as useValidateCoupon, aM as useVariantChildren } from './use-returns-DletRcs5.js';
5
5
  import * as react from 'react';
6
6
  import { Component, ReactNode, ErrorInfo } from 'react';
7
7
  import { EmporixAuthError, EmporixError, EmporixClient, LegalEntity } from '@viu/emporix-sdk';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- export { useActiveCart, useAddGroupMember, useAddToShoppingList, useAddressMutations, useAssignContact, useAvailabilities, useAvailability, useCancelOrder, useCart, useCartMutations, useCategories, useCategoriesInfinite, useCategory, useCategoryTree, useChangePassword, useCheckout, useCloudFunction, useCompany, useCompanyContacts, useCompanyGroups, useCompanyLocations, useCompanySwitcher, useCreateCart, useCreateCompany, useCreateLocation, useCreateReturn, useCreateShoppingList, useCustomerAddresses, useCustomerSession, useDefaultSite, useDeleteCompany, useDeleteLocation, useDeleteShoppingList, useInvokeCloudFunction, useMatchPrices, useMatchPricesChunked, useMyCompanies, useMyOrders, useMyOrdersInfinite, useMyReturns, useMyRewardPoints, useMyRewardPointsSummary, useMySegmentCategories, useMySegmentCategoriesInfinite, useMySegmentCategoryTree, useMySegmentItems, useMySegmentProducts, useMySegmentProductsInfinite, useMySegments, useOrder, useOrderTransition, usePasswordReset, usePaymentModes, useProduct, useProductByCode, useProductMedia, useProductNameSearch, useProductSearch, useProducts, useProductsByCodes, useProductsInCategory, useProductsInCategoryInfinite, useProductsInfinite, useRedeemCoupon, useRedeemOptions, useRedeemRewardPoints, useRemoveFromShoppingList, useRemoveGroupMember, useReorder, useReturn, useSalesOrder, useSetShoppingListItemQuantity, useShoppingLists, useSiteContext, useSites, useSubcategories, useUnassignContact, useUpdateCompany, useUpdateContactAssignment, useUpdateCustomer, useUpdateLocation, useUpdateSalesOrder, useValidateCoupon, useVariantChildren } from './chunk-KLLY4YD7.js';
2
- export { CompanyContextProvider, EmporixCompanyContext, EmporixProvider, useActiveCompany, useEmporix, useEmporixTelemetry } from './chunk-SMJ5IQZJ.js';
3
- export { createCookieStorage, createLocalStorageStorage, createMemoryStorage } from './chunk-FBQY2N7S.js';
1
+ export { useActiveCart, useAddGroupMember, useAddToShoppingList, useAddressMutations, useAssignContact, useAvailabilities, useAvailability, useCancelOrder, useCart, useCartMutations, useCategories, useCategoriesInfinite, useCategory, useCategoryTree, useChangePassword, useCheckout, useCloudFunction, useCompany, useCompanyContacts, useCompanyGroups, useCompanyLocations, useCompanySwitcher, useCreateCart, useCreateCompany, useCreateLocation, useCreateReturn, useCreateShoppingList, useCustomerAddresses, useCustomerSession, useDefaultSite, useDeleteCompany, useDeleteLocation, useDeleteShoppingList, useInvokeCloudFunction, useMatchPrices, useMatchPricesChunked, useMyCompanies, useMyOrders, useMyOrdersInfinite, useMyReturns, useMyRewardPoints, useMyRewardPointsSummary, useMySegmentCategories, useMySegmentCategoriesInfinite, useMySegmentCategoryTree, useMySegmentItems, useMySegmentProducts, useMySegmentProductsInfinite, useMySegments, useOrder, useOrderTransition, usePasswordReset, usePaymentModes, useProduct, useProductByCode, useProductMedia, useProductNameSearch, useProductSearch, useProducts, useProductsByCodes, useProductsInCategory, useProductsInCategoryInfinite, useProductsInfinite, useRedeemCoupon, useRedeemOptions, useRedeemRewardPoints, useRemoveFromShoppingList, useRemoveGroupMember, useReorder, useReturn, useSalesOrder, useSetShoppingListItemQuantity, useShoppingLists, useSiteContext, useSites, useSubcategories, useUnassignContact, useUpdateCompany, useUpdateContactAssignment, useUpdateCustomer, useUpdateLocation, useUpdateSalesOrder, useValidateCoupon, useVariantChildren } from './chunk-RVLQH55K.js';
2
+ export { CompanyContextProvider, EmporixCompanyContext, EmporixProvider, useActiveCompany, useEmporix, useEmporixTelemetry } from './chunk-4YDWCA7A.js';
3
+ export { createCookieStorage, createLocalStorageStorage, createMemoryStorage } from './chunk-VMDBYVWG.js';
4
4
  export { prefetchCart, prefetchOrder, prefetchProduct } from './chunk-TIS4BKHK.js';
5
5
  import { Component } from 'react';
6
6
  import { EmporixAuthError, EmporixError } from '@viu/emporix-sdk';
@@ -50,7 +50,7 @@ type EmporixTelemetryEvent = {
50
50
  success: boolean;
51
51
  } | {
52
52
  type: "storage.write";
53
- key: "customerToken" | "cartId" | "siteCode" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
53
+ key: "customerToken" | "cartId" | "siteCode" | "language" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
54
54
  } | {
55
55
  type: "company:switched";
56
56
  from: string | null;
@@ -82,6 +82,8 @@ interface SiteContextValue {
82
82
  currency: string | null;
83
83
  /** MS-4 populates this from the active site's DTO. */
84
84
  targetLocation: string | null;
85
+ /** Active language for localized reads (Accept-Language). `null` = site/tenant default. */
86
+ language: string | null;
85
87
  /**
86
88
  * Asynchronous site switch. Updates local state + storage immediately
87
89
  * (optimistic), then PATCHes `/session-context/{tenant}/me/context` so
@@ -101,6 +103,13 @@ interface SiteContextValue {
101
103
  * The chosen currency must be in the active site's `availableCurrencies`.
102
104
  */
103
105
  setCurrency: (currency: string) => Promise<void>;
106
+ /**
107
+ * Switch the active language at runtime. Sets the `Accept-Language` request
108
+ * header (via `setStorefrontContext`), invalidates the React-Query cache so
109
+ * localized reads refetch, and PATCHes an existing server session context.
110
+ * Does NOT clear the cart (language does not affect pricing).
111
+ */
112
+ setLanguage: (language: string) => Promise<void>;
104
113
  isSwitching: boolean;
105
114
  switchError: Error | null;
106
115
  }
@@ -116,6 +125,12 @@ interface EmporixProviderProps {
116
125
  * `client.config.credentials.storefront.context.siteCode` → `null`.
117
126
  */
118
127
  initialSiteCode?: string;
128
+ /**
129
+ * Initial active language. Resolution order: this prop → `storage.getLanguage()`
130
+ * → `client.config.credentials.storefront.context.language` → `null` (then
131
+ * seeded from the active site's `defaultLanguage` on mount).
132
+ */
133
+ initialLanguage?: string;
119
134
  /**
120
135
  * Initial active legal-entity id (B2B). When set, the CompanyContext
121
136
  * provider tries to match it against `companies.listMine()` once the
@@ -145,7 +160,7 @@ interface EmporixProviderProps {
145
160
  children: ReactNode;
146
161
  }
147
162
  /** Provides the SDK client, token storage, react-query client, and site context to the tree. */
148
- declare function EmporixProvider({ client, queryClient, storage, initialCustomerToken, initialSiteCode, initialActiveLegalEntityId, onTelemetry, autoRefreshCustomerToken, onCustomerSessionExpired, children, }: EmporixProviderProps): React.JSX.Element;
163
+ declare function EmporixProvider({ client, queryClient, storage, initialCustomerToken, initialSiteCode, initialLanguage, initialActiveLegalEntityId, onTelemetry, autoRefreshCustomerToken, onCustomerSessionExpired, children, }: EmporixProviderProps): React.JSX.Element;
149
164
  /** Returns the SDK client and token storage. Throws outside an {@link EmporixProvider}. */
150
165
  declare function useEmporix(): EmporixContextValue;
151
166
 
@@ -50,7 +50,7 @@ type EmporixTelemetryEvent = {
50
50
  success: boolean;
51
51
  } | {
52
52
  type: "storage.write";
53
- key: "customerToken" | "cartId" | "siteCode" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
53
+ key: "customerToken" | "cartId" | "siteCode" | "language" | "anonymousSession" | "activeLegalEntityId" | "refreshToken";
54
54
  } | {
55
55
  type: "company:switched";
56
56
  from: string | null;
@@ -82,6 +82,8 @@ interface SiteContextValue {
82
82
  currency: string | null;
83
83
  /** MS-4 populates this from the active site's DTO. */
84
84
  targetLocation: string | null;
85
+ /** Active language for localized reads (Accept-Language). `null` = site/tenant default. */
86
+ language: string | null;
85
87
  /**
86
88
  * Asynchronous site switch. Updates local state + storage immediately
87
89
  * (optimistic), then PATCHes `/session-context/{tenant}/me/context` so
@@ -101,6 +103,13 @@ interface SiteContextValue {
101
103
  * The chosen currency must be in the active site's `availableCurrencies`.
102
104
  */
103
105
  setCurrency: (currency: string) => Promise<void>;
106
+ /**
107
+ * Switch the active language at runtime. Sets the `Accept-Language` request
108
+ * header (via `setStorefrontContext`), invalidates the React-Query cache so
109
+ * localized reads refetch, and PATCHes an existing server session context.
110
+ * Does NOT clear the cart (language does not affect pricing).
111
+ */
112
+ setLanguage: (language: string) => Promise<void>;
104
113
  isSwitching: boolean;
105
114
  switchError: Error | null;
106
115
  }
@@ -116,6 +125,12 @@ interface EmporixProviderProps {
116
125
  * `client.config.credentials.storefront.context.siteCode` → `null`.
117
126
  */
118
127
  initialSiteCode?: string;
128
+ /**
129
+ * Initial active language. Resolution order: this prop → `storage.getLanguage()`
130
+ * → `client.config.credentials.storefront.context.language` → `null` (then
131
+ * seeded from the active site's `defaultLanguage` on mount).
132
+ */
133
+ initialLanguage?: string;
119
134
  /**
120
135
  * Initial active legal-entity id (B2B). When set, the CompanyContext
121
136
  * provider tries to match it against `companies.listMine()` once the
@@ -145,7 +160,7 @@ interface EmporixProviderProps {
145
160
  children: ReactNode;
146
161
  }
147
162
  /** Provides the SDK client, token storage, react-query client, and site context to the tree. */
148
- declare function EmporixProvider({ client, queryClient, storage, initialCustomerToken, initialSiteCode, initialActiveLegalEntityId, onTelemetry, autoRefreshCustomerToken, onCustomerSessionExpired, children, }: EmporixProviderProps): React.JSX.Element;
163
+ declare function EmporixProvider({ client, queryClient, storage, initialCustomerToken, initialSiteCode, initialLanguage, initialActiveLegalEntityId, onTelemetry, autoRefreshCustomerToken, onCustomerSessionExpired, children, }: EmporixProviderProps): React.JSX.Element;
149
164
  /** Returns the SDK client and token storage. Throws outside an {@link EmporixProvider}. */
150
165
  declare function useEmporix(): EmporixContextValue;
151
166
 
package/dist/provider.cjs CHANGED
@@ -32,6 +32,7 @@ function createMemoryStorage(opts = {}) {
32
32
  let cartId = null;
33
33
  let anon = null;
34
34
  let siteCode = null;
35
+ let language = null;
35
36
  let activeLegalEntityId = null;
36
37
  let refreshToken = null;
37
38
  const tokenListeners = /* @__PURE__ */ new Set();
@@ -62,6 +63,11 @@ function createMemoryStorage(opts = {}) {
62
63
  siteCode = code;
63
64
  all.notify("siteCode");
64
65
  },
66
+ getLanguage: () => language,
67
+ setLanguage: (l) => {
68
+ language = l;
69
+ all.notify("language");
70
+ },
65
71
  getActiveLegalEntityId: () => activeLegalEntityId,
66
72
  setActiveLegalEntityId: (id) => {
67
73
  activeLegalEntityId = id;
@@ -234,6 +240,7 @@ function EmporixProvider({
234
240
  storage,
235
241
  initialCustomerToken,
236
242
  initialSiteCode,
243
+ initialLanguage,
237
244
  initialActiveLegalEntityId,
238
245
  onTelemetry,
239
246
  autoRefreshCustomerToken,
@@ -387,6 +394,7 @@ function EmporixProvider({
387
394
  client,
388
395
  storage: value.storage,
389
396
  ...initialSiteCode !== void 0 ? { initialSiteCode } : {},
397
+ ...initialLanguage !== void 0 ? { initialLanguage } : {},
390
398
  children: /* @__PURE__ */ jsxRuntime.jsx(
391
399
  CompanyContextProvider,
392
400
  {
@@ -403,6 +411,7 @@ function SiteContextProvider({
403
411
  client,
404
412
  storage,
405
413
  initialSiteCode,
414
+ initialLanguage,
406
415
  children
407
416
  }) {
408
417
  const qc = reactQuery.useQueryClient();
@@ -415,11 +424,17 @@ function SiteContextProvider({
415
424
  const [currency, setCurrencyState] = react.useState(
416
425
  () => client.config?.credentials?.storefront?.context?.currency ?? null
417
426
  );
427
+ const [language, setLanguageState] = react.useState(() => {
428
+ if (initialLanguage !== void 0) return initialLanguage;
429
+ const fromStorage = storage.getLanguage();
430
+ if (fromStorage !== null) return fromStorage;
431
+ return client.config?.credentials?.storefront?.context?.language ?? null;
432
+ });
418
433
  const [targetLocation, setTargetLocation] = react.useState(null);
419
434
  const [isSwitching, setIsSwitching] = react.useState(false);
420
435
  const [switchError, setSwitchError] = react.useState(null);
421
436
  react.useEffect(() => {
422
- if (!siteCode || currency !== null && targetLocation !== null) return;
437
+ if (!siteCode || currency !== null && targetLocation !== null && language !== null) return;
423
438
  let cancelled = false;
424
439
  const token = storage.getCustomerToken();
425
440
  const authCtx = token ? emporixSdk.auth.customer(token) : emporixSdk.auth.anonymous();
@@ -436,12 +451,19 @@ function SiteContextProvider({
436
451
  if (cancelled) return;
437
452
  if (currency === null) setCurrencyState(site.currency);
438
453
  setTargetLocation(site.homeBase?.address?.country ?? null);
454
+ if (language === null && site.defaultLanguage) {
455
+ setLanguageState(site.defaultLanguage);
456
+ client.setStorefrontContext({ language: site.defaultLanguage });
457
+ }
439
458
  }).catch(() => {
440
459
  });
441
460
  return () => {
442
461
  cancelled = true;
443
462
  };
444
463
  }, [siteCode]);
464
+ react.useEffect(() => {
465
+ if (language) client.setStorefrontContext({ language });
466
+ }, []);
445
467
  const setSite = react.useCallback(
446
468
  async (code) => {
447
469
  storage.setSiteCode(code);
@@ -472,6 +494,10 @@ function SiteContextProvider({
472
494
  const nextTarget = site.homeBase?.address?.country ?? null;
473
495
  setCurrencyState(nextCurrency);
474
496
  setTargetLocation(nextTarget);
497
+ if (site.languages && !site.languages.includes(language ?? "") && site.defaultLanguage) {
498
+ setLanguageState(site.defaultLanguage);
499
+ client.setStorefrontContext({ language: site.defaultLanguage });
500
+ }
475
501
  await client.sessionContext.patch(
476
502
  {
477
503
  siteCode: code,
@@ -486,7 +512,7 @@ function SiteContextProvider({
486
512
  setIsSwitching(false);
487
513
  }
488
514
  },
489
- [client, storage, qc]
515
+ [client, storage, qc, language]
490
516
  );
491
517
  const setCurrency = react.useCallback(
492
518
  async (next) => {
@@ -511,17 +537,42 @@ function SiteContextProvider({
511
537
  },
512
538
  [client, storage, qc, siteCode]
513
539
  );
540
+ const setLanguage = react.useCallback(
541
+ async (next) => {
542
+ storage.setLanguage(next);
543
+ setLanguageState(next);
544
+ setSwitchError(null);
545
+ client.setStorefrontContext({ language: next });
546
+ void qc.invalidateQueries({ queryKey: ["emporix"] });
547
+ setIsSwitching(true);
548
+ try {
549
+ const token = storage.getCustomerToken();
550
+ const authCtx = token ? emporixSdk.auth.customer(token) : emporixSdk.auth.anonymous();
551
+ await client.sessionContext.patch(
552
+ { language: next, ...siteCode ? { siteCode } : {} },
553
+ authCtx
554
+ );
555
+ } catch (e) {
556
+ setSwitchError(e instanceof Error ? e : new Error(String(e)));
557
+ } finally {
558
+ setIsSwitching(false);
559
+ }
560
+ },
561
+ [client, storage, qc, siteCode]
562
+ );
514
563
  const value = react.useMemo(
515
564
  () => ({
516
565
  siteCode,
517
566
  currency,
518
567
  targetLocation,
568
+ language,
519
569
  setSite,
520
570
  setCurrency,
571
+ setLanguage,
521
572
  isSwitching,
522
573
  switchError
523
574
  }),
524
- [siteCode, currency, targetLocation, setSite, setCurrency, isSwitching, switchError]
575
+ [siteCode, currency, targetLocation, language, setSite, setCurrency, setLanguage, isSwitching, switchError]
525
576
  );
526
577
  return /* @__PURE__ */ jsxRuntime.jsx(EmporixSiteContext.Provider, { value, children });
527
578
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/storage/index.ts","../src/storage/memory.ts","../src/telemetry.ts","../src/company-context.tsx","../src/provider.tsx"],"names":["createContext","useContext","useQueryClient","useState","useRef","useCallback","auth","useEffect","useMemo","jsx","QueryClient","storage","QueryClientProvider"],"mappings":";;;;;;;;;;AA8DO,SAAS,iBAAA,GAGd;AACA,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAAoB;AAC1C,EAAA,OAAO;AAAA,IACL,IAAI,CAAA,EAAG;AACL,MAAA,SAAA,CAAU,IAAI,CAAC,CAAA;AACf,MAAA,OAAO,MAAM,SAAA,CAAU,MAAA,CAAO,CAAC,CAAA;AAAA,IACjC,CAAA;AAAA,IACA,OAAO,KAAA,EAAO;AACZ,MAAA,KAAA,MAAW,KAAK,SAAA,EAAW;AACzB,QAAA,IAAI;AACF,UAAA,CAAA,CAAE,KAAK,CAAA;AAAA,QACT,CAAA,CAAA,MAAQ;AAAA,QAER;AAAA,MACF;AAAA,IACF;AAAA,GACF;AACF;;;AC1EO,SAAS,mBAAA,CAAoB,IAAA,GAA6B,EAAC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AAA,GAChC;AAEA,EAAA,MAAM,KAAA,GAAQE,aAAAA;AAAA,IACZ,OAAO;AAAA,MACL,QAAA;AAAA,MACA,QAAA;AAAA,MACA,cAAA;AAAA,MACA,OAAA;AAAA,MACA,WAAA;AAAA,MACA,WAAA;AAAA,MACA;AAAA,KACF,CAAA;AAAA,IACA,CAAC,QAAA,EAAU,QAAA,EAAU,gBAAgB,OAAA,EAAS,WAAA,EAAa,aAAa,WAAW;AAAA,GACrF;AAEA,EAAA,uBAAOC,cAAAA,CAAC,kBAAA,CAAmB,QAAA,EAAnB,EAA4B,OAAe,QAAA,EAAS,CAAA;AAC9D;AAGO,SAAS,UAAA,GAAkC;AAChD,EAAA,MAAM,GAAA,GAAMR,iBAAW,cAAc,CAAA;AACrC,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAC7E,EAAA,OAAO,GAAA;AACT","file":"provider.cjs","sourcesContent":["/** Pluggable persistence for SDK session state. SSR-safe by default (memory). */\nexport interface EmporixStorage {\n // Customer token (unchanged).\n getCustomerToken(): string | null;\n setCustomerToken(token: string | null): void;\n subscribe?(listener: (token: string | null) => void): () => void;\n\n // Active guest / customer cart id.\n getCartId(): string | null;\n setCartId(id: string | null): void;\n\n // Anonymous session — used by DefaultTokenProvider (via EmporixProvider\n // wiring) to preserve sessionId across page reloads.\n getAnonymousSession(): PersistedAnonymousSession | null;\n setAnonymousSession(session: PersistedAnonymousSession | null): void;\n\n // Active site code (MS-2). `null` = no site bound yet.\n getSiteCode(): string | null;\n setSiteCode(code: string | null): void;\n\n // Active legal entity id (B2B). `null` = B2C mode.\n getActiveLegalEntityId(): string | null;\n setActiveLegalEntityId(id: string | null): void;\n\n // Refresh token — optional persistence. When absent, B2B company-switch\n // falls back to a local-state-only update (no server-side token rescope).\n getRefreshToken(): string | null;\n setRefreshToken(token: string | null): void;\n\n /**\n * Subscribe to any storage write. The listener receives the key that\n * changed. Returns an unsubscribe function. Optional — backends may no-op.\n * Used by the telemetry layer to emit `storage.write` events.\n */\n subscribeAll?(\n listener: (key: EmporixStorageKey) => void,\n ): () => void;\n}\n\n/** Minimal subset of `AnonymousSession` that needs to outlive a page load. */\nexport interface PersistedAnonymousSession {\n refreshToken: string;\n sessionId: string;\n}\n\n/** Backward-compat alias. New code should prefer `EmporixStorage`. */\nexport type TokenStorage = EmporixStorage;\n\n/** Keys that participate in {@link EmporixStorage.subscribeAll}. */\nexport type EmporixStorageKey =\n | \"customerToken\"\n | \"cartId\"\n | \"siteCode\"\n | \"anonymousSession\"\n | \"activeLegalEntityId\"\n | \"refreshToken\";\n\n/**\n * Internal: create a swallow-on-throw listener set used by all three storage\n * backends for `subscribeAll`. Centralizes the try/catch wrapper so a buggy\n * telemetry handler never breaks a storage write.\n */\nexport function createListenerSet<T>(): {\n add(l: (value: T) => void): () => void;\n notify(value: T): void;\n} {\n const listeners = new Set<(v: T) => void>();\n return {\n add(l) {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n notify(value) {\n for (const l of listeners) {\n try {\n l(value);\n } catch {\n // Swallow handler errors; telemetry must never break writes.\n }\n }\n },\n };\n}\n\n/**\n * Internal: parses a raw `anonymousSession` JSON payload (from localStorage\n * or a cookie) into a {@link PersistedAnonymousSession}. Returns `null` for\n * any malformed or missing input.\n */\nexport function parseAnonymousSession(raw: string | null): PersistedAnonymousSession | null {\n if (!raw) return null;\n try {\n const parsed = JSON.parse(raw) as Partial<PersistedAnonymousSession>;\n if (typeof parsed.refreshToken === \"string\" && typeof parsed.sessionId === \"string\") {\n return { refreshToken: parsed.refreshToken, sessionId: parsed.sessionId };\n }\n return null;\n } catch {\n return null;\n }\n}\n\nexport { createMemoryStorage } from \"./memory\";\nexport { createLocalStorageStorage } from \"./local-storage\";\nexport { createCookieStorage } from \"./cookie\";\n","import {\n createListenerSet,\n type EmporixStorage,\n type EmporixStorageKey,\n type PersistedAnonymousSession,\n} from \"./index\";\n\n/** In-memory token store. Default, SSR-safe, no persistence. */\nexport function createMemoryStorage(opts: { initial?: string } = {}): EmporixStorage {\n let token: string | null = opts.initial ?? null;\n let cartId: string | null = null;\n let anon: PersistedAnonymousSession | null = null;\n let siteCode: string | null = null;\n let activeLegalEntityId: string | null = null;\n let refreshToken: string | null = null;\n const tokenListeners = new Set<(t: string | null) => void>();\n const all = createListenerSet<EmporixStorageKey>();\n return {\n getCustomerToken: () => token,\n setCustomerToken: (t) => {\n token = t;\n for (const l of tokenListeners) l(token);\n all.notify(\"customerToken\");\n },\n subscribe: (l) => {\n tokenListeners.add(l);\n return () => tokenListeners.delete(l);\n },\n getCartId: () => cartId,\n setCartId: (id) => {\n cartId = id;\n all.notify(\"cartId\");\n },\n getAnonymousSession: () => anon,\n setAnonymousSession: (s) => {\n anon = s;\n all.notify(\"anonymousSession\");\n },\n getSiteCode: () => siteCode,\n setSiteCode: (code) => {\n siteCode = code;\n all.notify(\"siteCode\");\n },\n getActiveLegalEntityId: () => activeLegalEntityId,\n setActiveLegalEntityId: (id) => {\n activeLegalEntityId = id;\n all.notify(\"activeLegalEntityId\");\n },\n getRefreshToken: () => refreshToken,\n setRefreshToken: (t) => {\n refreshToken = t;\n all.notify(\"refreshToken\");\n },\n subscribeAll: (l) => all.add(l),\n };\n}\n","import { createContext, useContext } from \"react\";\n\n/**\n * All telemetry events emitted through the EmporixProvider's `onTelemetry`\n * callback. Discriminated by `type` — exhaustive switch is type-safe.\n *\n * Consumers can emit their own `{ type: \"custom\" }` events via\n * {@link useEmporixTelemetry}. Namespace `name` with an app-specific\n * prefix (e.g. `\"app.checkout-cta-click\"`) to avoid collisions with\n * future SDK event types.\n */\nexport type EmporixTelemetryEvent =\n // Cache lifecycle (React-Query QueryCache)\n | { type: \"cache.hit\"; queryKey: readonly unknown[]; tenant: string }\n | {\n type: \"cache.miss\";\n queryKey: readonly unknown[];\n tenant: string;\n durationMs: number;\n }\n | {\n type: \"query.refetch\";\n queryKey: readonly unknown[];\n tenant: string;\n reason: \"invalidate\" | \"focus\" | \"stale\";\n }\n | {\n type: \"query.error\";\n queryKey: readonly unknown[];\n tenant: string;\n error: unknown;\n }\n // Mutation lifecycle\n | {\n type: \"mutation.success\";\n mutationKey?: readonly unknown[];\n tenant: string;\n durationMs: number;\n }\n | {\n type: \"mutation.error\";\n mutationKey?: readonly unknown[];\n tenant: string;\n error: unknown;\n durationMs: number;\n }\n // Auth refresh (SDK-side)\n | {\n type: \"auth.refresh\";\n kind: \"anonymous\" | \"customer\";\n tenant: string;\n success: boolean;\n }\n // Storage writes\n | {\n type: \"storage.write\";\n key: \"customerToken\" | \"cartId\" | \"siteCode\" | \"anonymousSession\" | \"activeLegalEntityId\" | \"refreshToken\";\n }\n // Active-company switch (B2B)\n | {\n type: \"company:switched\";\n from: string | null;\n to: string | null;\n durationMs: number;\n }\n // Consumer-emitted\n | { type: \"custom\"; name: string; props?: Record<string, unknown> };\n\n/** Internal: the React context carrying the emit function down the tree. */\nexport const EmporixTelemetryContext = createContext<{\n emit: (event: EmporixTelemetryEvent) => void;\n} | null>(null);\n\n/**\n * Hook to emit custom telemetry events through the same channel as SDK\n * events. Throws when used outside an {@link EmporixProvider}.\n *\n * When the provider has no `onTelemetry` callback configured, `emit` is a\n * no-op — calling it is safe and incurs no overhead.\n */\nexport function useEmporixTelemetry(): {\n emit: (event: EmporixTelemetryEvent) => void;\n} {\n const ctx = useContext(EmporixTelemetryContext);\n if (!ctx) {\n throw new Error(\"useEmporixTelemetry must be used within an EmporixProvider\");\n }\n return ctx;\n}\n","import {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from \"react\";\nimport { useQueryClient } from \"@tanstack/react-query\";\nimport { auth, type EmporixClient, type LegalEntity } from \"@viu/emporix-sdk\";\nimport type { EmporixStorage } from \"./storage\";\nimport { useEmporixTelemetry } from \"./telemetry\";\n\nexport type CompanyMode = \"b2c\" | \"b2b\" | \"unresolved\";\n\nexport interface CompanyContextValue {\n /** Active legal entity. `null` = B2C mode. */\n activeCompany: LegalEntity | null;\n /** All legal entities the customer is assigned to. */\n myCompanies: LegalEntity[];\n /**\n * `b2b` = a company is active; `b2c` = none active (and ≤1 available);\n * `unresolved` = multiple companies available, none picked yet — the\n * storefront must render a picker.\n */\n mode: CompanyMode;\n status: \"idle\" | \"loading\" | \"switching\" | \"error\";\n error: unknown;\n /**\n * Switch the active company. Eagerly calls\n * `client.customers.refresh({ legalEntityId })` so the customer token is\n * rescoped server-side, drops the cart id, then invalidates company-scoped\n * queries. Falls back to a local-state-only update when no refresh token\n * is in storage (e.g. fresh page load with memory storage).\n */\n setActiveCompany: (legalEntityId: string | null) => Promise<void>;\n refetchMyCompanies: () => Promise<void>;\n}\n\nconst NULL_CTX: CompanyContextValue = {\n activeCompany: null,\n myCompanies: [],\n mode: \"b2c\",\n status: \"idle\",\n error: null,\n setActiveCompany: async () => {\n throw new Error(\"CompanyContextProvider not mounted\");\n },\n refetchMyCompanies: async () => {},\n};\n\nexport const EmporixCompanyContext = createContext<CompanyContextValue>(NULL_CTX);\n\n/** Returns the active-company context. Safe outside the provider — returns idle B2C defaults. */\nexport function useActiveCompany(): CompanyContextValue {\n return useContext(EmporixCompanyContext);\n}\n\nexport interface CompanyContextProviderProps {\n client: EmporixClient;\n storage: EmporixStorage;\n initialActiveLegalEntityId?: string | null;\n children: ReactNode;\n}\n\nexport function CompanyContextProvider({\n client,\n storage,\n initialActiveLegalEntityId,\n children,\n}: CompanyContextProviderProps): React.JSX.Element {\n const qc = useQueryClient();\n const { emit } = useEmporixTelemetry();\n const [myCompanies, setMyCompanies] = useState<LegalEntity[]>([]);\n const [activeCompany, setActive] = useState<LegalEntity | null>(null);\n const [status, setStatus] = useState<CompanyContextValue[\"status\"]>(\"idle\");\n const [error, setError] = useState<unknown>(null);\n // Ref so switchTo can capture the latest `activeCompany` for telemetry `from`.\n const activeRef = useRef<LegalEntity | null>(null);\n activeRef.current = activeCompany;\n\n /** Internal: eager refresh + storage write + state update. */\n const switchTo = useCallback(\n async (target: LegalEntity | null) => {\n const start = Date.now();\n const from = activeRef.current?.id ?? null;\n const refreshToken = storage.getRefreshToken();\n const token = storage.getCustomerToken();\n if (!refreshToken || !token) {\n // Local-state-only fallback — no rescope possible.\n setActive(target);\n storage.setActiveLegalEntityId(target?.id ?? null);\n } else {\n const next = await client.customers.refresh({\n refreshToken,\n ...(target ? { legalEntityId: target.id } : {}),\n });\n storage.setCustomerToken(next.customerToken);\n if (next.refreshToken) storage.setRefreshToken(next.refreshToken);\n storage.setCartId(null);\n storage.setActiveLegalEntityId(target?.id ?? null);\n setActive(target);\n qc.invalidateQueries({\n predicate: (q) =>\n Array.isArray(q.queryKey) &&\n q.queryKey.some(\n (k) =>\n k === \"cart\" ||\n k === \"companies\" ||\n k === \"customer\" ||\n k === from ||\n (target !== null && k === target.id),\n ),\n });\n }\n emit({\n type: \"company:switched\",\n from,\n to: target?.id ?? null,\n durationMs: Date.now() - start,\n });\n },\n [client, storage, qc, emit],\n );\n\n const load = useCallback(async () => {\n const token = storage.getCustomerToken();\n if (!token) {\n setMyCompanies([]);\n setActive(null);\n setStatus(\"idle\");\n return;\n }\n setStatus(\"loading\");\n try {\n const companies = await client.companies.listMine(auth.customer(token));\n setMyCompanies(companies);\n const persisted = initialActiveLegalEntityId ?? storage.getActiveLegalEntityId();\n const matched = persisted ? companies.find((c) => c.id === persisted) ?? null : null;\n if (matched) {\n setActive(matched);\n if (storage.getActiveLegalEntityId() !== matched.id) {\n storage.setActiveLegalEntityId(matched.id ?? null);\n }\n } else if (companies.length === 1) {\n await switchTo(companies[0] ?? null);\n } else {\n setActive(null);\n if (persisted && !matched) storage.setActiveLegalEntityId(null);\n }\n setStatus(\"idle\");\n } catch (e) {\n setError(e);\n setStatus(\"error\");\n }\n }, [client, storage, initialActiveLegalEntityId, switchTo]);\n\n useEffect(() => {\n void load();\n }, [load]);\n\n // Re-run bootstrap only on token-presence transitions (login/logout). A\n // mid-session token swap (e.g. switch-driven refresh) keeps prev/next both\n // truthy and is ignored — otherwise the auto-pick branch would clobber an\n // explicit B2C choice as soon as the new token is written.\n useEffect(() => {\n let prev = storage.getCustomerToken();\n return storage.subscribe?.((next) => {\n const becameAuth = !prev && next;\n const becameUnauth = prev && !next;\n prev = next;\n if (becameAuth || becameUnauth) void load();\n });\n }, [storage, load]);\n\n const setActiveCompany = useCallback(\n async (legalEntityId: string | null) => {\n setStatus(\"switching\");\n try {\n if (legalEntityId === null) {\n await switchTo(null);\n } else {\n const target = myCompanies.find((c) => c.id === legalEntityId) ?? null;\n if (!target) throw new Error(`setActiveCompany: unknown legalEntityId ${legalEntityId}`);\n await switchTo(target);\n }\n setStatus(\"idle\");\n } catch (e) {\n setError(e);\n setStatus(\"error\");\n throw e;\n }\n },\n [myCompanies, switchTo],\n );\n\n const value = useMemo<CompanyContextValue>(() => {\n const mode: CompanyMode = activeCompany\n ? \"b2b\"\n : myCompanies.length > 1\n ? \"unresolved\"\n : \"b2c\";\n return {\n activeCompany,\n myCompanies,\n mode,\n status,\n error,\n setActiveCompany,\n refetchMyCompanies: load,\n };\n }, [activeCompany, myCompanies, status, error, setActiveCompany, load]);\n\n return (\n <EmporixCompanyContext.Provider value={value}>{children}</EmporixCompanyContext.Provider>\n );\n}\n","import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from \"react\";\nimport { QueryClient, QueryClientProvider, useQueryClient } from \"@tanstack/react-query\";\nimport { auth, type EmporixClient } from \"@viu/emporix-sdk\";\nimport type { EmporixStorage } from \"./storage/index\";\nimport { createMemoryStorage } from \"./storage/memory\";\nimport { EmporixTelemetryContext, type EmporixTelemetryEvent } from \"./telemetry\";\nimport { CompanyContextProvider } from \"./company-context\";\n\ninterface EmporixContextValue {\n client: EmporixClient;\n storage: EmporixStorage;\n}\n\nexport interface SiteContextValue {\n siteCode: string | null;\n /** MS-4 populates this from the active site's DTO. */\n currency: string | null;\n /** MS-4 populates this from the active site's DTO. */\n targetLocation: string | null;\n /**\n * Asynchronous site switch. Updates local state + storage immediately\n * (optimistic), then PATCHes `/session-context/{tenant}/me/context` so\n * the server sees the same site on the next request. When no session\n * context exists yet (first visit, before any cart), the PATCH is\n * skipped — local state still flips.\n *\n * `isSwitching` is `true` while the PATCH is in flight. `switchError`\n * surfaces a PATCH failure; the optimistic state is NOT rolled back\n * (the cache was already invalidated, the UI already moved on).\n */\n setSite: (code: string | null) => Promise<void>;\n /**\n * Switch the active currency at runtime. Re-binds the anonymous price context\n * (so guest pricing changes even before a cart exists), clears the\n * currency-bound guest cart, and PATCHes an existing server session context.\n * The chosen currency must be in the active site's `availableCurrencies`.\n */\n setCurrency: (currency: string) => Promise<void>;\n isSwitching: boolean;\n switchError: Error | null;\n}\n\nconst EmporixContext = createContext<EmporixContextValue | null>(null);\nexport const EmporixSiteContext = createContext<SiteContextValue | null>(null);\n\n/**\n * Balanced React-Query defaults applied to the provider's fallback QueryClient\n * (only when no `queryClient` prop is passed). Keeps the Emporix API-quota in\n * check by suppressing window-focus refetches and capping retries.\n */\nconst DEFAULT_QUERY_OPTIONS = {\n staleTime: 30_000,\n refetchOnWindowFocus: false,\n retry: 1,\n} as const;\n\n/** Props for {@link EmporixProvider}. */\nexport interface EmporixProviderProps {\n client: EmporixClient;\n queryClient?: QueryClient;\n storage?: EmporixStorage;\n initialCustomerToken?: string;\n /**\n * Initial site code. Resolution order: this prop → `storage.getSiteCode()` →\n * `client.config.credentials.storefront.context.siteCode` → `null`.\n */\n initialSiteCode?: string;\n /**\n * Initial active legal-entity id (B2B). When set, the CompanyContext\n * provider tries to match it against `companies.listMine()` once the\n * customer is loaded; mismatches are dropped silently.\n */\n initialActiveLegalEntityId?: string | null;\n /**\n * Opt-in telemetry callback. Receives a typed event stream covering cache\n * hit/miss, refetches, errors, mutations, auth refreshes, storage writes,\n * and consumer-emitted custom events. Wire this to Datadog/Sentry/custom\n * analytics. The handler is wrapped in try/catch — a throwing handler\n * never breaks the provider.\n */\n onTelemetry?: (event: EmporixTelemetryEvent) => void;\n /**\n * Opt in to reactive customer-token auto-refresh: on a `customer`-kind 401,\n * the SDK refreshes once (via the stored refresh token + anonymous auth) and\n * retries. Default: false (the customer token stays caller-owned).\n */\n autoRefreshCustomerToken?: boolean;\n /**\n * Called when a customer-token refresh is needed but fails (refresh token\n * expired/revoked) or no refresh token is stored. Use to drive logout /\n * redirect to login.\n */\n onCustomerSessionExpired?: () => void;\n children: ReactNode;\n}\n\n/** Provides the SDK client, token storage, react-query client, and site context to the tree. */\nexport function EmporixProvider({\n client,\n queryClient,\n storage,\n initialCustomerToken,\n initialSiteCode,\n initialActiveLegalEntityId,\n onTelemetry,\n autoRefreshCustomerToken,\n onCustomerSessionExpired,\n children,\n}: EmporixProviderProps): React.JSX.Element {\n const value = useMemo<EmporixContextValue>(() => {\n const s =\n storage ??\n createMemoryStorage(\n initialCustomerToken !== undefined ? { initial: initialCustomerToken } : {},\n );\n if (initialCustomerToken && storage && storage.getCustomerToken() === null) {\n storage.setCustomerToken(initialCustomerToken);\n }\n return { client, storage: s };\n \n }, [client, storage, initialCustomerToken]);\n const qc = useMemo(\n () =>\n queryClient ??\n new QueryClient({ defaultOptions: { queries: DEFAULT_QUERY_OPTIONS } }),\n [queryClient],\n );\n\n // Idempotent one-time wiring: attaches a storage-backed adapter to the SDK's\n // token provider so anonymous sessions survive reloads. Runs once per\n // (client, storage) pair via useState's lazy initializer.\n useState(() => {\n client.tokenProvider.attachAnonymousStore?.({\n read: () => value.storage.getAnonymousSession(),\n write: (s) => value.storage.setAnonymousSession(s),\n });\n return null;\n });\n\n // Telemetry: stable safeEmit + context value. emit is no-op when no\n // onTelemetry callback was provided (no overhead).\n const safeEmit = useCallback(\n (event: EmporixTelemetryEvent) => {\n if (!onTelemetry) return;\n try {\n onTelemetry(event);\n } catch (err) {\n // eslint-disable-next-line no-console\n console.error(\"[emporix] telemetry handler threw:\", err);\n }\n },\n [onTelemetry],\n );\n const telemetryValue = useMemo(() => ({ emit: safeEmit }), [safeEmit]);\n\n // Source subscriptions: cache + mutation cache + token-provider + storage.\n // All only active when onTelemetry is provided.\n useEffect(() => {\n if (!onTelemetry) return;\n const startedAt = new Map<string, number>();\n\n const unsubQuery = qc.getQueryCache().subscribe((event) => {\n const key = event.query.queryKey;\n if (!Array.isArray(key) || key[0] !== \"emporix\") return;\n if (event.type === \"updated\") {\n const action = event.action as { type: string };\n if (action.type === \"fetch\") {\n const isRefetch = event.query.state.dataUpdateCount > 0;\n if (isRefetch) {\n safeEmit({\n type: \"query.refetch\",\n queryKey: key,\n tenant: client.tenant,\n reason: \"invalidate\",\n });\n }\n startedAt.set(event.query.queryHash, Date.now());\n } else if (action.type === \"success\") {\n const start = startedAt.get(event.query.queryHash);\n startedAt.delete(event.query.queryHash);\n safeEmit({\n type: \"cache.miss\",\n queryKey: key,\n tenant: client.tenant,\n durationMs: start ? Date.now() - start : 0,\n });\n } else if (action.type === \"error\") {\n startedAt.delete(event.query.queryHash);\n safeEmit({\n type: \"query.error\",\n queryKey: key,\n tenant: client.tenant,\n error: event.query.state.error,\n });\n }\n } else if (event.type === \"observerResultsUpdated\") {\n const s = event.query.state;\n if (s.status === \"success\" && s.fetchStatus === \"idle\" && s.dataUpdateCount > 0) {\n safeEmit({ type: \"cache.hit\", queryKey: key, tenant: client.tenant });\n }\n }\n });\n\n const unsubMut = qc.getMutationCache().subscribe((event) => {\n if (event.type !== \"updated\") return;\n const m = event.mutation;\n const dur = Date.now() - (m.state.submittedAt ?? Date.now());\n const mk = m.options.mutationKey;\n if (m.state.status === \"success\") {\n safeEmit({\n type: \"mutation.success\",\n ...(mk ? { mutationKey: mk as readonly unknown[] } : {}),\n tenant: client.tenant,\n durationMs: dur,\n });\n } else if (m.state.status === \"error\") {\n safeEmit({\n type: \"mutation.error\",\n ...(mk ? { mutationKey: mk as readonly unknown[] } : {}),\n tenant: client.tenant,\n error: m.state.error,\n durationMs: dur,\n });\n }\n });\n\n const unsubAuth = client.tokenProvider.onRefresh?.((evt) =>\n safeEmit({ type: \"auth.refresh\", ...evt, tenant: client.tenant }),\n );\n\n const unsubStorage = value.storage.subscribeAll?.((key) =>\n safeEmit({ type: \"storage.write\", key }),\n );\n\n return () => {\n unsubQuery();\n unsubMut();\n unsubAuth?.();\n unsubStorage?.();\n };\n }, [qc, onTelemetry, client, value.storage, safeEmit]);\n\n // Opt-in reactive customer-token auto-refresh. Registered on the client so\n // the core HttpClient can refresh-and-retry a customer 401. Single-flight is\n // handled in the core registry. Off unless `autoRefreshCustomerToken`.\n useEffect(() => {\n if (!autoRefreshCustomerToken) return;\n const storage = value.storage;\n client.setCustomerTokenRefresher({\n refresh: async () => {\n const refreshToken = storage.getRefreshToken();\n if (!refreshToken) {\n safeEmit({ type: \"auth.refresh\", kind: \"customer\", success: false, tenant: client.tenant });\n onCustomerSessionExpired?.();\n return null;\n }\n try {\n const legalEntityId = storage.getActiveLegalEntityId() ?? undefined;\n const s = await client.customers.refresh({\n refreshToken,\n ...(legalEntityId ? { legalEntityId } : {}),\n });\n storage.setCustomerToken(s.customerToken);\n if (s.refreshToken) storage.setRefreshToken(s.refreshToken);\n safeEmit({ type: \"auth.refresh\", kind: \"customer\", success: true, tenant: client.tenant });\n return s.customerToken;\n } catch {\n safeEmit({ type: \"auth.refresh\", kind: \"customer\", success: false, tenant: client.tenant });\n onCustomerSessionExpired?.();\n return null;\n }\n },\n });\n return () => client.setCustomerTokenRefresher(null);\n }, [autoRefreshCustomerToken, client, value.storage, safeEmit, onCustomerSessionExpired]);\n\n return (\n <EmporixContext.Provider value={value}>\n <EmporixTelemetryContext.Provider value={telemetryValue}>\n <QueryClientProvider client={qc}>\n <SiteContextProvider\n client={client}\n storage={value.storage}\n {...(initialSiteCode !== undefined ? { initialSiteCode } : {})}\n >\n <CompanyContextProvider\n client={client}\n storage={value.storage}\n {...(initialActiveLegalEntityId !== undefined\n ? { initialActiveLegalEntityId }\n : {})}\n >\n {children}\n </CompanyContextProvider>\n </SiteContextProvider>\n </QueryClientProvider>\n </EmporixTelemetryContext.Provider>\n </EmporixContext.Provider>\n );\n}\n\n/**\n * Manages the active-site state. Sits inside `QueryClientProvider` so\n * `setSite` can invalidate the React-Query cache on switch.\n */\nfunction SiteContextProvider({\n client,\n storage,\n initialSiteCode,\n children,\n}: {\n client: EmporixClient;\n storage: EmporixStorage;\n initialSiteCode?: string;\n children: ReactNode;\n}): React.JSX.Element {\n const qc = useQueryClient();\n const [siteCode, setSiteCodeState] = useState<string | null>(() => {\n if (initialSiteCode !== undefined) return initialSiteCode;\n const fromStorage = storage.getSiteCode();\n if (fromStorage !== null) return fromStorage;\n return client.config?.credentials?.storefront?.context?.siteCode ?? null;\n });\n const [currency, setCurrencyState] = useState<string | null>(\n () => client.config?.credentials?.storefront?.context?.currency ?? null,\n );\n const [targetLocation, setTargetLocation] = useState<string | null>(null);\n const [isSwitching, setIsSwitching] = useState(false);\n const [switchError, setSwitchError] = useState<Error | null>(null);\n\n // Mount-time derivation: if a siteCode is already resolved, fetch its DTO\n // once so currency + targetLocation populate without a user-driven switch.\n // A currency seeded from the client config is NOT overridden (the user's /\n // persisted choice wins); only fields still `null` are filled in.\n useEffect(() => {\n if (!siteCode || (currency !== null && targetLocation !== null)) return;\n let cancelled = false;\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n qc.fetchQuery({\n queryKey: [\n \"emporix\",\n \"site-by-code\",\n siteCode,\n { tenant: client.tenant, authKind: authCtx.kind },\n ],\n queryFn: () => client.sites.get(siteCode, authCtx),\n staleTime: 5 * 60_000,\n })\n .then((site) => {\n if (cancelled) return;\n if (currency === null) setCurrencyState(site.currency);\n setTargetLocation(site.homeBase?.address?.country ?? null);\n })\n .catch(() => {\n // Best-effort — silent. setSite-driven derivation surfaces real errors.\n });\n return () => {\n cancelled = true;\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [siteCode]);\n\n const setSite = useCallback(\n async (code: string | null) => {\n // 1) Optimistic flip — UI moves immediately.\n storage.setSiteCode(code);\n storage.setCartId(null);\n setSiteCodeState(code);\n setSwitchError(null);\n void qc.invalidateQueries({ queryKey: [\"emporix\"] });\n\n if (code === null) {\n setCurrencyState(null);\n setTargetLocation(null);\n return;\n }\n\n setIsSwitching(true);\n try {\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n // 2) Derive currency + targetLocation from the site DTO (cached 5min).\n const site = await qc.fetchQuery({\n queryKey: [\n \"emporix\",\n \"site-by-code\",\n code,\n { tenant: client.tenant, authKind: authCtx.kind },\n ],\n queryFn: () => client.sites.get(code, authCtx),\n staleTime: 5 * 60_000,\n });\n const nextCurrency = site.currency;\n const nextTarget = site.homeBase?.address?.country ?? null;\n setCurrencyState(nextCurrency);\n setTargetLocation(nextTarget);\n // 3) Push everything into the session-context PATCH.\n await client.sessionContext.patch(\n {\n siteCode: code,\n ...(nextCurrency ? { currency: nextCurrency } : {}),\n ...(nextTarget ? { targetLocation: nextTarget } : {}),\n },\n authCtx,\n );\n } catch (e) {\n setSwitchError(e instanceof Error ? e : new Error(String(e)));\n } finally {\n setIsSwitching(false);\n }\n },\n [client, storage, qc],\n );\n\n const setCurrency = useCallback(\n async (next: string) => {\n // Carts are currency-bound — drop the guest cart so a fresh one is created.\n storage.setCartId(null);\n setCurrencyState(next);\n setSwitchError(null);\n // Re-bind the anonymous price context so guest pricing uses the new\n // currency even before a session/cart exists (sessionContext.patch can't).\n client.setStorefrontContext({ currency: next });\n void qc.invalidateQueries({ queryKey: [\"emporix\"] });\n setIsSwitching(true);\n try {\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n // Update an existing server session context (no-op / returns false pre-cart).\n await client.sessionContext.patch(\n { currency: next, ...(siteCode ? { siteCode } : {}) },\n authCtx,\n );\n } catch (e) {\n setSwitchError(e instanceof Error ? e : new Error(String(e)));\n } finally {\n setIsSwitching(false);\n }\n },\n [client, storage, qc, siteCode],\n );\n\n const value = useMemo<SiteContextValue>(\n () => ({\n siteCode,\n currency,\n targetLocation,\n setSite,\n setCurrency,\n isSwitching,\n switchError,\n }),\n [siteCode, currency, targetLocation, setSite, setCurrency, isSwitching, switchError],\n );\n\n return <EmporixSiteContext.Provider value={value}>{children}</EmporixSiteContext.Provider>;\n}\n\n/** Returns the SDK client and token storage. Throws outside an {@link EmporixProvider}. */\nexport function useEmporix(): EmporixContextValue {\n const ctx = useContext(EmporixContext);\n if (!ctx) throw new Error(\"useEmporix must be used within an EmporixProvider\");\n return ctx;\n}\n"]}
1
+ {"version":3,"sources":["../src/storage/index.ts","../src/storage/memory.ts","../src/telemetry.ts","../src/company-context.tsx","../src/provider.tsx"],"names":["createContext","useContext","useQueryClient","useState","useRef","useCallback","auth","useEffect","useMemo","jsx","QueryClient","storage","QueryClientProvider"],"mappings":";;;;;;;;;;AAmEO,SAAS,iBAAA,GAGd;AACA,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAAoB;AAC1C,EAAA,OAAO;AAAA,IACL,IAAI,CAAA,EAAG;AACL,MAAA,SAAA,CAAU,IAAI,CAAC,CAAA;AACf,MAAA,OAAO,MAAM,SAAA,CAAU,MAAA,CAAO,CAAC,CAAA;AAAA,IACjC,CAAA;AAAA,IACA,OAAO,KAAA,EAAO;AACZ,MAAA,KAAA,MAAW,KAAK,SAAA,EAAW;AACzB,QAAA,IAAI;AACF,UAAA,CAAA,CAAE,KAAK,CAAA;AAAA,QACT,CAAA,CAAA,MAAQ;AAAA,QAER;AAAA,MACF;AAAA,IACF;AAAA,GACF;AACF;;;AC/EO,SAAS,mBAAA,CAAoB,IAAA,GAA6B,EAAC,EAAmB;AACnF,EAAA,IAAI,KAAA,GAAuB,KAAK,OAAA,IAAW,IAAA;AAC3C,EAAA,IAAI,MAAA,GAAwB,IAAA;AAC5B,EAAA,IAAI,IAAA,GAAyC,IAAA;AAC7C,EAAA,IAAI,QAAA,GAA0B,IAAA;AAC9B,EAAA,IAAI,QAAA,GAA0B,IAAA;AAC9B,EAAA,IAAI,mBAAA,GAAqC,IAAA;AACzC,EAAA,IAAI,YAAA,GA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AAChD,EAAA,MAAM,GAAA,GAAMR,iBAAW,cAAc,CAAA;AACrC,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,MAAM,mDAAmD,CAAA;AAC7E,EAAA,OAAO,GAAA;AACT","file":"provider.cjs","sourcesContent":["/** Pluggable persistence for SDK session state. SSR-safe by default (memory). */\nexport interface EmporixStorage {\n // Customer token (unchanged).\n getCustomerToken(): string | null;\n setCustomerToken(token: string | null): void;\n subscribe?(listener: (token: string | null) => void): () => void;\n\n // Active guest / customer cart id.\n getCartId(): string | null;\n setCartId(id: string | null): void;\n\n // Anonymous session — used by DefaultTokenProvider (via EmporixProvider\n // wiring) to preserve sessionId across page reloads.\n getAnonymousSession(): PersistedAnonymousSession | null;\n setAnonymousSession(session: PersistedAnonymousSession | null): void;\n\n // Active site code (MS-2). `null` = no site bound yet.\n getSiteCode(): string | null;\n setSiteCode(code: string | null): void;\n\n // Active language (Accept-Language). `null` = use the site/tenant default.\n getLanguage(): string | null;\n setLanguage(language: string | null): void;\n\n // Active legal entity id (B2B). `null` = B2C mode.\n getActiveLegalEntityId(): string | null;\n setActiveLegalEntityId(id: string | null): void;\n\n // Refresh token — optional persistence. When absent, B2B company-switch\n // falls back to a local-state-only update (no server-side token rescope).\n getRefreshToken(): string | null;\n setRefreshToken(token: string | null): void;\n\n /**\n * Subscribe to any storage write. The listener receives the key that\n * changed. Returns an unsubscribe function. Optional — backends may no-op.\n * Used by the telemetry layer to emit `storage.write` events.\n */\n subscribeAll?(\n listener: (key: EmporixStorageKey) => void,\n ): () => void;\n}\n\n/** Minimal subset of `AnonymousSession` that needs to outlive a page load. */\nexport interface PersistedAnonymousSession {\n refreshToken: string;\n sessionId: string;\n}\n\n/** Backward-compat alias. New code should prefer `EmporixStorage`. */\nexport type TokenStorage = EmporixStorage;\n\n/** Keys that participate in {@link EmporixStorage.subscribeAll}. */\nexport type EmporixStorageKey =\n | \"customerToken\"\n | \"cartId\"\n | \"siteCode\"\n | \"language\"\n | \"anonymousSession\"\n | \"activeLegalEntityId\"\n | \"refreshToken\";\n\n/**\n * Internal: create a swallow-on-throw listener set used by all three storage\n * backends for `subscribeAll`. Centralizes the try/catch wrapper so a buggy\n * telemetry handler never breaks a storage write.\n */\nexport function createListenerSet<T>(): {\n add(l: (value: T) => void): () => void;\n notify(value: T): void;\n} {\n const listeners = new Set<(v: T) => void>();\n return {\n add(l) {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n notify(value) {\n for (const l of listeners) {\n try {\n l(value);\n } catch {\n // Swallow handler errors; telemetry must never break writes.\n }\n }\n },\n };\n}\n\n/**\n * Internal: parses a raw `anonymousSession` JSON payload (from localStorage\n * or a cookie) into a {@link PersistedAnonymousSession}. Returns `null` for\n * any malformed or missing input.\n */\nexport function parseAnonymousSession(raw: string | null): PersistedAnonymousSession | null {\n if (!raw) return null;\n try {\n const parsed = JSON.parse(raw) as Partial<PersistedAnonymousSession>;\n if (typeof parsed.refreshToken === \"string\" && typeof parsed.sessionId === \"string\") {\n return { refreshToken: parsed.refreshToken, sessionId: parsed.sessionId };\n }\n return null;\n } catch {\n return null;\n }\n}\n\nexport { createMemoryStorage } from \"./memory\";\nexport { createLocalStorageStorage } from \"./local-storage\";\nexport { createCookieStorage } from \"./cookie\";\n","import {\n createListenerSet,\n type EmporixStorage,\n type EmporixStorageKey,\n type PersistedAnonymousSession,\n} from \"./index\";\n\n/** In-memory token store. Default, SSR-safe, no persistence. */\nexport function createMemoryStorage(opts: { initial?: string } = {}): EmporixStorage {\n let token: string | null = opts.initial ?? null;\n let cartId: string | null = null;\n let anon: PersistedAnonymousSession | null = null;\n let siteCode: string | null = null;\n let language: string | null = null;\n let activeLegalEntityId: string | null = null;\n let refreshToken: string | null = null;\n const tokenListeners = new Set<(t: string | null) => void>();\n const all = createListenerSet<EmporixStorageKey>();\n return {\n getCustomerToken: () => token,\n setCustomerToken: (t) => {\n token = t;\n for (const l of tokenListeners) l(token);\n all.notify(\"customerToken\");\n },\n subscribe: (l) => {\n tokenListeners.add(l);\n return () => tokenListeners.delete(l);\n },\n getCartId: () => cartId,\n setCartId: (id) => {\n cartId = id;\n all.notify(\"cartId\");\n },\n getAnonymousSession: () => anon,\n setAnonymousSession: (s) => {\n anon = s;\n all.notify(\"anonymousSession\");\n },\n getSiteCode: () => siteCode,\n setSiteCode: (code) => {\n siteCode = code;\n all.notify(\"siteCode\");\n },\n getLanguage: () => language,\n setLanguage: (l) => {\n language = l;\n all.notify(\"language\");\n },\n getActiveLegalEntityId: () => activeLegalEntityId,\n setActiveLegalEntityId: (id) => {\n activeLegalEntityId = id;\n all.notify(\"activeLegalEntityId\");\n },\n getRefreshToken: () => refreshToken,\n setRefreshToken: (t) => {\n refreshToken = t;\n all.notify(\"refreshToken\");\n },\n subscribeAll: (l) => all.add(l),\n };\n}\n","import { createContext, useContext } from \"react\";\n\n/**\n * All telemetry events emitted through the EmporixProvider's `onTelemetry`\n * callback. Discriminated by `type` — exhaustive switch is type-safe.\n *\n * Consumers can emit their own `{ type: \"custom\" }` events via\n * {@link useEmporixTelemetry}. Namespace `name` with an app-specific\n * prefix (e.g. `\"app.checkout-cta-click\"`) to avoid collisions with\n * future SDK event types.\n */\nexport type EmporixTelemetryEvent =\n // Cache lifecycle (React-Query QueryCache)\n | { type: \"cache.hit\"; queryKey: readonly unknown[]; tenant: string }\n | {\n type: \"cache.miss\";\n queryKey: readonly unknown[];\n tenant: string;\n durationMs: number;\n }\n | {\n type: \"query.refetch\";\n queryKey: readonly unknown[];\n tenant: string;\n reason: \"invalidate\" | \"focus\" | \"stale\";\n }\n | {\n type: \"query.error\";\n queryKey: readonly unknown[];\n tenant: string;\n error: unknown;\n }\n // Mutation lifecycle\n | {\n type: \"mutation.success\";\n mutationKey?: readonly unknown[];\n tenant: string;\n durationMs: number;\n }\n | {\n type: \"mutation.error\";\n mutationKey?: readonly unknown[];\n tenant: string;\n error: unknown;\n durationMs: number;\n }\n // Auth refresh (SDK-side)\n | {\n type: \"auth.refresh\";\n kind: \"anonymous\" | \"customer\";\n tenant: string;\n success: boolean;\n }\n // Storage writes\n | {\n type: \"storage.write\";\n key: \"customerToken\" | \"cartId\" | \"siteCode\" | \"language\" | \"anonymousSession\" | \"activeLegalEntityId\" | \"refreshToken\";\n }\n // Active-company switch (B2B)\n | {\n type: \"company:switched\";\n from: string | null;\n to: string | null;\n durationMs: number;\n }\n // Consumer-emitted\n | { type: \"custom\"; name: string; props?: Record<string, unknown> };\n\n/** Internal: the React context carrying the emit function down the tree. */\nexport const EmporixTelemetryContext = createContext<{\n emit: (event: EmporixTelemetryEvent) => void;\n} | null>(null);\n\n/**\n * Hook to emit custom telemetry events through the same channel as SDK\n * events. Throws when used outside an {@link EmporixProvider}.\n *\n * When the provider has no `onTelemetry` callback configured, `emit` is a\n * no-op — calling it is safe and incurs no overhead.\n */\nexport function useEmporixTelemetry(): {\n emit: (event: EmporixTelemetryEvent) => void;\n} {\n const ctx = useContext(EmporixTelemetryContext);\n if (!ctx) {\n throw new Error(\"useEmporixTelemetry must be used within an EmporixProvider\");\n }\n return ctx;\n}\n","import {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from \"react\";\nimport { useQueryClient } from \"@tanstack/react-query\";\nimport { auth, type EmporixClient, type LegalEntity } from \"@viu/emporix-sdk\";\nimport type { EmporixStorage } from \"./storage\";\nimport { useEmporixTelemetry } from \"./telemetry\";\n\nexport type CompanyMode = \"b2c\" | \"b2b\" | \"unresolved\";\n\nexport interface CompanyContextValue {\n /** Active legal entity. `null` = B2C mode. */\n activeCompany: LegalEntity | null;\n /** All legal entities the customer is assigned to. */\n myCompanies: LegalEntity[];\n /**\n * `b2b` = a company is active; `b2c` = none active (and ≤1 available);\n * `unresolved` = multiple companies available, none picked yet — the\n * storefront must render a picker.\n */\n mode: CompanyMode;\n status: \"idle\" | \"loading\" | \"switching\" | \"error\";\n error: unknown;\n /**\n * Switch the active company. Eagerly calls\n * `client.customers.refresh({ legalEntityId })` so the customer token is\n * rescoped server-side, drops the cart id, then invalidates company-scoped\n * queries. Falls back to a local-state-only update when no refresh token\n * is in storage (e.g. fresh page load with memory storage).\n */\n setActiveCompany: (legalEntityId: string | null) => Promise<void>;\n refetchMyCompanies: () => Promise<void>;\n}\n\nconst NULL_CTX: CompanyContextValue = {\n activeCompany: null,\n myCompanies: [],\n mode: \"b2c\",\n status: \"idle\",\n error: null,\n setActiveCompany: async () => {\n throw new Error(\"CompanyContextProvider not mounted\");\n },\n refetchMyCompanies: async () => {},\n};\n\nexport const EmporixCompanyContext = createContext<CompanyContextValue>(NULL_CTX);\n\n/** Returns the active-company context. Safe outside the provider — returns idle B2C defaults. */\nexport function useActiveCompany(): CompanyContextValue {\n return useContext(EmporixCompanyContext);\n}\n\nexport interface CompanyContextProviderProps {\n client: EmporixClient;\n storage: EmporixStorage;\n initialActiveLegalEntityId?: string | null;\n children: ReactNode;\n}\n\nexport function CompanyContextProvider({\n client,\n storage,\n initialActiveLegalEntityId,\n children,\n}: CompanyContextProviderProps): React.JSX.Element {\n const qc = useQueryClient();\n const { emit } = useEmporixTelemetry();\n const [myCompanies, setMyCompanies] = useState<LegalEntity[]>([]);\n const [activeCompany, setActive] = useState<LegalEntity | null>(null);\n const [status, setStatus] = useState<CompanyContextValue[\"status\"]>(\"idle\");\n const [error, setError] = useState<unknown>(null);\n // Ref so switchTo can capture the latest `activeCompany` for telemetry `from`.\n const activeRef = useRef<LegalEntity | null>(null);\n activeRef.current = activeCompany;\n\n /** Internal: eager refresh + storage write + state update. */\n const switchTo = useCallback(\n async (target: LegalEntity | null) => {\n const start = Date.now();\n const from = activeRef.current?.id ?? null;\n const refreshToken = storage.getRefreshToken();\n const token = storage.getCustomerToken();\n if (!refreshToken || !token) {\n // Local-state-only fallback — no rescope possible.\n setActive(target);\n storage.setActiveLegalEntityId(target?.id ?? null);\n } else {\n const next = await client.customers.refresh({\n refreshToken,\n ...(target ? { legalEntityId: target.id } : {}),\n });\n storage.setCustomerToken(next.customerToken);\n if (next.refreshToken) storage.setRefreshToken(next.refreshToken);\n storage.setCartId(null);\n storage.setActiveLegalEntityId(target?.id ?? null);\n setActive(target);\n qc.invalidateQueries({\n predicate: (q) =>\n Array.isArray(q.queryKey) &&\n q.queryKey.some(\n (k) =>\n k === \"cart\" ||\n k === \"companies\" ||\n k === \"customer\" ||\n k === from ||\n (target !== null && k === target.id),\n ),\n });\n }\n emit({\n type: \"company:switched\",\n from,\n to: target?.id ?? null,\n durationMs: Date.now() - start,\n });\n },\n [client, storage, qc, emit],\n );\n\n const load = useCallback(async () => {\n const token = storage.getCustomerToken();\n if (!token) {\n setMyCompanies([]);\n setActive(null);\n setStatus(\"idle\");\n return;\n }\n setStatus(\"loading\");\n try {\n const companies = await client.companies.listMine(auth.customer(token));\n setMyCompanies(companies);\n const persisted = initialActiveLegalEntityId ?? storage.getActiveLegalEntityId();\n const matched = persisted ? companies.find((c) => c.id === persisted) ?? null : null;\n if (matched) {\n setActive(matched);\n if (storage.getActiveLegalEntityId() !== matched.id) {\n storage.setActiveLegalEntityId(matched.id ?? null);\n }\n } else if (companies.length === 1) {\n await switchTo(companies[0] ?? null);\n } else {\n setActive(null);\n if (persisted && !matched) storage.setActiveLegalEntityId(null);\n }\n setStatus(\"idle\");\n } catch (e) {\n setError(e);\n setStatus(\"error\");\n }\n }, [client, storage, initialActiveLegalEntityId, switchTo]);\n\n useEffect(() => {\n void load();\n }, [load]);\n\n // Re-run bootstrap only on token-presence transitions (login/logout). A\n // mid-session token swap (e.g. switch-driven refresh) keeps prev/next both\n // truthy and is ignored — otherwise the auto-pick branch would clobber an\n // explicit B2C choice as soon as the new token is written.\n useEffect(() => {\n let prev = storage.getCustomerToken();\n return storage.subscribe?.((next) => {\n const becameAuth = !prev && next;\n const becameUnauth = prev && !next;\n prev = next;\n if (becameAuth || becameUnauth) void load();\n });\n }, [storage, load]);\n\n const setActiveCompany = useCallback(\n async (legalEntityId: string | null) => {\n setStatus(\"switching\");\n try {\n if (legalEntityId === null) {\n await switchTo(null);\n } else {\n const target = myCompanies.find((c) => c.id === legalEntityId) ?? null;\n if (!target) throw new Error(`setActiveCompany: unknown legalEntityId ${legalEntityId}`);\n await switchTo(target);\n }\n setStatus(\"idle\");\n } catch (e) {\n setError(e);\n setStatus(\"error\");\n throw e;\n }\n },\n [myCompanies, switchTo],\n );\n\n const value = useMemo<CompanyContextValue>(() => {\n const mode: CompanyMode = activeCompany\n ? \"b2b\"\n : myCompanies.length > 1\n ? \"unresolved\"\n : \"b2c\";\n return {\n activeCompany,\n myCompanies,\n mode,\n status,\n error,\n setActiveCompany,\n refetchMyCompanies: load,\n };\n }, [activeCompany, myCompanies, status, error, setActiveCompany, load]);\n\n return (\n <EmporixCompanyContext.Provider value={value}>{children}</EmporixCompanyContext.Provider>\n );\n}\n","import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from \"react\";\nimport { QueryClient, QueryClientProvider, useQueryClient } from \"@tanstack/react-query\";\nimport { auth, type EmporixClient } from \"@viu/emporix-sdk\";\nimport type { EmporixStorage } from \"./storage/index\";\nimport { createMemoryStorage } from \"./storage/memory\";\nimport { EmporixTelemetryContext, type EmporixTelemetryEvent } from \"./telemetry\";\nimport { CompanyContextProvider } from \"./company-context\";\n\ninterface EmporixContextValue {\n client: EmporixClient;\n storage: EmporixStorage;\n}\n\nexport interface SiteContextValue {\n siteCode: string | null;\n /** MS-4 populates this from the active site's DTO. */\n currency: string | null;\n /** MS-4 populates this from the active site's DTO. */\n targetLocation: string | null;\n /** Active language for localized reads (Accept-Language). `null` = site/tenant default. */\n language: string | null;\n /**\n * Asynchronous site switch. Updates local state + storage immediately\n * (optimistic), then PATCHes `/session-context/{tenant}/me/context` so\n * the server sees the same site on the next request. When no session\n * context exists yet (first visit, before any cart), the PATCH is\n * skipped — local state still flips.\n *\n * `isSwitching` is `true` while the PATCH is in flight. `switchError`\n * surfaces a PATCH failure; the optimistic state is NOT rolled back\n * (the cache was already invalidated, the UI already moved on).\n */\n setSite: (code: string | null) => Promise<void>;\n /**\n * Switch the active currency at runtime. Re-binds the anonymous price context\n * (so guest pricing changes even before a cart exists), clears the\n * currency-bound guest cart, and PATCHes an existing server session context.\n * The chosen currency must be in the active site's `availableCurrencies`.\n */\n setCurrency: (currency: string) => Promise<void>;\n /**\n * Switch the active language at runtime. Sets the `Accept-Language` request\n * header (via `setStorefrontContext`), invalidates the React-Query cache so\n * localized reads refetch, and PATCHes an existing server session context.\n * Does NOT clear the cart (language does not affect pricing).\n */\n setLanguage: (language: string) => Promise<void>;\n isSwitching: boolean;\n switchError: Error | null;\n}\n\nconst EmporixContext = createContext<EmporixContextValue | null>(null);\nexport const EmporixSiteContext = createContext<SiteContextValue | null>(null);\n\n/**\n * Balanced React-Query defaults applied to the provider's fallback QueryClient\n * (only when no `queryClient` prop is passed). Keeps the Emporix API-quota in\n * check by suppressing window-focus refetches and capping retries.\n */\nconst DEFAULT_QUERY_OPTIONS = {\n staleTime: 30_000,\n refetchOnWindowFocus: false,\n retry: 1,\n} as const;\n\n/** Props for {@link EmporixProvider}. */\nexport interface EmporixProviderProps {\n client: EmporixClient;\n queryClient?: QueryClient;\n storage?: EmporixStorage;\n initialCustomerToken?: string;\n /**\n * Initial site code. Resolution order: this prop → `storage.getSiteCode()` →\n * `client.config.credentials.storefront.context.siteCode` → `null`.\n */\n initialSiteCode?: string;\n /**\n * Initial active language. Resolution order: this prop → `storage.getLanguage()`\n * → `client.config.credentials.storefront.context.language` → `null` (then\n * seeded from the active site's `defaultLanguage` on mount).\n */\n initialLanguage?: string;\n /**\n * Initial active legal-entity id (B2B). When set, the CompanyContext\n * provider tries to match it against `companies.listMine()` once the\n * customer is loaded; mismatches are dropped silently.\n */\n initialActiveLegalEntityId?: string | null;\n /**\n * Opt-in telemetry callback. Receives a typed event stream covering cache\n * hit/miss, refetches, errors, mutations, auth refreshes, storage writes,\n * and consumer-emitted custom events. Wire this to Datadog/Sentry/custom\n * analytics. The handler is wrapped in try/catch — a throwing handler\n * never breaks the provider.\n */\n onTelemetry?: (event: EmporixTelemetryEvent) => void;\n /**\n * Opt in to reactive customer-token auto-refresh: on a `customer`-kind 401,\n * the SDK refreshes once (via the stored refresh token + anonymous auth) and\n * retries. Default: false (the customer token stays caller-owned).\n */\n autoRefreshCustomerToken?: boolean;\n /**\n * Called when a customer-token refresh is needed but fails (refresh token\n * expired/revoked) or no refresh token is stored. Use to drive logout /\n * redirect to login.\n */\n onCustomerSessionExpired?: () => void;\n children: ReactNode;\n}\n\n/** Provides the SDK client, token storage, react-query client, and site context to the tree. */\nexport function EmporixProvider({\n client,\n queryClient,\n storage,\n initialCustomerToken,\n initialSiteCode,\n initialLanguage,\n initialActiveLegalEntityId,\n onTelemetry,\n autoRefreshCustomerToken,\n onCustomerSessionExpired,\n children,\n}: EmporixProviderProps): React.JSX.Element {\n const value = useMemo<EmporixContextValue>(() => {\n const s =\n storage ??\n createMemoryStorage(\n initialCustomerToken !== undefined ? { initial: initialCustomerToken } : {},\n );\n if (initialCustomerToken && storage && storage.getCustomerToken() === null) {\n storage.setCustomerToken(initialCustomerToken);\n }\n return { client, storage: s };\n \n }, [client, storage, initialCustomerToken]);\n const qc = useMemo(\n () =>\n queryClient ??\n new QueryClient({ defaultOptions: { queries: DEFAULT_QUERY_OPTIONS } }),\n [queryClient],\n );\n\n // Idempotent one-time wiring: attaches a storage-backed adapter to the SDK's\n // token provider so anonymous sessions survive reloads. Runs once per\n // (client, storage) pair via useState's lazy initializer.\n useState(() => {\n client.tokenProvider.attachAnonymousStore?.({\n read: () => value.storage.getAnonymousSession(),\n write: (s) => value.storage.setAnonymousSession(s),\n });\n return null;\n });\n\n // Telemetry: stable safeEmit + context value. emit is no-op when no\n // onTelemetry callback was provided (no overhead).\n const safeEmit = useCallback(\n (event: EmporixTelemetryEvent) => {\n if (!onTelemetry) return;\n try {\n onTelemetry(event);\n } catch (err) {\n // eslint-disable-next-line no-console\n console.error(\"[emporix] telemetry handler threw:\", err);\n }\n },\n [onTelemetry],\n );\n const telemetryValue = useMemo(() => ({ emit: safeEmit }), [safeEmit]);\n\n // Source subscriptions: cache + mutation cache + token-provider + storage.\n // All only active when onTelemetry is provided.\n useEffect(() => {\n if (!onTelemetry) return;\n const startedAt = new Map<string, number>();\n\n const unsubQuery = qc.getQueryCache().subscribe((event) => {\n const key = event.query.queryKey;\n if (!Array.isArray(key) || key[0] !== \"emporix\") return;\n if (event.type === \"updated\") {\n const action = event.action as { type: string };\n if (action.type === \"fetch\") {\n const isRefetch = event.query.state.dataUpdateCount > 0;\n if (isRefetch) {\n safeEmit({\n type: \"query.refetch\",\n queryKey: key,\n tenant: client.tenant,\n reason: \"invalidate\",\n });\n }\n startedAt.set(event.query.queryHash, Date.now());\n } else if (action.type === \"success\") {\n const start = startedAt.get(event.query.queryHash);\n startedAt.delete(event.query.queryHash);\n safeEmit({\n type: \"cache.miss\",\n queryKey: key,\n tenant: client.tenant,\n durationMs: start ? Date.now() - start : 0,\n });\n } else if (action.type === \"error\") {\n startedAt.delete(event.query.queryHash);\n safeEmit({\n type: \"query.error\",\n queryKey: key,\n tenant: client.tenant,\n error: event.query.state.error,\n });\n }\n } else if (event.type === \"observerResultsUpdated\") {\n const s = event.query.state;\n if (s.status === \"success\" && s.fetchStatus === \"idle\" && s.dataUpdateCount > 0) {\n safeEmit({ type: \"cache.hit\", queryKey: key, tenant: client.tenant });\n }\n }\n });\n\n const unsubMut = qc.getMutationCache().subscribe((event) => {\n if (event.type !== \"updated\") return;\n const m = event.mutation;\n const dur = Date.now() - (m.state.submittedAt ?? Date.now());\n const mk = m.options.mutationKey;\n if (m.state.status === \"success\") {\n safeEmit({\n type: \"mutation.success\",\n ...(mk ? { mutationKey: mk as readonly unknown[] } : {}),\n tenant: client.tenant,\n durationMs: dur,\n });\n } else if (m.state.status === \"error\") {\n safeEmit({\n type: \"mutation.error\",\n ...(mk ? { mutationKey: mk as readonly unknown[] } : {}),\n tenant: client.tenant,\n error: m.state.error,\n durationMs: dur,\n });\n }\n });\n\n const unsubAuth = client.tokenProvider.onRefresh?.((evt) =>\n safeEmit({ type: \"auth.refresh\", ...evt, tenant: client.tenant }),\n );\n\n const unsubStorage = value.storage.subscribeAll?.((key) =>\n safeEmit({ type: \"storage.write\", key }),\n );\n\n return () => {\n unsubQuery();\n unsubMut();\n unsubAuth?.();\n unsubStorage?.();\n };\n }, [qc, onTelemetry, client, value.storage, safeEmit]);\n\n // Opt-in reactive customer-token auto-refresh. Registered on the client so\n // the core HttpClient can refresh-and-retry a customer 401. Single-flight is\n // handled in the core registry. Off unless `autoRefreshCustomerToken`.\n useEffect(() => {\n if (!autoRefreshCustomerToken) return;\n const storage = value.storage;\n client.setCustomerTokenRefresher({\n refresh: async () => {\n const refreshToken = storage.getRefreshToken();\n if (!refreshToken) {\n safeEmit({ type: \"auth.refresh\", kind: \"customer\", success: false, tenant: client.tenant });\n onCustomerSessionExpired?.();\n return null;\n }\n try {\n const legalEntityId = storage.getActiveLegalEntityId() ?? undefined;\n const s = await client.customers.refresh({\n refreshToken,\n ...(legalEntityId ? { legalEntityId } : {}),\n });\n storage.setCustomerToken(s.customerToken);\n if (s.refreshToken) storage.setRefreshToken(s.refreshToken);\n safeEmit({ type: \"auth.refresh\", kind: \"customer\", success: true, tenant: client.tenant });\n return s.customerToken;\n } catch {\n safeEmit({ type: \"auth.refresh\", kind: \"customer\", success: false, tenant: client.tenant });\n onCustomerSessionExpired?.();\n return null;\n }\n },\n });\n return () => client.setCustomerTokenRefresher(null);\n }, [autoRefreshCustomerToken, client, value.storage, safeEmit, onCustomerSessionExpired]);\n\n return (\n <EmporixContext.Provider value={value}>\n <EmporixTelemetryContext.Provider value={telemetryValue}>\n <QueryClientProvider client={qc}>\n <SiteContextProvider\n client={client}\n storage={value.storage}\n {...(initialSiteCode !== undefined ? { initialSiteCode } : {})}\n {...(initialLanguage !== undefined ? { initialLanguage } : {})}\n >\n <CompanyContextProvider\n client={client}\n storage={value.storage}\n {...(initialActiveLegalEntityId !== undefined\n ? { initialActiveLegalEntityId }\n : {})}\n >\n {children}\n </CompanyContextProvider>\n </SiteContextProvider>\n </QueryClientProvider>\n </EmporixTelemetryContext.Provider>\n </EmporixContext.Provider>\n );\n}\n\n/**\n * Manages the active-site state. Sits inside `QueryClientProvider` so\n * `setSite` can invalidate the React-Query cache on switch.\n */\nfunction SiteContextProvider({\n client,\n storage,\n initialSiteCode,\n initialLanguage,\n children,\n}: {\n client: EmporixClient;\n storage: EmporixStorage;\n initialSiteCode?: string;\n initialLanguage?: string;\n children: ReactNode;\n}): React.JSX.Element {\n const qc = useQueryClient();\n const [siteCode, setSiteCodeState] = useState<string | null>(() => {\n if (initialSiteCode !== undefined) return initialSiteCode;\n const fromStorage = storage.getSiteCode();\n if (fromStorage !== null) return fromStorage;\n return client.config?.credentials?.storefront?.context?.siteCode ?? null;\n });\n const [currency, setCurrencyState] = useState<string | null>(\n () => client.config?.credentials?.storefront?.context?.currency ?? null,\n );\n const [language, setLanguageState] = useState<string | null>(() => {\n if (initialLanguage !== undefined) return initialLanguage;\n const fromStorage = storage.getLanguage();\n if (fromStorage !== null) return fromStorage;\n return client.config?.credentials?.storefront?.context?.language ?? null;\n });\n const [targetLocation, setTargetLocation] = useState<string | null>(null);\n const [isSwitching, setIsSwitching] = useState(false);\n const [switchError, setSwitchError] = useState<Error | null>(null);\n\n // Mount-time derivation: if a siteCode is already resolved, fetch its DTO\n // once so currency + targetLocation populate without a user-driven switch.\n // A currency seeded from the client config is NOT overridden (the user's /\n // persisted choice wins); only fields still `null` are filled in.\n useEffect(() => {\n if (!siteCode || (currency !== null && targetLocation !== null && language !== null)) return;\n let cancelled = false;\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n qc.fetchQuery({\n queryKey: [\n \"emporix\",\n \"site-by-code\",\n siteCode,\n { tenant: client.tenant, authKind: authCtx.kind },\n ],\n queryFn: () => client.sites.get(siteCode, authCtx),\n staleTime: 5 * 60_000,\n })\n .then((site) => {\n if (cancelled) return;\n if (currency === null) setCurrencyState(site.currency);\n setTargetLocation(site.homeBase?.address?.country ?? null);\n if (language === null && site.defaultLanguage) {\n setLanguageState(site.defaultLanguage);\n client.setStorefrontContext({ language: site.defaultLanguage });\n }\n })\n .catch(() => {\n // Best-effort — silent. setSite-driven derivation surfaces real errors.\n });\n return () => {\n cancelled = true;\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [siteCode]);\n\n // Push the initially-resolved language (prop / storage / config) to the SDK so\n // the very first reads carry `Accept-Language` — React state alone does not\n // reach the client. Mount-only; later changes go through setLanguage / setSite.\n useEffect(() => {\n if (language) client.setStorefrontContext({ language });\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n const setSite = useCallback(\n async (code: string | null) => {\n // 1) Optimistic flip — UI moves immediately.\n storage.setSiteCode(code);\n storage.setCartId(null);\n setSiteCodeState(code);\n setSwitchError(null);\n void qc.invalidateQueries({ queryKey: [\"emporix\"] });\n\n if (code === null) {\n setCurrencyState(null);\n setTargetLocation(null);\n return;\n }\n\n setIsSwitching(true);\n try {\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n // 2) Derive currency + targetLocation from the site DTO (cached 5min).\n const site = await qc.fetchQuery({\n queryKey: [\n \"emporix\",\n \"site-by-code\",\n code,\n { tenant: client.tenant, authKind: authCtx.kind },\n ],\n queryFn: () => client.sites.get(code, authCtx),\n staleTime: 5 * 60_000,\n });\n const nextCurrency = site.currency;\n const nextTarget = site.homeBase?.address?.country ?? null;\n setCurrencyState(nextCurrency);\n setTargetLocation(nextTarget);\n // Reset the language if the new site doesn't support the active one.\n if (site.languages && !site.languages.includes(language ?? \"\") && site.defaultLanguage) {\n setLanguageState(site.defaultLanguage);\n client.setStorefrontContext({ language: site.defaultLanguage });\n }\n // 3) Push everything into the session-context PATCH.\n await client.sessionContext.patch(\n {\n siteCode: code,\n ...(nextCurrency ? { currency: nextCurrency } : {}),\n ...(nextTarget ? { targetLocation: nextTarget } : {}),\n },\n authCtx,\n );\n } catch (e) {\n setSwitchError(e instanceof Error ? e : new Error(String(e)));\n } finally {\n setIsSwitching(false);\n }\n },\n [client, storage, qc, language],\n );\n\n const setCurrency = useCallback(\n async (next: string) => {\n // Carts are currency-bound — drop the guest cart so a fresh one is created.\n storage.setCartId(null);\n setCurrencyState(next);\n setSwitchError(null);\n // Re-bind the anonymous price context so guest pricing uses the new\n // currency even before a session/cart exists (sessionContext.patch can't).\n client.setStorefrontContext({ currency: next });\n void qc.invalidateQueries({ queryKey: [\"emporix\"] });\n setIsSwitching(true);\n try {\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n // Update an existing server session context (no-op / returns false pre-cart).\n await client.sessionContext.patch(\n { currency: next, ...(siteCode ? { siteCode } : {}) },\n authCtx,\n );\n } catch (e) {\n setSwitchError(e instanceof Error ? e : new Error(String(e)));\n } finally {\n setIsSwitching(false);\n }\n },\n [client, storage, qc, siteCode],\n );\n\n const setLanguage = useCallback(\n async (next: string) => {\n storage.setLanguage(next);\n setLanguageState(next);\n setSwitchError(null);\n // Header source — applies to anonymous + pre-session reads too.\n client.setStorefrontContext({ language: next });\n void qc.invalidateQueries({ queryKey: [\"emporix\"] });\n setIsSwitching(true);\n try {\n const token = storage.getCustomerToken();\n const authCtx = token ? auth.customer(token) : auth.anonymous();\n // Update an existing server session context (no-op / returns false pre-cart).\n await client.sessionContext.patch(\n { language: next, ...(siteCode ? { siteCode } : {}) },\n authCtx,\n );\n } catch (e) {\n setSwitchError(e instanceof Error ? e : new Error(String(e)));\n } finally {\n setIsSwitching(false);\n }\n },\n [client, storage, qc, siteCode],\n );\n\n const value = useMemo<SiteContextValue>(\n () => ({\n siteCode,\n currency,\n targetLocation,\n language,\n setSite,\n setCurrency,\n setLanguage,\n isSwitching,\n switchError,\n }),\n [siteCode, currency, targetLocation, language, setSite, setCurrency, setLanguage, isSwitching, switchError],\n );\n\n return <EmporixSiteContext.Provider value={value}>{children}</EmporixSiteContext.Provider>;\n}\n\n/** Returns the SDK client and token storage. Throws outside an {@link EmporixProvider}. */\nexport function useEmporix(): EmporixContextValue {\n const ctx = useContext(EmporixContext);\n if (!ctx) throw new Error(\"useEmporix must be used within an EmporixProvider\");\n return ctx;\n}\n"]}
@@ -2,4 +2,4 @@ import 'react';
2
2
  import '@tanstack/react-query';
3
3
  import '@viu/emporix-sdk';
4
4
  import './storage.cjs';
5
- export { E as EmporixProvider, a as EmporixProviderProps, b as EmporixSiteContext, S as SiteContextValue, u as useEmporix } from './provider-CfgMAu8B.cjs';
5
+ export { E as EmporixProvider, a as EmporixProviderProps, b as EmporixSiteContext, S as SiteContextValue, u as useEmporix } from './provider-Ddo7aSOI.cjs';
@@ -2,4 +2,4 @@ import 'react';
2
2
  import '@tanstack/react-query';
3
3
  import '@viu/emporix-sdk';
4
4
  import './storage.js';
5
- export { E as EmporixProvider, a as EmporixProviderProps, b as EmporixSiteContext, S as SiteContextValue, u as useEmporix } from './provider--y2yz3Jj.js';
5
+ export { E as EmporixProvider, a as EmporixProviderProps, b as EmporixSiteContext, S as SiteContextValue, u as useEmporix } from './provider-Bcb-tOpS.js';
package/dist/provider.js CHANGED
@@ -1,4 +1,4 @@
1
- export { EmporixProvider, EmporixSiteContext, useEmporix } from './chunk-SMJ5IQZJ.js';
2
- import './chunk-FBQY2N7S.js';
1
+ export { EmporixProvider, EmporixSiteContext, useEmporix } from './chunk-4YDWCA7A.js';
2
+ import './chunk-VMDBYVWG.js';
3
3
  //# sourceMappingURL=provider.js.map
4
4
  //# sourceMappingURL=provider.js.map