@athoscommerce/snap-store-mobx 1.0.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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -0
  3. package/dist/cjs/Abstract/AbstractStore.d.ts +17 -0
  4. package/dist/cjs/Abstract/AbstractStore.d.ts.map +1 -0
  5. package/dist/cjs/Abstract/AbstractStore.js +28 -0
  6. package/dist/cjs/Autocomplete/AutocompleteStore.d.ts +40 -0
  7. package/dist/cjs/Autocomplete/AutocompleteStore.d.ts.map +1 -0
  8. package/dist/cjs/Autocomplete/AutocompleteStore.js +246 -0
  9. package/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.d.ts +13 -0
  10. package/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.d.ts.map +1 -0
  11. package/dist/cjs/Autocomplete/Stores/AutocompleteFacetStore.js +67 -0
  12. package/dist/cjs/Autocomplete/Stores/AutocompleteHistoryStore.d.ts +19 -0
  13. package/dist/cjs/Autocomplete/Stores/AutocompleteHistoryStore.d.ts.map +1 -0
  14. package/dist/cjs/Autocomplete/Stores/AutocompleteHistoryStore.js +57 -0
  15. package/dist/cjs/Autocomplete/Stores/AutocompleteQueryStore.d.ts +25 -0
  16. package/dist/cjs/Autocomplete/Stores/AutocompleteQueryStore.d.ts.map +1 -0
  17. package/dist/cjs/Autocomplete/Stores/AutocompleteQueryStore.js +34 -0
  18. package/dist/cjs/Autocomplete/Stores/AutocompleteStateStore.d.ts +29 -0
  19. package/dist/cjs/Autocomplete/Stores/AutocompleteStateStore.d.ts.map +1 -0
  20. package/dist/cjs/Autocomplete/Stores/AutocompleteStateStore.js +54 -0
  21. package/dist/cjs/Autocomplete/Stores/AutocompleteTermStore.d.ts +47 -0
  22. package/dist/cjs/Autocomplete/Stores/AutocompleteTermStore.d.ts.map +1 -0
  23. package/dist/cjs/Autocomplete/Stores/AutocompleteTermStore.js +102 -0
  24. package/dist/cjs/Autocomplete/Stores/AutocompleteTrendingStore.d.ts +20 -0
  25. package/dist/cjs/Autocomplete/Stores/AutocompleteTrendingStore.d.ts.map +1 -0
  26. package/dist/cjs/Autocomplete/Stores/AutocompleteTrendingStore.js +58 -0
  27. package/dist/cjs/Autocomplete/Stores/index.d.ts +7 -0
  28. package/dist/cjs/Autocomplete/Stores/index.d.ts.map +1 -0
  29. package/dist/cjs/Autocomplete/Stores/index.js +16 -0
  30. package/dist/cjs/Cart/CartStore.d.ts +15 -0
  31. package/dist/cjs/Cart/CartStore.d.ts.map +1 -0
  32. package/dist/cjs/Cart/CartStore.js +109 -0
  33. package/dist/cjs/Finder/FinderStore.d.ts +27 -0
  34. package/dist/cjs/Finder/FinderStore.d.ts.map +1 -0
  35. package/dist/cjs/Finder/FinderStore.js +156 -0
  36. package/dist/cjs/Finder/Stores/FinderSelectionStore.d.ts +73 -0
  37. package/dist/cjs/Finder/Stores/FinderSelectionStore.d.ts.map +1 -0
  38. package/dist/cjs/Finder/Stores/FinderSelectionStore.js +320 -0
  39. package/dist/cjs/Finder/Stores/index.d.ts +2 -0
  40. package/dist/cjs/Finder/Stores/index.d.ts.map +1 -0
  41. package/dist/cjs/Finder/Stores/index.js +5 -0
  42. package/dist/cjs/Meta/MetaStore.d.ts +20 -0
  43. package/dist/cjs/Meta/MetaStore.d.ts.map +1 -0
  44. package/dist/cjs/Meta/MetaStore.js +55 -0
  45. package/dist/cjs/Recommendation/RecommendationStore.d.ts +19 -0
  46. package/dist/cjs/Recommendation/RecommendationStore.d.ts.map +1 -0
  47. package/dist/cjs/Recommendation/RecommendationStore.js +97 -0
  48. package/dist/cjs/Recommendation/Stores/RecommendationProfileStore.d.ts +15 -0
  49. package/dist/cjs/Recommendation/Stores/RecommendationProfileStore.d.ts.map +1 -0
  50. package/dist/cjs/Recommendation/Stores/RecommendationProfileStore.js +30 -0
  51. package/dist/cjs/Recommendation/Stores/index.d.ts +2 -0
  52. package/dist/cjs/Recommendation/Stores/index.d.ts.map +1 -0
  53. package/dist/cjs/Recommendation/Stores/index.js +5 -0
  54. package/dist/cjs/Search/SearchStore.d.ts +27 -0
  55. package/dist/cjs/Search/SearchStore.d.ts.map +1 -0
  56. package/dist/cjs/Search/SearchStore.js +125 -0
  57. package/dist/cjs/Search/Stores/SearchFacetStore.d.ts +89 -0
  58. package/dist/cjs/Search/Stores/SearchFacetStore.d.ts.map +1 -0
  59. package/dist/cjs/Search/Stores/SearchFacetStore.js +368 -0
  60. package/dist/cjs/Search/Stores/SearchFilterStore.d.ts +44 -0
  61. package/dist/cjs/Search/Stores/SearchFilterStore.d.ts.map +1 -0
  62. package/dist/cjs/Search/Stores/SearchFilterStore.js +102 -0
  63. package/dist/cjs/Search/Stores/SearchHistoryStore.d.ts +20 -0
  64. package/dist/cjs/Search/Stores/SearchHistoryStore.d.ts.map +1 -0
  65. package/dist/cjs/Search/Stores/SearchHistoryStore.js +106 -0
  66. package/dist/cjs/Search/Stores/SearchMerchandisingStore.d.ts +36 -0
  67. package/dist/cjs/Search/Stores/SearchMerchandisingStore.d.ts.map +1 -0
  68. package/dist/cjs/Search/Stores/SearchMerchandisingStore.js +90 -0
  69. package/dist/cjs/Search/Stores/SearchPaginationStore.d.ts +57 -0
  70. package/dist/cjs/Search/Stores/SearchPaginationStore.d.ts.map +1 -0
  71. package/dist/cjs/Search/Stores/SearchPaginationStore.js +212 -0
  72. package/dist/cjs/Search/Stores/SearchQueryStore.d.ts +23 -0
  73. package/dist/cjs/Search/Stores/SearchQueryStore.d.ts.map +1 -0
  74. package/dist/cjs/Search/Stores/SearchQueryStore.js +41 -0
  75. package/dist/cjs/Search/Stores/SearchResultStore.d.ts +175 -0
  76. package/dist/cjs/Search/Stores/SearchResultStore.d.ts.map +1 -0
  77. package/dist/cjs/Search/Stores/SearchResultStore.js +749 -0
  78. package/dist/cjs/Search/Stores/SearchSortingStore.d.ts +32 -0
  79. package/dist/cjs/Search/Stores/SearchSortingStore.d.ts.map +1 -0
  80. package/dist/cjs/Search/Stores/SearchSortingStore.js +77 -0
  81. package/dist/cjs/Search/Stores/index.d.ts +9 -0
  82. package/dist/cjs/Search/Stores/index.d.ts.map +1 -0
  83. package/dist/cjs/Search/Stores/index.js +30 -0
  84. package/dist/cjs/Storage/StorageStore.d.ts +27 -0
  85. package/dist/cjs/Storage/StorageStore.d.ts.map +1 -0
  86. package/dist/cjs/Storage/StorageStore.js +176 -0
  87. package/dist/cjs/index.d.ts +12 -0
  88. package/dist/cjs/index.d.ts.map +1 -0
  89. package/dist/cjs/index.js +37 -0
  90. package/dist/cjs/types.d.ts +170 -0
  91. package/dist/cjs/types.d.ts.map +1 -0
  92. package/dist/cjs/types.js +9 -0
  93. package/dist/esm/Abstract/AbstractStore.d.ts +17 -0
  94. package/dist/esm/Abstract/AbstractStore.d.ts.map +1 -0
  95. package/dist/esm/Abstract/AbstractStore.js +22 -0
  96. package/dist/esm/Autocomplete/AutocompleteStore.d.ts +40 -0
  97. package/dist/esm/Autocomplete/AutocompleteStore.d.ts.map +1 -0
  98. package/dist/esm/Autocomplete/AutocompleteStore.js +210 -0
  99. package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.d.ts +13 -0
  100. package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.d.ts.map +1 -0
  101. package/dist/esm/Autocomplete/Stores/AutocompleteFacetStore.js +35 -0
  102. package/dist/esm/Autocomplete/Stores/AutocompleteHistoryStore.d.ts +19 -0
  103. package/dist/esm/Autocomplete/Stores/AutocompleteHistoryStore.d.ts.map +1 -0
  104. package/dist/esm/Autocomplete/Stores/AutocompleteHistoryStore.js +27 -0
  105. package/dist/esm/Autocomplete/Stores/AutocompleteQueryStore.d.ts +25 -0
  106. package/dist/esm/Autocomplete/Stores/AutocompleteQueryStore.d.ts.map +1 -0
  107. package/dist/esm/Autocomplete/Stores/AutocompleteQueryStore.js +28 -0
  108. package/dist/esm/Autocomplete/Stores/AutocompleteStateStore.d.ts +29 -0
  109. package/dist/esm/Autocomplete/Stores/AutocompleteStateStore.d.ts.map +1 -0
  110. package/dist/esm/Autocomplete/Stores/AutocompleteStateStore.js +42 -0
  111. package/dist/esm/Autocomplete/Stores/AutocompleteTermStore.d.ts +47 -0
  112. package/dist/esm/Autocomplete/Stores/AutocompleteTermStore.d.ts.map +1 -0
  113. package/dist/esm/Autocomplete/Stores/AutocompleteTermStore.js +56 -0
  114. package/dist/esm/Autocomplete/Stores/AutocompleteTrendingStore.d.ts +20 -0
  115. package/dist/esm/Autocomplete/Stores/AutocompleteTrendingStore.d.ts.map +1 -0
  116. package/dist/esm/Autocomplete/Stores/AutocompleteTrendingStore.js +27 -0
  117. package/dist/esm/Autocomplete/Stores/index.d.ts +7 -0
  118. package/dist/esm/Autocomplete/Stores/index.d.ts.map +1 -0
  119. package/dist/esm/Autocomplete/Stores/index.js +6 -0
  120. package/dist/esm/Cart/CartStore.d.ts +15 -0
  121. package/dist/esm/Cart/CartStore.d.ts.map +1 -0
  122. package/dist/esm/Cart/CartStore.js +74 -0
  123. package/dist/esm/Finder/FinderStore.d.ts +27 -0
  124. package/dist/esm/Finder/FinderStore.d.ts.map +1 -0
  125. package/dist/esm/Finder/FinderStore.js +118 -0
  126. package/dist/esm/Finder/Stores/FinderSelectionStore.d.ts +73 -0
  127. package/dist/esm/Finder/Stores/FinderSelectionStore.d.ts.map +1 -0
  128. package/dist/esm/Finder/Stores/FinderSelectionStore.js +258 -0
  129. package/dist/esm/Finder/Stores/index.d.ts +2 -0
  130. package/dist/esm/Finder/Stores/index.d.ts.map +1 -0
  131. package/dist/esm/Finder/Stores/index.js +1 -0
  132. package/dist/esm/Meta/MetaStore.d.ts +20 -0
  133. package/dist/esm/Meta/MetaStore.d.ts.map +1 -0
  134. package/dist/esm/Meta/MetaStore.js +45 -0
  135. package/dist/esm/Recommendation/RecommendationStore.d.ts +19 -0
  136. package/dist/esm/Recommendation/RecommendationStore.d.ts.map +1 -0
  137. package/dist/esm/Recommendation/RecommendationStore.js +71 -0
  138. package/dist/esm/Recommendation/Stores/RecommendationProfileStore.d.ts +15 -0
  139. package/dist/esm/Recommendation/Stores/RecommendationProfileStore.d.ts.map +1 -0
  140. package/dist/esm/Recommendation/Stores/RecommendationProfileStore.js +25 -0
  141. package/dist/esm/Recommendation/Stores/index.d.ts +2 -0
  142. package/dist/esm/Recommendation/Stores/index.d.ts.map +1 -0
  143. package/dist/esm/Recommendation/Stores/index.js +1 -0
  144. package/dist/esm/Search/SearchStore.d.ts +27 -0
  145. package/dist/esm/Search/SearchStore.d.ts.map +1 -0
  146. package/dist/esm/Search/SearchStore.js +102 -0
  147. package/dist/esm/Search/Stores/SearchFacetStore.d.ts +89 -0
  148. package/dist/esm/Search/Stores/SearchFacetStore.d.ts.map +1 -0
  149. package/dist/esm/Search/Stores/SearchFacetStore.js +292 -0
  150. package/dist/esm/Search/Stores/SearchFilterStore.d.ts +44 -0
  151. package/dist/esm/Search/Stores/SearchFilterStore.d.ts.map +1 -0
  152. package/dist/esm/Search/Stores/SearchFilterStore.js +70 -0
  153. package/dist/esm/Search/Stores/SearchHistoryStore.d.ts +20 -0
  154. package/dist/esm/Search/Stores/SearchHistoryStore.d.ts.map +1 -0
  155. package/dist/esm/Search/Stores/SearchHistoryStore.js +85 -0
  156. package/dist/esm/Search/Stores/SearchMerchandisingStore.d.ts +36 -0
  157. package/dist/esm/Search/Stores/SearchMerchandisingStore.d.ts.map +1 -0
  158. package/dist/esm/Search/Stores/SearchMerchandisingStore.js +61 -0
  159. package/dist/esm/Search/Stores/SearchPaginationStore.d.ts +57 -0
  160. package/dist/esm/Search/Stores/SearchPaginationStore.d.ts.map +1 -0
  161. package/dist/esm/Search/Stores/SearchPaginationStore.js +163 -0
  162. package/dist/esm/Search/Stores/SearchQueryStore.d.ts +23 -0
  163. package/dist/esm/Search/Stores/SearchQueryStore.d.ts.map +1 -0
  164. package/dist/esm/Search/Stores/SearchQueryStore.js +33 -0
  165. package/dist/esm/Search/Stores/SearchResultStore.d.ts +175 -0
  166. package/dist/esm/Search/Stores/SearchResultStore.d.ts.map +1 -0
  167. package/dist/esm/Search/Stores/SearchResultStore.js +648 -0
  168. package/dist/esm/Search/Stores/SearchSortingStore.d.ts +32 -0
  169. package/dist/esm/Search/Stores/SearchSortingStore.d.ts.map +1 -0
  170. package/dist/esm/Search/Stores/SearchSortingStore.js +67 -0
  171. package/dist/esm/Search/Stores/index.d.ts +9 -0
  172. package/dist/esm/Search/Stores/index.d.ts.map +1 -0
  173. package/dist/esm/Search/Stores/index.js +8 -0
  174. package/dist/esm/Storage/StorageStore.d.ts +27 -0
  175. package/dist/esm/Storage/StorageStore.d.ts.map +1 -0
  176. package/dist/esm/Storage/StorageStore.js +169 -0
  177. package/dist/esm/index.d.ts +12 -0
  178. package/dist/esm/index.d.ts.map +1 -0
  179. package/dist/esm/index.js +11 -0
  180. package/dist/esm/types.d.ts +170 -0
  181. package/dist/esm/types.d.ts.map +1 -0
  182. package/dist/esm/types.js +6 -0
  183. package/package.json +35 -0
