@configura/web-api 2.0.0 → 2.1.0-alpha.0

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