@configura/web-api 1.6.1 → 2.0.0-alpha.2

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 -18
  2. package/LICENSE +201 -201
  3. package/README.md +1 -1
  4. package/dist/CatalogueAPI.d.ts +551 -507
  5. package/dist/CatalogueAPI.js +293 -280
  6. package/dist/CfgMeasure.d.ts +32 -32
  7. package/dist/CfgMeasure.js +30 -30
  8. package/dist/CfgProduct.d.ts +268 -258
  9. package/dist/CfgProduct.js +778 -747
  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 -20
  15. package/dist/index.js +23 -20
  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 -188
  47. package/dist/productConfiguration/CfgFeature.js +645 -636
  48. package/dist/productConfiguration/CfgOption.d.ts +151 -150
  49. package/dist/productConfiguration/CfgOption.js +416 -426
  50. package/dist/productConfiguration/CfgProductConfiguration.d.ts +117 -120
  51. package/dist/productConfiguration/CfgProductConfiguration.js +307 -309
  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 +20 -20
  61. package/dist/syncGroups/SyncGroupsApplyMode.js +21 -21
  62. package/dist/syncGroups/SyncGroupsHandler.d.ts +40 -40
  63. package/dist/syncGroups/SyncGroupsHandler.js +359 -358
  64. package/dist/syncGroups/SyncGroupsPathHelper.d.ts +26 -26
  65. package/dist/syncGroups/SyncGroupsPathHelper.js +90 -90
  66. package/dist/syncGroups/SyncGroupsState.d.ts +35 -35
  67. package/dist/syncGroups/SyncGroupsState.js +125 -125
  68. package/dist/syncGroups/SyncGroupsTransaction.d.ts +154 -154
  69. package/dist/syncGroups/SyncGroupsTransaction.js +576 -576
  70. package/dist/tasks/TaskHandler.d.ts +77 -78
  71. package/dist/tasks/TaskHandler.js +275 -276
  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 -194
  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 -11
  79. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.js +277 -277
  80. package/dist/tests/testData/testDataCachedGetProduct.d.ts +5 -5
  81. package/dist/tests/testData/testDataCachedGetProduct.js +185 -185
  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 +12 -12
  87. package/dist/tests/testData/testDataOptions.js +60 -60
  88. package/dist/tests/testData/testDataProductAggregatedPrice.d.ts +6 -6
  89. package/dist/tests/testData/testDataProductAggregatedPrice.js +187 -187
  90. package/dist/tests/testData/testDataUpcharge.d.ts +8 -8
  91. package/dist/tests/testData/testDataUpcharge.js +119 -119
  92. package/dist/utilitiesCatalogueData.d.ts +42 -31
  93. package/dist/utilitiesCatalogueData.js +173 -162
  94. package/dist/utilitiesCataloguePermission.d.ts +38 -37
  95. package/dist/utilitiesCataloguePermission.js +79 -80
  96. package/dist/utilitiesNumericValues.d.ts +24 -24
  97. package/dist/utilitiesNumericValues.js +109 -109
  98. package/package.json +3 -3
