@configura/web-api 1.4.0-alpha.0 → 1.4.0-alpha.4

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 (57) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +1 -1
  3. package/dist/CatalogueAPI.d.ts +504 -502
  4. package/dist/CatalogueAPI.js +248 -206
  5. package/dist/CfgMeasure.d.ts +32 -32
  6. package/dist/CfgMeasure.js +30 -30
  7. package/dist/CfgProduct.d.ts +217 -217
  8. package/dist/CfgProduct.js +636 -634
  9. package/dist/index.d.ts +16 -16
  10. package/dist/index.js +16 -16
  11. package/dist/material/CfgMaterialMapping.d.ts +7 -7
  12. package/dist/material/CfgMaterialMapping.js +181 -181
  13. package/dist/material/CfgMtrlApplication.d.ts +18 -18
  14. package/dist/material/CfgMtrlApplication.js +43 -43
  15. package/dist/material/CfgMtrlApplicationSource.d.ts +7 -7
  16. package/dist/material/CfgMtrlApplicationSource.js +8 -8
  17. package/dist/material/CfgMtrlSource.d.ts +19 -19
  18. package/dist/material/CfgMtrlSource.js +40 -40
  19. package/dist/material/CfgMtrlSourceWithMetaData.d.ts +7 -7
  20. package/dist/material/CfgMtrlSourceWithMetaData.js +1 -1
  21. package/dist/productConfiguration/CfgFeature.d.ts +160 -160
  22. package/dist/productConfiguration/CfgFeature.js +593 -593
  23. package/dist/productConfiguration/CfgOption.d.ts +128 -128
  24. package/dist/productConfiguration/CfgOption.js +394 -394
  25. package/dist/productConfiguration/CfgProductConfiguration.d.ts +120 -120
  26. package/dist/productConfiguration/CfgProductConfiguration.js +307 -307
  27. package/dist/productConfiguration/filters.d.ts +15 -7
  28. package/dist/productConfiguration/filters.js +67 -29
  29. package/dist/productConfiguration/productParamsGenerator.d.ts +15 -15
  30. package/dist/productConfiguration/productParamsGenerator.js +51 -51
  31. package/dist/productConfiguration/utilitiesProductConfiguration.d.ts +17 -17
  32. package/dist/productConfiguration/utilitiesProductConfiguration.js +80 -80
  33. package/dist/productLoader.d.ts +33 -33
  34. package/dist/productLoader.js +49 -49
  35. package/dist/tests/testData/collectorForTest.d.ts +73 -73
  36. package/dist/tests/testData/collectorForTest.js +195 -195
  37. package/dist/tests/testData/dummyProductForTest.d.ts +4 -4
  38. package/dist/tests/testData/dummyProductForTest.js +36 -36
  39. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.d.ts +32 -32
  40. package/dist/tests/testData/testDataAdditionalProductInAdditionalProductInProductForTest.js +348 -381
  41. package/dist/tests/testData/testDataCachedGetProduct.d.ts +5 -5
  42. package/dist/tests/testData/testDataCachedGetProduct.js +196 -205
  43. package/dist/tests/testData/testDataCachedPostValidate.d.ts +7 -7
  44. package/dist/tests/testData/testDataCachedPostValidate.js +183 -195
  45. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.d.ts +3 -3
  46. package/dist/tests/testData/testDataNoAdditionalProductNoPropagateForTest.js +1099 -1119
  47. package/dist/tests/testData/testDataProductAggregatedPrice.d.ts +6 -28
  48. package/dist/tests/testData/testDataProductAggregatedPrice.js +198 -210
  49. package/dist/tests/testData/testDataUpcharge.d.ts +29 -29
  50. package/dist/tests/testData/testDataUpcharge.js +151 -163
  51. package/dist/utilitiesCatalogueData.d.ts +25 -22
  52. package/dist/utilitiesCatalogueData.js +64 -64
  53. package/dist/utilitiesCataloguePermission.d.ts +39 -39
  54. package/dist/utilitiesCataloguePermission.js +84 -84
  55. package/dist/utilitiesNumericValues.d.ts +24 -24
  56. package/dist/utilitiesNumericValues.js +109 -109
  57. package/package.json +3 -3
