@artisan-commerce/products 0.9.0-canary.9 → 0.9.0-canary.91

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 (70) hide show
  1. package/dist/bundle.cjs +1127 -0
  2. package/dist/bundle.cjs.map +1 -0
  3. package/dist/bundle.d.ts +165 -0
  4. package/dist/bundle.mjs +1120 -0
  5. package/dist/bundle.mjs.map +1 -0
  6. package/dist/bundle.umd.js +1133 -0
  7. package/dist/bundle.umd.js.map +1 -0
  8. package/package.json +42 -62
  9. package/CHANGELOG.md +0 -419
  10. package/build/config/constants.d.ts +0 -12
  11. package/build/errors/api.errors.d.ts +0 -7
  12. package/build/index.d.ts +0 -17
  13. package/build/lib/getCategories/getCategories.d.ts +0 -1
  14. package/build/lib/getCategories/getCategories.test.d.ts +0 -1
  15. package/build/lib/getProductDetails/getProductDetails.d.ts +0 -10
  16. package/build/lib/getProductDetails/getProductDetails.test.d.ts +0 -1
  17. package/build/lib/getProductDetails/getProductDetails.types.d.ts +0 -7
  18. package/build/lib/getProducts/getProducts.d.ts +0 -11
  19. package/build/lib/getProducts/getProducts.test.d.ts +0 -1
  20. package/build/lib/getProducts/getProducts.types.d.ts +0 -3
  21. package/build/lib/getRecommendedProducts/getRecommendedProducts.d.ts +0 -1
  22. package/build/lib/getRecommendedProducts/getRecommendedProducts.test.d.ts +0 -1
  23. package/build/lib/getRelatedProducts/getRelatedProducts.d.ts +0 -1
  24. package/build/lib/getRelatedProducts/getRelatedProducts.test.d.ts +0 -1
  25. package/build/lib/getStoreDetails/getStoreDetails.d.ts +0 -16
  26. package/build/lib/getStoreDetails/getStoreDetails.test.d.ts +0 -1
  27. package/build/lib/getStoreDetails/getStoreDetails.types.d.ts +0 -7
  28. package/build/lib/initProducts/initProducts.d.ts +0 -14
  29. package/build/lib/registerModifiersForm/registerModifiersForm.d.ts +0 -21
  30. package/build/lib/registerModifiersForm/registerModifiersForm.test.d.ts +0 -1
  31. package/build/lib/registerModifiersForm/registerModifiersForm.types.d.ts +0 -34
  32. package/build/lib/validateProduct/validateProduct.d.ts +0 -7
  33. package/build/lib/validateProduct/validateProduct.test.d.ts +0 -1
  34. package/build/lib/validateProduct/validateProduct.types.d.ts +0 -2
  35. package/build/main.bundle.js +0 -19
  36. package/build/report.json +0 -1
  37. package/build/services/fetchProductDetails/fetchProductDetails.service.d.ts +0 -3
  38. package/build/services/fetchProductDetails/fetchProductDetails.service.mock.d.ts +0 -2
  39. package/build/services/fetchProductDetails/fetchProductDetails.service.types.d.ts +0 -9
  40. package/build/services/fetchProducts/fetchPRoducts.service.mock.d.ts +0 -2
  41. package/build/services/fetchProducts/fetchProducts.service.d.ts +0 -2
  42. package/build/services/fetchProducts/fetchProducts.service.types.d.ts +0 -1
  43. package/build/services/fetchStoreDetails/fetchStoreDetails.service.d.ts +0 -2
  44. package/build/services/fetchStoreDetails/fetchStoreDetails.service.types.d.ts +0 -1
  45. package/build/tests/exports.test.d.ts +0 -1
  46. package/build/types/common.types.d.ts +0 -23
  47. package/build/types/http.types.d.ts +0 -96
  48. package/build/types/modifiers.types.d.ts +0 -62
  49. package/build/utils/assertions/assertions.utils.d.ts +0 -8
  50. package/build/utils/calculations/calculations.utils.d.ts +0 -20
  51. package/build/utils/calculations/calculations.utils.test.d.ts +0 -1
  52. package/build/utils/calculations/calculations.utils.types.d.ts +0 -11
  53. package/build/utils/common.utils.d.ts +0 -3
  54. package/build/utils/http/http.utils.d.ts +0 -5
  55. package/build/utils/http/purge.utils.d.ts +0 -38
  56. package/build/utils/http/request.utils.d.ts +0 -12
  57. package/build/utils/modifiers/common.utils.d.ts +0 -17
  58. package/build/utils/modifiers/form.utils.d.ts +0 -17
  59. package/build/utils/modifiers/handleChange.utils.d.ts +0 -1
  60. package/build/utils/modifiers/renderer.utils.d.ts +0 -11
  61. package/build/utils/modifiers/rendererTraversal.utils.d.ts +0 -4
  62. package/build/utils/modifiers/updateRenderer.utils.d.ts +0 -5
  63. package/build/utils/questionsAndAnswers/questionsAndAnswers.utils.d.ts +0 -5
  64. package/build/utils/questionsAndAnswers/questionsAndAnswers.utils.test.d.ts +0 -1
  65. package/build/utils/services.utils.d.ts +0 -6
  66. package/build/utils/state/state.utils.d.ts +0 -2
  67. package/build/utils/transformers/api.transformer.d.ts +0 -12
  68. package/build/utils/transformers/transformers.utils.d.ts +0 -10
  69. package/build/vendors.bundle.js +0 -1
  70. package/build/vendors.d.ts +0 -1
