@pradip1995/segment-primitives 0.4.13 → 0.4.15

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.
@@ -1,381 +1,381 @@
1
- import type { HttpTypes } from "@medusajs/types"
2
-
3
- export type ProductOptionsMap = Record<string, string | undefined>
4
-
5
- export function variantOptionsAsKeymap(
6
- variantOptions: HttpTypes.StoreProductVariant["options"]
7
- ): Record<string, string> {
8
- return (
9
- variantOptions?.reduce((acc: Record<string, string>, option) => {
10
- if (option.option_id) {
11
- acc[option.option_id] = option.value ?? ""
12
- }
13
- return acc
14
- }, {}) ?? {}
15
- )
16
- }
17
-
18
- export function findColorOption(
19
- product: HttpTypes.StoreProduct
20
- ): HttpTypes.StoreProductOption | undefined {
21
- return product.options?.find((opt) => /color|colour/i.test(opt.title ?? ""))
22
- }
23
-
24
- export function findSizeOption(
25
- product: HttpTypes.StoreProduct
26
- ): HttpTypes.StoreProductOption | undefined {
27
- return product.options?.find((opt) => /size/i.test(opt.title ?? ""))
28
- }
29
-
30
- export function hasOptionSelection(options: ProductOptionsMap): boolean {
31
- return Object.values(options).some(Boolean)
32
- }
33
-
34
- export function findExactVariant(
35
- product: HttpTypes.StoreProduct,
36
- options: ProductOptionsMap
37
- ): HttpTypes.StoreProductVariant | undefined {
38
- if (!product.variants?.length) {
39
- return undefined
40
- }
41
-
42
- const selectedEntries = Object.entries(options).filter(
43
- (entry): entry is [string, string] => Boolean(entry[1])
44
- )
45
-
46
- if (!selectedEntries.length) {
47
- return undefined
48
- }
49
-
50
- return product.variants.find((variant) => {
51
- const variantOptions = variantOptionsAsKeymap(variant.options)
52
- return selectedEntries.every(([optionId, value]) => variantOptions[optionId] === value)
53
- })
54
- }
55
-
56
- export function resolveMatchingVariants(
57
- product: HttpTypes.StoreProduct,
58
- options: ProductOptionsMap
59
- ): HttpTypes.StoreProductVariant[] {
60
- const selectedEntries = Object.entries(options).filter(
61
- (entry): entry is [string, string] => Boolean(entry[1])
62
- )
63
-
64
- if (!selectedEntries.length || !product.variants?.length) {
65
- return []
66
- }
67
-
68
- return product.variants.filter((variant) => {
69
- const variantOptions = variantOptionsAsKeymap(variant.options)
70
- return selectedEntries.every(([optionId, value]) => variantOptions[optionId] === value)
71
- })
72
- }
73
-
74
- /** Option values still valid given other selected options (e.g. colors for a chosen size). */
75
- export function getAvailableOptionValues(
76
- product: HttpTypes.StoreProduct,
77
- optionId: string,
78
- selectedOptions: ProductOptionsMap
79
- ): string[] {
80
- const otherSelections = Object.entries(selectedOptions).filter(
81
- ([id, value]) => id !== optionId && Boolean(value)
82
- )
83
-
84
- const matchingVariants =
85
- product.variants?.filter((variant) => {
86
- const variantOptions = variantOptionsAsKeymap(variant.options)
87
- return otherSelections.every(([id, value]) => variantOptions[id] === value)
88
- }) ?? []
89
-
90
- const values = new Set<string>()
91
- matchingVariants.forEach((variant) => {
92
- const value = variantOptionsAsKeymap(variant.options)[optionId]
93
- if (value) values.add(value)
94
- })
95
-
96
- return Array.from(values)
97
- }
98
-
99
- export function isOptionValueAvailable(
100
- product: HttpTypes.StoreProduct,
101
- optionId: string,
102
- value: string,
103
- selectedOptions: ProductOptionsMap
104
- ): boolean {
105
- return getAvailableOptionValues(product, optionId, selectedOptions).includes(value)
106
- }
107
-
108
- type ImageMatchKeys = {
109
- urlKeys: Set<string>
110
- idKeys: Set<string>
111
- }
112
-
113
- function addImageToMatchKeys(
114
- image: Pick<HttpTypes.StoreProductImage, "url" | "id"> | undefined | null,
115
- keys: ImageMatchKeys
116
- ) {
117
- if (!image?.url) return
118
-
119
- const cleanUrl = image.url.split("?")[0].toLowerCase().trim()
120
- const urlPath = cleanUrl.split("/").pop() || ""
121
-
122
- keys.urlKeys.add(cleanUrl)
123
- if (urlPath) keys.urlKeys.add(urlPath.toLowerCase())
124
- if (image.id) keys.idKeys.add(image.id)
125
- }
126
-
127
- function imageMatchesKeys(image: HttpTypes.StoreProductImage, keys: ImageMatchKeys): boolean {
128
- if (!image.url) return false
129
-
130
- const cleanUrl = image.url.split("?")[0].toLowerCase().trim()
131
- const urlPath = cleanUrl.split("/").pop() || ""
132
- const urlMatches =
133
- keys.urlKeys.has(cleanUrl) || (urlPath ? keys.urlKeys.has(urlPath.toLowerCase()) : false)
134
- const idMatches = image.id ? keys.idKeys.has(image.id) : false
135
-
136
- return urlMatches || idMatches
137
- }
138
-
139
- function getVariantAssignedImageKeys(
140
- product: HttpTypes.StoreProduct,
141
- excludeVariantId?: string
142
- ): ImageMatchKeys {
143
- const keys: ImageMatchKeys = { urlKeys: new Set(), idKeys: new Set() }
144
-
145
- product.variants?.forEach((variant) => {
146
- if (excludeVariantId && variant.id === excludeVariantId) return
147
- variant.images?.forEach((image) => {
148
- addImageToMatchKeys(image as HttpTypes.StoreProductImage, keys)
149
- })
150
- })
151
-
152
- return keys
153
- }
154
-
155
- function getUnsupportedProductImages(
156
- product: HttpTypes.StoreProduct,
157
- selectedVariantId: string
158
- ): HttpTypes.StoreProductImage[] {
159
- const productImages = product.images || []
160
- if (!productImages.length) return []
161
-
162
- const assignedKeys = getVariantAssignedImageKeys(product, selectedVariantId)
163
- return productImages.filter((image) => !imageMatchesKeys(image, assignedKeys))
164
- }
165
-
166
- function getKeysForImages(images: HttpTypes.StoreProductImage[]): ImageMatchKeys {
167
- const keys: ImageMatchKeys = { urlKeys: new Set(), idKeys: new Set() }
168
- images.forEach((image) => addImageToMatchKeys(image, keys))
169
- return keys
170
- }
171
-
172
- function mergeVariantAndGeneralImages(
173
- variantImages: HttpTypes.StoreProductImage[],
174
- generalImages: HttpTypes.StoreProductImage[]
175
- ): HttpTypes.StoreProductImage[] {
176
- const variantKeys = getKeysForImages(variantImages)
177
- const merged = [...variantImages]
178
-
179
- generalImages.forEach((image) => {
180
- if (!imageMatchesKeys(image, variantKeys)) {
181
- merged.push(image)
182
- }
183
- })
184
-
185
- return merged
186
- }
187
-
188
- function getVariantExclusiveImages(
189
- product: HttpTypes.StoreProduct,
190
- variant: HttpTypes.StoreProductVariant
191
- ): HttpTypes.StoreProductImage[] {
192
- const variantImages = (variant.images || []) as HttpTypes.StoreProductImage[]
193
- if (!variantImages.length) return []
194
-
195
- const otherVariantKeys = getVariantAssignedImageKeys(product, variant.id)
196
- return variantImages.filter((image) => !imageMatchesKeys(image, otherVariantKeys))
197
- }
198
-
199
- function getPrimaryVariantImage(
200
- product: HttpTypes.StoreProduct,
201
- variant: HttpTypes.StoreProductVariant
202
- ): HttpTypes.StoreProductImage | undefined {
203
- const variantImages = (variant.images || []) as HttpTypes.StoreProductImage[]
204
- if (!variantImages.length) return undefined
205
-
206
- const exclusiveImages = getVariantExclusiveImages(product, variant)
207
- if (exclusiveImages.length > 0) {
208
- const exclusiveKeys = getKeysForImages(exclusiveImages)
209
- let primaryIndex = -1
210
-
211
- variantImages.forEach((image, index) => {
212
- if (imageMatchesKeys(image, exclusiveKeys)) primaryIndex = index
213
- })
214
-
215
- if (primaryIndex >= 0) return variantImages[primaryIndex]
216
- return exclusiveImages[exclusiveImages.length - 1]
217
- }
218
-
219
- return variantImages[0]
220
- }
221
-
222
- function reorderWithPrimaryFirst(
223
- images: HttpTypes.StoreProductImage[],
224
- primaryImage?: HttpTypes.StoreProductImage
225
- ): HttpTypes.StoreProductImage[] {
226
- if (!primaryImage) return images
227
-
228
- const primaryKeys = getKeysForImages([primaryImage])
229
- const primaryIndex = images.findIndex((image) => imageMatchesKeys(image, primaryKeys))
230
- if (primaryIndex <= 0) return images
231
-
232
- const reordered = [...images]
233
- const [primary] = reordered.splice(primaryIndex, 1)
234
- return [primary, ...reordered]
235
- }
236
-
237
- function dedupeImages(images: HttpTypes.StoreProductImage[]): HttpTypes.StoreProductImage[] {
238
- const seen: ImageMatchKeys = { urlKeys: new Set(), idKeys: new Set() }
239
- const deduped: HttpTypes.StoreProductImage[] = []
240
-
241
- images.forEach((image) => {
242
- if (!image.url || imageMatchesKeys(image, seen)) return
243
- deduped.push(image)
244
- addImageToMatchKeys(image, seen)
245
- })
246
-
247
- return deduped
248
- }
249
-
250
- export function getImagesForVariant(
251
- product: HttpTypes.StoreProduct,
252
- selectedVariantId?: string
253
- ): HttpTypes.StoreProductImage[] {
254
- if (!selectedVariantId || !product.variants) {
255
- return product.images || []
256
- }
257
-
258
- const variant = product.variants.find((v) => v.id === selectedVariantId)
259
- if (variant?.images && variant.images.length > 0) {
260
- const variantImages = [...variant.images] as HttpTypes.StoreProductImage[]
261
- const generalImages = getUnsupportedProductImages(product, selectedVariantId)
262
- const primaryImage = getPrimaryVariantImage(product, variant)
263
- const merged = mergeVariantAndGeneralImages(variantImages, generalImages)
264
- return reorderWithPrimaryFirst(merged, primaryImage)
265
- }
266
-
267
- return product.images || []
268
- }
269
-
270
- export function getImagesForSelection(
271
- product: HttpTypes.StoreProduct,
272
- options: ProductOptionsMap
273
- ): HttpTypes.StoreProductImage[] {
274
- if (!hasOptionSelection(options)) {
275
- return product.images || []
276
- }
277
-
278
- const exactVariant = findExactVariant(product, options)
279
- if (exactVariant) {
280
- return getImagesForVariant(product, exactVariant.id)
281
- }
282
-
283
- const matchingVariants = resolveMatchingVariants(product, options)
284
- if (!matchingVariants.length) {
285
- return product.images || []
286
- }
287
-
288
- const aggregated: HttpTypes.StoreProductImage[] = []
289
- const exclusivePrimaries: HttpTypes.StoreProductImage[] = []
290
-
291
- matchingVariants.forEach((variant) => {
292
- aggregated.push(...getImagesForVariant(product, variant.id))
293
- const exclusiveImages = getVariantExclusiveImages(product, variant)
294
- if (exclusiveImages.length > 0) {
295
- const primary = getPrimaryVariantImage(product, variant)
296
- if (primary) exclusivePrimaries.push(primary)
297
- }
298
- })
299
-
300
- let images = dedupeImages(aggregated)
301
- const uniquePrimaries = dedupeImages(exclusivePrimaries)
302
-
303
- uniquePrimaries.forEach((primary) => {
304
- images = reorderWithPrimaryFirst(images, primary)
305
- })
306
-
307
- return images.length > 0 ? images : product.images || []
308
- }
309
-
310
- export function getDefaultImageIndexForVariant(
311
- product: HttpTypes.StoreProduct,
312
- selectedVariantId: string | undefined,
313
- images: HttpTypes.StoreProductImage[]
314
- ): number {
315
- if (!images.length || !selectedVariantId || !product.variants) return 0
316
-
317
- const variant = product.variants.find((v) => v.id === selectedVariantId)
318
- const primaryVariantImage = variant ? getPrimaryVariantImage(product, variant) : undefined
319
- if (!primaryVariantImage) return 0
320
-
321
- const primaryKeys = getKeysForImages([primaryVariantImage])
322
- const index = images.findIndex((image) => imageMatchesKeys(image, primaryKeys))
323
- return index >= 0 ? index : 0
324
- }
325
-
326
- export function getDefaultImageIndexForSelection(
327
- product: HttpTypes.StoreProduct,
328
- options: ProductOptionsMap,
329
- images: HttpTypes.StoreProductImage[]
330
- ): number {
331
- if (!images.length) return 0
332
-
333
- const exactVariant = findExactVariant(product, options)
334
- if (exactVariant) {
335
- return getDefaultImageIndexForVariant(product, exactVariant.id, images)
336
- }
337
-
338
- return 0
339
- }
340
-
341
- export function buildInitialOptions(
342
- product: HttpTypes.StoreProduct,
343
- selectedVariantId?: string
344
- ): ProductOptionsMap {
345
- if (selectedVariantId && product.variants) {
346
- const variant = product.variants.find((v) => v.id === selectedVariantId)
347
- if (variant) return variantOptionsAsKeymap(variant.options)
348
- }
349
-
350
- if (product.variants?.length === 1) {
351
- return variantOptionsAsKeymap(product.variants[0].options)
352
- }
353
-
354
- const partialOptions: ProductOptionsMap = {}
355
- product.options?.forEach((option) => {
356
- const uniqueValues = new Set(option.values?.map((v) => v.value).filter(Boolean))
357
- if (uniqueValues.size === 1) {
358
- const value = Array.from(uniqueValues)[0]
359
- if (typeof value === "string" && option.id) partialOptions[option.id] = value
360
- }
361
- })
362
-
363
- return partialOptions
364
- }
365
-
366
- export function sanitizeOptionsForAvailability(
367
- product: HttpTypes.StoreProduct,
368
- options: ProductOptionsMap
369
- ): ProductOptionsMap {
370
- const next = { ...options }
371
-
372
- product.options?.forEach((option) => {
373
- if (!option.id || !next[option.id]) return
374
- const available = getAvailableOptionValues(product, option.id, next)
375
- if (!available.includes(next[option.id]!)) {
376
- delete next[option.id]
377
- }
378
- })
379
-
380
- return next
381
- }
1
+ import type { HttpTypes } from "@medusajs/types"
2
+
3
+ export type ProductOptionsMap = Record<string, string | undefined>
4
+
5
+ export function variantOptionsAsKeymap(
6
+ variantOptions: HttpTypes.StoreProductVariant["options"]
7
+ ): Record<string, string> {
8
+ return (
9
+ variantOptions?.reduce((acc: Record<string, string>, option) => {
10
+ if (option.option_id) {
11
+ acc[option.option_id] = option.value ?? ""
12
+ }
13
+ return acc
14
+ }, {}) ?? {}
15
+ )
16
+ }
17
+
18
+ export function findColorOption(
19
+ product: HttpTypes.StoreProduct
20
+ ): HttpTypes.StoreProductOption | undefined {
21
+ return product.options?.find((opt) => /color|colour/i.test(opt.title ?? ""))
22
+ }
23
+
24
+ export function findSizeOption(
25
+ product: HttpTypes.StoreProduct
26
+ ): HttpTypes.StoreProductOption | undefined {
27
+ return product.options?.find((opt) => /size/i.test(opt.title ?? ""))
28
+ }
29
+
30
+ export function hasOptionSelection(options: ProductOptionsMap): boolean {
31
+ return Object.values(options).some(Boolean)
32
+ }
33
+
34
+ export function findExactVariant(
35
+ product: HttpTypes.StoreProduct,
36
+ options: ProductOptionsMap
37
+ ): HttpTypes.StoreProductVariant | undefined {
38
+ if (!product.variants?.length) {
39
+ return undefined
40
+ }
41
+
42
+ const selectedEntries = Object.entries(options).filter(
43
+ (entry): entry is [string, string] => Boolean(entry[1])
44
+ )
45
+
46
+ if (!selectedEntries.length) {
47
+ return undefined
48
+ }
49
+
50
+ return product.variants.find((variant) => {
51
+ const variantOptions = variantOptionsAsKeymap(variant.options)
52
+ return selectedEntries.every(([optionId, value]) => variantOptions[optionId] === value)
53
+ })
54
+ }
55
+
56
+ export function resolveMatchingVariants(
57
+ product: HttpTypes.StoreProduct,
58
+ options: ProductOptionsMap
59
+ ): HttpTypes.StoreProductVariant[] {
60
+ const selectedEntries = Object.entries(options).filter(
61
+ (entry): entry is [string, string] => Boolean(entry[1])
62
+ )
63
+
64
+ if (!selectedEntries.length || !product.variants?.length) {
65
+ return []
66
+ }
67
+
68
+ return product.variants.filter((variant) => {
69
+ const variantOptions = variantOptionsAsKeymap(variant.options)
70
+ return selectedEntries.every(([optionId, value]) => variantOptions[optionId] === value)
71
+ })
72
+ }
73
+
74
+ /** Option values still valid given other selected options (e.g. colors for a chosen size). */
75
+ export function getAvailableOptionValues(
76
+ product: HttpTypes.StoreProduct,
77
+ optionId: string,
78
+ selectedOptions: ProductOptionsMap
79
+ ): string[] {
80
+ const otherSelections = Object.entries(selectedOptions).filter(
81
+ ([id, value]) => id !== optionId && Boolean(value)
82
+ )
83
+
84
+ const matchingVariants =
85
+ product.variants?.filter((variant) => {
86
+ const variantOptions = variantOptionsAsKeymap(variant.options)
87
+ return otherSelections.every(([id, value]) => variantOptions[id] === value)
88
+ }) ?? []
89
+
90
+ const values = new Set<string>()
91
+ matchingVariants.forEach((variant) => {
92
+ const value = variantOptionsAsKeymap(variant.options)[optionId]
93
+ if (value) values.add(value)
94
+ })
95
+
96
+ return Array.from(values)
97
+ }
98
+
99
+ export function isOptionValueAvailable(
100
+ product: HttpTypes.StoreProduct,
101
+ optionId: string,
102
+ value: string,
103
+ selectedOptions: ProductOptionsMap
104
+ ): boolean {
105
+ return getAvailableOptionValues(product, optionId, selectedOptions).includes(value)
106
+ }
107
+
108
+ type ImageMatchKeys = {
109
+ urlKeys: Set<string>
110
+ idKeys: Set<string>
111
+ }
112
+
113
+ function addImageToMatchKeys(
114
+ image: Pick<HttpTypes.StoreProductImage, "url" | "id"> | undefined | null,
115
+ keys: ImageMatchKeys
116
+ ) {
117
+ if (!image?.url) return
118
+
119
+ const cleanUrl = image.url.split("?")[0].toLowerCase().trim()
120
+ const urlPath = cleanUrl.split("/").pop() || ""
121
+
122
+ keys.urlKeys.add(cleanUrl)
123
+ if (urlPath) keys.urlKeys.add(urlPath.toLowerCase())
124
+ if (image.id) keys.idKeys.add(image.id)
125
+ }
126
+
127
+ function imageMatchesKeys(image: HttpTypes.StoreProductImage, keys: ImageMatchKeys): boolean {
128
+ if (!image.url) return false
129
+
130
+ const cleanUrl = image.url.split("?")[0].toLowerCase().trim()
131
+ const urlPath = cleanUrl.split("/").pop() || ""
132
+ const urlMatches =
133
+ keys.urlKeys.has(cleanUrl) || (urlPath ? keys.urlKeys.has(urlPath.toLowerCase()) : false)
134
+ const idMatches = image.id ? keys.idKeys.has(image.id) : false
135
+
136
+ return urlMatches || idMatches
137
+ }
138
+
139
+ function getVariantAssignedImageKeys(
140
+ product: HttpTypes.StoreProduct,
141
+ excludeVariantId?: string
142
+ ): ImageMatchKeys {
143
+ const keys: ImageMatchKeys = { urlKeys: new Set(), idKeys: new Set() }
144
+
145
+ product.variants?.forEach((variant) => {
146
+ if (excludeVariantId && variant.id === excludeVariantId) return
147
+ variant.images?.forEach((image) => {
148
+ addImageToMatchKeys(image as HttpTypes.StoreProductImage, keys)
149
+ })
150
+ })
151
+
152
+ return keys
153
+ }
154
+
155
+ function getUnsupportedProductImages(
156
+ product: HttpTypes.StoreProduct,
157
+ selectedVariantId: string
158
+ ): HttpTypes.StoreProductImage[] {
159
+ const productImages = product.images || []
160
+ if (!productImages.length) return []
161
+
162
+ const assignedKeys = getVariantAssignedImageKeys(product, selectedVariantId)
163
+ return productImages.filter((image) => !imageMatchesKeys(image, assignedKeys))
164
+ }
165
+
166
+ function getKeysForImages(images: HttpTypes.StoreProductImage[]): ImageMatchKeys {
167
+ const keys: ImageMatchKeys = { urlKeys: new Set(), idKeys: new Set() }
168
+ images.forEach((image) => addImageToMatchKeys(image, keys))
169
+ return keys
170
+ }
171
+
172
+ function mergeVariantAndGeneralImages(
173
+ variantImages: HttpTypes.StoreProductImage[],
174
+ generalImages: HttpTypes.StoreProductImage[]
175
+ ): HttpTypes.StoreProductImage[] {
176
+ const variantKeys = getKeysForImages(variantImages)
177
+ const merged = [...variantImages]
178
+
179
+ generalImages.forEach((image) => {
180
+ if (!imageMatchesKeys(image, variantKeys)) {
181
+ merged.push(image)
182
+ }
183
+ })
184
+
185
+ return merged
186
+ }
187
+
188
+ function getVariantExclusiveImages(
189
+ product: HttpTypes.StoreProduct,
190
+ variant: HttpTypes.StoreProductVariant
191
+ ): HttpTypes.StoreProductImage[] {
192
+ const variantImages = (variant.images || []) as HttpTypes.StoreProductImage[]
193
+ if (!variantImages.length) return []
194
+
195
+ const otherVariantKeys = getVariantAssignedImageKeys(product, variant.id)
196
+ return variantImages.filter((image) => !imageMatchesKeys(image, otherVariantKeys))
197
+ }
198
+
199
+ function getPrimaryVariantImage(
200
+ product: HttpTypes.StoreProduct,
201
+ variant: HttpTypes.StoreProductVariant
202
+ ): HttpTypes.StoreProductImage | undefined {
203
+ const variantImages = (variant.images || []) as HttpTypes.StoreProductImage[]
204
+ if (!variantImages.length) return undefined
205
+
206
+ const exclusiveImages = getVariantExclusiveImages(product, variant)
207
+ if (exclusiveImages.length > 0) {
208
+ const exclusiveKeys = getKeysForImages(exclusiveImages)
209
+ let primaryIndex = -1
210
+
211
+ variantImages.forEach((image, index) => {
212
+ if (imageMatchesKeys(image, exclusiveKeys)) primaryIndex = index
213
+ })
214
+
215
+ if (primaryIndex >= 0) return variantImages[primaryIndex]
216
+ return exclusiveImages[exclusiveImages.length - 1]
217
+ }
218
+
219
+ return variantImages[0]
220
+ }
221
+
222
+ function reorderWithPrimaryFirst(
223
+ images: HttpTypes.StoreProductImage[],
224
+ primaryImage?: HttpTypes.StoreProductImage
225
+ ): HttpTypes.StoreProductImage[] {
226
+ if (!primaryImage) return images
227
+
228
+ const primaryKeys = getKeysForImages([primaryImage])
229
+ const primaryIndex = images.findIndex((image) => imageMatchesKeys(image, primaryKeys))
230
+ if (primaryIndex <= 0) return images
231
+
232
+ const reordered = [...images]
233
+ const [primary] = reordered.splice(primaryIndex, 1)
234
+ return [primary, ...reordered]
235
+ }
236
+
237
+ function dedupeImages(images: HttpTypes.StoreProductImage[]): HttpTypes.StoreProductImage[] {
238
+ const seen: ImageMatchKeys = { urlKeys: new Set(), idKeys: new Set() }
239
+ const deduped: HttpTypes.StoreProductImage[] = []
240
+
241
+ images.forEach((image) => {
242
+ if (!image.url || imageMatchesKeys(image, seen)) return
243
+ deduped.push(image)
244
+ addImageToMatchKeys(image, seen)
245
+ })
246
+
247
+ return deduped
248
+ }
249
+
250
+ export function getImagesForVariant(
251
+ product: HttpTypes.StoreProduct,
252
+ selectedVariantId?: string
253
+ ): HttpTypes.StoreProductImage[] {
254
+ if (!selectedVariantId || !product.variants) {
255
+ return product.images || []
256
+ }
257
+
258
+ const variant = product.variants.find((v) => v.id === selectedVariantId)
259
+ if (variant?.images && variant.images.length > 0) {
260
+ const variantImages = [...variant.images] as HttpTypes.StoreProductImage[]
261
+ const generalImages = getUnsupportedProductImages(product, selectedVariantId)
262
+ const primaryImage = getPrimaryVariantImage(product, variant)
263
+ const merged = mergeVariantAndGeneralImages(variantImages, generalImages)
264
+ return reorderWithPrimaryFirst(merged, primaryImage)
265
+ }
266
+
267
+ return product.images || []
268
+ }
269
+
270
+ export function getImagesForSelection(
271
+ product: HttpTypes.StoreProduct,
272
+ options: ProductOptionsMap
273
+ ): HttpTypes.StoreProductImage[] {
274
+ if (!hasOptionSelection(options)) {
275
+ return product.images || []
276
+ }
277
+
278
+ const exactVariant = findExactVariant(product, options)
279
+ if (exactVariant) {
280
+ return getImagesForVariant(product, exactVariant.id)
281
+ }
282
+
283
+ const matchingVariants = resolveMatchingVariants(product, options)
284
+ if (!matchingVariants.length) {
285
+ return product.images || []
286
+ }
287
+
288
+ const aggregated: HttpTypes.StoreProductImage[] = []
289
+ const exclusivePrimaries: HttpTypes.StoreProductImage[] = []
290
+
291
+ matchingVariants.forEach((variant) => {
292
+ aggregated.push(...getImagesForVariant(product, variant.id))
293
+ const exclusiveImages = getVariantExclusiveImages(product, variant)
294
+ if (exclusiveImages.length > 0) {
295
+ const primary = getPrimaryVariantImage(product, variant)
296
+ if (primary) exclusivePrimaries.push(primary)
297
+ }
298
+ })
299
+
300
+ let images = dedupeImages(aggregated)
301
+ const uniquePrimaries = dedupeImages(exclusivePrimaries)
302
+
303
+ uniquePrimaries.forEach((primary) => {
304
+ images = reorderWithPrimaryFirst(images, primary)
305
+ })
306
+
307
+ return images.length > 0 ? images : product.images || []
308
+ }
309
+
310
+ export function getDefaultImageIndexForVariant(
311
+ product: HttpTypes.StoreProduct,
312
+ selectedVariantId: string | undefined,
313
+ images: HttpTypes.StoreProductImage[]
314
+ ): number {
315
+ if (!images.length || !selectedVariantId || !product.variants) return 0
316
+
317
+ const variant = product.variants.find((v) => v.id === selectedVariantId)
318
+ const primaryVariantImage = variant ? getPrimaryVariantImage(product, variant) : undefined
319
+ if (!primaryVariantImage) return 0
320
+
321
+ const primaryKeys = getKeysForImages([primaryVariantImage])
322
+ const index = images.findIndex((image) => imageMatchesKeys(image, primaryKeys))
323
+ return index >= 0 ? index : 0
324
+ }
325
+
326
+ export function getDefaultImageIndexForSelection(
327
+ product: HttpTypes.StoreProduct,
328
+ options: ProductOptionsMap,
329
+ images: HttpTypes.StoreProductImage[]
330
+ ): number {
331
+ if (!images.length) return 0
332
+
333
+ const exactVariant = findExactVariant(product, options)
334
+ if (exactVariant) {
335
+ return getDefaultImageIndexForVariant(product, exactVariant.id, images)
336
+ }
337
+
338
+ return 0
339
+ }
340
+
341
+ export function buildInitialOptions(
342
+ product: HttpTypes.StoreProduct,
343
+ selectedVariantId?: string
344
+ ): ProductOptionsMap {
345
+ if (selectedVariantId && product.variants) {
346
+ const variant = product.variants.find((v) => v.id === selectedVariantId)
347
+ if (variant) return variantOptionsAsKeymap(variant.options)
348
+ }
349
+
350
+ if (product.variants?.length === 1) {
351
+ return variantOptionsAsKeymap(product.variants[0].options)
352
+ }
353
+
354
+ const partialOptions: ProductOptionsMap = {}
355
+ product.options?.forEach((option) => {
356
+ const uniqueValues = new Set(option.values?.map((v) => v.value).filter(Boolean))
357
+ if (uniqueValues.size === 1) {
358
+ const value = Array.from(uniqueValues)[0]
359
+ if (typeof value === "string" && option.id) partialOptions[option.id] = value
360
+ }
361
+ })
362
+
363
+ return partialOptions
364
+ }
365
+
366
+ export function sanitizeOptionsForAvailability(
367
+ product: HttpTypes.StoreProduct,
368
+ options: ProductOptionsMap
369
+ ): ProductOptionsMap {
370
+ const next = { ...options }
371
+
372
+ product.options?.forEach((option) => {
373
+ if (!option.id || !next[option.id]) return
374
+ const available = getAvailableOptionValues(product, option.id, next)
375
+ if (!available.includes(next[option.id]!)) {
376
+ delete next[option.id]
377
+ }
378
+ })
379
+
380
+ return next
381
+ }