@@ -1,426 +1,416 @@
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 { compareArrays, count, Observable, } from "@configura/web-utilities";
11
- import { CfgProduct } from "../CfgProduct.js";
12
- import { CfgMtrlApplication } from "../material/CfgMtrlApplication.js";
13
- import { CfgMtrlApplicationSource } from "../material/CfgMtrlApplicationSource.js";
14
- import { recursivelyGetPriceCodeValue } from "../utilitiesCatalogueData.js";
15
- import { NumericValuesSelection } from "../utilitiesNumericValues.js";
16
- import { CfgFeature, SelectionType } from "./CfgFeature.js";
17
- import { getMtrlPreview, syncCfgFeatures } from "./utilitiesProductConfiguration.js";
18
- export var ProductConfigurationBubbleMode;
19
- (function (ProductConfigurationBubbleMode) {
20
- /**
21
- * If this is select it will turns on all ancestors all the way up.
22
- */
23
- ProductConfigurationBubbleMode["BubbleSelected"] = "BubbleSelected";
24
- /**
25
- * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
26
- * after validate.
27
- */
28
- ProductConfigurationBubbleMode["Validate"] = "Validate";
29
- /**
30
- * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
31
- * after validate. If this is select it will turn on all ancestors all the way up.
32
- * So with this mode it is possible to select an option where its parents are not selected.
33
- */
34
- ProductConfigurationBubbleMode["ValidateAndBubbleSelected"] = "ValidateAndBubbleSelected";
35
- /**
36
- * Like ValidateAndBubbleSelected, but SyncGroups are applied after ValidateAndBubbleSelected
37
- * has been done
38
- */
39
- ProductConfigurationBubbleMode["ValidateAndBubbleSelectedAndApplySyncGroups"] = "ValidateAndBubbleSelectedAndApplySyncGroups";
40
- /**
41
- * Stop bubbling
42
- * This mode supports internal functionality and is not expected to be used by integrators.
43
- */
44
- ProductConfigurationBubbleMode["Stop"] = "Stop";
45
- /**
46
- * Bubble to the next level up the tree. In features-options the next level is considered
47
- * the next feature, so option levels are skipped over. The node we call from notifies its
48
- * parent, and the parent switches out the reference to the node. Then bubbling stops.
49
- * This mode supports internal functionality and is not expected to be used by integrators.
50
- */
51
- ProductConfigurationBubbleMode["OneLevel"] = "OneLevel";
52
- /**
53
- * Bubble to the closest CfgProduct without doing any validation
54
- * This mode supports internal functionality and is not expected to be used by integrators.
55
- */
56
- ProductConfigurationBubbleMode["ToParentProduct"] = "ToParentProduct";
57
- /**
58
- * Bubble to the root CfgProduct
59
- * This mode supports internal functionality and is not expected to be used by integrators.
60
- */
61
- ProductConfigurationBubbleMode["ToRoot"] = "ToRoot";
62
- })(ProductConfigurationBubbleMode || (ProductConfigurationBubbleMode = {}));
63
- /** Overlap in one child also count */
64
- function doesChildrenShareOptionsCode(features) {
65
- const optionCodeSet = new Set();
66
- for (const feature of features) {
67
- for (const option of feature.options) {
68
- const code = option.code;
69
- if (optionCodeSet.has(code)) {
70
- return true;
71
- }
72
- optionCodeSet.add(code);
73
- }
74
- }
75
- return false;
76
- }
77
- /**
78
- * This class is only meant to be used through CfgOption. It should never be instantiated on its
79
- * own. Normally the internal state of this class should never be directly modified. CfgOption is
80
- * the class that should be used and interacted with.
81
- */
82
- export class _CfgOptionInternal {
83
- constructor(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
84
- this.rawOption = rawOption;
85
- this.allRawFeatures = allRawFeatures;
86
- this.parent = parent;
87
- this.parentConfiguration = parentConfiguration;
88
- this.parentProduct = parentProduct;
89
- this.rootProduct = rootProduct;
90
- this.changeObservable = new Observable();
91
- /** Called by child to tell its parent that it has changed. */
92
- this._childHasChanged = (freshRef, bubbleMode) => __awaiter(this, void 0, void 0, function* () {
93
- const features = this._features;
94
- if (features === undefined) {
95
- throw Error("Child says it changed, but no children has actually been generated");
96
- }
97
- const i = features.findIndex((a) => a.isBackedBySame(freshRef));
98
- if (i === -1) {
99
- throw Error("Child feature not found");
100
- }
101
- features[i] = freshRef;
102
- // In CfgOption we let stop bubble slip through. This is because CfgOption is sort of
103
- // a semi level. Like Feature + Option together constitutes one level.
104
- if (bubbleMode === ProductConfigurationBubbleMode.ValidateAndBubbleSelected ||
105
- bubbleMode === ProductConfigurationBubbleMode.BubbleSelected) {
106
- // selectOption takes care of the bubble
107
- yield this.parent.selectOption(this, true, bubbleMode);
108
- }
109
- else {
110
- yield this.parent._childHasChanged(this, bubbleMode);
111
- }
112
- });
113
- this.getApiSelection = () => {
114
- const { features, isUseNumericValue, code, numericValue } = this;
115
- const selectionTrees = features.map((f) => f._internal.getApiSelection());
116
- const mergedSelectionTree = {};
117
- let anyItems = false;
118
- for (const selectionTree of selectionTrees) {
119
- if (selectionTree === undefined) {
120
- continue;
121
- }
122
- for (const key of Object.keys(selectionTree)) {
123
- if (mergedSelectionTree[key] !== undefined) {
124
- console.warn(`The key (${key}) is already used in the selection tree. Option key: "${this.key}".`);
125
- continue;
126
- }
127
- mergedSelectionTree[key] = selectionTree[key];
128
- anyItems = true;
129
- }
130
- }
131
- if (isUseNumericValue && numericValue === undefined) {
132
- throw new Error("numeric Sku feature with option without numeric value");
133
- }
134
- const selectedOption = {
135
- code,
136
- numericValue: isUseNumericValue && numericValue !== undefined
137
- ? { value: numericValue, unit: this.unit }
138
- : undefined,
139
- };
140
- if (anyItems) {
141
- selectedOption.next = mergedSelectionTree;
142
- }
143
- return selectedOption;
144
- };
145
- this.setApiSelection = (apiOptionSelection) => __awaiter(this, void 0, void 0, function* () {
146
- let features;
147
- if (apiOptionSelection === undefined) {
148
- features = this._features || []; // All already generated children
149
- }
150
- else {
151
- features = this.features; // This will generate all children
152
- }
153
- return (yield Promise.all(features.map((f) => f._internal.setApiSelection(apiOptionSelection ? apiOptionSelection.next : undefined)))).some((b) => b);
154
- });
155
- this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this.keyMatch(other, descriptionMatch) &&
156
- compareArrays(this.features, other.features, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
157
- this.tryMatchSelection = (other, descriptionMatch = false) => __awaiter(this, void 0, void 0, function* () {
158
- return (yield Promise.all(other.features.map((otherF) => (() => __awaiter(this, void 0, void 0, function* () {
159
- if (1 <
160
- count(other.features, (item) => item._internal.keyMatch(otherF._internal, descriptionMatch))) {
161
- console.warn("tryMatchSelection will ignore items with same key");
162
- return false;
163
- }
164
- const toSetFeatures = this.features.filter((f) => f._internal.keyMatch(otherF._internal, descriptionMatch));
165
- if (1 < toSetFeatures.length) {
166
- console.warn("tryMatchSelection will ignore items with same key");
167
- return false;
168
- }
169
- if (toSetFeatures.length === 0) {
170
- return false;
171
- }
172
- return yield toSetFeatures[0]._internal.tryMatchSelection(otherF._internal, descriptionMatch);
173
- }))()))).some((b) => b);
174
- });
175
- this.keyMatch = (other, descriptionMatch = false) => descriptionMatch
176
- ? this.description.toLowerCase() === other.description.toLowerCase()
177
- : this.code === other.code;
178
- this._getFeaturesWithCode = (code) => this.features.reduce((agg, feature) => {
179
- agg.push(...feature._internal._getFeaturesWithCode(code));
180
- return agg;
181
- }, []);
182
- // Description based key helps when switching between products with similar feature-options
183
- // tree and trying to retain made selection.
184
- this.key =
185
- this.description +
186
- (this.description === "" || siblingHasDuplicateDescription ? this.code : "");
187
- const rawRanges = rawOption.codeRanges;
188
- if (rawRanges === undefined || rawRanges.length === 0) {
189
- if (this.isUseNumericValue) {
190
- throw new Error("Options in numeric sku Features must have at lease one code range");
191
- }
192
- }
193
- else {
194
- this.allowedNumericValues = new NumericValuesSelection(rawRanges);
195
- this._numericValue = this.allowedNumericValues.first;
196
- }
197
- }
198
- isAllowedNumericValue(val) {
199
- const allowedNumericValues = this.allowedNumericValues;
200
- if (allowedNumericValues === undefined) {
201
- return false;
202
- }
203
- if (this.isUseNumericValue) {
204
- return allowedNumericValues.includesValue(val);
205
- }
206
- // There is some limited support for numeric values on non numeric features.
207
- // The first value is always used in these cases.
208
- return allowedNumericValues.first === val;
209
- }
210
- get code() {
211
- return this.rawOption.code;
212
- }
213
- get isUseNumericValue() {
214
- return this.parent.isUseNumericValue;
215
- }
216
- get numericValue() {
217
- return this._numericValue;
218
- }
219
- setNumericValue(val, doSelectOption) {
220
- return __awaiter(this, void 0, void 0, function* () {
221
- if (!this.isAllowedNumericValue(val)) {
222
- throw new Error(`The value ${val} is not allowed. This could be because: 1. There are no allowed ranges defined 2. The Feature is numeric selection and the value does not fit in the allowed values 3. The Feature is not numeric selection and the value is not the first in the allowed ranges.`);
223
- }
224
- let change = false;
225
- if (this.isUseNumericValue) {
226
- if (this._numericValue !== val) {
227
- this._numericValue = val;
228
- change = true;
229
- }
230
- // It could be that even though our value did not change some sibling value did, and
231
- // this could make it needed to bubble later. Maybe. A bit uncertain about why I did
232
- // not put this in the if-statement above //Linus
233
- if (yield this.parent.pushStretch()) {
234
- change = true;
235
- }
236
- }
237
- if (doSelectOption) {
238
- if (yield this.parent.selectOption(this, true, ProductConfigurationBubbleMode.Validate)) {
239
- change = true;
240
- }
241
- }
242
- else {
243
- if (change) {
244
- yield this.parent._childHasChanged(this, ProductConfigurationBubbleMode.OneLevel);
245
- }
246
- }
247
- return change;
248
- });
249
- }
250
- get unit() {
251
- return this.parent.unit;
252
- }
253
- get description() {
254
- return this.rawOption.description;
255
- }
256
- get selected() {
257
- return this.parent.isSelected(this);
258
- }
259
- get selectedChangeInProgress() {
260
- const syncGroupHandler = this.rootProduct.syncGroupHandler;
261
- if (syncGroupHandler === undefined) {
262
- return false;
263
- }
264
- const inProgressOption = syncGroupHandler.pending;
265
- if (inProgressOption === this) {
266
- return true;
267
- }
268
- if (!(this.selected && this.parent.selectionType === SelectionType.SelectOne)) {
269
- return false;
270
- }
271
- return this.parent.options.some((o) => o._internal === inProgressOption);
272
- }
273
- get ancestorsSelected() {
274
- return this.selected && this.parent.ancestorsSelected;
275
- }
276
- get mtrlApplications() {
277
- if (this._mtrlApplications === undefined) {
278
- this._mtrlApplications = (this.rawOption.mtrlApplications || []).map((m) => CfgMtrlApplication.fromMtrlLikeApplication(CfgMtrlApplicationSource.Option, m));
279
- }
280
- return this._mtrlApplications;
281
- }
282
- get thumbnail() {
283
- return this.rawOption.material || getMtrlPreview(this.mtrlApplications);
284
- }
285
- get upcharge() {
286
- let upcharge = this.rawOption.upcharge || 0;
287
- const priceCodes = this.rawOption.priceCodes || [];
288
- const prices = this.rootProduct.prices;
289
- upcharge += recursivelyGetPriceCodeValue(priceCodes, prices) || 0;
290
- return upcharge;
291
- }
292
- get priceChangeAtSelectChange() {
293
- if (!this.parent.hasUpcharge) {
294
- return undefined;
295
- }
296
- const upcharge = this.upcharge || 0;
297
- const isSelected = this.parent.isSelected(this);
298
- if (this.parent.selectionType === SelectionType.SelectMany) {
299
- return isSelected ? -upcharge : upcharge;
300
- }
301
- if (this.parent.selectionType === SelectionType.SelectOne) {
302
- if (isSelected) {
303
- return 0;
304
- }
305
- const selectedOptions = this.parent.selectedOptions;
306
- if (1 !== selectedOptions.length) {
307
- console.warn("More or less than one selected in select one");
308
- return upcharge;
309
- }
310
- return upcharge - (selectedOptions[0].upcharge || 0);
311
- }
312
- return undefined;
313
- }
314
- get features() {
315
- if (this._features === undefined) {
316
- const allRefs = this.rawOption.featureRefs || [];
317
- const features = syncCfgFeatures(allRefs, [], this.allRawFeatures, this, this.parentConfiguration, this.parentProduct, this.rootProduct);
318
- if (doesChildrenShareOptionsCode(features)) {
319
- throw new Error("Stage does not yet properly support Options that has multiple sub-features with overlapping option codes.");
320
- }
321
- this._features = features;
322
- }
323
- return this._features;
324
- }
325
- _freshRefDescendants() {
326
- const features = this._features || [];
327
- for (let i = 0; i < features.length; i++) {
328
- const featureInternal = features[i]._internal;
329
- featureInternal._freshRefDescendants();
330
- features[i] = CfgFeature._makeNewRefFrom(featureInternal);
331
- }
332
- }
333
- }
334
- export class CfgOption {
335
- /**
336
- * Private constructor and make-method because make new ref requires the constructor to
337
- * take an internal and we don't want those who instantiate CfgOption to have to be aware
338
- * of the internal.
339
- */
340
- constructor(_internal) {
341
- this._internal = _internal;
342
- this.isBackedBySame = (other) => this._internal === other._internal;
343
- this.setNumericValue = (val, doSelectOption) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setNumericValue(val, doSelectOption); });
344
- this.isAllowedNumericValue = (val) => this._internal.isAllowedNumericValue(val);
345
- /**
346
- * Selects this Option.
347
- * Only Options belonging to Features that are "select many" can be deselected.
348
- * Calling this will cause a validation call to the server.
349
- */
350
- this.setSelected = (on) => __awaiter(this, void 0, void 0, function* () {
351
- return yield this._internal.parent.selectOption(this._internal, on, ProductConfigurationBubbleMode.ValidateAndBubbleSelectedAndApplySyncGroups);
352
- });
353
- this.listenForChange = (l) => this._internal.changeObservable.listen(l);
354
- this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
355
- }
356
- static make(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
357
- return new this(new _CfgOptionInternal(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct));
358
- }
359
- /**
360
- * Makes an object wrapping the passed object. This is not a clone method,
361
- * it is a method to make a new outer reference. Like a shallow copy.
362
- * We use this to help frameworks that are build around using equals to detect change.
363
- */
364
- static _makeNewRefFrom(internal) {
365
- return new this(internal);
366
- }
367
- get parentProduct() {
368
- return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
369
- }
370
- get rootProduct() {
371
- return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
372
- }
373
- get rawOption() {
374
- return this._internal.rawOption;
375
- }
376
- get key() {
377
- return this._internal.key;
378
- }
379
- get code() {
380
- return this._internal.code;
381
- }
382
- get isUseNumericValue() {
383
- return this._internal.isUseNumericValue;
384
- }
385
- get numericValue() {
386
- return this._internal.numericValue;
387
- }
388
- get allowedNumericValues() {
389
- return this._internal.allowedNumericValues;
390
- }
391
- get unit() {
392
- return this._internal.unit;
393
- }
394
- get description() {
395
- return this._internal.description;
396
- }
397
- get selected() {
398
- return this._internal.selected;
399
- }
400
- /**
401
- * Selection state is in progress to be changed. This can be used in GUI
402
- * to display the state as transitioning, or as already changed.
403
- * If selectedChangeInProgress and:
404
- * selected is true, it means that this is about to get unselected
405
- * selected is false, it means that this is about to get selected
406
- */
407
- get selectedChangeInProgress() {
408
- return this._internal.selectedChangeInProgress;
409
- }
410
- /** Are all ancestors up to the CfgProductConfiguration selected? Includes self. */
411
- get ancestorsSelected() {
412
- return this._internal.ancestorsSelected;
413
- }
414
- get thumbnail() {
415
- return this._internal.thumbnail;
416
- }
417
- get upcharge() {
418
- return this._internal.upcharge;
419
- }
420
- get priceChangeAtSelectChange() {
421
- return this._internal.priceChangeAtSelectChange;
422
- }
423
- get features() {
424
- return this._internal.features;
425
- }
426
- }
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 { compareArrays, count, Observable, } from "@configura/web-utilities";
11
+ import { CfgProduct } from "../CfgProduct.js";
12
+ import { CfgMtrlApplication } from "../material/CfgMtrlApplication.js";
13
+ import { CfgMtrlApplicationSource } from "../material/CfgMtrlApplicationSource.js";
14
+ import { recursivelyGetPriceCodeValue } from "../utilitiesCatalogueData.js";
15
+ import { NumericValuesSelection } from "../utilitiesNumericValues.js";
16
+ import { CfgFeature, SelectionType } from "./CfgFeature.js";
17
+ import { getMtrlPreview, syncCfgFeatures } from "./utilitiesProductConfiguration.js";
18
+ export var ProductConfigurationBubbleMode;
19
+ (function (ProductConfigurationBubbleMode) {
20
+ /**
21
+ * If this is select it will turns on all ancestors all the way up.
22
+ */
23
+ ProductConfigurationBubbleMode["BubbleSelected"] = "BubbleSelected";
24
+ /**
25
+ * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
26
+ * after validate.
27
+ */
28
+ ProductConfigurationBubbleMode["Validate"] = "Validate";
29
+ /**
30
+ * Bubble to the closest CfgProduct, let it revalidate, then that will continue the bubble
31
+ * after validate. If this is select it will turn on all ancestors all the way up.
32
+ * So with this mode it is possible to select an option where its parents are not selected.
33
+ */
34
+ ProductConfigurationBubbleMode["ValidateAndBubbleSelected"] = "ValidateAndBubbleSelected";
35
+ /**
36
+ * Like ValidateAndBubbleSelected, but SyncGroups are applied after ValidateAndBubbleSelected
37
+ * has been done
38
+ */
39
+ ProductConfigurationBubbleMode["ValidateAndBubbleSelectedAndApplySyncGroups"] = "ValidateAndBubbleSelectedAndApplySyncGroups";
40
+ /**
41
+ * Stop bubbling
42
+ * This mode supports internal functionality and is not expected to be used by integrators.
43
+ */
44
+ ProductConfigurationBubbleMode["Stop"] = "Stop";
45
+ /**
46
+ * Bubble to the next level up the tree. In features-options the next level is considered
47
+ * the next feature, so option levels are skipped over. The node we call from notifies its
48
+ * parent, and the parent switches out the reference to the node. Then bubbling stops.
49
+ * This mode supports internal functionality and is not expected to be used by integrators.
50
+ */
51
+ ProductConfigurationBubbleMode["OneLevel"] = "OneLevel";
52
+ /**
53
+ * Bubble to the closest CfgProduct without doing any validation
54
+ * This mode supports internal functionality and is not expected to be used by integrators.
55
+ */
56
+ ProductConfigurationBubbleMode["ToParentProduct"] = "ToParentProduct";
57
+ /**
58
+ * Bubble to the root CfgProduct
59
+ * This mode supports internal functionality and is not expected to be used by integrators.
60
+ */
61
+ ProductConfigurationBubbleMode["ToRoot"] = "ToRoot";
62
+ })(ProductConfigurationBubbleMode || (ProductConfigurationBubbleMode = {}));
63
+ /** Overlap in one child also count */
64
+ function doesChildrenShareOptionsCode(features) {
65
+ const optionCodeSet = new Set();
66
+ for (const feature of features) {
67
+ for (const option of feature.options) {
68
+ const code = option.code;
69
+ if (optionCodeSet.has(code)) {
70
+ return true;
71
+ }
72
+ optionCodeSet.add(code);
73
+ }
74
+ }
75
+ return false;
76
+ }
77
+ /**
78
+ * This class is only meant to be used through CfgOption. It should never be instantiated on its
79
+ * own. Normally the internal state of this class should never be directly modified. CfgOption is
80
+ * the class that should be used and interacted with.
81
+ */
82
+ export class _CfgOptionInternal {
83
+ constructor(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
84
+ this.rawOption = rawOption;
85
+ this.allRawFeatures = allRawFeatures;
86
+ this.parent = parent;
87
+ this.parentConfiguration = parentConfiguration;
88
+ this.parentProduct = parentProduct;
89
+ this.rootProduct = rootProduct;
90
+ this.changeObservable = new Observable();
91
+ /** Called by child to tell its parent that it has changed. */
92
+ this._childHasChanged = (freshRef, bubbleMode, committed) => __awaiter(this, void 0, void 0, function* () {
93
+ const features = this._features;
94
+ if (features === undefined) {
95
+ throw Error("Child says it changed, but no children has actually been generated");
96
+ }
97
+ const i = features.findIndex((a) => a.isBackedBySame(freshRef));
98
+ if (i === -1) {
99
+ throw Error("Child feature not found");
100
+ }
101
+ features[i] = freshRef;
102
+ // In CfgOption we let stop bubble slip through. This is because CfgOption is sort of
103
+ // a semi level. Like Feature + Option together constitutes one level.
104
+ if (bubbleMode === ProductConfigurationBubbleMode.ValidateAndBubbleSelected ||
105
+ bubbleMode === ProductConfigurationBubbleMode.BubbleSelected) {
106
+ // selectOption takes care of the bubble
107
+ yield this.parent.selectOption(this, true, bubbleMode);
108
+ }
109
+ else {
110
+ yield this.parent._childHasChanged(this, bubbleMode, committed);
111
+ }
112
+ });
113
+ this.getDtoConf = (includeExtendedData) => {
114
+ const { features, isUseNumericValue, code, selected, numericValue } = this;
115
+ if (!selected) {
116
+ throw new Error("Currently only useable on selected options. Selected in the result is for future use.");
117
+ }
118
+ const result = {
119
+ code,
120
+ selected, // For future use
121
+ };
122
+ if (isUseNumericValue) {
123
+ if (numericValue === undefined) {
124
+ throw new Error("numeric Sku feature with option without numeric value");
125
+ }
126
+ else {
127
+ result.numericValue = { value: numericValue, unit: this.unit };
128
+ }
129
+ }
130
+ if (0 < features.length) {
131
+ result.features = features.map((f) => f._internal.getDtoConf(includeExtendedData));
132
+ }
133
+ return result;
134
+ };
135
+ this.setApiSelection = (apiOptionSelection) => __awaiter(this, void 0, void 0, function* () {
136
+ let features;
137
+ if (apiOptionSelection === undefined) {
138
+ features = this._features || []; // All already generated children
139
+ }
140
+ else {
141
+ features = this.features; // This will generate all children
142
+ }
143
+ return (yield Promise.all(features.map((f) => f._internal.setApiSelection(apiOptionSelection ? apiOptionSelection.next : undefined)))).some((b) => b);
144
+ });
145
+ this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this.keyMatch(other, descriptionMatch) &&
146
+ compareArrays(this.features, other.features, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
147
+ this.tryMatchSelection = (other, descriptionMatch = false) => __awaiter(this, void 0, void 0, function* () {
148
+ return (yield Promise.all(other.features.map((otherF) => (() => __awaiter(this, void 0, void 0, function* () {
149
+ if (1 <
150
+ count(other.features, (item) => item._internal.keyMatch(otherF._internal, descriptionMatch))) {
151
+ console.warn("tryMatchSelection will ignore items with same key");
152
+ return false;
153
+ }
154
+ const toSetFeatures = this.features.filter((f) => f._internal.keyMatch(otherF._internal, descriptionMatch));
155
+ if (1 < toSetFeatures.length) {
156
+ console.warn("tryMatchSelection will ignore items with same key");
157
+ return false;
158
+ }
159
+ if (toSetFeatures.length === 0) {
160
+ return false;
161
+ }
162
+ return yield toSetFeatures[0]._internal.tryMatchSelection(otherF._internal, descriptionMatch);
163
+ }))()))).some((b) => b);
164
+ });
165
+ this.keyMatch = (other, descriptionMatch = false) => descriptionMatch
166
+ ? this.description.toLowerCase() === other.description.toLowerCase()
167
+ : this.code === other.code;
168
+ this._getFeaturesWithCode = (code) => this.features.reduce((agg, feature) => {
169
+ agg.push(...feature._internal._getFeaturesWithCode(code));
170
+ return agg;
171
+ }, []);
172
+ // Description based key helps when switching between products with similar feature-options
173
+ // tree and trying to retain made selection.
174
+ this.key =
175
+ this.description +
176
+ (this.description === "" || siblingHasDuplicateDescription ? this.code : "");
177
+ const rawRanges = rawOption.codeRanges;
178
+ if (rawRanges === undefined || rawRanges.length === 0) {
179
+ if (this.isUseNumericValue) {
180
+ throw new Error("Options in numeric sku Features must have at lease one code range");
181
+ }
182
+ }
183
+ else {
184
+ this.allowedNumericValues = new NumericValuesSelection(rawRanges);
185
+ this._numericValue = this.allowedNumericValues.first;
186
+ }
187
+ }
188
+ isAllowedNumericValue(val) {
189
+ const allowedNumericValues = this.allowedNumericValues;
190
+ if (allowedNumericValues === undefined) {
191
+ return false;
192
+ }
193
+ if (this.isUseNumericValue) {
194
+ return allowedNumericValues.includesValue(val);
195
+ }
196
+ // There is some limited support for numeric values on non numeric features.
197
+ // The first value is always used in these cases.
198
+ return allowedNumericValues.first === val;
199
+ }
200
+ get code() {
201
+ return this.rawOption.code;
202
+ }
203
+ get isUseNumericValue() {
204
+ return this.parent.isUseNumericValue;
205
+ }
206
+ get numericValue() {
207
+ return this._numericValue;
208
+ }
209
+ setNumericValue(val, doSelectOption) {
210
+ return __awaiter(this, void 0, void 0, function* () {
211
+ if (!this.isAllowedNumericValue(val)) {
212
+ throw new Error(`The value ${val} is not allowed. This could be because: 1. There are no allowed ranges defined 2. The Feature is numeric selection and the value does not fit in the allowed values 3. The Feature is not numeric selection and the value is not the first in the allowed ranges.`);
213
+ }
214
+ let change = false;
215
+ if (this.isUseNumericValue) {
216
+ if (this._numericValue !== val) {
217
+ this._numericValue = val;
218
+ change = true;
219
+ }
220
+ // It could be that even though our value did not change some sibling value did, and
221
+ // this could make it needed to bubble later. Maybe. A bit uncertain about why I did
222
+ // not put this in the if-statement above //Linus
223
+ if (yield this.parent.pushStretch()) {
224
+ change = true;
225
+ }
226
+ }
227
+ if (doSelectOption) {
228
+ if (yield this.parent.selectOption(this, true, ProductConfigurationBubbleMode.Validate)) {
229
+ change = true;
230
+ }
231
+ }
232
+ else {
233
+ if (change) {
234
+ yield this.parent._childHasChanged(this, ProductConfigurationBubbleMode.Stop, false);
235
+ }
236
+ }
237
+ return change;
238
+ });
239
+ }
240
+ get unit() {
241
+ return this.parent.unit;
242
+ }
243
+ get description() {
244
+ return this.rawOption.description;
245
+ }
246
+ get selected() {
247
+ return this.parent.isSelected(this);
248
+ }
249
+ get selectedChangeInProgress() {
250
+ const syncGroupHandler = this.rootProduct.syncGroupHandler;
251
+ if (syncGroupHandler === undefined) {
252
+ return false;
253
+ }
254
+ const inProgressOption = syncGroupHandler.pending;
255
+ if (inProgressOption === this) {
256
+ return true;
257
+ }
258
+ if (!(this.selected && this.parent.selectionType === SelectionType.SelectOne)) {
259
+ return false;
260
+ }
261
+ return this.parent.options.some((o) => o._internal === inProgressOption);
262
+ }
263
+ get ancestorsSelected() {
264
+ return this.selected && this.parent.ancestorsSelected;
265
+ }
266
+ get mtrlApplications() {
267
+ if (this._mtrlApplications === undefined) {
268
+ this._mtrlApplications = (this.rawOption.mtrlApplications || []).map((m) => CfgMtrlApplication.fromMtrlLikeApplication(CfgMtrlApplicationSource.Option, m));
269
+ }
270
+ return this._mtrlApplications;
271
+ }
272
+ get thumbnail() {
273
+ return this.rawOption.material || getMtrlPreview(this.mtrlApplications);
274
+ }
275
+ get upcharge() {
276
+ let upcharge = this.rawOption.upcharge || 0;
277
+ const priceCodes = this.rawOption.priceCodes || [];
278
+ const prices = this.rootProduct.prices;
279
+ upcharge += recursivelyGetPriceCodeValue(priceCodes, prices) || 0;
280
+ return upcharge;
281
+ }
282
+ get priceChangeAtSelectChange() {
283
+ if (!this.parent.hasUpcharge) {
284
+ return undefined;
285
+ }
286
+ const upcharge = this.upcharge || 0;
287
+ const isSelected = this.parent.isSelected(this);
288
+ if (this.parent.selectionType === SelectionType.SelectMany) {
289
+ return isSelected ? -upcharge : upcharge;
290
+ }
291
+ if (this.parent.selectionType === SelectionType.SelectOne) {
292
+ if (isSelected) {
293
+ return 0;
294
+ }
295
+ const selectedOptions = this.parent.selectedOptions;
296
+ if (1 !== selectedOptions.length) {
297
+ console.warn("More or less than one selected in select one");
298
+ return upcharge;
299
+ }
300
+ return upcharge - (selectedOptions[0].upcharge || 0);
301
+ }
302
+ return undefined;
303
+ }
304
+ get features() {
305
+ if (this._features === undefined) {
306
+ const allRefs = this.rawOption.featureRefs || [];
307
+ const features = syncCfgFeatures(allRefs, [], this.allRawFeatures, this, this.parentConfiguration, this.parentProduct, this.rootProduct);
308
+ if (doesChildrenShareOptionsCode(features)) {
309
+ throw new Error("Stage does not yet properly support Options that has multiple sub-features with overlapping option codes.");
310
+ }
311
+ this._features = features;
312
+ }
313
+ return this._features;
314
+ }
315
+ _freshRefDescendants() {
316
+ const features = this._features || [];
317
+ for (let i = 0; i < features.length; i++) {
318
+ const featureInternal = features[i]._internal;
319
+ featureInternal._freshRefDescendants();
320
+ features[i] = CfgFeature._makeNewRefFrom(featureInternal);
321
+ }
322
+ }
323
+ }
324
+ export class CfgOption {
325
+ /**
326
+ * Private constructor and make-method because make new ref requires the constructor to
327
+ * take an internal and we don't want those who instantiate CfgOption to have to be aware
328
+ * of the internal.
329
+ */
330
+ constructor(_internal) {
331
+ this._internal = _internal;
332
+ this.isBackedBySame = (other) => this._internal === other._internal;
333
+ this.setNumericValue = (val, doSelectOption) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setNumericValue(val, doSelectOption); });
334
+ this.isAllowedNumericValue = (val) => this._internal.isAllowedNumericValue(val);
335
+ /**
336
+ * Selects this Option.
337
+ * Only Options belonging to Features that are "select many" can be deselected.
338
+ * Calling this will cause a validation call to the server.
339
+ */
340
+ this.setSelected = (on) => __awaiter(this, void 0, void 0, function* () {
341
+ return yield this._internal.parent.selectOption(this._internal, on, ProductConfigurationBubbleMode.ValidateAndBubbleSelectedAndApplySyncGroups);
342
+ });
343
+ this.listenForChange = (l) => this._internal.changeObservable.listen(l);
344
+ this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
345
+ }
346
+ static make(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct) {
347
+ return new this(new _CfgOptionInternal(rawOption, allRawFeatures, siblingHasDuplicateDescription, parent, parentConfiguration, parentProduct, rootProduct));
348
+ }
349
+ /**
350
+ * Makes an object wrapping the passed object. This is not a clone method,
351
+ * it is a method to make a new outer reference. Like a shallow copy.
352
+ * We use this to help frameworks that are build around using equals to detect change.
353
+ */
354
+ static _makeNewRefFrom(internal) {
355
+ return new this(internal);
356
+ }
357
+ get parentProduct() {
358
+ return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
359
+ }
360
+ get rootProduct() {
361
+ return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
362
+ }
363
+ get rawOption() {
364
+ return this._internal.rawOption;
365
+ }
366
+ get key() {
367
+ return this._internal.key;
368
+ }
369
+ get code() {
370
+ return this._internal.code;
371
+ }
372
+ get isUseNumericValue() {
373
+ return this._internal.isUseNumericValue;
374
+ }
375
+ get numericValue() {
376
+ return this._internal.numericValue;
377
+ }
378
+ get allowedNumericValues() {
379
+ return this._internal.allowedNumericValues;
380
+ }
381
+ get unit() {
382
+ return this._internal.unit;
383
+ }
384
+ get description() {
385
+ return this._internal.description;
386
+ }
387
+ get selected() {
388
+ return this._internal.selected;
389
+ }
390
+ /**
391
+ * Selection state is in progress to be changed. This can be used in GUI
392
+ * to display the state as transitioning, or as already changed.
393
+ * If selectedChangeInProgress and:
394
+ * selected is true, it means that this is about to get unselected
395
+ * selected is false, it means that this is about to get selected
396
+ */
397
+ get selectedChangeInProgress() {
398
+ return this._internal.selectedChangeInProgress;
399
+ }
400
+ /** Are all ancestors up to the CfgProductConfiguration selected? Includes self. */
401
+ get ancestorsSelected() {
402
+ return this._internal.ancestorsSelected;
403
+ }
404
+ get thumbnail() {
405
+ return this._internal.thumbnail;
406
+ }
407
+ get upcharge() {
408
+ return this._internal.upcharge;
409
+ }
410
+ get priceChangeAtSelectChange() {
411
+ return this._internal.priceChangeAtSelectChange;
412
+ }
413
+ get features() {
414
+ return this._internal.features;
415
+ }
416
+ }