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