@storepecker/storefront-core 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.
Files changed (80) hide show
  1. package/dist/address-DQEZzyUG.d.cts +19 -0
  2. package/dist/address-DQEZzyUG.d.ts +19 -0
  3. package/dist/api/index.cjs +367 -0
  4. package/dist/api/index.d.cts +127 -0
  5. package/dist/api/index.d.ts +127 -0
  6. package/dist/api/index.js +131 -0
  7. package/dist/booking-ClX-dhX7.d.ts +131 -0
  8. package/dist/booking-Drl7-IL7.d.cts +131 -0
  9. package/dist/cart-DVrn8hD0.d.cts +367 -0
  10. package/dist/cart-DVrn8hD0.d.ts +367 -0
  11. package/dist/checkout/index.cjs +228 -0
  12. package/dist/checkout/index.d.cts +251 -0
  13. package/dist/checkout/index.d.ts +251 -0
  14. package/dist/checkout/index.js +122 -0
  15. package/dist/chunk-4CVKE6CC.cjs +312 -0
  16. package/dist/chunk-4MJMNIAB.js +123 -0
  17. package/dist/chunk-57ZBPAXU.js +114 -0
  18. package/dist/chunk-7MQOVWLW.js +40 -0
  19. package/dist/chunk-7UH6REGV.js +12 -0
  20. package/dist/chunk-A6IO3HIN.cjs +151 -0
  21. package/dist/chunk-CFF632IQ.cjs +100 -0
  22. package/dist/chunk-CSISWBZZ.js +121 -0
  23. package/dist/chunk-E7WPE3PV.js +98 -0
  24. package/dist/chunk-FLBJKM6G.js +83 -0
  25. package/dist/chunk-GB3HW6DM.js +61 -0
  26. package/dist/chunk-GH75WUO2.cjs +42 -0
  27. package/dist/chunk-GQTO5ZE2.cjs +96 -0
  28. package/dist/chunk-JHGXIIBZ.cjs +37 -0
  29. package/dist/chunk-JQMLHRWL.js +21 -0
  30. package/dist/chunk-K44JZ5NL.cjs +212 -0
  31. package/dist/chunk-K5B62JZO.cjs +154 -0
  32. package/dist/chunk-KWKHHRRJ.cjs +15 -0
  33. package/dist/chunk-LBGIUNM6.js +30 -0
  34. package/dist/chunk-LC5ZAUKL.cjs +220 -0
  35. package/dist/chunk-N3CTXRFT.cjs +30 -0
  36. package/dist/chunk-QCQCFYYR.js +206 -0
  37. package/dist/chunk-QM73PI5L.cjs +218 -0
  38. package/dist/chunk-RCXQJ6XC.cjs +131 -0
  39. package/dist/chunk-UM24VDYB.js +216 -0
  40. package/dist/chunk-UXLUE3HW.cjs +69 -0
  41. package/dist/chunk-WFDKKOO7.js +210 -0
  42. package/dist/chunk-YUPBTD4M.js +289 -0
  43. package/dist/components/index.cjs +576 -0
  44. package/dist/components/index.css +329 -0
  45. package/dist/components/index.d.cts +58 -0
  46. package/dist/components/index.d.ts +58 -0
  47. package/dist/components/index.js +569 -0
  48. package/dist/coupons-C7iJ-1cy.d.cts +37 -0
  49. package/dist/coupons-CnfTKL1i.d.ts +37 -0
  50. package/dist/hooks/index.cjs +381 -0
  51. package/dist/hooks/index.d.cts +123 -0
  52. package/dist/hooks/index.d.ts +123 -0
  53. package/dist/hooks/index.js +364 -0
  54. package/dist/index-DTyIuLuI.d.cts +18 -0
  55. package/dist/index-DTyIuLuI.d.ts +18 -0
  56. package/dist/index.cjs +14 -0
  57. package/dist/index.d.cts +1 -0
  58. package/dist/index.d.ts +1 -0
  59. package/dist/index.js +1 -0
  60. package/dist/models/index.cjs +2 -0
  61. package/dist/models/index.d.cts +53 -0
  62. package/dist/models/index.d.ts +53 -0
  63. package/dist/models/index.js +1 -0
  64. package/dist/orders-CkUzv5Xh.d.ts +159 -0
  65. package/dist/orders-D0oC9ugD.d.cts +159 -0
  66. package/dist/phonepe-D7vn-9SR.d.cts +27 -0
  67. package/dist/phonepe-D7vn-9SR.d.ts +27 -0
  68. package/dist/store/index.cjs +149 -0
  69. package/dist/store/index.d.cts +46 -0
  70. package/dist/store/index.d.ts +46 -0
  71. package/dist/store/index.js +141 -0
  72. package/dist/useAddressForm-C-Uzug4d.d.ts +42 -0
  73. package/dist/useAddressForm-DgkCP1nG.d.cts +42 -0
  74. package/dist/utils/index.cjs +535 -0
  75. package/dist/utils/index.d.cts +507 -0
  76. package/dist/utils/index.d.ts +507 -0
  77. package/dist/utils/index.js +342 -0
  78. package/dist/wishlist-BzhnCAQD.d.ts +41 -0
  79. package/dist/wishlist-CBCZD66Y.d.cts +41 -0
  80. package/package.json +134 -0
