@configura/web-api 1.6.1-alpha.7 → 2.0.0-alpha.1

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 (98) hide show
  1. package/.eslintrc.json +18 -0
  2. package/LICENSE +201 -201
  3. package/README.md +1 -1
  4. package/dist/CatalogueAPI.d.ts +551 -512
  5. package/dist/CatalogueAPI.js +293 -277
  6. package/dist/CfgMeasure.d.ts +32 -32
  7. package/dist/CfgMeasure.js +30 -30
  8. package/dist/CfgProduct.d.ts +268 -239
  9. package/dist/CfgProduct.js +778 -668
  10. package/dist/CfgReferencePathHelper.d.ts +14 -0
  11. package/dist/CfgReferencePathHelper.js +13 -0
  12. package/dist/ConfigurationConverter.d.ts +5 -0
  13. package/dist/ConfigurationConverter.js +72 -0
  14. package/dist/index.d.ts +23 -18
  15. package/dist/index.js +23 -18
  16. package/dist/io/CfgHistoryManager.d.ts +51 -0
  17. package/dist/io/CfgHistoryManager.js +82 -0
  18. package/dist/io/CfgHistoryToProdConfConnector.d.ts +21 -0
  19. package/dist/io/CfgHistoryToProdConfConnector.js +56 -0
  20. package/dist/io/CfgIOManager.d.ts +49 -0
  21. package/dist/io/CfgIOManager.js +115 -0
  22. package/dist/io/CfgIOProdConfConnector.d.ts +53 -0
  23. package/dist/io/CfgIOProdConfConnector.js +141 -0
  24. package/dist/io/CfgObservableStateManager.d.ts +22 -0
  25. package/dist/io/CfgObservableStateManager.js +65 -0
  26. package/dist/io/CfgObservableStateToProdConfConnector.d.ts +15 -0
  27. package/dist/io/CfgObservableStateToProdConfConnector.js +16 -0
  28. package/dist/io/CfgWindowEventManager.d.ts +22 -0
  29. package/dist/io/CfgWindowEventManager.js +38 -0
  30. package/dist/io/CfgWindowMessageManager.d.ts +41 -0
  31. package/dist/io/CfgWindowMessageManager.js +84 -0
  32. package/dist/io/CfgWindowMessageToProdConfConnector.d.ts +17 -0
  33. package/dist/io/CfgWindowMessageToProdConfConnector.js +18 -0
  34. package/dist/io/index.d.ts +9 -0
  35. package/dist/io/index.js +8 -0
  36. package/dist/material/CfgMaterialMapping.d.ts +7 -7
  37. package/dist/material/CfgMaterialMapping.js +181 -181
  38. package/dist/material/CfgMtrlApplication.d.ts +18 -18
  39. package/dist/material/CfgMtrlApplication.js +43 -43
  40. package/dist/material/CfgMtrlApplicationSource.d.ts +7 -7
  41. package/dist/material/CfgMtrlApplicationSource.js +8 -8
  42. package/dist/material/CfgMtrlSource.d.ts +19 -19
  43. package/dist/material/CfgMtrlSource.js +40 -40
  44. package/dist/material/CfgMtrlSourceWithMetaData.d.ts +7 -7
  45. package/dist/material/CfgMtrlSourceWithMetaData.js +1 -1
  46. package/dist/productConfiguration/CfgFeature.d.ts +187 -178
  47. package/dist/productConfiguration/CfgFeature.js +645 -611
  48. package/dist/productConfiguration/CfgOption.d.ts +151 -128
  49. package/dist/productConfiguration/CfgOption.js +416 -394
  50. package/dist/productConfiguration/CfgProductConfiguration.d.ts +117 -120
  51. package/dist/productConfiguration/CfgProductConfiguration.js +307 -307
  52. package/dist/productConfiguration/filters.d.ts +15 -15
  53. package/dist/productConfiguration/filters.js +70 -70
  54. package/dist/productConfiguration/productParamsGenerator.d.ts +15 -15
  55. package/dist/productConfiguration/productParamsGenerator.js +51 -51
  56. package/dist/productConfiguration/utilitiesProductConfiguration.d.ts +17 -17
  57. package/dist/productConfiguration/utilitiesProductConfiguration.js +80 -80
  58. package/dist/productLoader.d.ts +33 -33
  59. package/dist/productLoader.js +49 -49
  60. package/dist/syncGroups/SyncGroupsApplyMode.d.ts +21 -0
  61. package/dist/syncGroups/SyncGroupsApplyMode.js +21 -0
  62. package/dist/syncGroups/SyncGroupsHandler.d.ts +41 -0
  63. package/dist/syncGroups/SyncGroupsHandler.js +359 -0
  64. package/dist/syncGroups/SyncGroupsPathHelper.d.ts +27 -0
  65. package/dist/syncGroups/SyncGroupsPathHelper.js +90 -0
  66. package/dist/syncGroups/SyncGroupsState.d.ts +36 -0
  67. package/dist/syncGroups/SyncGroupsState.js +125 -0
  68. package/dist/syncGroups/SyncGroupsTransaction.d.ts +155 -0
  69. package/dist/syncGroups/SyncGroupsTransaction.js +576 -0
  70. package/dist/tasks/TaskHandler.d.ts +77 -78
  71. package/dist/tasks/TaskHandler.js +275 -265
  72. package/dist/tasks/formats.d.ts +4 -4
  73. package/dist/tasks/formats.js +7 -7
  74. package/dist/tests/testData/collectorForTest.d.ts +73 -73
  75. package/dist/tests/testData/collectorForTest.js +194 -195
  76. package/dist/tests/testData/dummyProductForTest.d.ts +4 -4
  77. package/dist/tests/testData/dummyProductForTest.js +32 -36
  78. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.d.ts +11 -32
  79. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.js +277 -348
  80. package/dist/tests/testData/testDataCachedGetProduct.d.ts +5 -5
  81. package/dist/tests/testData/testDataCachedGetProduct.js +185 -196
  82. package/dist/tests/testData/testDataCachedPostValidate.d.ts +7 -7
  83. package/dist/tests/testData/testDataCachedPostValidate.js +183 -183
  84. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.d.ts +3 -3
  85. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.js +1099 -1099
  86. package/dist/tests/testData/testDataOptions.d.ts +13 -0
  87. package/dist/tests/testData/testDataOptions.js +60 -0
  88. package/dist/tests/testData/testDataProductAggregatedPrice.d.ts +6 -6
  89. package/dist/tests/testData/testDataProductAggregatedPrice.js +187 -198
  90. package/dist/tests/testData/testDataUpcharge.d.ts +8 -29
  91. package/dist/tests/testData/testDataUpcharge.js +119 -151
  92. package/dist/utilitiesCatalogueData.d.ts +33 -25
  93. package/dist/utilitiesCatalogueData.js +164 -66
  94. package/dist/utilitiesCataloguePermission.d.ts +32 -39
  95. package/dist/utilitiesCataloguePermission.js +76 -84
  96. package/dist/utilitiesNumericValues.d.ts +24 -24
  97. package/dist/utilitiesNumericValues.js +109 -109
  98. package/package.json +3 -3