@@ -1,593 +1,593 @@
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, convertLength, count, Observable, someMatch, toLengthUnit, } 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 { CfgOption, ProductConfigurationBubbleMode } from "./CfgOption.js";
15
- import { _CfgProductConfigurationInternal } from "./CfgProductConfiguration.js";
16
- import { getMtrlPreview } from "./utilitiesProductConfiguration.js";
17
- export var SelectionType;
18
- (function (SelectionType) {
19
- /**
20
- * All options are permanently selected. In our ui-component for this we skip over this level,
21
- * and immediately show the children, but you could do this differently, like for instance
22
- * showing the group heading.
23
- */
24
- SelectionType[SelectionType["Group"] = 0] = "Group";
25
- /**
26
- * One and only one can be selected at a time. This normally corresponds to either a dropdown
27
- * menu or radio buttons.
28
- */
29
- SelectionType[SelectionType["SelectOne"] = 1] = "SelectOne";
30
- /** Zero to all can be selected at a time. This normally corresponds to checkboxes. */
31
- SelectionType[SelectionType["SelectMany"] = 2] = "SelectMany";
32
- })(SelectionType || (SelectionType = {}));
33
- function getOptionFilter(option1) {
34
- return (option2) => option2._internal === option1;
35
- }
36
- /** Returns a fresh reference for the option at the given index */
37
- function doFreshRefOptionAtIndex(options, index, beforeNotify) {
38
- const freshRef = CfgOption._makeNewRefFrom(options[index]._internal);
39
- options[index] = freshRef;
40
- if (beforeNotify) {
41
- beforeNotify(freshRef);
42
- }
43
- freshRef._internal.changeObservable.notifyAll({ freshRef });
44
- return freshRef;
45
- }
46
- /**
47
- * Returns a fresh reference for the given option.
48
- * @throws Throws an error if the option is not found in options.
49
- */
50
- function doFreshRefOption(options, optionInternal, beforeNotify) {
51
- const i = options.findIndex(getOptionFilter(optionInternal));
52
- if (i === -1) {
53
- throw new Error("Did not find the option in options");
54
- }
55
- return doFreshRefOptionAtIndex(options, i, beforeNotify);
56
- }
57
- /**
58
- * This class is meant to only be used through CfgFeature. It should
59
- * never be instantiated on its own. Normally the internal state of this class
60
- * should never be directly modified. CfgFeature is the class that
61
- * should be used and interacted with.
62
- */
63
- export class _CfgFeatureInternal {
64
- constructor(rawFeature, allRawFeatures, key, // Unique amongst siblings
65
- parent, parentConfiguration, parentProduct, rootProduct) {
66
- this.rawFeature = rawFeature;
67
- this.allRawFeatures = allRawFeatures;
68
- this.key = key;
69
- this.parent = parent;
70
- this.parentConfiguration = parentConfiguration;
71
- this.parentProduct = parentProduct;
72
- this.rootProduct = rootProduct;
73
- this._selectedOptions = [];
74
- this.changeObservable = new Observable();
75
- this.setNumericValue = (val) => __awaiter(this, void 0, void 0, function* () {
76
- if (this.selectionType !== SelectionType.SelectOne) {
77
- throw new Error("Only SelectOne Features can have numeric values");
78
- }
79
- const option = this.options.find((option) => option.isAllowedNumericValue(val));
80
- if (option === undefined) {
81
- throw new Error("No Option allows the value you tried to set");
82
- }
83
- return yield option.setNumericValue(val, true);
84
- });
85
- this._notifyAllOfChange = (bubbleMode) => __awaiter(this, void 0, void 0, function* () {
86
- if (bubbleMode === ProductConfigurationBubbleMode.Stop) {
87
- return;
88
- }
89
- const parent = this.parent;
90
- const freshRef = CfgFeature._makeNewRefFrom(this);
91
- this.changeObservable.notifyAll({ freshRef });
92
- if (parent !== undefined) {
93
- yield parent._childHasChanged(freshRef, bubbleMode === ProductConfigurationBubbleMode.OneLevel
94
- ? ProductConfigurationBubbleMode.Stop
95
- : bubbleMode);
96
- }
97
- });
98
- /**
99
- * Called by child to tell its parent that it has changed.
100
- * @throws Will throw if options have not yet been generated. This should be impossible
101
- * as nonexisting children can not call their parent.
102
- */
103
- this._childHasChanged = (childOption, bubbleMode) => __awaiter(this, void 0, void 0, function* () {
104
- const options = this._options;
105
- if (options === undefined) {
106
- throw Error("Child says it changed, but no children has actually been generated");
107
- }
108
- const freshRef = doFreshRefOption(options, childOption);
109
- const selectedOptions = this._selectedOptions;
110
- const i = selectedOptions.findIndex(getOptionFilter(childOption));
111
- if (i !== -1) {
112
- selectedOptions[i] = freshRef;
113
- }
114
- yield this._notifyAllOfChange(bubbleMode);
115
- });
116
- this.getApiSelection = () => {
117
- const result = {};
118
- for (const option of this._selectedOptions) {
119
- result[option.code] = option._internal.getApiSelection();
120
- }
121
- return result;
122
- };
123
- this.setApiSelection = (apiOptionSelectionMap) => __awaiter(this, void 0, void 0, function* () {
124
- const selectionType = this.selectionType;
125
- const isGroup = selectionType === SelectionType.Group;
126
- const isSelectOne = selectionType === SelectionType.SelectOne;
127
- const isAllOptionsAffectedByAnySelection = this.isAllOptionsAffectedByAnySelection;
128
- let options;
129
- if (apiOptionSelectionMap === undefined) {
130
- options = this._options || []; // Already generated children (all or none)
131
- apiOptionSelectionMap = {};
132
- }
133
- else {
134
- options = this.options; // This will generate all children
135
- }
136
- let change = false;
137
- for (let i = 0; i < options.length; i++) {
138
- const option = options[i];
139
- const apiOptionSelection = apiOptionSelectionMap[option.code];
140
- const selectedIndex = this._selectedOptions.findIndex(getOptionFilter(option._internal));
141
- const isOn = isGroup || apiOptionSelection;
142
- if (isOn) {
143
- if (isSelectOne) {
144
- if (this.isUseNumericValue) {
145
- const { numericValue } = apiOptionSelection;
146
- if (numericValue === undefined) {
147
- throw new Error("No numeric value for numeric feature");
148
- }
149
- const { value, unit } = numericValue;
150
- if (yield option.setNumericValue(unit === undefined
151
- ? value
152
- : convertLength(value, toLengthUnit(unit), this.unit), false)) {
153
- change = true;
154
- }
155
- }
156
- }
157
- if (selectedIndex === -1) {
158
- if (isAllOptionsAffectedByAnySelection) {
159
- this._selectedOptions.push(option);
160
- }
161
- else {
162
- doFreshRefOptionAtIndex(options, i, (freshRef) => this._selectedOptions.push(freshRef));
163
- }
164
- change = true;
165
- }
166
- }
167
- else {
168
- if (selectedIndex !== -1) {
169
- this._selectedOptions.splice(selectedIndex, 1);
170
- if (!isAllOptionsAffectedByAnySelection) {
171
- doFreshRefOptionAtIndex(options, i);
172
- }
173
- change = true;
174
- }
175
- }
176
- if (yield option._internal.setApiSelection(apiOptionSelection)) {
177
- change = true;
178
- }
179
- }
180
- if (this.ancestorsSelected) {
181
- const selectionCount = this._selectedOptions.length;
182
- if (isSelectOne && selectionCount !== 1) {
183
- const wrongCountWarning = `A select-one Feature (i.e. neither optional nor multiple) should have exactly one Option selected. Feature key: "${this.key}". Actual: ${selectionCount}`;
184
- if (this.rootProduct.settings.strictSelectOneSelectionCount) {
185
- throw new Error(wrongCountWarning);
186
- }
187
- else {
188
- console.warn(wrongCountWarning);
189
- }
190
- }
191
- if (!isGroup) {
192
- const apiSelectionCount = Object.keys(apiOptionSelectionMap).length;
193
- if (selectionCount !== apiSelectionCount) {
194
- const wrongCountWarning = `All provided Options are expected to be selected. Feature key: "${this.key}". Expected: ${apiSelectionCount} Actual: ${selectionCount}`;
195
- if (this.rootProduct.settings.strictSetApiSelectionMatch) {
196
- throw new Error(wrongCountWarning);
197
- }
198
- else {
199
- console.warn(wrongCountWarning);
200
- }
201
- }
202
- }
203
- }
204
- if (change) {
205
- if (isAllOptionsAffectedByAnySelection) {
206
- this._freshRefAllOptions();
207
- }
208
- if (selectionType === SelectionType.SelectOne) {
209
- this.pushStretch();
210
- }
211
- // setApiSelection works its way top down and handles notifications on
212
- // each level by looking at change returned from its children. That way
213
- // we do not get multiple notifications for the same ancestral node even
214
- // if multiple descendants are affected by the setApiSelection. Therefore
215
- // we use OneLevel to make this feature notify and the parent update its
216
- // reference.
217
- yield this._notifyAllOfChange(ProductConfigurationBubbleMode.OneLevel);
218
- }
219
- return change;
220
- });
221
- /** Pushes to refresh stretch. Does not cause validation. */
222
- this.pushStretch = () => __awaiter(this, void 0, void 0, function* () {
223
- if (this.selectionType !== SelectionType.SelectOne) {
224
- throw new Error("Can only push stretch for SelectOne");
225
- }
226
- const value = this.numericValue;
227
- return (yield Promise.all((this.measureParamCodes || []).map((measureParamCode) => this.parentConfiguration.setStretchReferenceLength(measureParamCode, value, this.unit)))).some((change) => change);
228
- });
229
- this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this.keyMatch(other, descriptionMatch) &&
230
- compareArrays(this.options, other.options, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
231
- this.tryMatchSelection = (other, descriptionMatch = false) => __awaiter(this, void 0, void 0, function* () {
232
- const { selectionType, isUseNumericValue } = this;
233
- if (selectionType !== other.selectionType) {
234
- // Will not try if the selection type is different
235
- return false;
236
- }
237
- if (isUseNumericValue !== other.isUseNumericValue) {
238
- return false;
239
- }
240
- let change = false;
241
- if (isUseNumericValue) {
242
- // For numeric options we do not try to go any further down. It's becoming to
243
- // strange to make informed guesses on how to progress down the tree.
244
- const otherNumericValue = other.numericValue;
245
- if (otherNumericValue === undefined) {
246
- throw new Error("Numeric feature without numeric value, this should never happen");
247
- }
248
- else {
249
- if (yield this.setNumericValue(otherNumericValue)) {
250
- change = true;
251
- }
252
- }
253
- }
254
- else {
255
- const thisOptions = this.options;
256
- const otherOptions = other.options;
257
- change = (yield Promise.all(otherOptions.map((otherO) => (() => __awaiter(this, void 0, void 0, function* () {
258
- const otherOSelected = otherO.selected;
259
- if (selectionType === SelectionType.SelectOne && !otherOSelected) {
260
- return false;
261
- }
262
- if (1 <
263
- count(otherOptions, (item) => item._internal.keyMatch(otherO._internal, descriptionMatch))) {
264
- console.warn("tryMatchSelection will ignore options that have the same key");
265
- return false;
266
- }
267
- const toTryChangeOptions = thisOptions.filter((o) => otherO._internal.keyMatch(o._internal, descriptionMatch));
268
- if (1 < toTryChangeOptions.length) {
269
- console.warn("tryMatchSelection will ignore options that have the same key");
270
- return false;
271
- }
272
- if (toTryChangeOptions.length === 0) {
273
- return false;
274
- }
275
- const toTryChangeOption = toTryChangeOptions[0];
276
- let change = false;
277
- if (selectionType === SelectionType.SelectMany ||
278
- selectionType === SelectionType.SelectOne) {
279
- // The setSelected will only affect ourselves,
280
- // so we do not need to bubble the notification.
281
- // Instead we use the change variable to know
282
- // when to notify for this feature.
283
- change = yield toTryChangeOption._internal.parent.selectOption(toTryChangeOption._internal, otherOSelected, ProductConfigurationBubbleMode.Stop);
284
- }
285
- if (otherOSelected &&
286
- (yield toTryChangeOption._internal.tryMatchSelection(otherO, descriptionMatch))) {
287
- change = true;
288
- }
289
- return change;
290
- }))()))).some((b) => b);
291
- }
292
- if (change) {
293
- // tryMatchSelection works its way top down and handles notifications on
294
- // each level by looking at change returned from its children. That way
295
- // we do not get multiple notifications for the same ancestral node even
296
- // if multiple descendants are affected by the tryMatchSelection. Therefore
297
- // we use OneLevel to make this feature notify and the parent update its
298
- // reference.
299
- yield this._notifyAllOfChange(ProductConfigurationBubbleMode.OneLevel);
300
- }
301
- return change;
302
- });
303
- /**
304
- * Normally this is used through methods on CfgFeature and CfgOption. Use this
305
- * internal version if you need to control the bubbleMode.
306
- * Using a validate bubbleMode will cause validation calls to the server.
307
- */
308
- this.selectOption = (optionInternal, on, bubbleMode) => __awaiter(this, void 0, void 0, function* () {
309
- if (!on) {
310
- if (this.selectionType === SelectionType.Group) {
311
- throw new Error(`Multiple features are always selected and are not user selectable. Feature key: "${this.key}".`);
312
- }
313
- if (this.selectionType === SelectionType.SelectOne) {
314
- throw new Error(`Select one can never be deselected. Feature key: "${this.key}".`);
315
- }
316
- }
317
- const selectedOptions = this._selectedOptions;
318
- const isAllOptionsAffectedByAnySelection = this.isAllOptionsAffectedByAnySelection;
319
- const index = selectedOptions.findIndex(getOptionFilter(optionInternal));
320
- const isActualChange = (index === -1) === on;
321
- if (on) {
322
- // Calling this.options will populate (generate) all Options if they have not yet
323
- // been generated. As we are selecting an option we need all Options to be generated.
324
- const options = this.options;
325
- if (this.selectionType === SelectionType.SelectOne) {
326
- let removeOption = selectedOptions.shift();
327
- while (removeOption !== undefined) {
328
- if (!isAllOptionsAffectedByAnySelection) {
329
- doFreshRefOption(options, removeOption._internal);
330
- }
331
- removeOption = selectedOptions.shift();
332
- }
333
- }
334
- if (isAllOptionsAffectedByAnySelection) {
335
- selectedOptions.push(CfgOption._makeNewRefFrom(optionInternal));
336
- }
337
- else {
338
- doFreshRefOption(options, optionInternal, (freshRef) => selectedOptions.push(freshRef));
339
- }
340
- if (this.selectionType === SelectionType.SelectOne) {
341
- yield this.pushStretch();
342
- }
343
- }
344
- else {
345
- selectedOptions.splice(index, 1);
346
- if (!isAllOptionsAffectedByAnySelection) {
347
- // Accessing this._options gives us the Options that have been generated
348
- // or undefined if the Options have not yet been generated. As this action
349
- // is deselect and deselected is the default uninitialized state we can
350
- // safely ignore if undefined.
351
- const options = this._options;
352
- if (options !== undefined) {
353
- doFreshRefOption(options, optionInternal);
354
- }
355
- }
356
- }
357
- if (isAllOptionsAffectedByAnySelection) {
358
- this._freshRefAllOptions();
359
- }
360
- // If this was a deselect action we shall not bubble selected, that is, we shall not
361
- // select the ancestors if this action was deselect.
362
- yield this._notifyAllOfChange(!on && bubbleMode === ProductConfigurationBubbleMode.ValidateAndBubbleSelected
363
- ? ProductConfigurationBubbleMode.Validate
364
- : bubbleMode);
365
- return isActualChange;
366
- });
367
- this.isSelected = (option) => this.selectionType === SelectionType.Group ||
368
- this._selectedOptions.some(getOptionFilter(option));
369
- this.keyMatch = (other, descriptionMatch = false) => descriptionMatch
370
- ? this.description.toLowerCase() === other.description.toLowerCase()
371
- : this.code === other.code;
372
- /** Only in selected options */
373
- this._getFeaturesWithCode = (code) => this._selectedOptions.reduce((agg, selectedOption) => {
374
- agg.push(...selectedOption._internal._getFeaturesWithCode(code));
375
- return agg;
376
- }, code === this.code ? [this] : []);
377
- if (rawFeature.multiple) {
378
- this.selectionType = SelectionType.Group;
379
- }
380
- else if (rawFeature.optional) {
381
- this.selectionType = SelectionType.SelectMany;
382
- }
383
- else {
384
- this.selectionType = SelectionType.SelectOne;
385
- }
386
- this.hasUpcharge = rawFeature.options.some((option) => (option.upcharge !== undefined && option.upcharge !== 0) ||
387
- (option.priceCodes !== undefined && option.priceCodes.length > 0));
388
- }
389
- get code() {
390
- return this.rawFeature.code;
391
- }
392
- get groupCode() {
393
- return this.rawFeature.groupCode;
394
- }
395
- get isUseNumericValue() {
396
- return this.rawFeature.numericOrder;
397
- }
398
- get numericValue() {
399
- if (this.selectionType !== SelectionType.SelectOne) {
400
- throw new Error("Only SelectOne Features can have numeric values");
401
- }
402
- const { selectedOptions } = this;
403
- if (selectedOptions.length === 0) {
404
- return undefined;
405
- }
406
- const value = selectedOptions[0].numericValue;
407
- if (value === undefined) {
408
- if (this.isUseNumericValue) {
409
- throw new Error("No numericValue set on option with numeric sku feature");
410
- }
411
- return undefined;
412
- }
413
- return value;
414
- }
415
- get description() {
416
- return this.rawFeature.description;
417
- }
418
- /**
419
- * The MeasureParam class is re-used for different purposes. In Features it is used
420
- * to indicate which stretch measures inside Models shall be affected by this state
421
- * of this Feature. Hence only the code property is used.
422
- */
423
- get measureParamCodes() {
424
- var _a;
425
- return (_a = this.rawFeature.measureParams) === null || _a === void 0 ? void 0 : _a.map((measureParam) => measureParam.code);
426
- }
427
- get unit() {
428
- const unit = this.rawFeature.unit;
429
- return unit === undefined ? this.parentProduct.unit : toLengthUnit(unit);
430
- }
431
- get mtrlApplications() {
432
- if (this._mtrlApplications === undefined) {
433
- this._mtrlApplications = (this.rawFeature.mtrlApplications || []).map((m) => CfgMtrlApplication.fromMtrlLikeApplication(CfgMtrlApplicationSource.Feature, m));
434
- }
435
- return this._mtrlApplications;
436
- }
437
- get selectedOptions() {
438
- return this._selectedOptions;
439
- }
440
- get ancestorsSelected() {
441
- return (this.parent instanceof _CfgProductConfigurationInternal || this.parent.ancestorsSelected);
442
- }
443
- /**
444
- * If one option is selected or deselected this will potentially
445
- * affect all other Options on this Feature
446
- */
447
- get isAllOptionsAffectedByAnySelection() {
448
- return this.selectionType === SelectionType.SelectOne && this.hasUpcharge !== false;
449
- }
450
- get preview() {
451
- return getMtrlPreview(this._mtrlApplications);
452
- }
453
- get visibleIfAdditionalProduct() {
454
- return this.rawFeature.hideIfAdditionalProduct !== true;
455
- }
456
- get visibleIfMainProduct() {
457
- return this.rawFeature.hideIfMainProduct !== true;
458
- }
459
- get visible() {
460
- return this.parentProduct.isAdditionalProduct
461
- ? this.visibleIfAdditionalProduct
462
- : this.visibleIfMainProduct;
463
- }
464
- get options() {
465
- if (this._options === undefined) {
466
- const hasDuplicateDescription = someMatch(this.rawFeature.options, (l, r) => {
467
- return l.description.toLowerCase() === r.description.toLowerCase();
468
- });
469
- this._options = this.rawFeature.options.map((o) => CfgOption.make(o, this.allRawFeatures, hasDuplicateDescription, this, this.parentConfiguration, this.parentProduct, this.rootProduct));
470
- }
471
- return this._options;
472
- }
473
- /**
474
- * Make fresh references to all options on this feature.
475
- * Also includes currently selected options.
476
- * @throws Will throw if options have not yet been generated.
477
- */
478
- _freshRefAllOptions() {
479
- const options = this._options;
480
- const selectedOptions = this._selectedOptions;
481
- if (options === undefined) {
482
- throw new Error("We expect all options to be generated at freshref");
483
- }
484
- for (let i = 0; i < options.length; i++) {
485
- doFreshRefOptionAtIndex(options, i, (freshRef) => {
486
- const selectedIndex = selectedOptions.findIndex(getOptionFilter(freshRef._internal));
487
- if (selectedIndex !== -1) {
488
- selectedOptions[selectedIndex] = freshRef;
489
- }
490
- });
491
- }
492
- }
493
- _freshRefDescendants() {
494
- const options = this._options || [];
495
- for (let i = 0; i < options.length; i++) {
496
- const optionInternal = options[i]._internal;
497
- optionInternal._freshRefDescendants();
498
- options[i] = CfgOption._makeNewRefFrom(optionInternal);
499
- }
500
- }
501
- }
502
- export class CfgFeature {
503
- /**
504
- * Private constructor and make-method because make new ref requires the constructor to
505
- * take an internal and we don't want those who instantiate CfgFeature to have to be aware
506
- * of the internal.
507
- */
508
- constructor(_internal) {
509
- this._internal = _internal;
510
- this.isBackedBySame = (other) => this._internal === other._internal;
511
- /**
512
- * This will find the first option allowing the value, set the value on it and select it.
513
- * This is an implicit option-select.
514
- */
515
- this.setNumericValue = (val) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setNumericValue(val); });
516
- /**
517
- * Selects the passed Option.
518
- * Only Options belonging to Features that are "select many" can be deselected.
519
- * Calling this will cause a validation call to the server.
520
- */
521
- this.selectOption = (option, on) => __awaiter(this, void 0, void 0, function* () {
522
- return yield this._internal.selectOption(option._internal, on, ProductConfigurationBubbleMode.ValidateAndBubbleSelected);
523
- });
524
- this.isSelected = (option) => this._internal.isSelected(option._internal);
525
- this.listenForChange = (l) => this._internal.changeObservable.listen(l);
526
- this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
527
- }
528
- static make(rawFeature, allRawFeatures, key, parent, parentConfiguration, parentProduct, rootProduct) {
529
- return new this(new _CfgFeatureInternal(rawFeature, allRawFeatures, key, parent, parentConfiguration, parentProduct, rootProduct));
530
- }
531
- /**
532
- * Makes an object wrapping the passed object. This is not a clone method,
533
- * it is a method to make a new outer reference. Like a shallow copy.
534
- * We use this to help frameworks that are built around using equals to detect change.
535
- */
536
- static _makeNewRefFrom(internal) {
537
- return new this(internal);
538
- }
539
- get parentProduct() {
540
- return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
541
- }
542
- get rootProduct() {
543
- return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
544
- }
545
- get selectionType() {
546
- return this._internal.selectionType;
547
- }
548
- get key() {
549
- return this._internal.key;
550
- }
551
- get code() {
552
- return this._internal.code;
553
- }
554
- get groupCode() {
555
- return this._internal.groupCode;
556
- }
557
- /**
558
- * If true the options in the feature is selected by both sending its code and numeric value
559
- * when selecting.
560
- */
561
- get isUseNumericValue() {
562
- return this._internal.isUseNumericValue;
563
- }
564
- /** This will read the numeric value of the selected option. */
565
- get numericValue() {
566
- return this._internal.numericValue;
567
- }
568
- get unit() {
569
- return this._internal.unit;
570
- }
571
- get description() {
572
- return this._internal.description;
573
- }
574
- get hasUpcharge() {
575
- return this._internal.hasUpcharge;
576
- }
577
- get selectedOptions() {
578
- return this._internal.selectedOptions;
579
- }
580
- /** Are all ancestors up to the CfgProductConfiguration selected? */
581
- get ancestorsSelected() {
582
- return this._internal.ancestorsSelected;
583
- }
584
- get preview() {
585
- return this._internal.preview;
586
- }
587
- get options() {
588
- return this._internal.options;
589
- }
590
- get visible() {
591
- return this._internal.visible;
592
- }
593
- }
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, convertLength, count, Observable, someMatch, toLengthUnit, } 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 { CfgOption, ProductConfigurationBubbleMode } from "./CfgOption.js";
15
+ import { _CfgProductConfigurationInternal } from "./CfgProductConfiguration.js";
16
+ import { getMtrlPreview } from "./utilitiesProductConfiguration.js";
17
+ export var SelectionType;
18
+ (function (SelectionType) {
19
+ /**
20
+ * All options are permanently selected. In our ui-component for this we skip over this level,
21
+ * and immediately show the children, but you could do this differently, like for instance
22
+ * showing the group heading.
23
+ */
24
+ SelectionType[SelectionType["Group"] = 0] = "Group";
25
+ /**
26
+ * One and only one can be selected at a time. This normally corresponds to either a dropdown
27
+ * menu or radio buttons.
28
+ */
29
+ SelectionType[SelectionType["SelectOne"] = 1] = "SelectOne";
30
+ /** Zero to all can be selected at a time. This normally corresponds to checkboxes. */
31
+ SelectionType[SelectionType["SelectMany"] = 2] = "SelectMany";
32
+ })(SelectionType || (SelectionType = {}));
33
+ function getOptionFilter(option1) {
34
+ return (option2) => option2._internal === option1;
35
+ }
36
+ /** Returns a fresh reference for the option at the given index */
37
+ function doFreshRefOptionAtIndex(options, index, beforeNotify) {
38
+ const freshRef = CfgOption._makeNewRefFrom(options[index]._internal);
39
+ options[index] = freshRef;
40
+ if (beforeNotify) {
41
+ beforeNotify(freshRef);
42
+ }
43
+ freshRef._internal.changeObservable.notifyAll({ freshRef });
44
+ return freshRef;
45
+ }
46
+ /**
47
+ * Returns a fresh reference for the given option.
48
+ * @throws Throws an error if the option is not found in options.
49
+ */
50
+ function doFreshRefOption(options, optionInternal, beforeNotify) {
51
+ const i = options.findIndex(getOptionFilter(optionInternal));
52
+ if (i === -1) {
53
+ throw new Error("Did not find the option in options");
54
+ }
55
+ return doFreshRefOptionAtIndex(options, i, beforeNotify);
56
+ }
57
+ /**
58
+ * This class is meant to only be used through CfgFeature. It should
59
+ * never be instantiated on its own. Normally the internal state of this class
60
+ * should never be directly modified. CfgFeature is the class that
61
+ * should be used and interacted with.
62
+ */
63
+ export class _CfgFeatureInternal {
64
+ constructor(rawFeature, allRawFeatures, key, // Unique amongst siblings
65
+ parent, parentConfiguration, parentProduct, rootProduct) {
66
+ this.rawFeature = rawFeature;
67
+ this.allRawFeatures = allRawFeatures;
68
+ this.key = key;
69
+ this.parent = parent;
70
+ this.parentConfiguration = parentConfiguration;
71
+ this.parentProduct = parentProduct;
72
+ this.rootProduct = rootProduct;
73
+ this._selectedOptions = [];
74
+ this.changeObservable = new Observable();
75
+ this.setNumericValue = (val) => __awaiter(this, void 0, void 0, function* () {
76
+ if (this.selectionType !== SelectionType.SelectOne) {
77
+ throw new Error("Only SelectOne Features can have numeric values");
78
+ }
79
+ const option = this.options.find((option) => option.isAllowedNumericValue(val));
80
+ if (option === undefined) {
81
+ throw new Error("No Option allows the value you tried to set");
82
+ }
83
+ return yield option.setNumericValue(val, true);
84
+ });
85
+ this._notifyAllOfChange = (bubbleMode) => __awaiter(this, void 0, void 0, function* () {
86
+ if (bubbleMode === ProductConfigurationBubbleMode.Stop) {
87
+ return;
88
+ }
89
+ const parent = this.parent;
90
+ const freshRef = CfgFeature._makeNewRefFrom(this);
91
+ this.changeObservable.notifyAll({ freshRef });
92
+ if (parent !== undefined) {
93
+ yield parent._childHasChanged(freshRef, bubbleMode === ProductConfigurationBubbleMode.OneLevel
94
+ ? ProductConfigurationBubbleMode.Stop
95
+ : bubbleMode);
96
+ }
97
+ });
98
+ /**
99
+ * Called by child to tell its parent that it has changed.
100
+ * @throws Will throw if options have not yet been generated. This should be impossible
101
+ * as nonexisting children can not call their parent.
102
+ */
103
+ this._childHasChanged = (childOption, bubbleMode) => __awaiter(this, void 0, void 0, function* () {
104
+ const options = this._options;
105
+ if (options === undefined) {
106
+ throw Error("Child says it changed, but no children has actually been generated");
107
+ }
108
+ const freshRef = doFreshRefOption(options, childOption);
109
+ const selectedOptions = this._selectedOptions;
110
+ const i = selectedOptions.findIndex(getOptionFilter(childOption));
111
+ if (i !== -1) {
112
+ selectedOptions[i] = freshRef;
113
+ }
114
+ yield this._notifyAllOfChange(bubbleMode);
115
+ });
116
+ this.getApiSelection = () => {
117
+ const result = {};
118
+ for (const option of this._selectedOptions) {
119
+ result[option.code] = option._internal.getApiSelection();
120
+ }
121
+ return result;
122
+ };
123
+ this.setApiSelection = (apiOptionSelectionMap) => __awaiter(this, void 0, void 0, function* () {
124
+ const selectionType = this.selectionType;
125
+ const isGroup = selectionType === SelectionType.Group;
126
+ const isSelectOne = selectionType === SelectionType.SelectOne;
127
+ const isAllOptionsAffectedByAnySelection = this.isAllOptionsAffectedByAnySelection;
128
+ let options;
129
+ if (apiOptionSelectionMap === undefined) {
130
+ options = this._options || []; // Already generated children (all or none)
131
+ apiOptionSelectionMap = {};
132
+ }
133
+ else {
134
+ options = this.options; // This will generate all children
135
+ }
136
+ let change = false;
137
+ for (let i = 0; i < options.length; i++) {
138
+ const option = options[i];
139
+ const apiOptionSelection = apiOptionSelectionMap[option.code];
140
+ const selectedIndex = this._selectedOptions.findIndex(getOptionFilter(option._internal));
141
+ const isOn = isGroup || apiOptionSelection;
142
+ if (isOn) {
143
+ if (isSelectOne) {
144
+ if (this.isUseNumericValue) {
145
+ const { numericValue } = apiOptionSelection;
146
+ if (numericValue === undefined) {
147
+ throw new Error("No numeric value for numeric feature");
148
+ }
149
+ const { value, unit } = numericValue;
150
+ if (yield option.setNumericValue(unit === undefined
151
+ ? value
152
+ : convertLength(value, toLengthUnit(unit), this.unit), false)) {
153
+ change = true;
154
+ }
155
+ }
156
+ }
157
+ if (selectedIndex === -1) {
158
+ if (isAllOptionsAffectedByAnySelection) {
159
+ this._selectedOptions.push(option);
160
+ }
161
+ else {
162
+ doFreshRefOptionAtIndex(options, i, (freshRef) => this._selectedOptions.push(freshRef));
163
+ }
164
+ change = true;
165
+ }
166
+ }
167
+ else {
168
+ if (selectedIndex !== -1) {
169
+ this._selectedOptions.splice(selectedIndex, 1);
170
+ if (!isAllOptionsAffectedByAnySelection) {
171
+ doFreshRefOptionAtIndex(options, i);
172
+ }
173
+ change = true;
174
+ }
175
+ }
176
+ if (yield option._internal.setApiSelection(apiOptionSelection)) {
177
+ change = true;
178
+ }
179
+ }
180
+ if (this.ancestorsSelected) {
181
+ const selectionCount = this._selectedOptions.length;
182
+ if (isSelectOne && selectionCount !== 1) {
183
+ const wrongCountWarning = `A select-one Feature (i.e. neither optional nor multiple) should have exactly one Option selected. Feature key: "${this.key}". Actual: ${selectionCount}`;
184
+ if (this.rootProduct.settings.strictSelectOneSelectionCount) {
185
+ throw new Error(wrongCountWarning);
186
+ }
187
+ else {
188
+ console.warn(wrongCountWarning);
189
+ }
190
+ }
191
+ if (!isGroup) {
192
+ const apiSelectionCount = Object.keys(apiOptionSelectionMap).length;
193
+ if (selectionCount !== apiSelectionCount) {
194
+ const wrongCountWarning = `All provided Options are expected to be selected. Feature key: "${this.key}". Expected: ${apiSelectionCount} Actual: ${selectionCount}`;
195
+ if (this.rootProduct.settings.strictSetApiSelectionMatch) {
196
+ throw new Error(wrongCountWarning);
197
+ }
198
+ else {
199
+ console.warn(wrongCountWarning);
200
+ }
201
+ }
202
+ }
203
+ }
204
+ if (change) {
205
+ if (isAllOptionsAffectedByAnySelection) {
206
+ this._freshRefAllOptions();
207
+ }
208
+ if (selectionType === SelectionType.SelectOne) {
209
+ this.pushStretch();
210
+ }
211
+ // setApiSelection works its way top down and handles notifications on
212
+ // each level by looking at change returned from its children. That way
213
+ // we do not get multiple notifications for the same ancestral node even
214
+ // if multiple descendants are affected by the setApiSelection. Therefore
215
+ // we use OneLevel to make this feature notify and the parent update its
216
+ // reference.
217
+ yield this._notifyAllOfChange(ProductConfigurationBubbleMode.OneLevel);
218
+ }
219
+ return change;
220
+ });
221
+ /** Pushes to refresh stretch. Does not cause validation. */
222
+ this.pushStretch = () => __awaiter(this, void 0, void 0, function* () {
223
+ if (this.selectionType !== SelectionType.SelectOne) {
224
+ throw new Error("Can only push stretch for SelectOne");
225
+ }
226
+ const value = this.numericValue;
227
+ return (yield Promise.all((this.measureParamCodes || []).map((measureParamCode) => this.parentConfiguration.setStretchReferenceLength(measureParamCode, value, this.unit)))).some((change) => change);
228
+ });
229
+ this.structureCompare = (other, strictOrder = true, descriptionMatch = false) => this.keyMatch(other, descriptionMatch) &&
230
+ compareArrays(this.options, other.options, (l, r) => l._internal.structureCompare(r._internal, strictOrder, descriptionMatch), strictOrder);
231
+ this.tryMatchSelection = (other, descriptionMatch = false) => __awaiter(this, void 0, void 0, function* () {
232
+ const { selectionType, isUseNumericValue } = this;
233
+ if (selectionType !== other.selectionType) {
234
+ // Will not try if the selection type is different
235
+ return false;
236
+ }
237
+ if (isUseNumericValue !== other.isUseNumericValue) {
238
+ return false;
239
+ }
240
+ let change = false;
241
+ if (isUseNumericValue) {
242
+ // For numeric options we do not try to go any further down. It's becoming to
243
+ // strange to make informed guesses on how to progress down the tree.
244
+ const otherNumericValue = other.numericValue;
245
+ if (otherNumericValue === undefined) {
246
+ throw new Error("Numeric feature without numeric value, this should never happen");
247
+ }
248
+ else {
249
+ if (yield this.setNumericValue(otherNumericValue)) {
250
+ change = true;
251
+ }
252
+ }
253
+ }
254
+ else {
255
+ const thisOptions = this.options;
256
+ const otherOptions = other.options;
257
+ change = (yield Promise.all(otherOptions.map((otherO) => (() => __awaiter(this, void 0, void 0, function* () {
258
+ const otherOSelected = otherO.selected;
259
+ if (selectionType === SelectionType.SelectOne && !otherOSelected) {
260
+ return false;
261
+ }
262
+ if (1 <
263
+ count(otherOptions, (item) => item._internal.keyMatch(otherO._internal, descriptionMatch))) {
264
+ console.warn("tryMatchSelection will ignore options that have the same key");
265
+ return false;
266
+ }
267
+ const toTryChangeOptions = thisOptions.filter((o) => otherO._internal.keyMatch(o._internal, descriptionMatch));
268
+ if (1 < toTryChangeOptions.length) {
269
+ console.warn("tryMatchSelection will ignore options that have the same key");
270
+ return false;
271
+ }
272
+ if (toTryChangeOptions.length === 0) {
273
+ return false;
274
+ }
275
+ const toTryChangeOption = toTryChangeOptions[0];
276
+ let change = false;
277
+ if (selectionType === SelectionType.SelectMany ||
278
+ selectionType === SelectionType.SelectOne) {
279
+ // The setSelected will only affect ourselves,
280
+ // so we do not need to bubble the notification.
281
+ // Instead we use the change variable to know
282
+ // when to notify for this feature.
283
+ change = yield toTryChangeOption._internal.parent.selectOption(toTryChangeOption._internal, otherOSelected, ProductConfigurationBubbleMode.Stop);
284
+ }
285
+ if (otherOSelected &&
286
+ (yield toTryChangeOption._internal.tryMatchSelection(otherO, descriptionMatch))) {
287
+ change = true;
288
+ }
289
+ return change;
290
+ }))()))).some((b) => b);
291
+ }
292
+ if (change) {
293
+ // tryMatchSelection works its way top down and handles notifications on
294
+ // each level by looking at change returned from its children. That way
295
+ // we do not get multiple notifications for the same ancestral node even
296
+ // if multiple descendants are affected by the tryMatchSelection. Therefore
297
+ // we use OneLevel to make this feature notify and the parent update its
298
+ // reference.
299
+ yield this._notifyAllOfChange(ProductConfigurationBubbleMode.OneLevel);
300
+ }
301
+ return change;
302
+ });
303
+ /**
304
+ * Normally this is used through methods on CfgFeature and CfgOption. Use this
305
+ * internal version if you need to control the bubbleMode.
306
+ * Using a validate bubbleMode will cause validation calls to the server.
307
+ */
308
+ this.selectOption = (optionInternal, on, bubbleMode) => __awaiter(this, void 0, void 0, function* () {
309
+ if (!on) {
310
+ if (this.selectionType === SelectionType.Group) {
311
+ throw new Error(`Multiple features are always selected and are not user selectable. Feature key: "${this.key}".`);
312
+ }
313
+ if (this.selectionType === SelectionType.SelectOne) {
314
+ throw new Error(`Select one can never be deselected. Feature key: "${this.key}".`);
315
+ }
316
+ }
317
+ const selectedOptions = this._selectedOptions;
318
+ const isAllOptionsAffectedByAnySelection = this.isAllOptionsAffectedByAnySelection;
319
+ const index = selectedOptions.findIndex(getOptionFilter(optionInternal));
320
+ const isActualChange = (index === -1) === on;
321
+ if (on) {
322
+ // Calling this.options will populate (generate) all Options if they have not yet
323
+ // been generated. As we are selecting an option we need all Options to be generated.
324
+ const options = this.options;
325
+ if (this.selectionType === SelectionType.SelectOne) {
326
+ let removeOption = selectedOptions.shift();
327
+ while (removeOption !== undefined) {
328
+ if (!isAllOptionsAffectedByAnySelection) {
329
+ doFreshRefOption(options, removeOption._internal);
330
+ }
331
+ removeOption = selectedOptions.shift();
332
+ }
333
+ }
334
+ if (isAllOptionsAffectedByAnySelection) {
335
+ selectedOptions.push(CfgOption._makeNewRefFrom(optionInternal));
336
+ }
337
+ else {
338
+ doFreshRefOption(options, optionInternal, (freshRef) => selectedOptions.push(freshRef));
339
+ }
340
+ if (this.selectionType === SelectionType.SelectOne) {
341
+ yield this.pushStretch();
342
+ }
343
+ }
344
+ else {
345
+ selectedOptions.splice(index, 1);
346
+ if (!isAllOptionsAffectedByAnySelection) {
347
+ // Accessing this._options gives us the Options that have been generated
348
+ // or undefined if the Options have not yet been generated. As this action
349
+ // is deselect and deselected is the default uninitialized state we can
350
+ // safely ignore if undefined.
351
+ const options = this._options;
352
+ if (options !== undefined) {
353
+ doFreshRefOption(options, optionInternal);
354
+ }
355
+ }
356
+ }
357
+ if (isAllOptionsAffectedByAnySelection) {
358
+ this._freshRefAllOptions();
359
+ }
360
+ // If this was a deselect action we shall not bubble selected, that is, we shall not
361
+ // select the ancestors if this action was deselect.
362
+ yield this._notifyAllOfChange(!on && bubbleMode === ProductConfigurationBubbleMode.ValidateAndBubbleSelected
363
+ ? ProductConfigurationBubbleMode.Validate
364
+ : bubbleMode);
365
+ return isActualChange;
366
+ });
367
+ this.isSelected = (option) => this.selectionType === SelectionType.Group ||
368
+ this._selectedOptions.some(getOptionFilter(option));
369
+ this.keyMatch = (other, descriptionMatch = false) => descriptionMatch
370
+ ? this.description.toLowerCase() === other.description.toLowerCase()
371
+ : this.code === other.code;
372
+ /** Only in selected options */
373
+ this._getFeaturesWithCode = (code) => this._selectedOptions.reduce((agg, selectedOption) => {
374
+ agg.push(...selectedOption._internal._getFeaturesWithCode(code));
375
+ return agg;
376
+ }, code === this.code ? [this] : []);
377
+ if (rawFeature.multiple) {
378
+ this.selectionType = SelectionType.Group;
379
+ }
380
+ else if (rawFeature.optional) {
381
+ this.selectionType = SelectionType.SelectMany;
382
+ }
383
+ else {
384
+ this.selectionType = SelectionType.SelectOne;
385
+ }
386
+ this.hasUpcharge = rawFeature.options.some((option) => (option.upcharge !== undefined && option.upcharge !== 0) ||
387
+ (option.priceCodes !== undefined && option.priceCodes.length > 0));
388
+ }
389
+ get code() {
390
+ return this.rawFeature.code;
391
+ }
392
+ get groupCode() {
393
+ return this.rawFeature.groupCode;
394
+ }
395
+ get isUseNumericValue() {
396
+ return this.rawFeature.numericOrder;
397
+ }
398
+ get numericValue() {
399
+ if (this.selectionType !== SelectionType.SelectOne) {
400
+ throw new Error("Only SelectOne Features can have numeric values");
401
+ }
402
+ const { selectedOptions } = this;
403
+ if (selectedOptions.length === 0) {
404
+ return undefined;
405
+ }
406
+ const value = selectedOptions[0].numericValue;
407
+ if (value === undefined) {
408
+ if (this.isUseNumericValue) {
409
+ throw new Error("No numericValue set on option with numeric sku feature");
410
+ }
411
+ return undefined;
412
+ }
413
+ return value;
414
+ }
415
+ get description() {
416
+ return this.rawFeature.description;
417
+ }
418
+ /**
419
+ * The MeasureParam class is re-used for different purposes. In Features it is used
420
+ * to indicate which stretch measures inside Models shall be affected by this state
421
+ * of this Feature. Hence only the code property is used.
422
+ */
423
+ get measureParamCodes() {
424
+ var _a;
425
+ return (_a = this.rawFeature.measureParams) === null || _a === void 0 ? void 0 : _a.map((measureParam) => measureParam.code);
426
+ }
427
+ get unit() {
428
+ const unit = this.rawFeature.unit;
429
+ return unit === undefined ? this.parentProduct.unit : toLengthUnit(unit);
430
+ }
431
+ get mtrlApplications() {
432
+ if (this._mtrlApplications === undefined) {
433
+ this._mtrlApplications = (this.rawFeature.mtrlApplications || []).map((m) => CfgMtrlApplication.fromMtrlLikeApplication(CfgMtrlApplicationSource.Feature, m));
434
+ }
435
+ return this._mtrlApplications;
436
+ }
437
+ get selectedOptions() {
438
+ return this._selectedOptions;
439
+ }
440
+ get ancestorsSelected() {
441
+ return (this.parent instanceof _CfgProductConfigurationInternal || this.parent.ancestorsSelected);
442
+ }
443
+ /**
444
+ * If one option is selected or deselected this will potentially
445
+ * affect all other Options on this Feature
446
+ */
447
+ get isAllOptionsAffectedByAnySelection() {
448
+ return this.selectionType === SelectionType.SelectOne && this.hasUpcharge !== false;
449
+ }
450
+ get preview() {
451
+ return getMtrlPreview(this._mtrlApplications);
452
+ }
453
+ get visibleIfAdditionalProduct() {
454
+ return this.rawFeature.hideIfAdditionalProduct !== true;
455
+ }
456
+ get visibleIfMainProduct() {
457
+ return this.rawFeature.hideIfMainProduct !== true;
458
+ }
459
+ get visible() {
460
+ return this.parentProduct.isAdditionalProduct
461
+ ? this.visibleIfAdditionalProduct
462
+ : this.visibleIfMainProduct;
463
+ }
464
+ get options() {
465
+ if (this._options === undefined) {
466
+ const hasDuplicateDescription = someMatch(this.rawFeature.options, (l, r) => {
467
+ return l.description.toLowerCase() === r.description.toLowerCase();
468
+ });
469
+ this._options = this.rawFeature.options.map((o) => CfgOption.make(o, this.allRawFeatures, hasDuplicateDescription, this, this.parentConfiguration, this.parentProduct, this.rootProduct));
470
+ }
471
+ return this._options;
472
+ }
473
+ /**
474
+ * Make fresh references to all options on this feature.
475
+ * Also includes currently selected options.
476
+ * @throws Will throw if options have not yet been generated.
477
+ */
478
+ _freshRefAllOptions() {
479
+ const options = this._options;
480
+ const selectedOptions = this._selectedOptions;
481
+ if (options === undefined) {
482
+ throw new Error("We expect all options to be generated at freshref");
483
+ }
484
+ for (let i = 0; i < options.length; i++) {
485
+ doFreshRefOptionAtIndex(options, i, (freshRef) => {
486
+ const selectedIndex = selectedOptions.findIndex(getOptionFilter(freshRef._internal));
487
+ if (selectedIndex !== -1) {
488
+ selectedOptions[selectedIndex] = freshRef;
489
+ }
490
+ });
491
+ }
492
+ }
493
+ _freshRefDescendants() {
494
+ const options = this._options || [];
495
+ for (let i = 0; i < options.length; i++) {
496
+ const optionInternal = options[i]._internal;
497
+ optionInternal._freshRefDescendants();
498
+ options[i] = CfgOption._makeNewRefFrom(optionInternal);
499
+ }
500
+ }
501
+ }
502
+ export class CfgFeature {
503
+ /**
504
+ * Private constructor and make-method because make new ref requires the constructor to
505
+ * take an internal and we don't want those who instantiate CfgFeature to have to be aware
506
+ * of the internal.
507
+ */
508
+ constructor(_internal) {
509
+ this._internal = _internal;
510
+ this.isBackedBySame = (other) => this._internal === other._internal;
511
+ /**
512
+ * This will find the first option allowing the value, set the value on it and select it.
513
+ * This is an implicit option-select.
514
+ */
515
+ this.setNumericValue = (val) => __awaiter(this, void 0, void 0, function* () { return yield this._internal.setNumericValue(val); });
516
+ /**
517
+ * Selects the passed Option.
518
+ * Only Options belonging to Features that are "select many" can be deselected.
519
+ * Calling this will cause a validation call to the server.
520
+ */
521
+ this.selectOption = (option, on) => __awaiter(this, void 0, void 0, function* () {
522
+ return yield this._internal.selectOption(option._internal, on, ProductConfigurationBubbleMode.ValidateAndBubbleSelected);
523
+ });
524
+ this.isSelected = (option) => this._internal.isSelected(option._internal);
525
+ this.listenForChange = (l) => this._internal.changeObservable.listen(l);
526
+ this.stopListenForChange = (l) => this._internal.changeObservable.stopListen(l);
527
+ }
528
+ static make(rawFeature, allRawFeatures, key, parent, parentConfiguration, parentProduct, rootProduct) {
529
+ return new this(new _CfgFeatureInternal(rawFeature, allRawFeatures, key, parent, parentConfiguration, parentProduct, rootProduct));
530
+ }
531
+ /**
532
+ * Makes an object wrapping the passed object. This is not a clone method,
533
+ * it is a method to make a new outer reference. Like a shallow copy.
534
+ * We use this to help frameworks that are built around using equals to detect change.
535
+ */
536
+ static _makeNewRefFrom(internal) {
537
+ return new this(internal);
538
+ }
539
+ get parentProduct() {
540
+ return CfgProduct._makeNewRefFrom(this._internal.parentProduct);
541
+ }
542
+ get rootProduct() {
543
+ return CfgProduct._makeNewRefFrom(this._internal.rootProduct);
544
+ }
545
+ get selectionType() {
546
+ return this._internal.selectionType;
547
+ }
548
+ get key() {
549
+ return this._internal.key;
550
+ }
551
+ get code() {
552
+ return this._internal.code;
553
+ }
554
+ get groupCode() {
555
+ return this._internal.groupCode;
556
+ }
557
+ /**
558
+ * If true the options in the feature is selected by both sending its code and numeric value
559
+ * when selecting.
560
+ */
561
+ get isUseNumericValue() {
562
+ return this._internal.isUseNumericValue;
563
+ }
564
+ /** This will read the numeric value of the selected option. */
565
+ get numericValue() {
566
+ return this._internal.numericValue;
567
+ }
568
+ get unit() {
569
+ return this._internal.unit;
570
+ }
571
+ get description() {
572
+ return this._internal.description;
573
+ }
574
+ get hasUpcharge() {
575
+ return this._internal.hasUpcharge;
576
+ }
577
+ get selectedOptions() {
578
+ return this._internal.selectedOptions;
579
+ }
580
+ /** Are all ancestors up to the CfgProductConfiguration selected? */
581
+ get ancestorsSelected() {
582
+ return this._internal.ancestorsSelected;
583
+ }
584
+ get preview() {
585
+ return this._internal.preview;
586
+ }
587
+ get options() {
588
+ return this._internal.options;
589
+ }
590
+ get visible() {
591
+ return this._internal.visible;
592
+ }
593
+ }