@@ -0,0 +1,220 @@
1
+ 'use strict';
2
+
3
+ var chunkA6IO3HIN_cjs = require('./chunk-A6IO3HIN.cjs');
4
+ var chunkK44JZ5NL_cjs = require('./chunk-K44JZ5NL.cjs');
5
+ var chunkN3CTXRFT_cjs = require('./chunk-N3CTXRFT.cjs');
6
+ var axios = require('axios');
7
+
8
+ // src/checkout/payment.ts
9
+ function openRazorpayCheckout(order, callbacks) {
10
+ if (!order) return;
11
+ chunkK44JZ5NL_cjs.pixelEvents.initiateCheckout(order, "razorpay");
12
+ const {
13
+ razorpay_order_id,
14
+ razorpay_public_token,
15
+ total_amount,
16
+ razorpay_order_currency,
17
+ store_color_scheme,
18
+ store_logo,
19
+ store_name,
20
+ customer_name,
21
+ customer_phone,
22
+ customer_email
23
+ } = order;
24
+ const cloudFront = chunkN3CTXRFT_cjs.getCloudFront();
25
+ const options = {
26
+ key: razorpay_public_token,
27
+ amount: Number(total_amount) * 100,
28
+ currency: razorpay_order_currency,
29
+ name: store_name,
30
+ image: cloudFront ? `${cloudFront}${store_logo}` : store_logo,
31
+ order_id: razorpay_order_id,
32
+ handler: async (response) => {
33
+ handleRazorpaySuccess(
34
+ {
35
+ razorpay_payment_id: response.razorpay_payment_id,
36
+ razorpay_subscription_id: response.razorpay_subscription_id,
37
+ razorpay_signature: response.razorpay_signature,
38
+ order_id: order.id,
39
+ razorpay_order_id,
40
+ payment_id: response.razorpay_payment_id
41
+ },
42
+ order,
43
+ callbacks
44
+ );
45
+ },
46
+ theme: {
47
+ color: store_color_scheme?.primary,
48
+ accent: store_color_scheme?.secondary
49
+ },
50
+ prefill: {
51
+ name: customer_name,
52
+ email: customer_email,
53
+ phone: customer_phone
54
+ },
55
+ modal: {
56
+ ondismiss: () => handleRazorpayFail(order, callbacks)
57
+ }
58
+ };
59
+ const paymentObject = new window.Razorpay(options);
60
+ paymentObject.on("payment.failed", () => {
61
+ handleRazorpayFail(order, callbacks);
62
+ });
63
+ paymentObject.open();
64
+ }
65
+ async function handleRazorpaySuccess(data, order, callbacks) {
66
+ try {
67
+ callbacks.onPaymentStart?.();
68
+ await chunkA6IO3HIN_cjs.razorpayOrderSuccess(data);
69
+ chunkK44JZ5NL_cjs.pixelEvents.purchase(order);
70
+ chunkK44JZ5NL_cjs.pixelEvents.checkoutCompleted(order);
71
+ callbacks.onCartReset();
72
+ callbacks.onSuccess(order);
73
+ } catch {
74
+ callbacks.onFailure(order);
75
+ } finally {
76
+ callbacks.onPaymentEnd?.();
77
+ }
78
+ }
79
+ async function handleRazorpayFail(order, callbacks) {
80
+ try {
81
+ await chunkA6IO3HIN_cjs.razorpayOrderFail({
82
+ order_id: order.id,
83
+ razorpay_order_id: order.razorpay_order_id
84
+ });
85
+ } catch {
86
+ }
87
+ callbacks.onFailure(order);
88
+ }
89
+ function handlePhonepePayment(order, callbacks) {
90
+ if (!order) return;
91
+ chunkK44JZ5NL_cjs.pixelEvents.initiateCheckout(order, "phonepe");
92
+ if (!order.phonepe_redirect_url) {
93
+ callbacks.onFailure(order);
94
+ return;
95
+ }
96
+ callbacks.onRedirect(order.phonepe_redirect_url);
97
+ }
98
+ function handleTabbyPayment(order, callbacks) {
99
+ if (!order) return;
100
+ chunkK44JZ5NL_cjs.pixelEvents.initiateCheckout(order, "tabby");
101
+ if (!order.tabby_redirect_url) {
102
+ callbacks.onFailure(order);
103
+ return;
104
+ }
105
+ callbacks.onRedirect(order.tabby_redirect_url);
106
+ }
107
+ async function handleCODPayment(order, callbacks, options) {
108
+ if (!order) return;
109
+ try {
110
+ callbacks.onPaymentStart?.();
111
+ chunkK44JZ5NL_cjs.pixelEvents.initiateCheckout(order, "cod");
112
+ if (options?.isPublic) {
113
+ await chunkA6IO3HIN_cjs.publicMakeCODOrder(order.order_number);
114
+ } else {
115
+ await chunkA6IO3HIN_cjs.makeCODOrder(order.id);
116
+ }
117
+ chunkK44JZ5NL_cjs.pixelEvents.purchase(order);
118
+ chunkK44JZ5NL_cjs.pixelEvents.checkoutCompleted(order);
119
+ callbacks.onCartReset();
120
+ callbacks.onSuccess(order);
121
+ } catch {
122
+ callbacks.onFailure(order);
123
+ } finally {
124
+ callbacks.onPaymentEnd?.();
125
+ }
126
+ }
127
+ function handleStripePayment(order, callbacks) {
128
+ if (!order) return;
129
+ chunkK44JZ5NL_cjs.pixelEvents.initiateCheckout(order, "stripe");
130
+ if (order.stripe_payment_link_url) {
131
+ callbacks.onRedirect(order.stripe_payment_link_url);
132
+ return;
133
+ }
134
+ if (order.stripe_client_secret) {
135
+ callbacks.onStripeModalOpen?.(order);
136
+ return;
137
+ }
138
+ callbacks.onFailure(order);
139
+ }
140
+ async function handleStripeSuccess(order, paymentIntentId, callbacks) {
141
+ try {
142
+ callbacks.onPaymentStart?.();
143
+ await chunkA6IO3HIN_cjs.stripeOrderSuccess({
144
+ order_id: order.id,
145
+ payment_intent_id: paymentIntentId
146
+ });
147
+ chunkK44JZ5NL_cjs.pixelEvents.purchase(order);
148
+ chunkK44JZ5NL_cjs.pixelEvents.checkoutCompleted(order);
149
+ callbacks.onCartReset();
150
+ callbacks.onSuccess(order);
151
+ } catch {
152
+ callbacks.onFailure(order);
153
+ } finally {
154
+ callbacks.onPaymentEnd?.();
155
+ }
156
+ }
157
+ function processPayment(order, paymentMethod, callbacks, options) {
158
+ if (paymentMethod === "cod") {
159
+ handleCODPayment(order, callbacks, options);
160
+ return;
161
+ }
162
+ if (order.tabby_redirect_url) {
163
+ handleTabbyPayment(order, callbacks);
164
+ return;
165
+ }
166
+ if (order.stripe_client_secret || order.stripe_payment_link_url) {
167
+ handleStripePayment(order, callbacks);
168
+ return;
169
+ }
170
+ if (order.razorpay_public_token) {
171
+ openRazorpayCheckout(order, callbacks);
172
+ return;
173
+ }
174
+ handlePhonepePayment(order, callbacks);
175
+ }
176
+ async function fetchAvailableCoupons() {
177
+ const response = await chunkA6IO3HIN_cjs.getCoupons();
178
+ return response.data;
179
+ }
180
+ async function fetchAppliedCoupon() {
181
+ try {
182
+ const response = await chunkA6IO3HIN_cjs.getAppliedCoupon();
183
+ return response.data || null;
184
+ } catch {
185
+ return null;
186
+ }
187
+ }
188
+ async function applyCheckoutCoupon(code, callbacks) {
189
+ try {
190
+ const response = await chunkA6IO3HIN_cjs.applyCoupon(code);
191
+ if (response.data?.coupon) {
192
+ await callbacks.onCartRefresh();
193
+ return response.data.coupon;
194
+ }
195
+ throw new Error("Failed to apply coupon");
196
+ } catch (error) {
197
+ if (error instanceof axios.AxiosError) {
198
+ throw new Error(error.response?.data?.detail || "Failed to apply coupon");
199
+ }
200
+ throw error;
201
+ }
202
+ }
203
+ async function removeCheckoutCoupon(couponCode, callbacks) {
204
+ await chunkA6IO3HIN_cjs.removeCoupon(couponCode);
205
+ await callbacks.onCartRefresh();
206
+ }
207
+
208
+ exports.applyCheckoutCoupon = applyCheckoutCoupon;
209
+ exports.fetchAppliedCoupon = fetchAppliedCoupon;
210
+ exports.fetchAvailableCoupons = fetchAvailableCoupons;
211
+ exports.handleCODPayment = handleCODPayment;
212
+ exports.handlePhonepePayment = handlePhonepePayment;
213
+ exports.handleRazorpayFail = handleRazorpayFail;
214
+ exports.handleRazorpaySuccess = handleRazorpaySuccess;
215
+ exports.handleStripePayment = handleStripePayment;
216
+ exports.handleStripeSuccess = handleStripeSuccess;
217
+ exports.handleTabbyPayment = handleTabbyPayment;
218
+ exports.openRazorpayCheckout = openRazorpayCheckout;
219
+ exports.processPayment = processPayment;
220
+ exports.removeCheckoutCoupon = removeCheckoutCoupon;
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ // src/utils/config.ts
4
+ var _config = null;
5
+ function initConfig(config) {
6
+ _config = config;
7
+ }
8
+ function getConfig() {
9
+ if (!_config) {
10
+ throw new Error(
11
+ "Snapstore config not initialized. Call initConfig() in your app's entry point."
12
+ );
13
+ }
14
+ return _config;
15
+ }
16
+ var getPublicApiEndpoint = () => getConfig().publicApiEndpoint;
17
+ var getApiEndpoint = () => getConfig().apiEndpoint;
18
+ var getStoreId = () => getConfig().storeId || "";
19
+ var getSecretKey = () => getConfig().secretKey || "alohomora";
20
+ var getCloudFront = () => getConfig().cloudFront;
21
+ var getPhonePeApiEndpoint = () => getConfig().phonePeApiEndpoint;
22
+
23
+ exports.getApiEndpoint = getApiEndpoint;
24
+ exports.getCloudFront = getCloudFront;
25
+ exports.getConfig = getConfig;
26
+ exports.getPhonePeApiEndpoint = getPhonePeApiEndpoint;
27
+ exports.getPublicApiEndpoint = getPublicApiEndpoint;
28
+ exports.getSecretKey = getSecretKey;
29
+ exports.getStoreId = getStoreId;
30
+ exports.initConfig = initConfig;
@@ -0,0 +1,206 @@
1
+ import { razorpayOrderSuccess, razorpayOrderFail, publicMakeCODOrder, makeCODOrder, stripeOrderSuccess, getCoupons, getAppliedCoupon, applyCoupon, removeCoupon } from './chunk-4MJMNIAB.js';
2
+ import { pixelEvents } from './chunk-WFDKKOO7.js';
3
+ import { getCloudFront } from './chunk-JQMLHRWL.js';
4
+ import { AxiosError } from 'axios';
5
+
6
+ // src/checkout/payment.ts
7
+ function openRazorpayCheckout(order, callbacks) {
8
+ if (!order) return;
9
+ pixelEvents.initiateCheckout(order, "razorpay");
10
+ const {
11
+ razorpay_order_id,
12
+ razorpay_public_token,
13
+ total_amount,
14
+ razorpay_order_currency,
15
+ store_color_scheme,
16
+ store_logo,
17
+ store_name,
18
+ customer_name,
19
+ customer_phone,
20
+ customer_email
21
+ } = order;
22
+ const cloudFront = getCloudFront();
23
+ const options = {
24
+ key: razorpay_public_token,
25
+ amount: Number(total_amount) * 100,
26
+ currency: razorpay_order_currency,
27
+ name: store_name,
28
+ image: cloudFront ? `${cloudFront}${store_logo}` : store_logo,
29
+ order_id: razorpay_order_id,
30
+ handler: async (response) => {
31
+ handleRazorpaySuccess(
32
+ {
33
+ razorpay_payment_id: response.razorpay_payment_id,
34
+ razorpay_subscription_id: response.razorpay_subscription_id,
35
+ razorpay_signature: response.razorpay_signature,
36
+ order_id: order.id,
37
+ razorpay_order_id,
38
+ payment_id: response.razorpay_payment_id
39
+ },
40
+ order,
41
+ callbacks
42
+ );
43
+ },
44
+ theme: {
45
+ color: store_color_scheme?.primary,
46
+ accent: store_color_scheme?.secondary
47
+ },
48
+ prefill: {
49
+ name: customer_name,
50
+ email: customer_email,
51
+ phone: customer_phone
52
+ },
53
+ modal: {
54
+ ondismiss: () => handleRazorpayFail(order, callbacks)
55
+ }
56
+ };
57
+ const paymentObject = new window.Razorpay(options);
58
+ paymentObject.on("payment.failed", () => {
59
+ handleRazorpayFail(order, callbacks);
60
+ });
61
+ paymentObject.open();
62
+ }
63
+ async function handleRazorpaySuccess(data, order, callbacks) {
64
+ try {
65
+ callbacks.onPaymentStart?.();
66
+ await razorpayOrderSuccess(data);
67
+ pixelEvents.purchase(order);
68
+ pixelEvents.checkoutCompleted(order);
69
+ callbacks.onCartReset();
70
+ callbacks.onSuccess(order);
71
+ } catch {
72
+ callbacks.onFailure(order);
73
+ } finally {
74
+ callbacks.onPaymentEnd?.();
75
+ }
76
+ }
77
+ async function handleRazorpayFail(order, callbacks) {
78
+ try {
79
+ await razorpayOrderFail({
80
+ order_id: order.id,
81
+ razorpay_order_id: order.razorpay_order_id
82
+ });
83
+ } catch {
84
+ }
85
+ callbacks.onFailure(order);
86
+ }
87
+ function handlePhonepePayment(order, callbacks) {
88
+ if (!order) return;
89
+ pixelEvents.initiateCheckout(order, "phonepe");
90
+ if (!order.phonepe_redirect_url) {
91
+ callbacks.onFailure(order);
92
+ return;
93
+ }
94
+ callbacks.onRedirect(order.phonepe_redirect_url);
95
+ }
96
+ function handleTabbyPayment(order, callbacks) {
97
+ if (!order) return;
98
+ pixelEvents.initiateCheckout(order, "tabby");
99
+ if (!order.tabby_redirect_url) {
100
+ callbacks.onFailure(order);
101
+ return;
102
+ }
103
+ callbacks.onRedirect(order.tabby_redirect_url);
104
+ }
105
+ async function handleCODPayment(order, callbacks, options) {
106
+ if (!order) return;
107
+ try {
108
+ callbacks.onPaymentStart?.();
109
+ pixelEvents.initiateCheckout(order, "cod");
110
+ if (options?.isPublic) {
111
+ await publicMakeCODOrder(order.order_number);
112
+ } else {
113
+ await makeCODOrder(order.id);
114
+ }
115
+ pixelEvents.purchase(order);
116
+ pixelEvents.checkoutCompleted(order);
117
+ callbacks.onCartReset();
118
+ callbacks.onSuccess(order);
119
+ } catch {
120
+ callbacks.onFailure(order);
121
+ } finally {
122
+ callbacks.onPaymentEnd?.();
123
+ }
124
+ }
125
+ function handleStripePayment(order, callbacks) {
126
+ if (!order) return;
127
+ pixelEvents.initiateCheckout(order, "stripe");
128
+ if (order.stripe_payment_link_url) {
129
+ callbacks.onRedirect(order.stripe_payment_link_url);
130
+ return;
131
+ }
132
+ if (order.stripe_client_secret) {
133
+ callbacks.onStripeModalOpen?.(order);
134
+ return;
135
+ }
136
+ callbacks.onFailure(order);
137
+ }
138
+ async function handleStripeSuccess(order, paymentIntentId, callbacks) {
139
+ try {
140
+ callbacks.onPaymentStart?.();
141
+ await stripeOrderSuccess({
142
+ order_id: order.id,
143
+ payment_intent_id: paymentIntentId
144
+ });
145
+ pixelEvents.purchase(order);
146
+ pixelEvents.checkoutCompleted(order);
147
+ callbacks.onCartReset();
148
+ callbacks.onSuccess(order);
149
+ } catch {
150
+ callbacks.onFailure(order);
151
+ } finally {
152
+ callbacks.onPaymentEnd?.();
153
+ }
154
+ }
155
+ function processPayment(order, paymentMethod, callbacks, options) {
156
+ if (paymentMethod === "cod") {
157
+ handleCODPayment(order, callbacks, options);
158
+ return;
159
+ }
160
+ if (order.tabby_redirect_url) {
161
+ handleTabbyPayment(order, callbacks);
162
+ return;
163
+ }
164
+ if (order.stripe_client_secret || order.stripe_payment_link_url) {
165
+ handleStripePayment(order, callbacks);
166
+ return;
167
+ }
168
+ if (order.razorpay_public_token) {
169
+ openRazorpayCheckout(order, callbacks);
170
+ return;
171
+ }
172
+ handlePhonepePayment(order, callbacks);
173
+ }
174
+ async function fetchAvailableCoupons() {
175
+ const response = await getCoupons();
176
+ return response.data;
177
+ }
178
+ async function fetchAppliedCoupon() {
179
+ try {
180
+ const response = await getAppliedCoupon();
181
+ return response.data || null;
182
+ } catch {
183
+ return null;
184
+ }
185
+ }
186
+ async function applyCheckoutCoupon(code, callbacks) {
187
+ try {
188
+ const response = await applyCoupon(code);
189
+ if (response.data?.coupon) {
190
+ await callbacks.onCartRefresh();
191
+ return response.data.coupon;
192
+ }
193
+ throw new Error("Failed to apply coupon");
194
+ } catch (error) {
195
+ if (error instanceof AxiosError) {
196
+ throw new Error(error.response?.data?.detail || "Failed to apply coupon");
197
+ }
198
+ throw error;
199
+ }
200
+ }
201
+ async function removeCheckoutCoupon(couponCode, callbacks) {
202
+ await removeCoupon(couponCode);
203
+ await callbacks.onCartRefresh();
204
+ }
205
+
206
+ export { applyCheckoutCoupon, fetchAppliedCoupon, fetchAvailableCoupons, handleCODPayment, handlePhonepePayment, handleRazorpayFail, handleRazorpaySuccess, handleStripePayment, handleStripeSuccess, handleTabbyPayment, openRazorpayCheckout, processPayment, removeCheckoutCoupon };
@@ -0,0 +1,218 @@
1
+ 'use strict';
2
+
3
+ var chunkJHGXIIBZ_cjs = require('./chunk-JHGXIIBZ.cjs');
4
+ var chunk4CVKE6CC_cjs = require('./chunk-4CVKE6CC.cjs');
5
+ var chunkK5B62JZO_cjs = require('./chunk-K5B62JZO.cjs');
6
+ var react = require('react');
7
+ var formik = require('formik');
8
+ var libphonenumberJs = require('libphonenumber-js');
9
+
10
+ var defaultInitialValues = {
11
+ ...chunkK5B62JZO_cjs.addressFormInitialValues,
12
+ is_default: false,
13
+ country_obj: "IN"
14
+ };
15
+ function useAddressForm(options = {}) {
16
+ const {
17
+ address,
18
+ onSuccess,
19
+ onError,
20
+ locationCacheKey = chunkK5B62JZO_cjs.IP_API_CACHE_KEY,
21
+ defaultCountryId = 105
22
+ } = options;
23
+ const isEditing = !!address;
24
+ const [countries, setCountries] = react.useState([]);
25
+ const [states, setStates] = react.useState([]);
26
+ const [selectedCountry, setSelectedCountry] = react.useState(null);
27
+ const [addressMeta, setAddressMeta] = react.useState(null);
28
+ const [isSubmitting, setIsSubmitting] = react.useState(false);
29
+ const formik$1 = formik.useFormik({
30
+ initialValues: defaultInitialValues,
31
+ validationSchema: chunkK5B62JZO_cjs.addressValidationSchema,
32
+ onSubmit: async (values) => {
33
+ try {
34
+ setIsSubmitting(true);
35
+ const payload = chunkK5B62JZO_cjs.buildSubmitPayload(values, selectedCountry, states);
36
+ let response;
37
+ if (isEditing) {
38
+ response = await chunkJHGXIIBZ_cjs.updateAddress({
39
+ ...payload,
40
+ address_id: address.id
41
+ });
42
+ } else {
43
+ response = await chunkJHGXIIBZ_cjs.addAddress(payload);
44
+ }
45
+ if (response) {
46
+ formik$1.resetForm();
47
+ onSuccess?.(response.data, isEditing);
48
+ }
49
+ } catch (error) {
50
+ console.error(error);
51
+ onError?.(error);
52
+ } finally {
53
+ setIsSubmitting(false);
54
+ }
55
+ }
56
+ });
57
+ react.useEffect(() => {
58
+ const fetchCountries = async () => {
59
+ try {
60
+ const response = await chunkJHGXIIBZ_cjs.getCountries();
61
+ const sortedCountries = response?.data?.sort((a, b) => {
62
+ if (a.id === defaultCountryId) return -1;
63
+ if (b.id === defaultCountryId) return 1;
64
+ return 0;
65
+ });
66
+ setCountries(sortedCountries);
67
+ } catch (error) {
68
+ console.error(error);
69
+ }
70
+ };
71
+ fetchCountries();
72
+ }, [defaultCountryId]);
73
+ const supportedCountries = react.useMemo(
74
+ () => countries?.filter((c) => libphonenumberJs.isSupportedCountry(c.tld_code)) ?? [],
75
+ [countries]
76
+ );
77
+ const countryCodeOptions = react.useMemo(() => {
78
+ return supportedCountries?.map((c) => {
79
+ const callingCode = libphonenumberJs.getCountryCallingCode(c?.tld_code);
80
+ return {
81
+ value: c.tld_code,
82
+ label: `${c.name} (+${callingCode})`,
83
+ country: c
84
+ };
85
+ }) ?? [];
86
+ }, [supportedCountries]);
87
+ react.useEffect(() => {
88
+ if (!selectedCountry) return;
89
+ const fetchStates = async () => {
90
+ try {
91
+ const response = await chunkJHGXIIBZ_cjs.getSubdivisions(selectedCountry.id);
92
+ setStates(response.data.results);
93
+ } catch (error) {
94
+ console.error(error);
95
+ }
96
+ };
97
+ const fetchAddressMeta = async () => {
98
+ try {
99
+ const response = await chunk4CVKE6CC_cjs.getAddressMeta(
100
+ selectedCountry.tld_code
101
+ );
102
+ setAddressMeta(response);
103
+ if (!response?.hasPostal) {
104
+ formik$1.setFieldValue("pincode", "N/A");
105
+ } else {
106
+ if (formik$1.values.pincode === "N/A") {
107
+ formik$1.setFieldValue("pincode", "");
108
+ }
109
+ }
110
+ } catch (error) {
111
+ console.error(error);
112
+ }
113
+ };
114
+ fetchStates();
115
+ fetchAddressMeta();
116
+ }, [selectedCountry]);
117
+ react.useEffect(() => {
118
+ if (countries.length > 0 && formik$1.values.customer_country_code && !selectedCountry) {
119
+ const found = countries.find(
120
+ (c) => c.tld_code === formik$1.values.customer_country_code
121
+ );
122
+ if (found) {
123
+ setSelectedCountry(found);
124
+ }
125
+ }
126
+ }, [countries, formik$1.values.customer_country_code]);
127
+ react.useEffect(() => {
128
+ if (supportedCountries.length === 0) return;
129
+ if (address) {
130
+ const addressValues = { ...address };
131
+ if (addressValues.country_obj) {
132
+ const foundCountry = supportedCountries.find(
133
+ (c) => c.id === Number(addressValues.country_obj)
134
+ );
135
+ if (foundCountry) {
136
+ setSelectedCountry(foundCountry);
137
+ }
138
+ }
139
+ if (addressValues?.customer_country_code) {
140
+ const foundCountry = supportedCountries.find(
141
+ (c) => libphonenumberJs.getCountryCallingCode(c.tld_code) === addressValues.customer_country_code.slice(1)
142
+ );
143
+ if (foundCountry) {
144
+ addressValues.customer_country_code = foundCountry.tld_code;
145
+ }
146
+ }
147
+ formik$1.setValues(addressValues);
148
+ } else {
149
+ const locationData = chunk4CVKE6CC_cjs.getCache(locationCacheKey);
150
+ if (locationData) {
151
+ const { country_code = "IN" } = locationData;
152
+ const foundCountry = supportedCountries.find(
153
+ (c) => c.tld_code === country_code
154
+ );
155
+ if (foundCountry) {
156
+ setSelectedCountry(foundCountry);
157
+ }
158
+ formik$1.setValues({
159
+ ...defaultInitialValues,
160
+ customer_country_code: country_code,
161
+ country_obj: country_code
162
+ });
163
+ } else {
164
+ formik$1.setValues(defaultInitialValues);
165
+ }
166
+ }
167
+ }, [address, supportedCountries]);
168
+ const handlePhoneCountryChange = react.useCallback(
169
+ (value) => {
170
+ formik$1.setFieldValue("customer_country_code", value);
171
+ },
172
+ // eslint-disable-next-line react-hooks/exhaustive-deps
173
+ []
174
+ );
175
+ const handleCountryCodeChange = react.useCallback(
176
+ (value) => {
177
+ const country = supportedCountries.find(
178
+ (c) => c.tld_code === value
179
+ );
180
+ if (country) {
181
+ setSelectedCountry(country);
182
+ }
183
+ },
184
+ [supportedCountries]
185
+ );
186
+ const handleStateChange = react.useCallback(
187
+ (e) => {
188
+ formik$1.setFieldValue("state_obj", e.target.value);
189
+ },
190
+ // eslint-disable-next-line react-hooks/exhaustive-deps
191
+ []
192
+ );
193
+ const handlePhoneChange = react.useCallback(
194
+ (e) => {
195
+ const cleaned = e.target.value.trim().replace(/^0+/, "");
196
+ formik$1.setFieldValue("customer_phone", cleaned);
197
+ },
198
+ // eslint-disable-next-line react-hooks/exhaustive-deps
199
+ []
200
+ );
201
+ return {
202
+ formik: formik$1,
203
+ countries,
204
+ supportedCountries,
205
+ states,
206
+ selectedCountry,
207
+ countryCodeOptions,
208
+ addressMeta,
209
+ handlePhoneCountryChange,
210
+ handleCountryCodeChange,
211
+ handleStateChange,
212
+ handlePhoneChange,
213
+ isSubmitting,
214
+ isEditing
215
+ };
216
+ }
217
+
218
+ exports.useAddressForm = useAddressForm;