@@ -0,0 +1,1127 @@
1
+ 'use strict';
2
+
3
+ const isBaseProduct = (product) => {
4
+ const { name, images, prices, productId } = product;
5
+ if (typeof name !== "string") {
6
+ return false;
7
+ }
8
+ if (typeof productId !== "string") {
9
+ return false;
10
+ }
11
+ if (typeof prices !== "object" || typeof prices === "object" && Object.keys(prices).length < 1) {
12
+ return false;
13
+ }
14
+ if (!Array.isArray(images)) {
15
+ return false;
16
+ }
17
+ if (product.questionId)
18
+ return false;
19
+ return true;
20
+ };
21
+ const isProductDetails = (product) => {
22
+ const validBaseProduct = isBaseProduct(product);
23
+ if (!validBaseProduct) {
24
+ return false;
25
+ }
26
+ const { questions } = product;
27
+ if (!Array.isArray(questions)) {
28
+ return false;
29
+ }
30
+ return true;
31
+ };
32
+ const isCartProduct = (product) => {
33
+ const validProductDetails = isProductDetails(product);
34
+ if (!validProductDetails) {
35
+ return false;
36
+ }
37
+ const { hash, createdAt, updatedAt } = product;
38
+ const { amount, priceCategory, comment, questionsAndAnswers } = product;
39
+ if (typeof hash !== "string") {
40
+ return false;
41
+ }
42
+ if (typeof createdAt !== "string") {
43
+ return false;
44
+ }
45
+ if (typeof updatedAt !== "string") {
46
+ return false;
47
+ }
48
+ if (typeof amount !== "number") {
49
+ return false;
50
+ }
51
+ if (typeof comment !== "string") {
52
+ return false;
53
+ }
54
+ if (!["NORMAL", "POINTS"].includes(priceCategory)) {
55
+ return false;
56
+ }
57
+ if (!Array.isArray(questionsAndAnswers)) {
58
+ return false;
59
+ }
60
+ return true;
61
+ };
62
+ const isFormCartProduct = (product) => {
63
+ var _a;
64
+ const validCartProduct = isCartProduct(product);
65
+ if (!validCartProduct) {
66
+ return false;
67
+ }
68
+ const { questions } = product;
69
+ if (!Array.isArray(questions)) {
70
+ return false;
71
+ }
72
+ const answers = (_a = questions == null ? void 0 : questions[0]) == null ? void 0 : _a.answers;
73
+ if ((answers == null ? void 0 : answers.length) && typeof answers[0].amount === "undefined") {
74
+ return false;
75
+ }
76
+ return true;
77
+ };
78
+
79
+ var assertions_utils = /*#__PURE__*/Object.freeze({
80
+ __proto__: null,
81
+ isBaseProduct: isBaseProduct,
82
+ isCartProduct: isCartProduct,
83
+ isFormCartProduct: isFormCartProduct,
84
+ isProductDetails: isProductDetails
85
+ });
86
+
87
+ const logError = (message) => {
88
+ if (process.env.NODE_ENV === "production") {
89
+ console.error(message);
90
+ } else {
91
+ throw new Error(message);
92
+ }
93
+ };
94
+
95
+ const getGroupType = (group) => {
96
+ const { min, max, type } = group;
97
+ if (type) {
98
+ if (type === "QUANTITY" || type === "SELECT") {
99
+ return "COUNTER";
100
+ }
101
+ return type;
102
+ }
103
+ if (min === 0 && max === 1) {
104
+ return "CHECKBOX";
105
+ }
106
+ if (min === 1 && max === 1) {
107
+ return "RADIO";
108
+ }
109
+ if (min === 0 && (max === 0 || max === Infinity)) {
110
+ return "COUNTER";
111
+ }
112
+ if (max >= min) {
113
+ return "COUNTER";
114
+ }
115
+ throw new Error(`Modifier group bad configured. min ${min}, max ${max}, type ${type}`);
116
+ };
117
+ const getModifierType = (config) => {
118
+ const { groupType, min, max, type } = config;
119
+ if (groupType === "RADIO") {
120
+ return "RADIO";
121
+ }
122
+ if (type) {
123
+ if (type === "QUANTITY" || type === "SELECT") {
124
+ return "COUNTER";
125
+ }
126
+ return type;
127
+ }
128
+ if (min === 1 && max === 1) {
129
+ return "RADIO";
130
+ }
131
+ if (min === 0 && max === 1) {
132
+ return "CHECKBOX";
133
+ }
134
+ if (min === 0 && (max === 0 || max === Infinity)) {
135
+ return "COUNTER";
136
+ }
137
+ if (max >= min) {
138
+ return "COUNTER";
139
+ }
140
+ return groupType;
141
+ };
142
+ const getModifierMin = (groupConfig) => {
143
+ const { min, modifiersCount } = groupConfig;
144
+ if (modifiersCount === 1) {
145
+ return min;
146
+ }
147
+ return 0;
148
+ };
149
+ const getModifierMax = (groupConfig) => {
150
+ const { max, type } = groupConfig;
151
+ const normalizedMax = max || Infinity;
152
+ if (type === "RADIO") {
153
+ return 1;
154
+ }
155
+ if (type === "CHECKBOX") {
156
+ return 1;
157
+ }
158
+ if (type === "COUNTER") {
159
+ return normalizedMax;
160
+ }
161
+ return normalizedMax;
162
+ };
163
+ const getModifierErrors = (modifier, amount, strict) => {
164
+ const errors = [];
165
+ const { max, min } = modifier;
166
+ if (amount > max) {
167
+ errors.push({
168
+ code: 101,
169
+ message: `Amount is heigher than the maximum allowed amount ${max}`
170
+ });
171
+ }
172
+ if (strict && amount < min) {
173
+ errors.push({
174
+ code: 102,
175
+ message: `Amount is lower than the minimum allowed amount ${min}`
176
+ });
177
+ }
178
+ return errors;
179
+ };
180
+ const getGroupModifierErrors = (group, strict) => {
181
+ const errors = [];
182
+ const { modifiers, data } = group;
183
+ const { min } = data;
184
+ let { max } = data;
185
+ if (min === 0 && max === 0) {
186
+ max = Infinity;
187
+ }
188
+ const amount = modifiers.reduce((acc, modifier) => {
189
+ return acc + modifier.amount;
190
+ }, 0);
191
+ if (amount > max) {
192
+ errors.push({
193
+ code: 201,
194
+ message: `Amount is heigher than the maximum allowed amount ${max}`
195
+ });
196
+ }
197
+ if (strict && amount < min) {
198
+ errors.push({
199
+ code: 202,
200
+ message: `Amount is lower than the minimum allowed amount ${min}`
201
+ });
202
+ }
203
+ return errors;
204
+ };
205
+ const getGroupRendererStatus = (group) => {
206
+ const { touched, errors, required, data, modifiers } = group;
207
+ const { min } = data;
208
+ const childrenWithError = modifiers.some((modifier) => modifier.status === "FAIL");
209
+ const childrenIdle = modifiers.some((modifier) => modifier.status === "IDLE");
210
+ const childrenIncomplete = modifiers.some((modifier) => modifier.status === "INCOMPLETE");
211
+ const amount = modifiers.reduce((acc, modifier) => {
212
+ return acc + modifier.amount;
213
+ }, 0);
214
+ return mapRenderStatusHelper({
215
+ touched,
216
+ errors,
217
+ required,
218
+ min,
219
+ amount,
220
+ childrenWithError,
221
+ childrenIdle,
222
+ childrenIncomplete,
223
+ reviewChildren: true
224
+ });
225
+ };
226
+ const getModifierRendererStatus = (modifier) => {
227
+ const childrenWithError = modifier.groups.some((group) => group.status === "FAIL");
228
+ const childrenIdle = modifier.groups.some((group) => group.status === "IDLE");
229
+ const childrenIncomplete = modifier.groups.some((group) => group.status === "INCOMPLETE");
230
+ const { touched, errors, required, min, amount } = modifier;
231
+ const reviewChildren = amount > 0;
232
+ return mapRenderStatusHelper({
233
+ touched,
234
+ errors,
235
+ required,
236
+ min,
237
+ amount,
238
+ childrenWithError,
239
+ childrenIdle,
240
+ childrenIncomplete,
241
+ reviewChildren
242
+ });
243
+ };
244
+ const mapRenderStatusHelper = (config) => {
245
+ const { touched, errors, required, min, amount, reviewChildren } = config;
246
+ const { childrenWithError, childrenIdle, childrenIncomplete } = config;
247
+ const firstRender = amount > 0 && !touched;
248
+ if (!firstRender && !touched && required) {
249
+ return "IDLE";
250
+ }
251
+ if (!firstRender && !touched && !required) {
252
+ return "OK";
253
+ }
254
+ if (errors.length) {
255
+ return "FAIL";
256
+ }
257
+ if (reviewChildren && childrenWithError) {
258
+ return "FAIL";
259
+ }
260
+ if (reviewChildren && (childrenIdle || childrenIncomplete)) {
261
+ return "INCOMPLETE";
262
+ }
263
+ if (amount < min) {
264
+ return "INCOMPLETE";
265
+ }
266
+ if (amount >= min) {
267
+ return "OK";
268
+ }
269
+ logError(`Unhandled case. Unable to compute the renderer status ${JSON.stringify({
270
+ touched,
271
+ errors,
272
+ required,
273
+ min,
274
+ amount
275
+ }, void 0, 2)}`);
276
+ return "FAIL";
277
+ };
278
+ const mapGlobalRendererStatus = (renderer) => {
279
+ const allChildrenOk = renderer.every((group) => {
280
+ const { status, required } = group;
281
+ const ok = status === "OK";
282
+ const idleNotRequired = status === "IDLE" && !required;
283
+ return ok || idleNotRequired;
284
+ });
285
+ if (allChildrenOk) {
286
+ return "OK";
287
+ }
288
+ const childrenIsIdle = renderer.every((group) => !group.touched);
289
+ if (childrenIsIdle) {
290
+ return "IDLE";
291
+ }
292
+ const chidrenWithError = renderer.some((group) => group.status === "FAIL");
293
+ if (chidrenWithError) {
294
+ return "FAIL";
295
+ }
296
+ return "INCOMPLETE";
297
+ };
298
+
299
+ var __defProp$7 = Object.defineProperty;
300
+ var __defProps$5 = Object.defineProperties;
301
+ var __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors;
302
+ var __getOwnPropSymbols$7 = Object.getOwnPropertySymbols;
303
+ var __hasOwnProp$7 = Object.prototype.hasOwnProperty;
304
+ var __propIsEnum$7 = Object.prototype.propertyIsEnumerable;
305
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
306
+ var __spreadValues$7 = (a, b) => {
307
+ for (var prop in b || (b = {}))
308
+ if (__hasOwnProp$7.call(b, prop))
309
+ __defNormalProp$7(a, prop, b[prop]);
310
+ if (__getOwnPropSymbols$7)
311
+ for (var prop of __getOwnPropSymbols$7(b)) {
312
+ if (__propIsEnum$7.call(b, prop))
313
+ __defNormalProp$7(a, prop, b[prop]);
314
+ }
315
+ return a;
316
+ };
317
+ var __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
318
+ const multiplyProductQA = (product) => {
319
+ return operationOnProductQA(product, (answer, productAmount) => {
320
+ const accAmount = answer.amount * productAmount;
321
+ const fixedAnswer = multiplyProductQA(__spreadProps$5(__spreadValues$7({}, answer), {
322
+ amount: accAmount
323
+ }));
324
+ return __spreadProps$5(__spreadValues$7({}, fixedAnswer), { amount: answer.amount * productAmount });
325
+ });
326
+ };
327
+ const divideProductQA = (product) => {
328
+ return operationOnProductQA(product, (answer, productAmount) => {
329
+ const fixedAnswer = divideProductQA(answer);
330
+ return __spreadProps$5(__spreadValues$7({}, fixedAnswer), { amount: answer.amount / productAmount });
331
+ });
332
+ };
333
+ const operationOnProductQA = (product, answerEnhancer) => {
334
+ const { amount: productAmount = 1, questionsAndAnswers = [] } = product;
335
+ const newQuestionsAndAnswers = questionsAndAnswers.map((question) => {
336
+ const answers = question.answers.map((answer) => {
337
+ return answerEnhancer(answer, productAmount);
338
+ });
339
+ return __spreadProps$5(__spreadValues$7({}, question), { answers });
340
+ });
341
+ return __spreadProps$5(__spreadValues$7({}, product), {
342
+ questionsAndAnswers: newQuestionsAndAnswers
343
+ });
344
+ };
345
+
346
+ var questionsAndAnswers_utils = /*#__PURE__*/Object.freeze({
347
+ __proto__: null,
348
+ divideProductQA: divideProductQA,
349
+ multiplyProductQA: multiplyProductQA,
350
+ operationOnProductQA: operationOnProductQA
351
+ });
352
+
353
+ var __defProp$6 = Object.defineProperty;
354
+ var __defProps$4 = Object.defineProperties;
355
+ var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
356
+ var __getOwnPropSymbols$6 = Object.getOwnPropertySymbols;
357
+ var __hasOwnProp$6 = Object.prototype.hasOwnProperty;
358
+ var __propIsEnum$6 = Object.prototype.propertyIsEnumerable;
359
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
360
+ var __spreadValues$6 = (a, b) => {
361
+ for (var prop in b || (b = {}))
362
+ if (__hasOwnProp$6.call(b, prop))
363
+ __defNormalProp$6(a, prop, b[prop]);
364
+ if (__getOwnPropSymbols$6)
365
+ for (var prop of __getOwnPropSymbols$6(b)) {
366
+ if (__propIsEnum$6.call(b, prop))
367
+ __defNormalProp$6(a, prop, b[prop]);
368
+ }
369
+ return a;
370
+ };
371
+ var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
372
+ const toCartProduct = (product, overrides = {}) => {
373
+ if (isCartProduct(product)) {
374
+ return __spreadValues$6(__spreadValues$6({}, product), overrides);
375
+ }
376
+ if (isProductDetails(product)) {
377
+ return toCartProductFromProductDetails(product, overrides);
378
+ }
379
+ const productDetails = toProductDetails(product);
380
+ return toCartProductFromProductDetails(productDetails, overrides);
381
+ };
382
+ const toCartProductFromProductDetails = (product, overrides = {}) => {
383
+ const { questionsAndAnswers, priceCategory, amount } = product;
384
+ const newCartProduct = __spreadValues$6(__spreadProps$4(__spreadValues$6({}, product), {
385
+ questionsAndAnswers: questionsAndAnswers != null ? questionsAndAnswers : [],
386
+ amount: amount != null ? amount : 0,
387
+ priceCategory: priceCategory != null ? priceCategory : "NORMAL",
388
+ comment: "",
389
+ alerts: [],
390
+ hash: "",
391
+ createdAt: "",
392
+ updatedAt: ""
393
+ }), overrides);
394
+ return newCartProduct;
395
+ };
396
+ const toProductDetails = (product, overrides = {}) => {
397
+ return __spreadValues$6(__spreadProps$4(__spreadValues$6({}, product), {
398
+ addDirectlyToCart: true,
399
+ isPriceVip: false,
400
+ manufacturer: [],
401
+ maxAmountForSale: 0,
402
+ questions: [],
403
+ schedule: [],
404
+ benefitId: null,
405
+ categories: null
406
+ }), overrides);
407
+ };
408
+ const toFormCartProduct = (product, overrides = {}) => {
409
+ if (isFormCartProduct(product)) {
410
+ return __spreadValues$6(__spreadValues$6({}, product), overrides);
411
+ }
412
+ if (isProductDetails(product)) {
413
+ return toFormCartProductFromProductDetails(product, overrides);
414
+ }
415
+ const productDetails = toProductDetails(product);
416
+ return toFormCartProductFromProductDetails(productDetails, overrides);
417
+ };
418
+ const toFormCartProductHelper = (product, overrides = {}) => {
419
+ const fixedProduct = divideProductQA(product);
420
+ const { priceCategory, productId, amount } = fixedProduct;
421
+ const newQuestions = fixedProduct.questions.map((question) => {
422
+ var _a;
423
+ const pairQuestion = (_a = fixedProduct.questionsAndAnswers) == null ? void 0 : _a.find((q) => {
424
+ return q.questionId === question.questionId;
425
+ });
426
+ const selectedQuestion = pairQuestion != null ? pairQuestion : question;
427
+ const { questionId, min, max } = selectedQuestion;
428
+ const answerConfig = {
429
+ min,
430
+ max,
431
+ type: getGroupType(selectedQuestion),
432
+ modifiersCount: question.answers.length};
433
+ return __spreadProps$4(__spreadValues$6({}, selectedQuestion), {
434
+ answers: question.answers.map((answer) => {
435
+ var _a2, _b, _c;
436
+ const pairAnswer = pairQuestion == null ? void 0 : pairQuestion.answers.find((a) => {
437
+ return a.amount > 0 && a.productId === answer.productId;
438
+ });
439
+ const selectedAnswer = pairAnswer != null ? pairAnswer : answer;
440
+ const min2 = getModifierMin(answerConfig);
441
+ const max2 = getModifierMax(answerConfig);
442
+ const type = getModifierType({
443
+ min: min2,
444
+ max: max2,
445
+ groupType: answerConfig.type,
446
+ type: selectedAnswer.renderType
447
+ });
448
+ const nested = (_b = (_a2 = selectedAnswer.questions) == null ? void 0 : _a2.length) != null ? _b : 0;
449
+ const partialNewAnswer = __spreadProps$4(__spreadValues$6({}, selectedAnswer), {
450
+ amount: (_c = selectedAnswer == null ? void 0 : selectedAnswer.amount) != null ? _c : 0,
451
+ priceCategory,
452
+ questionId,
453
+ min: min2,
454
+ max: max2,
455
+ renderType: type
456
+ });
457
+ return __spreadProps$4(__spreadValues$6({}, partialNewAnswer), {
458
+ questions: nested ? toFormCartProductHelper(partialNewAnswer).questions : []
459
+ });
460
+ })
461
+ });
462
+ });
463
+ return __spreadValues$6(__spreadProps$4(__spreadValues$6({}, fixedProduct), {
464
+ questions: newQuestions
465
+ }), overrides);
466
+ };
467
+ const toFormCartProductFromProductDetails = (product, overrides = {}) => {
468
+ const cartProduct = toCartProductFromProductDetails(product);
469
+ return toFormCartProductHelper(cartProduct, overrides);
470
+ };
471
+ const toFormCartProductFromAnswer = (modifier, overrides = {}) => {
472
+ return toFormCartProductHelper(modifier, overrides);
473
+ };
474
+ const normalizeProductDetails = (product) => {
475
+ const { questions } = product;
476
+ return __spreadProps$4(__spreadValues$6({}, product), {
477
+ questions: questions.map((question) => {
478
+ const { answers } = question;
479
+ return __spreadProps$4(__spreadValues$6({}, question), {
480
+ answers: answers.map((answer) => {
481
+ const { questions: questions2 } = answer;
482
+ return __spreadProps$4(__spreadValues$6({}, answer), {
483
+ questions: questions2 != null ? questions2 : []
484
+ });
485
+ })
486
+ });
487
+ })
488
+ });
489
+ };
490
+
491
+ var transformers_utils = /*#__PURE__*/Object.freeze({
492
+ __proto__: null,
493
+ normalizeProductDetails: normalizeProductDetails,
494
+ toCartProduct: toCartProduct,
495
+ toFormCartProduct: toFormCartProduct,
496
+ toFormCartProductFromAnswer: toFormCartProductFromAnswer,
497
+ toFormCartProductFromProductDetails: toFormCartProductFromProductDetails,
498
+ toFormCartProductHelper: toFormCartProductHelper
499
+ });
500
+
501
+ var __defProp$5 = Object.defineProperty;
502
+ var __defProps$3 = Object.defineProperties;
503
+ var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
504
+ var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
505
+ var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
506
+ var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
507
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
508
+ var __spreadValues$5 = (a, b) => {
509
+ for (var prop in b || (b = {}))
510
+ if (__hasOwnProp$5.call(b, prop))
511
+ __defNormalProp$5(a, prop, b[prop]);
512
+ if (__getOwnPropSymbols$5)
513
+ for (var prop of __getOwnPropSymbols$5(b)) {
514
+ if (__propIsEnum$5.call(b, prop))
515
+ __defNormalProp$5(a, prop, b[prop]);
516
+ }
517
+ return a;
518
+ };
519
+ var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
520
+ const buildProductFromRenderer = (renderer, product) => {
521
+ const baseCartProduct = toCartProduct(product);
522
+ return __spreadProps$3(__spreadValues$5({}, baseCartProduct), {
523
+ questionsAndAnswers: buildQuestionAndAnswersFromRenderer(renderer),
524
+ updatedAt: ""
525
+ });
526
+ };
527
+ const buildQuestionAndAnswersFromRenderer = (groups) => {
528
+ return groups.map((group) => {
529
+ return __spreadProps$3(__spreadValues$5({}, group.data), {
530
+ answers: buildAnswersFromRenderer(group.modifiers)
531
+ });
532
+ });
533
+ };
534
+ const buildAnswersFromRenderer = (modifiers) => {
535
+ return modifiers.map((modifier) => {
536
+ if (!modifier.amount) {
537
+ return __spreadProps$3(__spreadValues$5({}, modifier.data), { questionsAndAnswers: [] });
538
+ }
539
+ return __spreadProps$3(__spreadValues$5({}, modifier.data), {
540
+ questionsAndAnswers: buildQuestionAndAnswersFromRenderer(modifier.groups)
541
+ });
542
+ });
543
+ };
544
+
545
+ var __defProp$4 = Object.defineProperty;
546
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
547
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
548
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
549
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
550
+ var __spreadValues$4 = (a, b) => {
551
+ for (var prop in b || (b = {}))
552
+ if (__hasOwnProp$4.call(b, prop))
553
+ __defNormalProp$4(a, prop, b[prop]);
554
+ if (__getOwnPropSymbols$4)
555
+ for (var prop of __getOwnPropSymbols$4(b)) {
556
+ if (__propIsEnum$4.call(b, prop))
557
+ __defNormalProp$4(a, prop, b[prop]);
558
+ }
559
+ return a;
560
+ };
561
+ const getRendererPathNode = (groups, path) => {
562
+ let remainingPath = [...path];
563
+ let segments = groups;
564
+ let node = groups[0];
565
+ while (remainingPath.length) {
566
+ node = findNodeInPath(segments, remainingPath[0]);
567
+ remainingPath = remainingPath.slice(1);
568
+ const isGroup = Object.prototype.hasOwnProperty.call(node, "modifiers");
569
+ if (isGroup) {
570
+ segments = node.modifiers;
571
+ } else {
572
+ segments = node.groups;
573
+ }
574
+ }
575
+ return node;
576
+ };
577
+ const setRendererPathNode = (renderer, path, replacement) => {
578
+ let remainingPath = [...path];
579
+ let segments = renderer;
580
+ let node = findNodeInPath(segments, remainingPath[0]);
581
+ let isGroup = Object.prototype.hasOwnProperty.call(node, "modifiers");
582
+ while (remainingPath.length > 1) {
583
+ node = findNodeInPath(segments, remainingPath[0]);
584
+ remainingPath = remainingPath.slice(1);
585
+ isGroup = Object.prototype.hasOwnProperty.call(node, "modifiers");
586
+ if (isGroup) {
587
+ segments = [...node.modifiers];
588
+ } else {
589
+ segments = [...node.groups];
590
+ }
591
+ }
592
+ const replacerId = remainingPath[0];
593
+ if (path.length === 1) {
594
+ node = { groups: renderer };
595
+ isGroup = false;
596
+ }
597
+ if (isGroup) {
598
+ const newModifiers = node.modifiers.map((item) => {
599
+ if (item.id === replacerId) {
600
+ return __spreadValues$4({}, replacement);
601
+ }
602
+ return __spreadValues$4({}, item);
603
+ });
604
+ node.modifiers = newModifiers;
605
+ } else {
606
+ const newGroups = node.groups.map((item) => {
607
+ if (item.id === replacerId) {
608
+ return __spreadValues$4({}, replacement);
609
+ }
610
+ return __spreadValues$4({}, item);
611
+ });
612
+ node.groups = newGroups;
613
+ }
614
+ if (path.length === 1) {
615
+ return [...node.groups];
616
+ }
617
+ return [...renderer];
618
+ };
619
+ const findNodeInPath = (segments, segmentId) => {
620
+ const node = segments.find((segment) => {
621
+ return segment.id === segmentId;
622
+ });
623
+ if (!node) {
624
+ throw new Error(`Path segment ${segmentId} doesn't exist in segments ${JSON.stringify(segments.map((segment) => segment.id))}`);
625
+ }
626
+ return node;
627
+ };
628
+
629
+ const getProductTotals = (product, amount = product.amount > 0 ? product.amount : 1) => {
630
+ if (amount < 1) {
631
+ throw new Error("Cannot set the amount of getProductTotals with a value less than 1");
632
+ }
633
+ const cartProduct = product;
634
+ const { priceCategory } = cartProduct;
635
+ const isPoints = priceCategory === "POINTS";
636
+ const sanitize = !!cartProduct.updatedAt;
637
+ const sanitized = sanitize ? divideProductQA(cartProduct) : product;
638
+ return {
639
+ unitGrossPrice: getProductGrossPrice(sanitized, 1),
640
+ unitTotalDiscounts: getProductTotalDiscounts(),
641
+ unitTotalTaxes: !isPoints ? getProductTotalTaxes(sanitized, 1) : 0,
642
+ unitNetPrice: getProductNetPrice(sanitized, 1),
643
+ amount,
644
+ grossPrice: getProductGrossPrice(sanitized, amount),
645
+ totalDiscounts: getProductTotalDiscounts(),
646
+ totalTaxes: !isPoints ? getProductTotalTaxes(sanitized, amount) : 0,
647
+ netPrice: getProductNetPrice(sanitized, amount)
648
+ };
649
+ };
650
+ const getProductTotalTaxes = (product, setAmount) => {
651
+ const cartProduct = toCartProduct(product, { amount: setAmount });
652
+ const { prices, amount, priceCategory } = cartProduct;
653
+ const { taxes, grossPrice } = prices[priceCategory];
654
+ const taxTotal = getTaxTotal(taxes, grossPrice);
655
+ const questionsSum = getModifiersTotalTaxes(cartProduct, setAmount);
656
+ if (taxTotal === null || questionsSum === null) {
657
+ return null;
658
+ }
659
+ return questionsSum + taxTotal * amount;
660
+ };
661
+ const getModifiersTotalTaxes = (product, setAmount) => {
662
+ var _a, _b;
663
+ return (_b = (_a = product.questionsAndAnswers) == null ? void 0 : _a.reduce((acc, question) => {
664
+ const answersSum = question.answers.reduce((acc2, answer) => {
665
+ const { prices, amount, priceCategory } = answer;
666
+ const { taxes, grossPrice } = prices[priceCategory];
667
+ const taxTotal = getTaxTotal(taxes, grossPrice);
668
+ const childrenTotal = getModifiersTotalTaxes(answer, setAmount);
669
+ return acc2 + amount * (Number(taxTotal) + childrenTotal);
670
+ }, 0);
671
+ return acc + answersSum * setAmount;
672
+ }, 0)) != null ? _b : 0;
673
+ };
674
+ const getProductGrossPrice = (product, setAmount) => {
675
+ return getProductFieldPrice(product, "grossPrice", setAmount);
676
+ };
677
+ const getProductTotalDiscounts = (product, setAmount) => {
678
+ return null;
679
+ };
680
+ const getProductNetPrice = (product, setAmount) => {
681
+ return getProductFieldPrice(product, "netPrice", setAmount);
682
+ };
683
+ const getProductFieldPrice = (product, field, setAmount) => {
684
+ const cartProduct = toCartProduct(product, { amount: setAmount });
685
+ const { prices, amount, priceCategory } = cartProduct;
686
+ const priceField = prices[priceCategory][field];
687
+ const questionsSum = getModifiersFieldTotal(cartProduct, field);
688
+ return (questionsSum + priceField) * amount;
689
+ };
690
+ const getModifiersFieldTotal = (product, field) => {
691
+ var _a, _b;
692
+ return (_b = (_a = product.questionsAndAnswers) == null ? void 0 : _a.reduce((acc, question) => {
693
+ const answersSum = question.answers.reduce((acc2, answer) => {
694
+ const { prices, amount, priceCategory } = answer;
695
+ const priceField = prices[priceCategory][field];
696
+ const childrenTotal = getModifiersFieldTotal(answer, field);
697
+ return acc2 + amount * (priceField + childrenTotal);
698
+ }, 0);
699
+ return acc + answersSum;
700
+ }, 0)) != null ? _b : 0;
701
+ };
702
+ const getTaxTotal = (taxes, grossPrice) => {
703
+ if (!taxes.length) {
704
+ return null;
705
+ }
706
+ return taxes.reduce((acc, tax) => {
707
+ return acc + tax.percentage / 100 * grossPrice;
708
+ }, 0);
709
+ };
710
+
711
+ var __defProp$3 = Object.defineProperty;
712
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
713
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
714
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
715
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
716
+ var __spreadValues$3 = (a, b) => {
717
+ for (var prop in b || (b = {}))
718
+ if (__hasOwnProp$3.call(b, prop))
719
+ __defNormalProp$3(a, prop, b[prop]);
720
+ if (__getOwnPropSymbols$3)
721
+ for (var prop of __getOwnPropSymbols$3(b)) {
722
+ if (__propIsEnum$3.call(b, prop))
723
+ __defNormalProp$3(a, prop, b[prop]);
724
+ }
725
+ return a;
726
+ };
727
+ const _State = class {
728
+ constructor(state) {
729
+ this._state = state;
730
+ this._initialState = state;
731
+ }
732
+ static getInstance(initialState) {
733
+ if (!_State._instance) {
734
+ _State._instance = new _State(initialState);
735
+ }
736
+ return _State._instance;
737
+ }
738
+ setState(overrides) {
739
+ this._state = __spreadValues$3(__spreadValues$3({}, this._state), overrides);
740
+ }
741
+ get state() {
742
+ if (this._state === null) {
743
+ throw new Error("The state has not been initialized, make sure to call State.init beforehand");
744
+ }
745
+ return this._state;
746
+ }
747
+ checkInit() {
748
+ return !!this._state;
749
+ }
750
+ reset() {
751
+ this._state = this._initialState;
752
+ }
753
+ };
754
+ let State$1 = _State;
755
+ State$1._instance = null;
756
+
757
+ const initialState = {
758
+ forms: {}
759
+ };
760
+ const State = () => {
761
+ return State$1.getInstance(initialState);
762
+ };
763
+
764
+ var __defProp$2 = Object.defineProperty;
765
+ var __defProps$2 = Object.defineProperties;
766
+ var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
767
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
768
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
769
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
770
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
771
+ var __spreadValues$2 = (a, b) => {
772
+ for (var prop in b || (b = {}))
773
+ if (__hasOwnProp$2.call(b, prop))
774
+ __defNormalProp$2(a, prop, b[prop]);
775
+ if (__getOwnPropSymbols$2)
776
+ for (var prop of __getOwnPropSymbols$2(b)) {
777
+ if (__propIsEnum$2.call(b, prop))
778
+ __defNormalProp$2(a, prop, b[prop]);
779
+ }
780
+ return a;
781
+ };
782
+ var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
783
+ const buildRenderer = (product, config, previous) => {
784
+ const { rootId, path } = config;
785
+ const groupsRenderer = product.questions.map((modifierGroup) => {
786
+ const previousGroup = previous == null ? void 0 : previous.find((group) => {
787
+ return group.id === modifierGroup.questionId;
788
+ });
789
+ return buildModifierGroupRenderer(modifierGroup, {
790
+ amount: product.amount,
791
+ rootId,
792
+ path: [...path]
793
+ }, previousGroup);
794
+ });
795
+ return groupsRenderer;
796
+ };
797
+ const buildModifierGroupRenderer = (modifierGroup, parentConfig, previous) => {
798
+ const { questionId, name, description, max, min, images } = modifierGroup;
799
+ const { amount, rootId, path } = parentConfig;
800
+ const { errors, touched } = previous != null ? previous : {};
801
+ const partialGroup = {
802
+ id: questionId,
803
+ name,
804
+ description: description != null ? description : "",
805
+ images: [...images != null ? images : []],
806
+ data: __spreadValues$2({}, modifierGroup),
807
+ errors: errors != null ? errors : [],
808
+ touched: touched != null ? touched : false,
809
+ status: "IDLE",
810
+ hidden: amount < 1,
811
+ required: min > 0,
812
+ type: getGroupType(modifierGroup)
813
+ };
814
+ const config = {
815
+ rootId,
816
+ type: partialGroup.type,
817
+ modifiersCount: modifierGroup.answers.length,
818
+ amount,
819
+ path: [...path, questionId]
820
+ };
821
+ const modifiersRenderer = modifierGroup.answers.map((modifier) => {
822
+ const previousModifier = previous == null ? void 0 : previous.modifiers.find((modifierRenderer) => {
823
+ return modifierRenderer.id === modifier.productId;
824
+ });
825
+ return buildModifierRenderer(modifier, config, previousModifier);
826
+ });
827
+ const groupWithoutStatus = __spreadProps$2(__spreadValues$2({}, partialGroup), {
828
+ modifiers: modifiersRenderer
829
+ });
830
+ return __spreadProps$2(__spreadValues$2({}, groupWithoutStatus), {
831
+ status: getGroupRendererStatus(groupWithoutStatus)
832
+ });
833
+ };
834
+ const buildModifierRenderer = (modifier, config, previous) => {
835
+ var _a, _b;
836
+ const { productId, questionId, name, images, amount } = modifier;
837
+ const { renderType, min, max } = modifier;
838
+ const subproduct = toFormCartProductFromAnswer(modifier);
839
+ const { rootId, path } = config;
840
+ const updatedPath = [...path, productId];
841
+ const groups = buildRenderer(subproduct, {
842
+ rootId,
843
+ path: updatedPath
844
+ }, previous == null ? void 0 : previous.groups);
845
+ const modifierWithoutStatus = {
846
+ id: productId,
847
+ groupId: questionId,
848
+ name,
849
+ images: [...images],
850
+ amount,
851
+ data: __spreadValues$2({}, modifier),
852
+ groups,
853
+ errors: (_a = previous == null ? void 0 : previous.errors) != null ? _a : [],
854
+ hidden: config.amount < 1,
855
+ min,
856
+ max,
857
+ required: min > 0,
858
+ touched: (_b = previous == null ? void 0 : previous.touched) != null ? _b : false,
859
+ status: "IDLE",
860
+ totals: getProductTotals(__spreadProps$2(__spreadValues$2({}, modifier), { amount })),
861
+ type: renderType,
862
+ handleChange: (amount2) => {
863
+ handleModifierChange(config.rootId, updatedPath, amount2);
864
+ }
865
+ };
866
+ return __spreadProps$2(__spreadValues$2({}, modifierWithoutStatus), {
867
+ status: getModifierRendererStatus(modifierWithoutStatus)
868
+ });
869
+ };
870
+ const handleModifierChange = (productId, path, amount) => {
871
+ const forms = __spreadValues$2({}, State().state.forms);
872
+ const form = forms[productId];
873
+ const node = getRendererPathNode(form.renderer, path);
874
+ let newRenderer = updateModifier(form.renderer, path, node, amount, false);
875
+ newRenderer = updateParentGroup(productId, newRenderer, path, false);
876
+ const updatedProduct = buildProductFromRenderer(newRenderer, form.product);
877
+ const updatedForm = __spreadProps$2(__spreadValues$2({}, form), {
878
+ renderer: newRenderer,
879
+ product: updatedProduct,
880
+ status: mapGlobalRendererStatus(newRenderer)
881
+ });
882
+ const newForms = __spreadProps$2(__spreadValues$2({}, forms), { [productId]: updatedForm });
883
+ State().setState({ forms: newForms });
884
+ const returnForm = __spreadValues$2({}, updatedForm);
885
+ delete returnForm.callback;
886
+ form.callback(returnForm);
887
+ };
888
+ const updateModifier = (renderer, path, node, amount, strict) => {
889
+ const newModifierRenderer = __spreadProps$2(__spreadValues$2({}, node), {
890
+ amount,
891
+ touched: true,
892
+ errors: getModifierErrors(node, amount, strict),
893
+ totals: getProductTotals(__spreadProps$2(__spreadValues$2({}, node.data), { amount })),
894
+ data: __spreadProps$2(__spreadValues$2({}, node.data), {
895
+ amount
896
+ })
897
+ });
898
+ newModifierRenderer.status = getModifierRendererStatus(newModifierRenderer);
899
+ return setRendererPathNode(renderer, path, newModifierRenderer);
900
+ };
901
+ const updateParentGroup = (rootId, renderer, path, strict) => {
902
+ const parentPath = path.slice(0, -1);
903
+ const parentGroup = getRendererPathNode(renderer, parentPath);
904
+ let newModifierRenderers = [...parentGroup.modifiers];
905
+ if (parentGroup.type === "RADIO" && !strict) {
906
+ newModifierRenderers = parentGroup.modifiers.map((modifier) => {
907
+ if (modifier.type !== "RADIO") {
908
+ return __spreadValues$2({}, modifier);
909
+ }
910
+ if (modifier.id === path[path.length - 1]) {
911
+ return __spreadValues$2({}, modifier);
912
+ }
913
+ return __spreadProps$2(__spreadValues$2({}, modifier), {
914
+ amount: 0,
915
+ error: "",
916
+ totals: getProductTotals(__spreadProps$2(__spreadValues$2({}, modifier.data), { amount: 0 })),
917
+ data: __spreadProps$2(__spreadValues$2({}, modifier.data), {
918
+ amount: 0
919
+ })
920
+ });
921
+ });
922
+ }
923
+ newModifierRenderers = newModifierRenderers.map((modifier) => {
924
+ const subproduct = toFormCartProductFromAnswer(modifier.data);
925
+ const newPath = [...path.slice(0, -1), modifier.id];
926
+ const updatedModifier = __spreadProps$2(__spreadValues$2({}, modifier), {
927
+ groups: buildRenderer(subproduct, {
928
+ rootId,
929
+ path: newPath
930
+ }, modifier.groups)
931
+ });
932
+ updatedModifier.errors = getModifierErrors(updatedModifier, updatedModifier.amount, strict);
933
+ updatedModifier.status = getModifierRendererStatus(updatedModifier);
934
+ return updatedModifier;
935
+ });
936
+ const updatedGroupAnswers = newModifierRenderers.map((modifier) => {
937
+ return __spreadValues$2({}, modifier.data);
938
+ });
939
+ const newGroupData = __spreadProps$2(__spreadValues$2({}, parentGroup.data), { answers: updatedGroupAnswers });
940
+ const newGroupRenderer = __spreadProps$2(__spreadValues$2({}, parentGroup), {
941
+ touched: true,
942
+ modifiers: newModifierRenderers,
943
+ data: newGroupData
944
+ });
945
+ newGroupRenderer.errors = getGroupModifierErrors(newGroupRenderer, strict);
946
+ newGroupRenderer.status = getGroupRendererStatus(newGroupRenderer);
947
+ let newRenderer = setRendererPathNode(renderer, parentPath, newGroupRenderer);
948
+ if (parentPath.length > 1) {
949
+ newRenderer = updateParentModifier(rootId, newRenderer, parentPath, strict);
950
+ }
951
+ return newRenderer;
952
+ };
953
+ const updateParentModifier = (rootId, renderer, path, strict) => {
954
+ const parentPath = path.slice(0, -1);
955
+ const parentModifier = getRendererPathNode(renderer, parentPath);
956
+ const updatedModifierQuestions = parentModifier.groups.map((group) => {
957
+ return __spreadValues$2({}, group.data);
958
+ });
959
+ const newAnswers = buildQuestionAndAnswersFromRenderer(parentModifier.groups);
960
+ const newModifierData = __spreadProps$2(__spreadValues$2({}, parentModifier.data), {
961
+ questions: updatedModifierQuestions,
962
+ questionsAndAnswers: newAnswers
963
+ });
964
+ const newModifierGroups = parentModifier.groups.map((group) => {
965
+ const newGroup = __spreadValues$2({}, group);
966
+ newGroup.errors = getGroupModifierErrors(newGroup, strict);
967
+ newGroup.status = getGroupRendererStatus(newGroup);
968
+ return newGroup;
969
+ });
970
+ const newModifierRenderer = __spreadProps$2(__spreadValues$2({}, parentModifier), {
971
+ touched: true,
972
+ groups: newModifierGroups,
973
+ data: newModifierData
974
+ });
975
+ newModifierRenderer.errors = getModifierErrors(newModifierRenderer, newModifierRenderer.amount, strict);
976
+ newModifierRenderer.status = getModifierRendererStatus(newModifierRenderer);
977
+ let newRenderer = setRendererPathNode(renderer, parentPath, newModifierRenderer);
978
+ if (parentPath.length > 1) {
979
+ newRenderer = updateParentGroup(rootId, newRenderer, parentPath, strict);
980
+ }
981
+ return newRenderer;
982
+ };
983
+
984
+ var __defProp$1 = Object.defineProperty;
985
+ var __defProps$1 = Object.defineProperties;
986
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
987
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
988
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
989
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
990
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
991
+ var __spreadValues$1 = (a, b) => {
992
+ for (var prop in b || (b = {}))
993
+ if (__hasOwnProp$1.call(b, prop))
994
+ __defNormalProp$1(a, prop, b[prop]);
995
+ if (__getOwnPropSymbols$1)
996
+ for (var prop of __getOwnPropSymbols$1(b)) {
997
+ if (__propIsEnum$1.call(b, prop))
998
+ __defNormalProp$1(a, prop, b[prop]);
999
+ }
1000
+ return a;
1001
+ };
1002
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1003
+ const validateProduct = (productId) => {
1004
+ const forms = __spreadValues$1({}, State().state.forms);
1005
+ const form = forms[productId];
1006
+ const { renderer, callback, product } = form;
1007
+ let latestRenderer = [...renderer];
1008
+ renderer.forEach((group) => {
1009
+ const path = [group.id];
1010
+ latestRenderer = validateChildModifiers(group, latestRenderer, productId, path);
1011
+ });
1012
+ const updatedProduct = buildProductFromRenderer(latestRenderer, product);
1013
+ const updatedForm = __spreadProps$1(__spreadValues$1({}, form), {
1014
+ renderer: latestRenderer,
1015
+ product: updatedProduct,
1016
+ status: mapGlobalRendererStatus(latestRenderer)
1017
+ });
1018
+ const newForms = __spreadProps$1(__spreadValues$1({}, forms), { [productId]: updatedForm });
1019
+ State().setState({ forms: newForms });
1020
+ const returnForm = __spreadValues$1({}, updatedForm);
1021
+ delete returnForm.callback;
1022
+ callback(returnForm);
1023
+ return updatedForm.status;
1024
+ };
1025
+ const validateChildModifiers = (group, renderer, productId, path) => {
1026
+ const { modifiers, id } = group;
1027
+ const isLeaft = !modifiers.length;
1028
+ let latestRenderer = [...renderer];
1029
+ if (!isLeaft) {
1030
+ modifiers.forEach((modifier) => {
1031
+ const newPath = [...path, modifier.id];
1032
+ latestRenderer = validateChildGroups(modifier, latestRenderer, productId, newPath);
1033
+ });
1034
+ return latestRenderer;
1035
+ } else {
1036
+ console.warn(`Attempting to validate a Modifier group without modifiers, this will likely result in unexpected behaviour.
1037
+ Please update the bad group ${id} of product ${0}.`);
1038
+ return latestRenderer;
1039
+ }
1040
+ };
1041
+ const validateChildGroups = (modifier, renderer, productId, path) => {
1042
+ const { groups, amount, status, required } = modifier;
1043
+ const isLeaft = !groups.length || amount === 0;
1044
+ let latestRenderer = [...renderer];
1045
+ if (isLeaft) {
1046
+ const badIdle = status === "IDLE" && required;
1047
+ const badStatus = badIdle || status === "FAIL" || status === "INCOMPLETE";
1048
+ const newStatus = badStatus ? "FAIL" : status;
1049
+ const replacement = __spreadProps$1(__spreadValues$1({}, modifier), { status: newStatus });
1050
+ const newPath = [...path];
1051
+ latestRenderer = updateModifier(latestRenderer, newPath, replacement, replacement.amount, true);
1052
+ latestRenderer = updateParentGroup(productId, latestRenderer, newPath, true);
1053
+ } else {
1054
+ groups.forEach((group) => {
1055
+ const newPath = [...path, group.id];
1056
+ latestRenderer = validateChildModifiers(group, latestRenderer, productId, newPath);
1057
+ });
1058
+ }
1059
+ return latestRenderer;
1060
+ };
1061
+
1062
+ const createForm = (product) => {
1063
+ var _a;
1064
+ const formCartProduct = toFormCartProduct(product, {
1065
+ amount: (_a = product.amount) != null ? _a : 1
1066
+ });
1067
+ const renderer = buildRenderer(formCartProduct, {
1068
+ rootId: formCartProduct.productId,
1069
+ path: []
1070
+ });
1071
+ const updatedProduct = buildProductFromRenderer(renderer, product);
1072
+ return {
1073
+ product: updatedProduct,
1074
+ renderer,
1075
+ validate: () => validateProduct(product.productId),
1076
+ status: mapGlobalRendererStatus(renderer)
1077
+ };
1078
+ };
1079
+
1080
+ var __defProp = Object.defineProperty;
1081
+ var __defProps = Object.defineProperties;
1082
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
1083
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
1084
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
1085
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
1086
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1087
+ var __spreadValues = (a, b) => {
1088
+ for (var prop in b || (b = {}))
1089
+ if (__hasOwnProp.call(b, prop))
1090
+ __defNormalProp(a, prop, b[prop]);
1091
+ if (__getOwnPropSymbols)
1092
+ for (var prop of __getOwnPropSymbols(b)) {
1093
+ if (__propIsEnum.call(b, prop))
1094
+ __defNormalProp(a, prop, b[prop]);
1095
+ }
1096
+ return a;
1097
+ };
1098
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
1099
+ const registerModifiersForm = (product, callback) => {
1100
+ const { productId } = product;
1101
+ const { forms } = State().state;
1102
+ const formExists = !!forms[productId];
1103
+ if (formExists) {
1104
+ logError(`Form for product with id ${productId} has already been register. Please unsubscribe if you need to register a new form.`);
1105
+ return () => {
1106
+ };
1107
+ }
1108
+ const form = createForm(product);
1109
+ const newForms = __spreadProps(__spreadValues({}, forms), { [productId]: __spreadProps(__spreadValues({}, form), { callback }) });
1110
+ State().setState({ forms: newForms });
1111
+ callback(__spreadValues({}, form));
1112
+ return () => unsubscriber(productId);
1113
+ };
1114
+ const unsubscriber = (productId) => {
1115
+ const { forms } = State().state;
1116
+ const newForms = __spreadValues({}, forms);
1117
+ delete newForms[productId];
1118
+ State().setState({ forms: newForms });
1119
+ };
1120
+
1121
+ exports.assertions = assertions_utils;
1122
+ exports.getProductTotals = getProductTotals;
1123
+ exports.modifierUtils = questionsAndAnswers_utils;
1124
+ exports.registerModifiersForm = registerModifiersForm;
1125
+ exports.transformers = transformers_utils;
1126
+ exports.validateProduct = validateProduct;
1127
+ //# sourceMappingURL=bundle.cjs.map