@@ -0,0 +1,576 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { assert } from "@configura/web-utilities";
11
+ import { CfgProductBubbleMode } from "../CfgProduct.js";
12
+ import { SelectionType, } from "../productConfiguration/CfgFeature.js";
13
+ import { ProductConfigurationBubbleMode, } from "../productConfiguration/CfgOption.js";
14
+ import { SyncGroupsApplyMode } from "./SyncGroupsApplyMode.js";
15
+ import { SyncGroupsPathHelper } from "./SyncGroupsPathHelper.js";
16
+ /**
17
+ * The Transaction is a transient object used to track all the changes made to the supplied
18
+ * SyncGroupState as a result of a (normally) single initial event, like when opening a product
19
+ * or the user selecting an option on an already open product.
20
+ *
21
+ * All state changes are made on an internal copy of the original SyncGroupState (called target)
22
+ * so they can later be used (i.e. committed) at once to the visible product, or safely discarded
23
+ * without affecting anything.
24
+ *
25
+ * The Transaction keeps track of which Features and SyncGroups have been affected so far, to
26
+ * eliminate the risk of infinite loops.
27
+ *
28
+ * Terminology
29
+ * ===========
30
+ *
31
+ * You APPLY things onto the transaction in order to update the sync state inside the transaction.
32
+ * The transaction can then UPDATE other things in order to apply it's sync state onto them.
33
+ *
34
+ * Transaction.applyThing() ...onto the transaction
35
+ * Transaction.updateThing() ...with the transaction
36
+ *
37
+ * @see SyncGroupHandler.ts for more information, including the general resolution algorithm.
38
+ */
39
+ export class SyncGroupsTransaction {
40
+ constructor(syncState, updateMode, productLoader, original, target, initial) {
41
+ this._closed = false;
42
+ this.affectedSelectOneFeatures = new Set();
43
+ this.affectedSelectManyOptions = new Set();
44
+ this.affectedSelectOneSyncGroups = new Set();
45
+ this.affectedSelectManySyncGroupsAndOptions = new Map();
46
+ this.syncState = syncState.clone();
47
+ this.originalSyncState = syncState;
48
+ this.updateMode = updateMode;
49
+ this.productLoader = productLoader;
50
+ this.originalProduct = original;
51
+ this.target = target;
52
+ this.initial = initial;
53
+ }
54
+ static make(syncState, updateMode, product, productLoader, assumeNoStartState) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const t = new this(syncState, updateMode, productLoader, product, yield product.clone(), assumeNoStartState ? undefined : yield product.clone());
57
+ return t;
58
+ });
59
+ }
60
+ /************************************************************************
61
+ * Public API (intentionally limited)
62
+ ************************************************************************/
63
+ get isClosed() {
64
+ return this._closed;
65
+ }
66
+ close() {
67
+ this._closed = true;
68
+ }
69
+ /**
70
+ * This is (among other) the entry point when loading a new product.
71
+ *
72
+ * @returns true if at least one Feature changed it state. This is a bit counter intuitive,
73
+ * but as this method will hand over to applyProduct if it updates the SyncState it can cause
74
+ * the Features to change. And we need to pass that information back to know when to stop.
75
+ */
76
+ applyRootProduct() {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ const productWithInitial = { target: this.target, initial: this.initial };
79
+ if (!(yield this.applyProduct(productWithInitial))) {
80
+ // All done!
81
+ return false;
82
+ }
83
+ return yield this.updateRootProduct(undefined);
84
+ });
85
+ }
86
+ /**
87
+ * This is the entry point for an active (often user) selection of an option.
88
+ */
89
+ selectOption(optionPath, on) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ const option = SyncGroupsPathHelper.getOptionFromPath(optionPath, this.target);
92
+ const feature = option.parent;
93
+ let change = false;
94
+ let productToValidate = undefined;
95
+ // At this point in handling sync groups we go back to normal selecting.
96
+ //
97
+ // This is because a normal selection can bring features into scope which should push to or
98
+ // (if the SyncGroup is defined) pull from the Sync State.
99
+ //
100
+ // We use BubbleSelected (without validation) as we can delay the validation until after we
101
+ // have applied the SyncGroup (if any).
102
+ if (yield feature.selectOption(option, on, ProductConfigurationBubbleMode.BubbleSelected)) {
103
+ productToValidate = feature.parentProduct;
104
+ change = true;
105
+ switch (feature.selectionType) {
106
+ case SelectionType.SelectOne:
107
+ this.affectedSelectOneFeatures.add(feature);
108
+ break;
109
+ case SelectionType.SelectMany:
110
+ this.affectedSelectManyOptions.add(option);
111
+ break;
112
+ default:
113
+ throw new Error("Should not happen");
114
+ }
115
+ }
116
+ switch (feature.selectionType) {
117
+ case SelectionType.SelectOne:
118
+ this.applySelectOneFeature(feature, true, false);
119
+ break;
120
+ case SelectionType.SelectMany:
121
+ this.applySelectManyOption(option, true);
122
+ break;
123
+ }
124
+ if (yield this.updateRootProduct(productToValidate)) {
125
+ change = true;
126
+ }
127
+ return change;
128
+ });
129
+ }
130
+ /**
131
+ * Overwrites the original Product and SyncGroupState (supplied when creating the Transaction)
132
+ * with the internal versions inside this Transaction.
133
+ *
134
+ * @throws error if the transaction has already been closed.
135
+ */
136
+ commit() {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ assert(this.isClosed === false, "Trying to commit a closed Transaction");
139
+ this.originalSyncState.copyFrom(this.syncState);
140
+ yield this.originalProduct.copyFrom(this.target, false, this.productLoader);
141
+ });
142
+ }
143
+ /************************************************************************
144
+ * Updating things with the Transaction's SyncState
145
+ ************************************************************************/
146
+ /**
147
+ * Apply current sync groups on those who wants to listen until there is no more to settle.
148
+ * @returns true if at least one Feature changed selected Option
149
+ */
150
+ updateRootProduct(productToValidate) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ const productsToValidate = new Set();
153
+ if (productToValidate !== undefined) {
154
+ productsToValidate.add(productToValidate);
155
+ }
156
+ const productWithInitial = { target: this.target, initial: this.initial };
157
+ yield this.updateProduct(productWithInitial, productsToValidate);
158
+ if (productsToValidate.size === 0) {
159
+ // All have been settled, continue to pullPhase
160
+ return yield this.applyRootProduct();
161
+ }
162
+ if (this.isClosed) {
163
+ // We could exit in more places when the transaction has been aborted, but as
164
+ // revalidate is really the only thing that could be expensive time consuming we only
165
+ // check here.
166
+ return false;
167
+ }
168
+ const promises = [];
169
+ for (const product of productsToValidate) {
170
+ promises.push(product._revalidate(CfgProductBubbleMode.ToRoot, this.productLoader, true));
171
+ }
172
+ const revalidationResults = yield Promise.all(promises);
173
+ if (revalidationResults.every((r) => !r)) {
174
+ this.close();
175
+ return false;
176
+ }
177
+ // Apply over again, to settle deeper down. Our theory is that the front of "settled" will
178
+ // move deeper and deeper into the tree.
179
+ yield this.updateRootProduct(undefined);
180
+ // We had productsToValidate, and so there must have been a change.
181
+ return true;
182
+ });
183
+ }
184
+ /**
185
+ * Applies the SyncState to the Product and it's AdditionalProducts (sub-products).
186
+ * @param productsToValidate To this all products that will need validation are added.
187
+ */
188
+ updateProduct(product, productsToValidate) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ const promises = [];
191
+ promises.push(this.updateFeatures(getFeaturesFromProduct(product), productsToValidate));
192
+ for (const additionalProduct of getAdditionalProducts(product)) {
193
+ promises.push(this.updateProduct(additionalProduct, productsToValidate));
194
+ }
195
+ yield Promise.all(promises);
196
+ });
197
+ }
198
+ /**
199
+ * Applies the SyncState to an array of Features.
200
+ * @param productsToValidate To this all products that will need validation are added.
201
+ */
202
+ updateFeatures(features, productsToValidate) {
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ yield Promise.all(features.map((feature) => __awaiter(this, void 0, void 0, function* () {
205
+ switch (yield this.updateFeature(feature, productsToValidate)) {
206
+ case "stop":
207
+ return;
208
+ case "recurseDown":
209
+ yield this.updateOptions(getSelectedOptions(feature), productsToValidate);
210
+ return;
211
+ }
212
+ })));
213
+ });
214
+ }
215
+ /**
216
+ * Applies the SyncState to an array of Options.
217
+ * @param productsToValidate To this all products that will need validation are added.
218
+ */
219
+ updateOptions(options, productsToValidate) {
220
+ return __awaiter(this, void 0, void 0, function* () {
221
+ yield Promise.all(options.map((option) => {
222
+ return this.updateFeatures(getFeaturesFromOption(option), productsToValidate);
223
+ }));
224
+ });
225
+ }
226
+ /**
227
+ * Applies the SyncState to a Feature
228
+ * @param productsToValidate To this all products that will need validation are added
229
+ * @returns Whether we shall stop recursing down (because we are in a state which we
230
+ * expect to be resolved later), we shall continue recursing down.
231
+ */
232
+ updateFeature(featureWithInitial, productsToValidate) {
233
+ return __awaiter(this, void 0, void 0, function* () {
234
+ const feature = featureWithInitial.target;
235
+ const syncCode = feature.getSyncCode("pull");
236
+ if (syncCode === undefined) {
237
+ // Just continue down, features with no sync groups are always settled
238
+ return "recurseDown";
239
+ }
240
+ if (syncCode === false) {
241
+ // Here we only handle pull. Initializing those missing in SyncMap
242
+ // we do later. We wait until we have settled as much as we can with what
243
+ // we have now, to increase chances of the values being written to the
244
+ // SyncState being the right ones.
245
+ return "stop";
246
+ }
247
+ switch (feature.selectionType) {
248
+ case SelectionType.Group:
249
+ return "recurseDown";
250
+ case SelectionType.SelectOne:
251
+ return yield this.updateSelectOneFeature(syncCode, featureWithInitial, productsToValidate);
252
+ case SelectionType.SelectMany:
253
+ return yield this.updateSelectManyFeature(syncCode, featureWithInitial, productsToValidate);
254
+ }
255
+ });
256
+ }
257
+ /**
258
+ * Decides if the SyncState can be applied to the SelectOne Feature, and then changes
259
+ * the state of the Feature if so.
260
+ * @param syncCode What SyncGroup the Feature belongs to
261
+ * @param productsToValidate To this all products that will need validation are added
262
+ * @returns Whether we shall stop recursing down (because we are in a state which we
263
+ * expect to be resolved later), we shall continue recursing down.
264
+ */
265
+ updateSelectOneFeature(syncCode, featureWithInitial, productsToValidate) {
266
+ return __awaiter(this, void 0, void 0, function* () {
267
+ const feature = featureWithInitial.target;
268
+ const featureDidJustComeIntoScope = featureWithInitial.initial === undefined;
269
+ if (this.affectedSelectOneFeatures.has(feature)) {
270
+ // This feature has already changed selection once for this transaction. We expect
271
+ // this to happen very rarely, as the algorithm should settle the selection tree
272
+ // further and further out. Nevertheless, this safeguard is needed to avoid infinite
273
+ // looping if for example the server would return the same data over and over.
274
+ return "recurseDown";
275
+ }
276
+ if (!featureDidJustComeIntoScope && !this.affectedSelectOneSyncGroups.has(syncCode)) {
277
+ return "recurseDown";
278
+ }
279
+ const currentSyncGroupValue = this.syncState.getForSelectOne(syncCode);
280
+ if (currentSyncGroupValue === undefined) {
281
+ // This branch will have to be settled later. Don't go further down here.
282
+ return "stop";
283
+ }
284
+ const selectedOption = feature.selectedOptions[0];
285
+ if (selectedOption !== undefined && selectedOption.code === currentSyncGroupValue) {
286
+ // Settled, continue
287
+ return "recurseDown";
288
+ }
289
+ const optionToSelect = feature.options.find((o) => o.code === currentSyncGroupValue);
290
+ if (optionToSelect === undefined) {
291
+ // No option which can be selected (keep current value and recurse down)
292
+ return "recurseDown";
293
+ }
294
+ // Update the value. Validations are collected so that we do not send more than necessary.
295
+ // Do not recurse further as we will change the state and so what is selected now won't be
296
+ // selected then.
297
+ yield feature.selectOption(optionToSelect._internal, true, ProductConfigurationBubbleMode.ToRoot);
298
+ this.affectedSelectOneFeatures.add(feature);
299
+ productsToValidate.add(feature.parentProduct);
300
+ return "stop";
301
+ });
302
+ }
303
+ /**
304
+ * Decides if the SyncState can be applied to Options in the SelectMany Feature, and
305
+ * then changes the state of the Options if so.
306
+ * @param syncCode What SyncGroup the Feature belongs to
307
+ * @param productsToValidate To this all products that will need validation are added
308
+ * @returns Always "stop" as recursion is handled internally. Return for consistency.
309
+ */
310
+ updateSelectManyFeature(syncCode, feature, productsToValidate) {
311
+ return __awaiter(this, void 0, void 0, function* () {
312
+ const optionsToContinueDown = [];
313
+ for (const option of getOptions(feature)) {
314
+ switch (yield this.updateSelectManyOption(syncCode, option, productsToValidate)) {
315
+ case "stop":
316
+ continue;
317
+ case "recurseDown":
318
+ optionsToContinueDown.push(option);
319
+ continue;
320
+ }
321
+ }
322
+ yield this.updateOptions(optionsToContinueDown, productsToValidate);
323
+ // stop as the method above handles the recursion down
324
+ return "stop";
325
+ });
326
+ }
327
+ /**
328
+ * Decides if the SyncState can be applied to the SelectMany Option, and then changes
329
+ * the state of the Option if so.
330
+ * @param syncCode What SyncGroup the Feature belongs to
331
+ * @param productsToValidate To this all products that will need validation are added
332
+ * @returns Whether we shall stop recursing down (because we are in a state which we
333
+ * expect to be resolved later), we shall continue recursing down.
334
+ */
335
+ updateSelectManyOption(syncCode, optionWithInitial, productsToValidate) {
336
+ return __awaiter(this, void 0, void 0, function* () {
337
+ const option = optionWithInitial.target;
338
+ const featureDidJustComeIntoScope = optionWithInitial.initial === undefined;
339
+ const optionSelected = option.selected;
340
+ const recurseOrStopIfNoChange = optionSelected ? "recurseDown" : "stop";
341
+ if (this.affectedSelectManyOptions.has(option)) {
342
+ // This option has already changed selection once for this transaction. We expect
343
+ // this to happen very rarely, as the algorithm should settle the selection tree
344
+ // further and further out. Nevertheless, this safeguard is needed to avoid infinite
345
+ // looping if for example the server would return the same data over and over.
346
+ return recurseOrStopIfNoChange;
347
+ }
348
+ if (!featureDidJustComeIntoScope &&
349
+ !this.hasSyncGroupAffectedForSelectMany(syncCode, option)) {
350
+ return "recurseDown";
351
+ }
352
+ const syncGroupValueForOption = this.syncState.getForSelectMany(syncCode, option.code);
353
+ if (syncGroupValueForOption === undefined) {
354
+ // The sync group has no opinion on this. If it is selected, just continue down.
355
+ return recurseOrStopIfNoChange;
356
+ }
357
+ if (syncGroupValueForOption === optionSelected) {
358
+ // We are in sync for this option
359
+ return recurseOrStopIfNoChange;
360
+ }
361
+ const feature = option.parent;
362
+ // Update the value. Validations are collected so that we do not
363
+ // send more than necessary. Do not recurse further as we will change
364
+ // the state and so what is selected now won't be selected then.
365
+ yield feature.selectOption(option, syncGroupValueForOption, ProductConfigurationBubbleMode.ToRoot);
366
+ this.affectedSelectManyOptions.add(option);
367
+ productsToValidate.add(feature.parentProduct);
368
+ return "stop";
369
+ });
370
+ }
371
+ /************************************************************************
372
+ * Applying things to the Transaction's SyncState
373
+ ************************************************************************/
374
+ applyProduct(product) {
375
+ return __awaiter(this, void 0, void 0, function* () {
376
+ let change = false;
377
+ if (yield this.applyFeatures(getFeaturesFromProduct(product))) {
378
+ if (this.updateMode === SyncGroupsApplyMode.Strict) {
379
+ return true;
380
+ }
381
+ change = true;
382
+ }
383
+ for (const additionalProduct of getAdditionalProducts(product)) {
384
+ if (yield this.applyProduct(additionalProduct)) {
385
+ if (this.updateMode === SyncGroupsApplyMode.Strict) {
386
+ return true;
387
+ }
388
+ change = true;
389
+ }
390
+ }
391
+ return change;
392
+ });
393
+ }
394
+ applyFeatures(featureWithInitials) {
395
+ return __awaiter(this, void 0, void 0, function* () {
396
+ let change = false;
397
+ for (const featureWithInitial of featureWithInitials) {
398
+ const feature = featureWithInitial.target;
399
+ switch (feature.selectionType) {
400
+ case SelectionType.SelectOne:
401
+ if (this.applySelectOneFeature(feature, false, featureWithInitial.initial === undefined)) {
402
+ change = true;
403
+ }
404
+ break;
405
+ case SelectionType.SelectMany:
406
+ if (this.applySelectManyFeature(featureWithInitial)) {
407
+ change = true;
408
+ }
409
+ break;
410
+ }
411
+ if (change && this.updateMode === SyncGroupsApplyMode.Strict) {
412
+ return true;
413
+ }
414
+ if (yield this.applyOptions(getSelectedOptions(featureWithInitial))) {
415
+ if (this.updateMode === SyncGroupsApplyMode.Strict) {
416
+ return true;
417
+ }
418
+ change = true;
419
+ }
420
+ }
421
+ return change;
422
+ });
423
+ }
424
+ applyOptions(options) {
425
+ return __awaiter(this, void 0, void 0, function* () {
426
+ let change = false;
427
+ for (const option of options) {
428
+ if (yield this.applyFeatures(getFeaturesFromOption(option))) {
429
+ if (this.updateMode === SyncGroupsApplyMode.Strict) {
430
+ return true;
431
+ }
432
+ change = true;
433
+ }
434
+ }
435
+ return change;
436
+ });
437
+ }
438
+ applySelectOneFeature(feature, activeSelectionForce, featureDidJustComeIntoScope) {
439
+ const selectionType = feature.selectionType;
440
+ if (selectionType !== SelectionType.SelectOne) {
441
+ throw new Error("can only be used for selectOne");
442
+ }
443
+ const syncCode = feature.getSyncCode("push");
444
+ if (syncCode === undefined || syncCode === false) {
445
+ return false;
446
+ }
447
+ if (this.affectedSelectOneSyncGroups.has(syncCode)) {
448
+ return false;
449
+ }
450
+ const currentSyncGroupOptionCode = this.syncState.getForSelectOne(syncCode);
451
+ const option = feature.selectedOptions[0];
452
+ if (option === undefined) {
453
+ // Options with no default are never written
454
+ return false;
455
+ }
456
+ if (activeSelectionForce) {
457
+ // To make re-apply happen, even if it actually does not update the sync group
458
+ this.affectedSelectOneSyncGroups.add(syncCode);
459
+ }
460
+ if (option.code === currentSyncGroupOptionCode) {
461
+ return false;
462
+ }
463
+ // featureDidJustComeIntoScope, in CET there is a feature that if a feature appears which
464
+ // can not be set to the current sync group value, then it will set in the opposite
465
+ // direction. Like if the sync group was empty. To avoid bouncing back and forth we will
466
+ // need to enforce that a sync group can only be updated once per transaction
467
+ if (!(activeSelectionForce ||
468
+ currentSyncGroupOptionCode === undefined ||
469
+ (featureDidJustComeIntoScope &&
470
+ feature.options.every((o) => currentSyncGroupOptionCode !== o.code)))) {
471
+ return false;
472
+ }
473
+ this.affectedSelectOneSyncGroups.add(syncCode);
474
+ this.syncState.setForSelectOne(syncCode, option.code);
475
+ return true;
476
+ }
477
+ applySelectManyFeature(featureWithInitial) {
478
+ let change = false;
479
+ for (const optionWithInitial of getOptions(featureWithInitial)) {
480
+ if (this.applySelectManyOption(optionWithInitial.target, false)) {
481
+ if (this.updateMode === SyncGroupsApplyMode.Strict) {
482
+ return true;
483
+ }
484
+ change = true;
485
+ }
486
+ }
487
+ return change;
488
+ }
489
+ applySelectManyOption(option, activeSelectionForce) {
490
+ const feature = option.parent;
491
+ if (feature.selectionType !== SelectionType.SelectMany) {
492
+ throw new Error("can only be used for selectMany");
493
+ }
494
+ const syncCode = feature.getSyncCode("push");
495
+ if (syncCode === undefined || syncCode === false) {
496
+ return false;
497
+ }
498
+ if (this.hasSyncGroupAffectedForSelectMany(syncCode, option)) {
499
+ return false;
500
+ }
501
+ const optionSelected = option.selected;
502
+ const currentSyncGroupValue = this.syncState.getForSelectMany(syncCode, option.code);
503
+ if (activeSelectionForce) {
504
+ // To make re-apply happen, even if it actually does not update the sync group
505
+ this.addSyncGroupAffectedForSelectMany(syncCode, option);
506
+ }
507
+ // We only initialize if the option is selected or we force.
508
+ // activeSelectionForce = active selection by the user.
509
+ if (!(currentSyncGroupValue === undefined && optionSelected) &&
510
+ !(activeSelectionForce && currentSyncGroupValue !== optionSelected)) {
511
+ return false;
512
+ }
513
+ this.addSyncGroupAffectedForSelectMany(syncCode, option);
514
+ this.syncState.setForSelectMany(syncCode, option.code, optionSelected);
515
+ return true;
516
+ }
517
+ addSyncGroupAffectedForSelectMany(syncCode, option) {
518
+ let forSyncCode = this.affectedSelectManySyncGroupsAndOptions.get(syncCode);
519
+ if (forSyncCode === undefined) {
520
+ forSyncCode = new Set();
521
+ this.affectedSelectManySyncGroupsAndOptions.set(syncCode, forSyncCode);
522
+ }
523
+ forSyncCode.add(option.code);
524
+ }
525
+ hasSyncGroupAffectedForSelectMany(syncCode, option) {
526
+ var _a;
527
+ return ((_a = this.affectedSelectManySyncGroupsAndOptions.get(syncCode)) === null || _a === void 0 ? void 0 : _a.has(option.code)) === true;
528
+ }
529
+ }
530
+ function getAdditionalProducts(product) {
531
+ const initial = product.initial;
532
+ return product.target.additionalProducts.map((childTarget) => {
533
+ const refKey = childTarget.refKey;
534
+ const childInitial = initial === null || initial === void 0 ? void 0 : initial.additionalProducts.find((p) => refKey === p.refKey);
535
+ return {
536
+ target: childTarget._internal,
537
+ initial: childInitial === null || childInitial === void 0 ? void 0 : childInitial._internal,
538
+ };
539
+ });
540
+ }
541
+ function pairOptions(targets, initials) {
542
+ return targets.map((childTarget) => {
543
+ const key = childTarget.key;
544
+ const childInitial = initials === null || initials === void 0 ? void 0 : initials.find((o) => key === o.key);
545
+ return {
546
+ target: childTarget._internal,
547
+ initial: childInitial === null || childInitial === void 0 ? void 0 : childInitial._internal,
548
+ };
549
+ });
550
+ }
551
+ function getSelectedOptions(feature) {
552
+ var _a;
553
+ return pairOptions(feature.target.selectedOptions, (_a = feature.initial) === null || _a === void 0 ? void 0 : _a.selectedOptions);
554
+ }
555
+ function getOptions(feature) {
556
+ var _a;
557
+ return pairOptions(feature.target.options, (_a = feature.initial) === null || _a === void 0 ? void 0 : _a.options);
558
+ }
559
+ function pairFeatures(targets, initials) {
560
+ return targets.map((childTarget) => {
561
+ const key = childTarget.key;
562
+ const childInitial = initials === null || initials === void 0 ? void 0 : initials.find((f) => key === f.key);
563
+ return {
564
+ target: childTarget._internal,
565
+ initial: childInitial === null || childInitial === void 0 ? void 0 : childInitial._internal,
566
+ };
567
+ });
568
+ }
569
+ function getFeaturesFromProduct(product) {
570
+ var _a;
571
+ return pairFeatures(product.target.configuration.features, (_a = product.initial) === null || _a === void 0 ? void 0 : _a.configuration.features);
572
+ }
573
+ function getFeaturesFromOption(option) {
574
+ var _a;
575
+ return pairFeatures(option.target.features, (_a = option.initial) === null || _a === void 0 ? void 0 : _a.features);
576
+ }