@viu/emporix-sdk-react 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1128 @@
1
+ import { useEmporix, EmporixSiteContext, useActiveCompany } from './chunk-D43CSHK3.js';
2
+ import { useContext, useState, useEffect, useCallback } from 'react';
3
+ import { useQueryClient, useQuery, useMutation, useInfiniteQuery } from '@tanstack/react-query';
4
+ import { auth, EmporixError } from '@viu/emporix-sdk';
5
+
6
+ // src/hooks/internal/bootstrap-cart.ts
7
+ async function bootstrapCart(opts) {
8
+ return opts.qc.fetchQuery({
9
+ queryKey: [
10
+ "emporix",
11
+ "cart-bootstrap",
12
+ {
13
+ tenant: opts.client.tenant,
14
+ // ctx.kind is the discriminator of AuthContext — same string as the
15
+ // legacy `authKind` param, derived directly so callers can't drift.
16
+ authKind: opts.ctx.kind,
17
+ siteCode: opts.siteCode,
18
+ ...opts.type !== void 0 ? { type: opts.type } : {},
19
+ ...opts.legalEntityId !== void 0 ? { legalEntityId: opts.legalEntityId } : {}
20
+ }
21
+ ],
22
+ queryFn: () => opts.client.carts.getCurrent(opts.ctx, {
23
+ siteCode: opts.siteCode,
24
+ ...opts.type !== void 0 ? { type: opts.type } : {},
25
+ ...opts.legalEntityId !== void 0 ? { legalEntityId: opts.legalEntityId } : {},
26
+ create: true
27
+ }),
28
+ staleTime: Infinity
29
+ });
30
+ }
31
+
32
+ // src/hooks/use-customer-session.ts
33
+ var EMPTY_SESSION = {
34
+ token: null,
35
+ refreshToken: null,
36
+ saasToken: null
37
+ };
38
+ function useCustomerSession() {
39
+ const { client, storage } = useEmporix();
40
+ const qc = useQueryClient();
41
+ const siteCtx = useContext(EmporixSiteContext);
42
+ const [session, setSession] = useState(() => ({
43
+ token: storage.getCustomerToken(),
44
+ refreshToken: null,
45
+ saasToken: null
46
+ }));
47
+ useEffect(() => {
48
+ return storage.subscribe?.((t) => setSession((s) => ({ ...s, token: t })));
49
+ }, [storage]);
50
+ const meQuery = useQuery({
51
+ queryKey: ["emporix", "customer", "me", { tenant: client.tenant, hasToken: session.token !== null }],
52
+ enabled: session.token !== null,
53
+ queryFn: () => client.customers.me(auth.customer(session.token)),
54
+ // 30s — matches Balanced default. Lets honourPreferredSite's fetchQuery
55
+ // (with staleTime: Infinity) reuse the cache instead of refetching.
56
+ staleTime: 3e4
57
+ });
58
+ const login = useCallback(
59
+ async (input) => {
60
+ const result = await client.customers.login(input);
61
+ storage.setCustomerToken(result.customerToken);
62
+ storage.setRefreshToken(result.refreshToken || null);
63
+ setSession({
64
+ token: result.customerToken,
65
+ refreshToken: result.refreshToken || null,
66
+ saasToken: result.saasToken || null
67
+ });
68
+ await onboardCustomerCart({
69
+ qc,
70
+ client,
71
+ storage,
72
+ customerToken: result.customerToken
73
+ });
74
+ await honourPreferredSite({
75
+ qc,
76
+ client,
77
+ customerToken: result.customerToken,
78
+ siteCtx
79
+ });
80
+ await qc.invalidateQueries({ queryKey: ["emporix", "customer"], refetchType: "none" });
81
+ await qc.invalidateQueries({ queryKey: ["emporix", "cart"], refetchType: "none" });
82
+ },
83
+ [client, storage, qc, siteCtx]
84
+ );
85
+ const signup = useCallback(
86
+ async (input) => {
87
+ await client.customers.signup(input);
88
+ },
89
+ [client]
90
+ );
91
+ const applySession = useCallback(
92
+ async (incoming) => {
93
+ storage.setCustomerToken(incoming.customerToken);
94
+ storage.setRefreshToken(incoming.refreshToken || null);
95
+ setSession({
96
+ token: incoming.customerToken,
97
+ refreshToken: incoming.refreshToken || null,
98
+ saasToken: incoming.saasToken || null
99
+ });
100
+ await onboardCustomerCart({
101
+ qc,
102
+ client,
103
+ storage,
104
+ customerToken: incoming.customerToken
105
+ });
106
+ await honourPreferredSite({
107
+ qc,
108
+ client,
109
+ customerToken: incoming.customerToken,
110
+ siteCtx
111
+ });
112
+ await qc.invalidateQueries({ queryKey: ["emporix", "customer"], refetchType: "none" });
113
+ await qc.invalidateQueries({ queryKey: ["emporix", "cart"], refetchType: "none" });
114
+ },
115
+ [client, storage, qc, siteCtx]
116
+ );
117
+ const socialLogin = useCallback(
118
+ async (input) => {
119
+ await applySession(await client.customers.socialLogin(input));
120
+ },
121
+ [client, applySession]
122
+ );
123
+ const exchangeToken = useCallback(
124
+ async (input) => {
125
+ await applySession(await client.customers.exchangeToken(input));
126
+ },
127
+ [client, applySession]
128
+ );
129
+ const logout = useCallback(async () => {
130
+ if (session.token) {
131
+ try {
132
+ await client.customers.logout(auth.customer(session.token));
133
+ } catch {
134
+ }
135
+ }
136
+ storage.setCustomerToken(null);
137
+ storage.setRefreshToken(null);
138
+ storage.setActiveLegalEntityId(null);
139
+ setSession(EMPTY_SESSION);
140
+ qc.removeQueries({ queryKey: ["emporix", "customer"] });
141
+ qc.removeQueries({ queryKey: ["emporix", "cart"] });
142
+ }, [client, session.token, storage, qc]);
143
+ const refresh = useCallback(async () => {
144
+ await meQuery.refetch();
145
+ }, [meQuery]);
146
+ const refreshSession = useCallback(async () => {
147
+ if (!session.refreshToken) return;
148
+ const refreshed = await client.customers.refresh({
149
+ refreshToken: session.refreshToken,
150
+ ...session.saasToken ? { saasToken: session.saasToken } : {}
151
+ });
152
+ storage.setCustomerToken(refreshed.customerToken);
153
+ if (refreshed.refreshToken) storage.setRefreshToken(refreshed.refreshToken);
154
+ setSession((s) => ({
155
+ token: refreshed.customerToken,
156
+ refreshToken: refreshed.refreshToken || s.refreshToken,
157
+ saasToken: refreshed.saasToken || s.saasToken
158
+ }));
159
+ await qc.invalidateQueries({ queryKey: ["emporix", "customer"] });
160
+ await qc.invalidateQueries({ queryKey: ["emporix", "cart"] });
161
+ }, [client, storage, qc, session.refreshToken, session.saasToken]);
162
+ return {
163
+ customerToken: session.token,
164
+ refreshToken: session.refreshToken,
165
+ customer: meQuery.data ?? null,
166
+ isAuthenticated: session.token !== null,
167
+ isLoading: meQuery.isLoading && session.token !== null,
168
+ login,
169
+ signup,
170
+ socialLogin,
171
+ exchangeToken,
172
+ logout,
173
+ refresh,
174
+ refreshSession
175
+ };
176
+ }
177
+ async function honourPreferredSite(opts) {
178
+ const { qc, client, customerToken, siteCtx } = opts;
179
+ if (!siteCtx) return;
180
+ try {
181
+ const me = await qc.fetchQuery({
182
+ queryKey: [
183
+ "emporix",
184
+ "customer",
185
+ "me",
186
+ { tenant: client.tenant, hasToken: true }
187
+ ],
188
+ queryFn: () => client.customers.me(auth.customer(customerToken)),
189
+ // Reuse whatever meQuery already wrote (login flow runs meQuery in
190
+ // parallel). Without this, fetchQuery refetches if meQuery's data is
191
+ // already stale (default staleTime: 0 on meQuery).
192
+ staleTime: Infinity
193
+ });
194
+ const preferred = me.preferredSite;
195
+ if (preferred && siteCtx.siteCode !== preferred) {
196
+ await siteCtx.setSite(preferred);
197
+ }
198
+ } catch {
199
+ }
200
+ }
201
+ async function onboardCustomerCart(opts) {
202
+ const { qc, client, storage, customerToken } = opts;
203
+ const siteCode = client.config?.credentials?.storefront?.context?.siteCode;
204
+ if (!siteCode) return;
205
+ const ctx = auth.customer(customerToken);
206
+ try {
207
+ const customerCart = await bootstrapCart({
208
+ qc,
209
+ client,
210
+ ctx,
211
+ siteCode
212
+ });
213
+ const customerCartId = customerCart?.id;
214
+ if (!customerCartId) return;
215
+ const anonCartId = storage.getCartId();
216
+ if (anonCartId && anonCartId !== customerCartId) {
217
+ await client.carts.merge(customerCartId, [anonCartId], ctx);
218
+ }
219
+ storage.setCartId(customerCartId);
220
+ } catch {
221
+ }
222
+ }
223
+ function useReadAuth(override) {
224
+ const { storage } = useEmporix();
225
+ if (override) return { ctx: override };
226
+ const token = storage.getCustomerToken();
227
+ return token ? { ctx: auth.customer(token) } : { ctx: auth.anonymous() };
228
+ }
229
+ function useCustomerOnlyCtx() {
230
+ const { storage } = useEmporix();
231
+ const token = storage.getCustomerToken();
232
+ if (!token) {
233
+ throw new Error("Requires a logged-in customer (no token in storage)");
234
+ }
235
+ return auth.customer(token);
236
+ }
237
+ function useReadSite() {
238
+ const ctx = useContext(EmporixSiteContext);
239
+ return { siteCode: ctx?.siteCode ?? null };
240
+ }
241
+
242
+ // src/hooks/internal/query-keys.ts
243
+ function emporixKey(resource, args, context) {
244
+ const meta = {
245
+ tenant: context.tenant,
246
+ authKind: context.authKind
247
+ };
248
+ if (context.siteCode !== void 0) {
249
+ meta.siteCode = context.siteCode;
250
+ }
251
+ return ["emporix", resource, ...args, meta];
252
+ }
253
+ function useEmporixInfinite(opts) {
254
+ return useInfiniteQuery({
255
+ queryKey: opts.queryKey,
256
+ initialPageParam: 1,
257
+ queryFn: ({ pageParam }) => opts.fetchPage(pageParam),
258
+ getNextPageParam: (last) => last.hasNextPage ? last.pageNumber + 1 : void 0,
259
+ ...opts.enabled !== void 0 ? { enabled: opts.enabled } : {},
260
+ ...opts.staleTime !== void 0 ? { staleTime: opts.staleTime } : {}
261
+ });
262
+ }
263
+
264
+ // src/hooks/use-products.ts
265
+ var PRODUCTS_STALE_TIME = 6e4;
266
+ function useProduct(productId, options = {}) {
267
+ const { client } = useEmporix();
268
+ const { ctx } = useReadAuth(options.auth);
269
+ const { siteCode } = useReadSite();
270
+ return useQuery({
271
+ queryKey: emporixKey("product", [productId], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
272
+ queryFn: () => client.products.get(productId, void 0, ctx),
273
+ staleTime: PRODUCTS_STALE_TIME
274
+ });
275
+ }
276
+ function useProducts(params = {}, options = {}) {
277
+ const { client } = useEmporix();
278
+ const { ctx } = useReadAuth(options.auth);
279
+ const { siteCode } = useReadSite();
280
+ return useQuery({
281
+ queryKey: emporixKey("products", [params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
282
+ queryFn: () => client.products.list(params, ctx),
283
+ staleTime: PRODUCTS_STALE_TIME
284
+ });
285
+ }
286
+ function useProductsInfinite(params = {}, options = {}) {
287
+ const { client } = useEmporix();
288
+ const { ctx } = useReadAuth(options.auth);
289
+ const { siteCode } = useReadSite();
290
+ return useEmporixInfinite({
291
+ queryKey: emporixKey("products-infinite", [params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
292
+ fetchPage: (pageNumber) => client.products.list(
293
+ params.pageSize !== void 0 ? { pageNumber, pageSize: params.pageSize } : { pageNumber },
294
+ ctx
295
+ ),
296
+ staleTime: PRODUCTS_STALE_TIME
297
+ });
298
+ }
299
+ function useProductByCode(code, options = {}) {
300
+ const { client } = useEmporix();
301
+ const { ctx } = useReadAuth(options.auth);
302
+ const { siteCode } = useReadSite();
303
+ return useQuery({
304
+ queryKey: emporixKey("product-by-code", [code], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
305
+ enabled: typeof code === "string" && code !== "",
306
+ queryFn: () => client.products.getByCode(code, ctx),
307
+ staleTime: PRODUCTS_STALE_TIME
308
+ });
309
+ }
310
+ function useProductSearch(query, params = {}, options = {}) {
311
+ const { client } = useEmporix();
312
+ const { ctx } = useReadAuth(options.auth);
313
+ const { siteCode } = useReadSite();
314
+ return useQuery({
315
+ queryKey: emporixKey("product-search", [query, params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
316
+ enabled: typeof query === "string" && query.trim() !== "",
317
+ queryFn: () => client.products.search(query, params, ctx),
318
+ staleTime: PRODUCTS_STALE_TIME
319
+ });
320
+ }
321
+ var CATEGORIES_STALE_TIME = 5 * 6e4;
322
+ function useCategory(categoryId, options = {}) {
323
+ const { client } = useEmporix();
324
+ const { ctx } = useReadAuth(options.auth);
325
+ const { siteCode } = useReadSite();
326
+ return useQuery({
327
+ queryKey: emporixKey("category", [categoryId], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
328
+ queryFn: () => client.categories.get(categoryId, ctx),
329
+ staleTime: CATEGORIES_STALE_TIME
330
+ });
331
+ }
332
+ function useCategories(params = {}, options = {}) {
333
+ const { client } = useEmporix();
334
+ const { ctx } = useReadAuth(options.auth);
335
+ const { siteCode } = useReadSite();
336
+ return useQuery({
337
+ queryKey: emporixKey("categories", [params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
338
+ queryFn: () => client.categories.list(params, ctx),
339
+ staleTime: CATEGORIES_STALE_TIME
340
+ });
341
+ }
342
+ function useCategoriesInfinite(params = {}, options = {}) {
343
+ const { client } = useEmporix();
344
+ const { ctx } = useReadAuth(options.auth);
345
+ const { siteCode } = useReadSite();
346
+ return useEmporixInfinite({
347
+ queryKey: emporixKey("categories-infinite", [params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
348
+ fetchPage: (pageNumber) => client.categories.list(
349
+ params.pageSize !== void 0 ? { pageNumber, pageSize: params.pageSize } : { pageNumber },
350
+ ctx
351
+ ),
352
+ staleTime: CATEGORIES_STALE_TIME
353
+ });
354
+ }
355
+ function useCategoryTree(rootId, options = {}) {
356
+ const { client } = useEmporix();
357
+ const { ctx } = useReadAuth(options.auth);
358
+ const { siteCode } = useReadSite();
359
+ return useQuery({
360
+ queryKey: emporixKey("category-tree", [rootId ?? null], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
361
+ queryFn: () => client.categories.tree(rootId, ctx),
362
+ staleTime: CATEGORIES_STALE_TIME
363
+ });
364
+ }
365
+ function useProductsInCategory(categoryId, params = {}, options = {}) {
366
+ const { client } = useEmporix();
367
+ const { ctx } = useReadAuth(options.auth);
368
+ const { siteCode } = useReadSite();
369
+ return useQuery({
370
+ queryKey: emporixKey("products-in-category", [categoryId, params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
371
+ enabled: typeof categoryId === "string" && categoryId !== "",
372
+ queryFn: () => client.categories.productsIn(categoryId, params, ctx),
373
+ staleTime: CATEGORIES_STALE_TIME
374
+ });
375
+ }
376
+ function useProductsInCategoryInfinite(categoryId, params = {}, options = {}) {
377
+ const { client } = useEmporix();
378
+ const { ctx } = useReadAuth(options.auth);
379
+ const { siteCode } = useReadSite();
380
+ return useEmporixInfinite({
381
+ queryKey: emporixKey("products-in-category-infinite", [categoryId, params], { tenant: client.tenant, authKind: ctx.kind, siteCode }),
382
+ enabled: typeof categoryId === "string" && categoryId !== "",
383
+ fetchPage: (pageNumber) => client.categories.productsIn(
384
+ categoryId,
385
+ params.pageSize !== void 0 ? { pageNumber, pageSize: params.pageSize } : { pageNumber },
386
+ ctx
387
+ ),
388
+ staleTime: CATEGORIES_STALE_TIME
389
+ });
390
+ }
391
+ function useCart(cartId, options = {}) {
392
+ const { client, storage } = useEmporix();
393
+ const { ctx } = useReadAuth(options.auth);
394
+ const { siteCode } = useReadSite();
395
+ const { activeCompany } = useActiveCompany();
396
+ const resolvedId = cartId ?? storage.getCartId() ?? void 0;
397
+ return useQuery({
398
+ queryKey: emporixKey(
399
+ "cart",
400
+ [resolvedId ?? null, activeCompany?.id ?? null],
401
+ { tenant: client.tenant, authKind: ctx.kind, siteCode }
402
+ ),
403
+ enabled: resolvedId !== void 0,
404
+ queryFn: () => client.carts.get(resolvedId, ctx)
405
+ });
406
+ }
407
+ function useCartMutations(cartId) {
408
+ const { client, storage } = useEmporix();
409
+ const qc = useQueryClient();
410
+ const { ctx } = useReadAuth();
411
+ const { siteCode } = useReadSite();
412
+ const { activeCompany } = useActiveCompany();
413
+ const resolveId = () => {
414
+ const id = cartId ?? storage.getCartId();
415
+ if (!id) {
416
+ throw new EmporixError(
417
+ "useCartMutations: no cartId available \u2014 pass one explicitly or call useActiveCart({ create: true }) first"
418
+ );
419
+ }
420
+ return id;
421
+ };
422
+ const keyFor = (id) => emporixKey(
423
+ "cart",
424
+ [id, activeCompany?.id ?? null],
425
+ { tenant: client.tenant, authKind: ctx.kind, siteCode }
426
+ );
427
+ function make(run, optimistic) {
428
+ return useMutation({
429
+ mutationFn: async (vars) => run(resolveId(), vars),
430
+ onMutate: async (vars) => {
431
+ const id = resolveId();
432
+ const key = keyFor(id);
433
+ await qc.cancelQueries({ queryKey: key });
434
+ const previous = qc.getQueryData(key);
435
+ if (optimistic) qc.setQueryData(key, optimistic(previous, vars));
436
+ return { previous, key };
437
+ },
438
+ onError: (_e, _v, c) => {
439
+ if (c) qc.setQueryData(c.key, c.previous);
440
+ },
441
+ onSuccess: (cart, _v, c) => {
442
+ if (c) qc.setQueryData(c.key, cart);
443
+ }
444
+ });
445
+ }
446
+ return {
447
+ addItem: make(
448
+ (id, v) => client.carts.addItem(id, v, ctx),
449
+ (prev, v) => prev ? {
450
+ ...prev,
451
+ // Optimistic placeholder; replaced by the real item on success.
452
+ items: [
453
+ ...prev.items ?? [],
454
+ {
455
+ id: `optimistic-${v.product?.id ?? "item"}`,
456
+ ...v
457
+ }
458
+ ]
459
+ } : prev
460
+ ),
461
+ updateItem: make((id, v) => client.carts.updateItem(id, v.itemId, v.patch, ctx)),
462
+ removeItem: make(
463
+ (id, v) => client.carts.removeItem(id, v.itemId, ctx),
464
+ (prev, v) => prev ? { ...prev, items: (prev.items ?? []).filter((i) => i.id !== v.itemId) } : prev
465
+ ),
466
+ clear: make(
467
+ (id) => client.carts.clear(id, ctx),
468
+ (prev) => prev ? { ...prev, items: [] } : prev
469
+ ),
470
+ applyCoupon: make((id, v) => client.carts.applyCoupon(id, v.code, ctx)),
471
+ removeCoupon: make((id, v) => client.carts.removeCoupon(id, v.code, ctx)),
472
+ setShippingAddress: make((id, v) => client.carts.setShippingAddress(id, v, ctx)),
473
+ setBillingAddress: make((id, v) => client.carts.setBillingAddress(id, v, ctx))
474
+ };
475
+ }
476
+ function useCreateCart() {
477
+ const { client, storage } = useEmporix();
478
+ const qc = useQueryClient();
479
+ const { ctx } = useReadAuth();
480
+ return useMutation({
481
+ mutationFn: (input) => client.carts.create(input, ctx),
482
+ onSuccess: async (cart) => {
483
+ if (cart.cartId) storage.setCartId(cart.cartId);
484
+ await qc.invalidateQueries({ queryKey: ["emporix", "cart"] });
485
+ }
486
+ });
487
+ }
488
+ function useActiveCart(opts) {
489
+ const { client, storage } = useEmporix();
490
+ const qc = useQueryClient();
491
+ const { ctx } = useReadAuth(opts?.auth);
492
+ const { siteCode: activeSite } = useReadSite();
493
+ const { activeCompany } = useActiveCompany();
494
+ const [cartId, setCartId] = useState(() => storage.getCartId());
495
+ const effectiveLegalEntityId = opts?.legalEntityId ?? activeCompany?.id;
496
+ useEffect(() => {
497
+ if (cartId !== null) return;
498
+ if (!opts?.create) return;
499
+ const siteCode = activeSite ?? client.config?.credentials?.storefront?.context?.siteCode;
500
+ if (!siteCode) return;
501
+ let cancelled = false;
502
+ bootstrapCart({
503
+ qc,
504
+ client,
505
+ ctx,
506
+ siteCode,
507
+ ...opts.type !== void 0 ? { type: opts.type } : {},
508
+ ...effectiveLegalEntityId !== void 0 ? { legalEntityId: effectiveLegalEntityId } : {}
509
+ }).then((cart) => {
510
+ if (cancelled) return;
511
+ if (cart?.id) {
512
+ storage.setCartId(cart.id);
513
+ setCartId(cart.id);
514
+ }
515
+ }).catch(() => {
516
+ });
517
+ return () => {
518
+ cancelled = true;
519
+ };
520
+ }, [cartId, opts?.create, opts?.type, effectiveLegalEntityId, ctx.kind, activeSite]);
521
+ const inner = useCart(cartId ?? void 0, opts?.auth ? { auth: opts.auth } : {});
522
+ const data = cartId === null ? null : inner.data;
523
+ return { ...inner, data };
524
+ }
525
+ var PAYMENT_MODES_STALE_TIME = 10 * 6e4;
526
+ function customerOnlyCtx(token) {
527
+ if (!token) throw new Error("usePaymentModes requires a logged-in customer token");
528
+ return auth.customer(token);
529
+ }
530
+ function useCheckout() {
531
+ const { client } = useEmporix();
532
+ const { ctx } = useReadAuth();
533
+ const { activeCompany } = useActiveCompany();
534
+ const withLE = (input) => {
535
+ if (!activeCompany?.id) return input;
536
+ if ("legalEntityId" in input) return input;
537
+ return { ...input, legalEntityId: activeCompany.id };
538
+ };
539
+ const placeOrder = useMutation({
540
+ mutationFn: (v) => client.checkout.placeOrder(withLE(v.input), ctx, {
541
+ ...v.saasToken !== void 0 ? { saasToken: v.saasToken } : {},
542
+ ...v.siteCode !== void 0 ? { siteCode: v.siteCode } : {}
543
+ })
544
+ });
545
+ const placeOrderFromQuote = useMutation({
546
+ mutationFn: (v) => client.checkout.placeOrderFromQuote(withLE(v.input), ctx, {
547
+ ...v.saasToken !== void 0 ? { saasToken: v.saasToken } : {},
548
+ ...v.siteCode !== void 0 ? { siteCode: v.siteCode } : {}
549
+ })
550
+ });
551
+ return { placeOrder, placeOrderFromQuote };
552
+ }
553
+ function usePaymentModes(options = {}) {
554
+ const { client, storage } = useEmporix();
555
+ const token = storage.getCustomerToken();
556
+ const { siteCode } = useReadSite();
557
+ const { activeCompany } = useActiveCompany();
558
+ return useQuery({
559
+ queryKey: emporixKey(
560
+ "payment-modes",
561
+ [activeCompany?.id ?? null],
562
+ { tenant: client.tenant, authKind: "customer", siteCode }
563
+ ),
564
+ enabled: (options.enabled ?? true) && token !== null,
565
+ queryFn: () => client.payments.listPaymentModes(customerOnlyCtx(token)),
566
+ staleTime: PAYMENT_MODES_STALE_TIME
567
+ });
568
+ }
569
+ var PRICES_STALE_TIME = 6e4;
570
+ function useMatchPrices(input, options = {}) {
571
+ const { client } = useEmporix();
572
+ const { siteCode } = useReadSite();
573
+ const ctx = options.customerToken ? auth.customer(options.customerToken) : auth.anonymous();
574
+ return useQuery({
575
+ queryKey: [
576
+ "emporix",
577
+ "match-prices",
578
+ { tenant: client.tenant, input, anon: !options.customerToken, siteCode }
579
+ ],
580
+ enabled: (options.enabled ?? true) && (input.items?.length ?? 0) > 0,
581
+ queryFn: () => client.prices.matchByContext(input, ctx),
582
+ staleTime: PRICES_STALE_TIME
583
+ });
584
+ }
585
+
586
+ // src/hooks/use-product-media.ts
587
+ function useProductMedia(productId) {
588
+ const q = useProduct(productId);
589
+ const data = q.data?.productMedia;
590
+ return { data, isLoading: q.isLoading, error: q.error };
591
+ }
592
+ var SEGMENTS_STALE_TIME = 5 * 6e4;
593
+ function customerCtx(token) {
594
+ if (!token) throw new Error("requires a customer token in storage");
595
+ return auth.customer(token);
596
+ }
597
+ function useMySegments(query = {}) {
598
+ const { client, storage } = useEmporix();
599
+ const token = storage.getCustomerToken();
600
+ const { siteCode } = useReadSite();
601
+ return useQuery({
602
+ queryKey: ["emporix", "segment", "list", { tenant: client.tenant, query, siteCode }],
603
+ enabled: token !== null,
604
+ queryFn: () => client.segments.list(query, customerCtx(token)),
605
+ staleTime: SEGMENTS_STALE_TIME
606
+ });
607
+ }
608
+ function useMySegmentItems(query = {}) {
609
+ const { client, storage } = useEmporix();
610
+ const token = storage.getCustomerToken();
611
+ const { siteCode } = useReadSite();
612
+ return useQuery({
613
+ queryKey: ["emporix", "segment", "items", { tenant: client.tenant, query, siteCode }],
614
+ enabled: token !== null,
615
+ queryFn: () => client.segments.listItems(query, customerCtx(token)),
616
+ staleTime: SEGMENTS_STALE_TIME
617
+ });
618
+ }
619
+ function useMySegmentCategoryTree(query = {}) {
620
+ const { client, storage } = useEmporix();
621
+ const token = storage.getCustomerToken();
622
+ const { siteCode } = useReadSite();
623
+ return useQuery({
624
+ queryKey: ["emporix", "segment", "categoryTree", { tenant: client.tenant, query, siteCode }],
625
+ enabled: token !== null,
626
+ queryFn: () => client.segments.getCategoryTree(query, customerCtx(token)),
627
+ staleTime: SEGMENTS_STALE_TIME
628
+ });
629
+ }
630
+ function useMySegmentProducts(query = {}) {
631
+ const { client, storage } = useEmporix();
632
+ const token = storage.getCustomerToken();
633
+ const { siteCode } = useReadSite();
634
+ return useQuery({
635
+ queryKey: ["emporix", "segment", "myProducts", { tenant: client.tenant, query, siteCode }],
636
+ enabled: token !== null,
637
+ queryFn: () => client.segments.listMyProducts(query, customerCtx(token)),
638
+ staleTime: SEGMENTS_STALE_TIME
639
+ });
640
+ }
641
+ function useMySegmentProductsInfinite(query = {}) {
642
+ const { client, storage } = useEmporix();
643
+ const token = storage.getCustomerToken();
644
+ const { siteCode } = useReadSite();
645
+ return useEmporixInfinite({
646
+ queryKey: [
647
+ "emporix",
648
+ "segment",
649
+ "myProductsInfinite",
650
+ { tenant: client.tenant, query, siteCode }
651
+ ],
652
+ enabled: token !== null,
653
+ fetchPage: (pageNumber) => client.segments.listMyProducts(
654
+ { ...query, pageNumber, pageSize: query.pageSize ?? 20 },
655
+ customerCtx(token)
656
+ ),
657
+ staleTime: SEGMENTS_STALE_TIME
658
+ });
659
+ }
660
+ function useMySegmentCategories(query = {}) {
661
+ const { client, storage } = useEmporix();
662
+ const token = storage.getCustomerToken();
663
+ const { siteCode } = useReadSite();
664
+ return useQuery({
665
+ queryKey: ["emporix", "segment", "myCategories", { tenant: client.tenant, query, siteCode }],
666
+ enabled: token !== null,
667
+ queryFn: () => client.segments.listMyCategories(query, customerCtx(token)),
668
+ staleTime: SEGMENTS_STALE_TIME
669
+ });
670
+ }
671
+ function useMySegmentCategoriesInfinite(query = {}) {
672
+ const { client, storage } = useEmporix();
673
+ const token = storage.getCustomerToken();
674
+ const { siteCode } = useReadSite();
675
+ return useEmporixInfinite({
676
+ queryKey: [
677
+ "emporix",
678
+ "segment",
679
+ "myCategoriesInfinite",
680
+ { tenant: client.tenant, query, siteCode }
681
+ ],
682
+ enabled: token !== null,
683
+ fetchPage: (pageNumber) => client.segments.listMyCategories(
684
+ { ...query, pageNumber, pageSize: query.pageSize ?? 20 },
685
+ customerCtx(token)
686
+ ),
687
+ staleTime: SEGMENTS_STALE_TIME
688
+ });
689
+ }
690
+ function useUpdateCustomer() {
691
+ const { client } = useEmporix();
692
+ const ctx = useCustomerOnlyCtx();
693
+ const qc = useQueryClient();
694
+ return useMutation({
695
+ mutationFn: (patch) => client.customers.update(patch, ctx),
696
+ onSuccess: () => {
697
+ void qc.invalidateQueries({ queryKey: ["emporix", "customer", "me"] });
698
+ }
699
+ });
700
+ }
701
+ function useChangePassword() {
702
+ const { client } = useEmporix();
703
+ const ctx = useCustomerOnlyCtx();
704
+ return useMutation({
705
+ mutationFn: (input) => client.customers.changePassword(input, ctx)
706
+ });
707
+ }
708
+ var ADDRESSES_KEY = ["emporix", "customer", "addresses"];
709
+ function useCustomerAddresses(options = {}) {
710
+ const { client, storage } = useEmporix();
711
+ const token = storage.getCustomerToken();
712
+ const { activeCompany } = useActiveCompany();
713
+ const ctx = options.auth ?? (token ? auth.customer(token) : null);
714
+ return useQuery({
715
+ queryKey: [
716
+ ...ADDRESSES_KEY,
717
+ { tenant: client.tenant, hasToken: token !== null, legalEntityId: activeCompany?.id ?? null }
718
+ ],
719
+ enabled: ctx !== null,
720
+ queryFn: () => client.customers.addresses.list(ctx)
721
+ });
722
+ }
723
+ function useAddressMutations() {
724
+ const { client } = useEmporix();
725
+ const ctx = useCustomerOnlyCtx();
726
+ const qc = useQueryClient();
727
+ const invalidate = () => {
728
+ void qc.invalidateQueries({ queryKey: ADDRESSES_KEY });
729
+ };
730
+ return {
731
+ add: useMutation({
732
+ mutationFn: (input) => client.customers.addresses.add(input, ctx),
733
+ onSuccess: invalidate
734
+ }),
735
+ update: useMutation({
736
+ mutationFn: ({ id, patch }) => client.customers.addresses.update(id, patch, ctx),
737
+ onSuccess: invalidate
738
+ }),
739
+ remove: useMutation({
740
+ mutationFn: ({ id }) => client.customers.addresses.remove(id, ctx),
741
+ onSuccess: invalidate
742
+ })
743
+ };
744
+ }
745
+ function usePasswordReset() {
746
+ const { client } = useEmporix();
747
+ const anonCtx = auth.anonymous();
748
+ return {
749
+ request: useMutation({
750
+ mutationFn: (input) => client.customers.requestPasswordReset(input, anonCtx)
751
+ }),
752
+ confirm: useMutation({
753
+ mutationFn: (input) => client.customers.confirmPasswordReset(input, anonCtx)
754
+ })
755
+ };
756
+ }
757
+ var SITES_STALE_TIME = 10 * 6e4;
758
+ function useSites(options = {}) {
759
+ const { client } = useEmporix();
760
+ const { ctx } = useReadAuth(options.auth);
761
+ return useQuery({
762
+ queryKey: emporixKey("sites", [], { tenant: client.tenant, authKind: ctx.kind }),
763
+ queryFn: () => client.sites.list(ctx),
764
+ staleTime: SITES_STALE_TIME
765
+ });
766
+ }
767
+ function useDefaultSite(options = {}) {
768
+ const { client } = useEmporix();
769
+ const { ctx } = useReadAuth(options.auth);
770
+ return useQuery({
771
+ queryKey: emporixKey("site-default", [], { tenant: client.tenant, authKind: ctx.kind }),
772
+ queryFn: () => client.sites.current(ctx),
773
+ staleTime: SITES_STALE_TIME
774
+ });
775
+ }
776
+ function useSiteContext() {
777
+ const ctx = useContext(EmporixSiteContext);
778
+ if (!ctx) {
779
+ throw new Error("useSiteContext must be used within an EmporixProvider");
780
+ }
781
+ return ctx;
782
+ }
783
+ function useMyCompanies() {
784
+ const { client, storage } = useEmporix();
785
+ const token = storage.getCustomerToken();
786
+ return useQuery({
787
+ queryKey: emporixKey("companies", ["mine"], {
788
+ tenant: client.tenant,
789
+ authKind: token ? "customer" : "anonymous"
790
+ }),
791
+ enabled: token !== null,
792
+ queryFn: () => client.companies.listMine(auth.customer(token))
793
+ });
794
+ }
795
+ function useCompany(legalEntityId) {
796
+ const { client, storage } = useEmporix();
797
+ const token = storage.getCustomerToken();
798
+ return useQuery({
799
+ queryKey: emporixKey("companies", [legalEntityId ?? null], {
800
+ tenant: client.tenant,
801
+ authKind: token ? "customer" : "anonymous"
802
+ }),
803
+ enabled: token !== null && legalEntityId !== void 0,
804
+ queryFn: () => client.companies.get(legalEntityId, auth.customer(token))
805
+ });
806
+ }
807
+ function useCompanyContacts(legalEntityId) {
808
+ const { client, storage } = useEmporix();
809
+ const token = storage.getCustomerToken();
810
+ return useQuery({
811
+ queryKey: emporixKey("companies", ["contacts", legalEntityId ?? null], {
812
+ tenant: client.tenant,
813
+ authKind: token ? "customer" : "anonymous"
814
+ }),
815
+ enabled: token !== null && legalEntityId !== void 0,
816
+ queryFn: () => client.contacts.listForCompany(legalEntityId, auth.customer(token))
817
+ });
818
+ }
819
+ function useCompanyLocations(legalEntityId) {
820
+ const { client, storage } = useEmporix();
821
+ const token = storage.getCustomerToken();
822
+ return useQuery({
823
+ queryKey: emporixKey("companies", ["locations", legalEntityId ?? null], {
824
+ tenant: client.tenant,
825
+ authKind: token ? "customer" : "anonymous"
826
+ }),
827
+ enabled: token !== null && legalEntityId !== void 0,
828
+ queryFn: () => client.locations.listForCompany(legalEntityId, auth.customer(token))
829
+ });
830
+ }
831
+ function useCompanyGroups(legalEntityId) {
832
+ const { client, storage } = useEmporix();
833
+ const token = storage.getCustomerToken();
834
+ return useQuery({
835
+ queryKey: emporixKey("companies", ["groups", legalEntityId ?? null], {
836
+ tenant: client.tenant,
837
+ authKind: token ? "customer" : "anonymous"
838
+ }),
839
+ enabled: token !== null && legalEntityId !== void 0,
840
+ queryFn: () => client.customerGroups.listForCompany(legalEntityId, auth.customer(token))
841
+ });
842
+ }
843
+ function useCustomerAuthResolver() {
844
+ const { storage } = useEmporix();
845
+ return () => {
846
+ const token = storage.getCustomerToken();
847
+ if (!token) throw new Error("Mutation requires a logged-in customer token");
848
+ return auth.customer(token);
849
+ };
850
+ }
851
+ function useCreateCompany() {
852
+ const { client } = useEmporix();
853
+ const resolveAuth = useCustomerAuthResolver();
854
+ const qc = useQueryClient();
855
+ return useMutation({
856
+ mutationFn: (input) => client.companies.create(input, resolveAuth()),
857
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["emporix", "companies", "mine"] })
858
+ });
859
+ }
860
+ function useUpdateCompany() {
861
+ const { client } = useEmporix();
862
+ const resolveAuth = useCustomerAuthResolver();
863
+ const qc = useQueryClient();
864
+ return useMutation({
865
+ mutationFn: ({ id, patch }) => client.companies.update(id, patch, resolveAuth()),
866
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["emporix", "companies"] })
867
+ });
868
+ }
869
+ function useDeleteCompany() {
870
+ const { client } = useEmporix();
871
+ const resolveAuth = useCustomerAuthResolver();
872
+ const qc = useQueryClient();
873
+ return useMutation({
874
+ mutationFn: (id) => client.companies.delete(id, resolveAuth()),
875
+ onSuccess: () => qc.invalidateQueries({ queryKey: ["emporix", "companies"] })
876
+ });
877
+ }
878
+ function useAssignContact() {
879
+ const { client } = useEmporix();
880
+ const resolveAuth = useCustomerAuthResolver();
881
+ const qc = useQueryClient();
882
+ return useMutation({
883
+ mutationFn: (input) => client.contacts.assign(input, resolveAuth()),
884
+ onSuccess: () => qc.invalidateQueries({ predicate: (q) => q.queryKey.includes("contacts") })
885
+ });
886
+ }
887
+ function useUpdateContactAssignment() {
888
+ const { client } = useEmporix();
889
+ const resolveAuth = useCustomerAuthResolver();
890
+ const qc = useQueryClient();
891
+ return useMutation({
892
+ mutationFn: ({ id, patch }) => client.contacts.update(id, patch, resolveAuth()),
893
+ onSuccess: () => qc.invalidateQueries({ predicate: (q) => q.queryKey.includes("contacts") })
894
+ });
895
+ }
896
+ function useUnassignContact() {
897
+ const { client } = useEmporix();
898
+ const resolveAuth = useCustomerAuthResolver();
899
+ const qc = useQueryClient();
900
+ return useMutation({
901
+ mutationFn: (id) => client.contacts.unassign(id, resolveAuth()),
902
+ onSuccess: () => qc.invalidateQueries({ predicate: (q) => q.queryKey.includes("contacts") })
903
+ });
904
+ }
905
+ function useCreateLocation() {
906
+ const { client } = useEmporix();
907
+ const resolveAuth = useCustomerAuthResolver();
908
+ const qc = useQueryClient();
909
+ return useMutation({
910
+ mutationFn: (input) => client.locations.create(input, resolveAuth()),
911
+ onSuccess: () => qc.invalidateQueries({ predicate: (q) => q.queryKey.includes("locations") })
912
+ });
913
+ }
914
+ function useUpdateLocation() {
915
+ const { client } = useEmporix();
916
+ const resolveAuth = useCustomerAuthResolver();
917
+ const qc = useQueryClient();
918
+ return useMutation({
919
+ mutationFn: ({ id, patch }) => client.locations.update(id, patch, resolveAuth()),
920
+ onSuccess: () => qc.invalidateQueries({ predicate: (q) => q.queryKey.includes("locations") })
921
+ });
922
+ }
923
+ function useDeleteLocation() {
924
+ const { client } = useEmporix();
925
+ const resolveAuth = useCustomerAuthResolver();
926
+ const qc = useQueryClient();
927
+ return useMutation({
928
+ mutationFn: (id) => client.locations.delete(id, resolveAuth()),
929
+ onSuccess: () => qc.invalidateQueries({ predicate: (q) => q.queryKey.includes("locations") })
930
+ });
931
+ }
932
+ function useCompanySwitcher() {
933
+ const ctx = useActiveCompany();
934
+ const switchFn = useCallback(
935
+ (legalEntityId) => ctx.setActiveCompany(legalEntityId),
936
+ [ctx]
937
+ );
938
+ const clearFn = useCallback(() => ctx.setActiveCompany(null), [ctx]);
939
+ return {
940
+ companies: ctx.myCompanies,
941
+ active: ctx.activeCompany,
942
+ status: ctx.status,
943
+ switch: switchFn,
944
+ clear: clearFn
945
+ };
946
+ }
947
+ function useMyOrders(options = {}) {
948
+ const { client, storage } = useEmporix();
949
+ const { activeCompany } = useActiveCompany();
950
+ const { siteCode } = useReadSite();
951
+ const token = storage.getCustomerToken();
952
+ const effectiveLE = options.legalEntityId === null ? void 0 : options.legalEntityId ?? activeCompany?.id;
953
+ return useQuery({
954
+ queryKey: emporixKey(
955
+ "orders",
956
+ ["mine", effectiveLE ?? null, options.status ?? null, options.pageNumber ?? 1, options.pageSize ?? null],
957
+ { tenant: client.tenant, authKind: token ? "customer" : "anonymous", siteCode }
958
+ ),
959
+ enabled: token !== null,
960
+ queryFn: () => client.orders.listMine(auth.customer(token), {
961
+ ...options.pageNumber !== void 0 ? { pageNumber: options.pageNumber } : {},
962
+ ...options.pageSize !== void 0 ? { pageSize: options.pageSize } : {},
963
+ ...options.status !== void 0 ? { status: options.status } : {},
964
+ ...effectiveLE !== void 0 ? { legalEntityId: effectiveLE } : {},
965
+ ...siteCode ? { siteCode } : {},
966
+ ...options.saasToken !== void 0 ? { saasToken: options.saasToken } : {}
967
+ })
968
+ });
969
+ }
970
+ function useMyOrdersInfinite(options = {}) {
971
+ const { client, storage } = useEmporix();
972
+ const { activeCompany } = useActiveCompany();
973
+ const { siteCode } = useReadSite();
974
+ const token = storage.getCustomerToken();
975
+ const effectiveLE = options.legalEntityId === null ? void 0 : options.legalEntityId ?? activeCompany?.id;
976
+ return useEmporixInfinite({
977
+ queryKey: emporixKey(
978
+ "orders",
979
+ ["mine-infinite", effectiveLE ?? null, options.status ?? null, options.pageSize ?? null],
980
+ { tenant: client.tenant, authKind: token ? "customer" : "anonymous", siteCode }
981
+ ),
982
+ enabled: token !== null,
983
+ fetchPage: (pageNumber) => client.orders.listMine(auth.customer(token), {
984
+ pageNumber,
985
+ ...options.pageSize !== void 0 ? { pageSize: options.pageSize } : {},
986
+ ...options.status !== void 0 ? { status: options.status } : {},
987
+ ...effectiveLE !== void 0 ? { legalEntityId: effectiveLE } : {},
988
+ ...siteCode ? { siteCode } : {},
989
+ ...options.saasToken !== void 0 ? { saasToken: options.saasToken } : {}
990
+ })
991
+ });
992
+ }
993
+ function useOrder(orderId, options = {}) {
994
+ const { client, storage } = useEmporix();
995
+ const token = storage.getCustomerToken();
996
+ return useQuery({
997
+ queryKey: emporixKey("orders", [orderId ?? null], {
998
+ tenant: client.tenant,
999
+ authKind: token ? "customer" : "anonymous"
1000
+ }),
1001
+ enabled: token !== null && orderId !== void 0,
1002
+ queryFn: () => client.orders.get(
1003
+ orderId,
1004
+ auth.customer(token),
1005
+ options.saasToken ? { saasToken: options.saasToken } : {}
1006
+ )
1007
+ });
1008
+ }
1009
+ function useCancelOrder() {
1010
+ const { client, storage } = useEmporix();
1011
+ const qc = useQueryClient();
1012
+ return useMutation({
1013
+ mutationKey: ["emporix", "orders", "cancel"],
1014
+ mutationFn: async (input) => {
1015
+ const token = storage.getCustomerToken();
1016
+ if (!token) throw new Error("useCancelOrder: requires a logged-in customer");
1017
+ const { orderId, saasToken } = typeof input === "string" ? { orderId: input, saasToken: void 0 } : input;
1018
+ await client.orders.cancel(
1019
+ orderId,
1020
+ auth.customer(token),
1021
+ saasToken ? { saasToken } : {}
1022
+ );
1023
+ },
1024
+ onSuccess: () => qc.invalidateQueries({
1025
+ predicate: (q) => Array.isArray(q.queryKey) && q.queryKey[1] === "orders"
1026
+ })
1027
+ });
1028
+ }
1029
+ function useOrderTransition() {
1030
+ const { client, storage } = useEmporix();
1031
+ const qc = useQueryClient();
1032
+ return useMutation({
1033
+ mutationKey: ["emporix", "orders", "transition"],
1034
+ mutationFn: async ({ orderId, status, comment, saasToken }) => {
1035
+ const token = storage.getCustomerToken();
1036
+ if (!token) throw new Error("useOrderTransition: requires a logged-in customer");
1037
+ await client.orders.transition(
1038
+ orderId,
1039
+ status,
1040
+ auth.customer(token),
1041
+ {
1042
+ ...comment !== void 0 ? { comment } : {},
1043
+ ...saasToken !== void 0 ? { saasToken } : {}
1044
+ }
1045
+ );
1046
+ },
1047
+ onSuccess: () => qc.invalidateQueries({
1048
+ predicate: (q) => Array.isArray(q.queryKey) && q.queryKey[1] === "orders"
1049
+ })
1050
+ });
1051
+ }
1052
+ function useReorder() {
1053
+ const { client, storage } = useEmporix();
1054
+ const qc = useQueryClient();
1055
+ return useMutation({
1056
+ mutationKey: ["emporix", "orders", "reorder"],
1057
+ mutationFn: async ({ orderId, saasToken }) => {
1058
+ const token = storage.getCustomerToken();
1059
+ if (!token) throw new Error("useReorder: requires a logged-in customer");
1060
+ const ctx = auth.customer(token);
1061
+ const order = await qc.fetchQuery({
1062
+ queryKey: emporixKey("orders", [orderId], { tenant: client.tenant, authKind: ctx.kind }),
1063
+ queryFn: () => client.orders.get(orderId, ctx, saasToken ? { saasToken } : {})
1064
+ });
1065
+ const cartId = storage.getCartId();
1066
+ if (!cartId) throw new Error("useReorder: no active cart id in storage");
1067
+ if (order.items.length === 0) return { added: 0, errors: [] };
1068
+ const batchBody = order.items.map((item) => ({
1069
+ product: { id: item.productId },
1070
+ quantity: item.quantity
1071
+ }));
1072
+ const res = await client.carts.addItemsBatch(cartId, batchBody, ctx);
1073
+ let added = 0;
1074
+ const errors = [];
1075
+ for (const entry of res) {
1076
+ if (entry.status >= 200 && entry.status < 300) {
1077
+ added += 1;
1078
+ } else {
1079
+ errors.push(
1080
+ new Error(
1081
+ `addItemsBatch entry ${entry.index ?? "?"}: status=${entry.status}${entry.errorMessage ? " " + entry.errorMessage : ""}`
1082
+ )
1083
+ );
1084
+ }
1085
+ }
1086
+ return { added, errors };
1087
+ },
1088
+ onSuccess: () => {
1089
+ qc.invalidateQueries({ predicate: (q) => Array.isArray(q.queryKey) && q.queryKey[1] === "cart" });
1090
+ }
1091
+ });
1092
+ }
1093
+ function useSalesOrder(orderId, authCtx) {
1094
+ const { client } = useEmporix();
1095
+ return useQuery({
1096
+ queryKey: emporixKey("salesorders", [orderId ?? null], {
1097
+ tenant: client.tenant,
1098
+ authKind: authCtx?.kind ?? "anonymous"
1099
+ }),
1100
+ enabled: orderId !== void 0 && authCtx !== void 0,
1101
+ queryFn: () => client.salesOrders.get(orderId, authCtx)
1102
+ });
1103
+ }
1104
+ function useUpdateSalesOrder() {
1105
+ const { client } = useEmporix();
1106
+ const qc = useQueryClient();
1107
+ return useMutation({
1108
+ mutationKey: ["emporix", "salesorders", "update"],
1109
+ mutationFn: async ({ orderId, patch, auth: auth20, recalculate }) => {
1110
+ if (!auth20) throw new Error("useUpdateSalesOrder: requires an auth context");
1111
+ return client.salesOrders.update(
1112
+ orderId,
1113
+ patch,
1114
+ auth20,
1115
+ recalculate !== void 0 ? { recalculate } : {}
1116
+ );
1117
+ },
1118
+ onSuccess: (_data, vars) => {
1119
+ qc.invalidateQueries({
1120
+ predicate: (q) => Array.isArray(q.queryKey) && (q.queryKey[1] === "salesorders" || q.queryKey[1] === "orders" && q.queryKey[2] === vars.orderId)
1121
+ });
1122
+ }
1123
+ });
1124
+ }
1125
+
1126
+ export { useActiveCart, useAddressMutations, useAssignContact, useCancelOrder, useCart, useCartMutations, useCategories, useCategoriesInfinite, useCategory, useCategoryTree, useChangePassword, useCheckout, useCompany, useCompanyContacts, useCompanyGroups, useCompanyLocations, useCompanySwitcher, useCreateCart, useCreateCompany, useCreateLocation, useCustomerAddresses, useCustomerSession, useDefaultSite, useDeleteCompany, useDeleteLocation, useMatchPrices, useMyCompanies, useMyOrders, useMyOrdersInfinite, useMySegmentCategories, useMySegmentCategoriesInfinite, useMySegmentCategoryTree, useMySegmentItems, useMySegmentProducts, useMySegmentProductsInfinite, useMySegments, useOrder, useOrderTransition, usePasswordReset, usePaymentModes, useProduct, useProductByCode, useProductMedia, useProductSearch, useProducts, useProductsInCategory, useProductsInCategoryInfinite, useProductsInfinite, useReorder, useSalesOrder, useSiteContext, useSites, useUnassignContact, useUpdateCompany, useUpdateContactAssignment, useUpdateCustomer, useUpdateLocation, useUpdateSalesOrder };
1127
+ //# sourceMappingURL=chunk-N3VDSKCT.js.map
1128
+ //# sourceMappingURL=chunk-N3VDSKCT.js.map