@@ -0,0 +1,749 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
34
+ }
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ var __importDefault = (this && this.__importDefault) || function (mod) {
38
+ return (mod && mod.__esModule) ? mod : { "default": mod };
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.Variant = exports.VariantSelection = exports.Variants = exports.ProductMask = exports.Badges = exports.Product = exports.Banner = exports.SearchResultStore = void 0;
42
+ var mobx_1 = require("mobx");
43
+ var deepmerge_1 = __importDefault(require("deepmerge"));
44
+ var is_plain_object_1 = require("is-plain-object");
45
+ var VARIANT_ATTRIBUTE = 'ss-variant-option';
46
+ var VARIANT_ATTRIBUTE_SELECTED = 'ss-variant-option-selected';
47
+ var SearchResultStore = /** @class */ (function (_super) {
48
+ __extends(SearchResultStore, _super);
49
+ function SearchResultStore(params) {
50
+ var _a, _b, _c, _d, _e, _f;
51
+ var _g = params || {}, config = _g.config, data = _g.data, state = _g.state, stores = _g.stores;
52
+ var _h = data || {}, search = _h.search, meta = _h.meta, previousSearch = _h.previousSearch;
53
+ var _j = search || {}, results = _j.results, merchandising = _j.merchandising, pagination = _j.pagination;
54
+ var previousResults = (stores || {}).previousResults;
55
+ var loaded = (state || {}).loaded;
56
+ var resultsArr = (results || []).map(function (result, idx) {
57
+ var _a, _b;
58
+ return new Product({
59
+ config: config,
60
+ data: { result: result, meta: meta },
61
+ position: idx + 1,
62
+ responseId: ((_b = (_a = params.data.search) === null || _a === void 0 ? void 0 : _a.tracking) === null || _b === void 0 ? void 0 : _b.responseId) || '',
63
+ });
64
+ });
65
+ var variantConfig = (_a = config === null || config === void 0 ? void 0 : config.settings) === null || _a === void 0 ? void 0 : _a.variants;
66
+ // preselected variant options
67
+ if ((_b = variantConfig === null || variantConfig === void 0 ? void 0 : variantConfig.realtime) === null || _b === void 0 ? void 0 : _b.enabled) {
68
+ // attach click events - ONLY happens once (known limitation)
69
+ if (!loaded && (resultsArr === null || resultsArr === void 0 ? void 0 : resultsArr.length)) {
70
+ var processedSelects_1 = new Set();
71
+ document.querySelectorAll("[".concat(VARIANT_ATTRIBUTE, "]")).forEach(function (elem) {
72
+ if (elem.tagName == 'OPTION') {
73
+ var parentSelect_1 = elem.closest('select');
74
+ if (parentSelect_1) {
75
+ if (!processedSelects_1.has(parentSelect_1)) {
76
+ processedSelects_1.add(parentSelect_1);
77
+ parentSelect_1.addEventListener('change', function (e) {
78
+ var _a;
79
+ var val = (_a = e.target) === null || _a === void 0 ? void 0 : _a.value;
80
+ var clickedOption = Array.from(parentSelect_1.querySelectorAll("[".concat(VARIANT_ATTRIBUTE, "]"))).filter(function (elem) { return elem.value == val; });
81
+ if (clickedOption.length > 0) {
82
+ variantOptionClick(clickedOption[0], variantConfig, resultsArr);
83
+ }
84
+ });
85
+ }
86
+ }
87
+ else {
88
+ console.warn('Warning: unable to add realtime variant event listener for element - ', elem);
89
+ }
90
+ }
91
+ else {
92
+ elem.addEventListener('click', function () {
93
+ variantOptionClick(elem, variantConfig, resultsArr);
94
+ });
95
+ }
96
+ });
97
+ }
98
+ // check for attributes for preselection
99
+ if (resultsArr.length) {
100
+ var options_1 = {};
101
+ // grab values from elements on the page to form preselected elements
102
+ document.querySelectorAll("[".concat(VARIANT_ATTRIBUTE_SELECTED, "]")).forEach(function (elem) {
103
+ var attr = elem.getAttribute(VARIANT_ATTRIBUTE);
104
+ if (attr) {
105
+ var _a = attr.split(':'), option = _a[0], value = _a[1];
106
+ if (option && value) {
107
+ options_1[option.toLowerCase()] = [value.toLowerCase()];
108
+ }
109
+ }
110
+ });
111
+ makeVariantSelections(variantConfig, options_1, resultsArr);
112
+ }
113
+ }
114
+ // only when infinite is enabled
115
+ if (((_d = (_c = config === null || config === void 0 ? void 0 : config.settings) === null || _c === void 0 ? void 0 : _c.infinite) === null || _d === void 0 ? void 0 : _d.enabled) && previousResults) {
116
+ // logic to determine when to concatenate previous results
117
+ // this logic is not bullet proof, but it is highly unlikely that the current and previous pagination data would ever be sequential unless paginating
118
+ // TODO: implement "load previous" with a limit to how many total products can be displayed
119
+ // the limit would be enforced by unloading (trimming) results based on the direction of the new search (previous page, next page)
120
+ if ((pagination === null || pagination === void 0 ? void 0 : pagination.page) && ((_e = previousSearch === null || previousSearch === void 0 ? void 0 : previousSearch.pagination) === null || _e === void 0 ? void 0 : _e.page) && pagination.page == previousSearch.pagination.page + 1) {
121
+ resultsArr = (previousResults || []).concat(resultsArr);
122
+ }
123
+ }
124
+ // add banners to results - banner data includes ALL banners within the entire search results (even if not on the current page)
125
+ if (pagination && ((_f = merchandising === null || merchandising === void 0 ? void 0 : merchandising.content) === null || _f === void 0 ? void 0 : _f.inline)) {
126
+ // ensure banners are sorted by index
127
+ var banners = merchandising.content.inline
128
+ .sort(function (a, b) {
129
+ return a.config.position.index - b.config.position.index;
130
+ })
131
+ .map(function (banner) {
132
+ var _a, _b;
133
+ return new Banner({
134
+ data: { banner: banner, responseId: ((_b = (_a = params.data.search) === null || _a === void 0 ? void 0 : _a.tracking) === null || _b === void 0 ? void 0 : _b.responseId) || '' },
135
+ });
136
+ });
137
+ if (banners && pagination.totalResults) {
138
+ resultsArr = addBannersToResults(config, resultsArr, banners, pagination);
139
+ }
140
+ }
141
+ return _super.apply(this, resultsArr) || this;
142
+ }
143
+ Object.defineProperty(SearchResultStore, Symbol.species, {
144
+ get: function () {
145
+ return Array;
146
+ },
147
+ enumerable: false,
148
+ configurable: true
149
+ });
150
+ return SearchResultStore;
151
+ }(Array));
152
+ exports.SearchResultStore = SearchResultStore;
153
+ var Banner = /** @class */ (function () {
154
+ function Banner(bannerData) {
155
+ this.type = 'banner';
156
+ this.attributes = {};
157
+ this.mappings = {
158
+ core: {},
159
+ };
160
+ this.custom = {};
161
+ var _a = (bannerData === null || bannerData === void 0 ? void 0 : bannerData.data) || {}, banner = _a.banner, responseId = _a.responseId;
162
+ var htmlString = banner.value;
163
+ var match = typeof htmlString === 'string' && htmlString.match(/data-banner-id="(\d+)"/);
164
+ var uid = match ? match[1] : 'ss-ib-' + banner.config.position.index;
165
+ this.id = uid;
166
+ this.responseId = responseId;
167
+ this.config = banner.config;
168
+ this.value = banner.value;
169
+ (0, mobx_1.makeObservable)(this, {
170
+ id: mobx_1.observable,
171
+ mappings: mobx_1.observable,
172
+ attributes: mobx_1.observable,
173
+ });
174
+ }
175
+ return Banner;
176
+ }());
177
+ exports.Banner = Banner;
178
+ var Product = /** @class */ (function () {
179
+ function Product(productData) {
180
+ var _a, _b;
181
+ this.type = 'product';
182
+ this.attributes = {};
183
+ this.mappings = {
184
+ core: {},
185
+ };
186
+ this.custom = {};
187
+ this.quantity = 1;
188
+ this.mask = new ProductMask();
189
+ var config = (productData || {}).config;
190
+ var _c = (productData === null || productData === void 0 ? void 0 : productData.data) || {}, result = _c.result, meta = _c.meta;
191
+ this.id = result.id;
192
+ this.attributes = result.attributes;
193
+ this.mappings = result.mappings;
194
+ this.position = productData.position;
195
+ this.badges = new Badges({
196
+ data: {
197
+ meta: meta,
198
+ result: result,
199
+ },
200
+ });
201
+ this.responseId = result.responseId || productData.responseId;
202
+ // @ts-ignore - need to add bundleSeed to snapi-types
203
+ if (result.bundleSeed) {
204
+ // @ts-ignore - need to add bundleSeed to snapi-types
205
+ this.bundleSeed = Boolean(result.bundleSeed);
206
+ }
207
+ if (result.variants && result.variants.data) {
208
+ // if variants are already parsed, use them
209
+ this.variants = new Variants({
210
+ data: {
211
+ mask: this.mask,
212
+ variants: result.variants.data,
213
+ optionConfig: result.variants.optionConfig,
214
+ preferences: (_a = result.variants) === null || _a === void 0 ? void 0 : _a.preferences, // TODO: fix typing
215
+ meta: meta,
216
+ },
217
+ config: (_b = config === null || config === void 0 ? void 0 : config.settings) === null || _b === void 0 ? void 0 : _b.variants,
218
+ });
219
+ }
220
+ (0, mobx_1.makeObservable)(this, {
221
+ id: mobx_1.observable,
222
+ display: mobx_1.computed,
223
+ mappings: mobx_1.observable,
224
+ attributes: mobx_1.observable,
225
+ custom: mobx_1.observable,
226
+ quantity: mobx_1.observable,
227
+ });
228
+ }
229
+ Object.defineProperty(Product.prototype, "display", {
230
+ get: function () {
231
+ return (0, deepmerge_1.default)({ id: this.id, mappings: this.mappings, attributes: this.attributes, badges: this.badges }, this.mask.data, {
232
+ isMergeableObject: is_plain_object_1.isPlainObject,
233
+ });
234
+ },
235
+ enumerable: false,
236
+ configurable: true
237
+ });
238
+ return Product;
239
+ }());
240
+ exports.Product = Product;
241
+ var Badges = /** @class */ (function () {
242
+ function Badges(badgesData) {
243
+ this.all = [];
244
+ var data = (badgesData || {}).data;
245
+ var _a = data || {}, meta = _a.meta, result = _a.result;
246
+ this.all = (result.badges || [])
247
+ .filter(function (badge) {
248
+ var _a, _b, _c;
249
+ // remove badges that are not in the meta or are disabled
250
+ return !!((badge === null || badge === void 0 ? void 0 : badge.tag) && ((_a = meta.badges) === null || _a === void 0 ? void 0 : _a.tags) && ((_b = meta.badges) === null || _b === void 0 ? void 0 : _b.tags[badge.tag]) && ((_c = meta.badges) === null || _c === void 0 ? void 0 : _c.tags[badge.tag].enabled));
251
+ })
252
+ .map(function (badge) {
253
+ var _a, _b;
254
+ // merge badge with badge meta data
255
+ var metaBadgeData = (_b = (_a = meta.badges) === null || _a === void 0 ? void 0 : _a.tags) === null || _b === void 0 ? void 0 : _b[badge.tag];
256
+ return __assign(__assign({}, badge), metaBadgeData);
257
+ })
258
+ .sort(function (a, b) {
259
+ return a.priority - b.priority;
260
+ });
261
+ (0, mobx_1.makeObservable)(this, {
262
+ all: mobx_1.observable,
263
+ tags: mobx_1.computed,
264
+ locations: mobx_1.computed,
265
+ });
266
+ }
267
+ // get all the result badges that are in a specific location
268
+ Badges.prototype.atLocation = function (location) {
269
+ var locations = Array.isArray(location) ? location : [location];
270
+ return this.all.filter(function (badge) {
271
+ // filter location
272
+ return locations.some(function (location) { return badge.location.startsWith("".concat(location, "/")) || badge.location == location; });
273
+ });
274
+ };
275
+ Object.defineProperty(Badges.prototype, "tags", {
276
+ get: function () {
277
+ return this.all.reduce(function (badgeMap, badge) {
278
+ badgeMap[badge.tag] = badge;
279
+ return badgeMap;
280
+ }, {});
281
+ },
282
+ enumerable: false,
283
+ configurable: true
284
+ });
285
+ Object.defineProperty(Badges.prototype, "locations", {
286
+ get: function () {
287
+ return this.all.reduce(function (locationMap, badge) {
288
+ // put badge in location by path
289
+ var _a = badge.location.split('/'), section = _a[0], tag = _a[1];
290
+ locationMap[section] = locationMap[section] || {};
291
+ locationMap[section][tag] = (locationMap[section][tag] || []).concat(badge);
292
+ return locationMap;
293
+ }, {});
294
+ },
295
+ enumerable: false,
296
+ configurable: true
297
+ });
298
+ return Badges;
299
+ }());
300
+ exports.Badges = Badges;
301
+ // Mask is used to power the product display for quick attribute swapping
302
+ var ProductMask = /** @class */ (function () {
303
+ function ProductMask() {
304
+ this.data = {};
305
+ (0, mobx_1.makeObservable)(this, {
306
+ data: mobx_1.observable,
307
+ });
308
+ }
309
+ ProductMask.prototype.merge = function (mask) {
310
+ // TODO: look into making more performant
311
+ // needed to prevent infinite re-render on merge with same data
312
+ if (JSON.stringify((0, deepmerge_1.default)(this.data, mask)) != JSON.stringify(this.data)) {
313
+ this.data = (0, deepmerge_1.default)(this.data, mask);
314
+ }
315
+ };
316
+ ProductMask.prototype.set = function (mask) {
317
+ // TODO: look into making more performant
318
+ // needed to prevent infinite re-render on set with same data
319
+ if (JSON.stringify(mask) != JSON.stringify(this.data)) {
320
+ this.data = mask;
321
+ }
322
+ };
323
+ ProductMask.prototype.reset = function () {
324
+ this.data = {};
325
+ };
326
+ return ProductMask;
327
+ }());
328
+ exports.ProductMask = ProductMask;
329
+ var Variants = /** @class */ (function () {
330
+ function Variants(variantData) {
331
+ var _this = this;
332
+ var _a;
333
+ this.data = [];
334
+ this.selections = [];
335
+ var _b = variantData || {}, config = _b.config, data = _b.data;
336
+ var _c = data || {}, variants = _c.variants, mask = _c.mask, meta = _c.meta;
337
+ var preferences = ((_a = variantData === null || variantData === void 0 ? void 0 : variantData.data) === null || _a === void 0 ? void 0 : _a.preferences) || {};
338
+ // setting function in constructor to prevent exposing mask as class property
339
+ this.setActive = function (variant) {
340
+ _this.active = variant;
341
+ var activeBadges = new Badges({
342
+ data: {
343
+ meta: meta,
344
+ result: variant,
345
+ },
346
+ });
347
+ mask.set({ mappings: _this.active.mappings, attributes: _this.active.attributes, badges: activeBadges });
348
+ };
349
+ if (config) {
350
+ this.config = config;
351
+ }
352
+ if (data.optionConfig) {
353
+ this.optionConfig = data.optionConfig;
354
+ }
355
+ this.update(variants, config, preferences);
356
+ }
357
+ Variants.prototype.update = function (variantData, config, preferences) {
358
+ var _this = this;
359
+ if (config === void 0) { config = this.config; }
360
+ try {
361
+ var options_2 = [];
362
+ // create variants objects
363
+ this.data = variantData
364
+ .filter(function (variant) { var _a, _b; return ((_a = _this.config) === null || _a === void 0 ? void 0 : _a.showDisabledSelectionValues) || ((_b = variant.mappings.core) === null || _b === void 0 ? void 0 : _b.available) !== false; })
365
+ .map(function (variant) {
366
+ var _a, _b, _c, _d;
367
+ // normalize price fields ensuring they are numbers
368
+ if ((_a = variant.mappings.core) === null || _a === void 0 ? void 0 : _a.price) {
369
+ variant.mappings.core.price = Number((_b = variant.mappings.core) === null || _b === void 0 ? void 0 : _b.price);
370
+ }
371
+ if ((_c = variant.mappings.core) === null || _c === void 0 ? void 0 : _c.msrp) {
372
+ variant.mappings.core.msrp = Number((_d = variant.mappings.core) === null || _d === void 0 ? void 0 : _d.msrp);
373
+ }
374
+ return variant;
375
+ })
376
+ .map(function (variant) {
377
+ Object.keys(variant.options).forEach(function (variantOption) {
378
+ if (!options_2.includes(variantOption)) {
379
+ options_2.push(variantOption);
380
+ }
381
+ });
382
+ return new Variant({
383
+ data: { variant: variant },
384
+ });
385
+ });
386
+ //need to reset this.selections first
387
+ this.selections = [];
388
+ options_2.map(function (option) {
389
+ var _a, _b;
390
+ var variantOptionConfig = ((_a = _this.config) === null || _a === void 0 ? void 0 : _a.options) && _this.config.options[option];
391
+ _this.selections.push(new VariantSelection({
392
+ config: variantOptionConfig,
393
+ optionConfig: (_b = _this.optionConfig) === null || _b === void 0 ? void 0 : _b[option],
394
+ data: {
395
+ variants: _this,
396
+ selectorField: option,
397
+ },
398
+ }));
399
+ });
400
+ var preselectedOptions_1 = {};
401
+ //api preferences
402
+ if (preferences) {
403
+ Object.keys(preferences).forEach(function (option) {
404
+ preselectedOptions_1[option] = preferences[option];
405
+ });
406
+ }
407
+ //user defined preferences - these override API preferences
408
+ if (config === null || config === void 0 ? void 0 : config.options) {
409
+ Object.keys(config === null || config === void 0 ? void 0 : config.options).forEach(function (option) {
410
+ if (config.options[option].preSelected) {
411
+ preselectedOptions_1[option] = config.options[option].preSelected;
412
+ }
413
+ });
414
+ }
415
+ if (config === null || config === void 0 ? void 0 : config.autoSelect) {
416
+ // select first available
417
+ this.makeSelections(preselectedOptions_1);
418
+ }
419
+ }
420
+ catch (err) {
421
+ // failed to parse the variant JSON
422
+ console.error(err, "Invalid variant JSON for: ".concat(variantData));
423
+ }
424
+ };
425
+ Variants.prototype.makeSelections = function (options) {
426
+ // options = {color: 'Blue', size: 'L'};
427
+ if (!options || !Object.keys(options).length) {
428
+ // select first available for each selection
429
+ this.selections.forEach(function (selection) {
430
+ var firstAvailableOption = selection.values.find(function (value) { return value.available; });
431
+ if (firstAvailableOption) {
432
+ selection.select(firstAvailableOption.value, true);
433
+ }
434
+ });
435
+ }
436
+ else {
437
+ this.selections.forEach(function (selection, idx) {
438
+ // filter by first available, then by preselected option preference
439
+ //make all options available for first selection.
440
+ var availableOptions = selection.values.filter(function (value) { return (idx == 0 ? true : value.available); });
441
+ var preferedOptions = options[selection.field.toLowerCase()];
442
+ var preferencedOption = selection.selected || availableOptions[0];
443
+ // if theres a preference for that field
444
+ if (preferedOptions) {
445
+ var checkIfAvailable_1 = function (preference) {
446
+ //see if that option is in the available options
447
+ var availablePreferedOptions = availableOptions.find(function (value) { return value.value.toString().toLowerCase() == (preference === null || preference === void 0 ? void 0 : preference.toString().toLowerCase()); });
448
+ //use it
449
+ if (availablePreferedOptions) {
450
+ preferencedOption = availablePreferedOptions;
451
+ }
452
+ };
453
+ if (Array.isArray(preferedOptions)) {
454
+ //loop through each preference option
455
+ preferedOptions.forEach(function (preference) {
456
+ checkIfAvailable_1(preference);
457
+ });
458
+ }
459
+ else {
460
+ checkIfAvailable_1(preferedOptions);
461
+ }
462
+ }
463
+ if (preferencedOption) {
464
+ selection.select(preferencedOption.value, true);
465
+ }
466
+ });
467
+ }
468
+ };
469
+ Variants.prototype.refineSelections = function (fromSelection) {
470
+ var _this = this;
471
+ // need to ensure the update originator is at the BOTTOM of the list for refinement
472
+ var orderedSelections = __spreadArray([], this.selections, true);
473
+ orderedSelections.sort(function (a) {
474
+ if (a.field == fromSelection.field) {
475
+ return 1;
476
+ }
477
+ return -1;
478
+ });
479
+ // refine selections ensuring that the selection that triggered the update refines LAST
480
+ orderedSelections.forEach(function (selection) { return selection.refineValues(_this); });
481
+ // check to see if we have enough selections made to update the display
482
+ var selectedSelections = this.selections.filter(function (selection) { var _a, _b; return (_b = (_a = selection.selected) === null || _a === void 0 ? void 0 : _a.value) === null || _b === void 0 ? void 0 : _b.length; });
483
+ if (selectedSelections.length) {
484
+ var availableVariants = this.data;
485
+ var _loop_1 = function (selectedSelection) {
486
+ availableVariants = availableVariants.filter(function (variant) { var _a, _b; return ((_a = selectedSelection.selected) === null || _a === void 0 ? void 0 : _a.value) == ((_b = variant.options[selectedSelection.field]) === null || _b === void 0 ? void 0 : _b.value) && variant.available; });
487
+ };
488
+ // loop through selectedSelections and only include available products that match current selections
489
+ for (var _i = 0, selectedSelections_1 = selectedSelections; _i < selectedSelections_1.length; _i++) {
490
+ var selectedSelection = selectedSelections_1[_i];
491
+ _loop_1(selectedSelection);
492
+ }
493
+ // set active variant
494
+ if (availableVariants.length == 1) {
495
+ var variantToSet_1 = availableVariants[0];
496
+ //need to update all unselected selections to match the single result
497
+ var unselectedSelections = this.selections.filter(function (selection) { return !selection.selected; });
498
+ unselectedSelections.forEach(function (selection) {
499
+ var field = selection.field;
500
+ var valueToSet = variantToSet_1.options[field].value;
501
+ selection.select(valueToSet);
502
+ });
503
+ this.setActive(variantToSet_1);
504
+ }
505
+ }
506
+ };
507
+ return Variants;
508
+ }());
509
+ exports.Variants = Variants;
510
+ var VariantSelection = /** @class */ (function () {
511
+ function VariantSelection(variantSelectionData) {
512
+ var _this = this;
513
+ this.selected = undefined;
514
+ this.previouslySelected = undefined;
515
+ this.values = [];
516
+ var _a = variantSelectionData || {}, data = _a.data, config = _a.config, optionConfig = _a.optionConfig;
517
+ var _b = data || {}, variants = _b.variants, selectorField = _b.selectorField;
518
+ this.field = selectorField;
519
+ this.type = optionConfig === null || optionConfig === void 0 ? void 0 : optionConfig.type;
520
+ this.count = optionConfig === null || optionConfig === void 0 ? void 0 : optionConfig.count;
521
+ this.label = (config === null || config === void 0 ? void 0 : config.label) || selectorField;
522
+ this.config = config || {};
523
+ // needed to prevent attaching variants as class property
524
+ this.variantsUpdate = function () { return variants.refineSelections(_this); };
525
+ // create possible values from the data and refine them
526
+ this.refineValues(variants);
527
+ (0, mobx_1.makeObservable)(this, {
528
+ selected: mobx_1.observable,
529
+ values: mobx_1.observable,
530
+ });
531
+ }
532
+ VariantSelection.prototype.refineValues = function (variants) {
533
+ var _this = this;
534
+ // current selection should only consider OTHER selections for availability
535
+ var selectedSelections = variants.selections.filter(function (selection) { return selection.field != _this.field && selection.selected; });
536
+ var availableVariants = variants.data.filter(function (variant) { return variant.available; });
537
+ var _loop_2 = function (selectedSelection) {
538
+ availableVariants = availableVariants.filter(function (variant) { var _a, _b, _c; return ((_a = selectedSelection.selected) === null || _a === void 0 ? void 0 : _a.value) == ((_c = (_b = variant.options) === null || _b === void 0 ? void 0 : _b[selectedSelection.field]) === null || _c === void 0 ? void 0 : _c.value) && variant.available; });
539
+ };
540
+ // loop through selectedSelections and remove products that do not match
541
+ for (var _i = 0, selectedSelections_2 = selectedSelections; _i < selectedSelections_2.length; _i++) {
542
+ var selectedSelection = selectedSelections_2[_i];
543
+ _loop_2(selectedSelection);
544
+ }
545
+ var newValues = variants.data
546
+ .filter(function (variant) { return variant.options[_this.field]; })
547
+ .reduce(function (values, variant) {
548
+ var _a;
549
+ if (!values.some(function (val) { return variant.options[_this.field].value == val.value; })) {
550
+ var value_1 = variant.options[_this.field].value;
551
+ var thumbnailImageUrl = (_a = variant.mappings.core) === null || _a === void 0 ? void 0 : _a.thumbnailImageUrl;
552
+ // A value should only be disabled if there are NO available variants in the entire dataset that have this value for the current field
553
+ var allAvailableVariants = variants.data.filter(function (variant) { return variant.available; });
554
+ var disabledValue = !allAvailableVariants.some(function (availableVariant) {
555
+ return availableVariant.options[_this.field].value === value_1;
556
+ });
557
+ var mappedValue = {
558
+ value: value_1,
559
+ label: value_1,
560
+ thumbnailImageUrl: thumbnailImageUrl,
561
+ available: Boolean(availableVariants.some(function (availableVariant) { return availableVariant.options[_this.field].value == variant.options[_this.field].value; })),
562
+ disabled: disabledValue,
563
+ };
564
+ if (_this.config.thumbnailBackgroundImages) {
565
+ mappedValue.backgroundImageUrl = thumbnailImageUrl;
566
+ }
567
+ else if (variant.options[_this.field].backgroundImageUrl) {
568
+ mappedValue.backgroundImageUrl = variant.options[_this.field].backgroundImageUrl;
569
+ }
570
+ if (variant.options[_this.field].background) {
571
+ mappedValue.background = variant.options[_this.field].background;
572
+ }
573
+ if (_this.config.mappings && _this.config.mappings && _this.config.mappings[value_1.toString().toLowerCase()]) {
574
+ var mapping = _this.config.mappings[value_1.toString().toLowerCase()];
575
+ if (mapping.label) {
576
+ mappedValue.label = mapping.label;
577
+ }
578
+ if (mapping.background) {
579
+ mappedValue.background = mapping.background;
580
+ }
581
+ if (mapping.backgroundImageUrl) {
582
+ mappedValue.backgroundImageUrl = mapping.backgroundImageUrl;
583
+ }
584
+ }
585
+ values.push(mappedValue);
586
+ }
587
+ // TODO: use sorting function from config
588
+ return values;
589
+ }, []);
590
+ // if selection has been made
591
+ if (this.selected) {
592
+ // check if the selection is stil available
593
+ if (!newValues.some(function (val) { var _a; return val.value == ((_a = _this.selected) === null || _a === void 0 ? void 0 : _a.value) && val.available; })) {
594
+ // the selection is no longer available, attempt to select previous selection
595
+ if (this.selected !== this.previouslySelected &&
596
+ this.previouslySelected &&
597
+ newValues.some(function (val) { var _a; return val.value == ((_a = _this.previouslySelected) === null || _a === void 0 ? void 0 : _a.value) && val.available; })) {
598
+ this.select(this.previouslySelected.value, true);
599
+ }
600
+ else {
601
+ // choose the first available option if previous seletions are unavailable
602
+ var availableValues = newValues.filter(function (val) { return val.available; });
603
+ if (newValues.length && availableValues.length) {
604
+ var nextAvailableValue = availableValues[0].value;
605
+ if (this.selected.value !== nextAvailableValue) {
606
+ this.select(nextAvailableValue, true);
607
+ }
608
+ }
609
+ }
610
+ }
611
+ }
612
+ this.values = newValues;
613
+ };
614
+ VariantSelection.prototype.reset = function () {
615
+ this.selected = undefined;
616
+ this.values.forEach(function (val) { return (val.available = false); });
617
+ };
618
+ VariantSelection.prototype.select = function (value, internalSelection) {
619
+ if (internalSelection === void 0) { internalSelection = false; }
620
+ var valueExist = this.values.find(function (val) { return val.value == value; });
621
+ if (valueExist) {
622
+ if (!internalSelection) {
623
+ this.previouslySelected = this.selected;
624
+ }
625
+ this.selected = valueExist;
626
+ this.variantsUpdate();
627
+ }
628
+ };
629
+ return VariantSelection;
630
+ }());
631
+ exports.VariantSelection = VariantSelection;
632
+ var Variant = /** @class */ (function () {
633
+ function Variant(variantData) {
634
+ var _a, _b;
635
+ this.type = 'variant';
636
+ this.attributes = {};
637
+ this.mappings = {
638
+ core: {},
639
+ };
640
+ this.custom = {};
641
+ var data = (variantData || {}).data;
642
+ var variant = (data || {}).variant;
643
+ this.attributes = variant.attributes || {};
644
+ this.mappings = variant.mappings;
645
+ this.options = variant.options;
646
+ // construct badges from data (need meta)
647
+ this.badges = variant.badges || [];
648
+ this.available = (_b = (_a = this.mappings.core) === null || _a === void 0 ? void 0 : _a.available) !== null && _b !== void 0 ? _b : true;
649
+ (0, mobx_1.makeObservable)(this, {
650
+ attributes: mobx_1.observable,
651
+ mappings: mobx_1.observable,
652
+ custom: mobx_1.observable,
653
+ available: mobx_1.observable,
654
+ });
655
+ }
656
+ return Variant;
657
+ }());
658
+ exports.Variant = Variant;
659
+ function addBannersToResults(config, results, allBanners, paginationData) {
660
+ var _a, _b, _c;
661
+ var bannersAndResults = __spreadArray([], results, true);
662
+ var paginationBegin = paginationData.pageSize * (paginationData.page - 1) + 1;
663
+ var paginationEnd = paginationData.pageSize * paginationData.page;
664
+ // if infinite scroll is enabled, we need to adjust the begin position
665
+ if ((_b = (_a = config === null || config === void 0 ? void 0 : config.settings) === null || _a === void 0 ? void 0 : _a.infinite) === null || _b === void 0 ? void 0 : _b.enabled)
666
+ paginationBegin = 1;
667
+ // if the end of the page is greater than the total results, adjust the end
668
+ if (paginationData.pageSize * paginationData.page > paginationData.totalResults)
669
+ paginationEnd = paginationData.totalResults;
670
+ // banners that have not allready been injected
671
+ var bannersNotInResults = allBanners.filter(function (banner) { return !bannersAndResults.some(function (result) { return result.id == banner.id; }); });
672
+ // banners that have an index position within the current set of results
673
+ var bannersToInject = bannersNotInResults.filter(function (banner) {
674
+ // find banners that are within the current pagination set
675
+ var index = banner.config.position.index;
676
+ return index >= paginationBegin - 1 && index <= paginationEnd - 1;
677
+ });
678
+ // banners can have an index greater than the total results, these should be injected at the end
679
+ var bannersToInjectAtEnd = bannersNotInResults.filter(function (banner) {
680
+ var index = banner.config.position.index;
681
+ return index >= paginationData.totalResults;
682
+ });
683
+ // inject banners that have index position within current set into the results
684
+ bannersToInject.forEach(function (banner) {
685
+ var adjustedIndex = banner.config.position.index - (paginationBegin - 1);
686
+ bannersAndResults.splice(adjustedIndex, 0, banner);
687
+ });
688
+ // inject banners at the end that fall within the current pagination set (but ensure there is room based on the begin and end indexes)
689
+ bannersToInjectAtEnd.forEach(function (banner, index) {
690
+ var resultIndex = paginationData.totalResults - (bannersToInjectAtEnd.length - index);
691
+ if (resultIndex >= paginationBegin - 1 && resultIndex <= paginationEnd - 1) {
692
+ bannersAndResults.splice(resultIndex, 0, banner);
693
+ }
694
+ });
695
+ // when using infinite, need to adjust the inline banner responseIds
696
+ if ((_c = config === null || config === void 0 ? void 0 : config.settings) === null || _c === void 0 ? void 0 : _c.infinite) {
697
+ bannersAndResults.forEach(function (item, index) {
698
+ if (item.type === 'banner') {
699
+ // need to determine what the correctResponseId is based on the index position, pageSize (current page) and adjacent product responseIds
700
+ var pageSize = paginationData.pageSize;
701
+ var currentPage = Math.floor(index / pageSize) + 1;
702
+ var firstItemIndexOnPage = (currentPage - 1) * pageSize;
703
+ var lastItemIndexOnPage = firstItemIndexOnPage + pageSize - 1;
704
+ // find the first product on the current page to grab its responseId
705
+ for (var i = firstItemIndexOnPage; i < lastItemIndexOnPage; i++) {
706
+ if (bannersAndResults[i].type === 'product') {
707
+ item.responseId = bannersAndResults[i].responseId;
708
+ break;
709
+ }
710
+ }
711
+ }
712
+ });
713
+ }
714
+ return bannersAndResults;
715
+ }
716
+ function variantOptionClick(elem, variantConfig, results) {
717
+ var options = {};
718
+ var attr = elem.getAttribute(VARIANT_ATTRIBUTE);
719
+ if (attr) {
720
+ var _a = attr.split(':'), option = _a[0], value = _a[1];
721
+ if (!option || !value) {
722
+ console.error('Error!: realtime variant is missing option or value (option:value)!', elem, attr);
723
+ }
724
+ else {
725
+ options[option.toLowerCase()] = [value.toLowerCase()];
726
+ makeVariantSelections(variantConfig, options, results);
727
+ }
728
+ }
729
+ }
730
+ function makeVariantSelections(variantConfig, options, results) {
731
+ var _a, _b;
732
+ var filteredResults = results;
733
+ // filter based on config
734
+ (_b = (_a = variantConfig.realtime) === null || _a === void 0 ? void 0 : _a.filters) === null || _b === void 0 ? void 0 : _b.forEach(function (filter) {
735
+ if (filter == 'first') {
736
+ filteredResults = [filteredResults[0]];
737
+ }
738
+ if (filter == 'unaltered') {
739
+ filteredResults = filteredResults.filter(function (result) { var _a; return !((_a = result.variants) === null || _a === void 0 ? void 0 : _a.selections.some(function (selection) { return selection.previouslySelected; })); });
740
+ }
741
+ });
742
+ filteredResults.forEach(function (result) {
743
+ var _a;
744
+ // no banner types
745
+ if (result.type == 'product') {
746
+ (_a = result.variants) === null || _a === void 0 ? void 0 : _a.makeSelections(options);
747
+ }
748
+ });
749
+ }