From 24e9e1c3ee776fbd2168d2291f6b4ce55e69f5c2 Mon Sep 17 00:00:00 2001
From: James <Jamestp19@gmail.com>
Date: Wed, 23 Oct 2024 15:51:18 -0400
Subject: [PATCH] [ALS-7494] Ensure selected facet counts are updated when
 required (#270)

---
 .../components/explorer/FacetCategory.svelte   | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/lib/components/explorer/FacetCategory.svelte b/src/lib/components/explorer/FacetCategory.svelte
index 4fbfdfa2..45998625 100644
--- a/src/lib/components/explorer/FacetCategory.svelte
+++ b/src/lib/components/explorer/FacetCategory.svelte
@@ -34,15 +34,17 @@
     let facetsToDisplay = facets.filter((f) => !hiddenFacetsForCategory.includes(f.name));
 
     //Put selected facets at the top
-    $selectedFacets.forEach((facet) => {
-      let index = facetsToDisplay.findIndex((f) => f.name === facet.name);
-      if (index > -1) {
-        facetsToDisplay.splice(index, 1);
-      }
-    });
-    facetsToDisplay.unshift(
-      ...$selectedFacets.filter((facet) => facet.category === facetCategory.name),
+    const selectedFacetsMap = new Map($selectedFacets.map((facet) => [facet.name, facet]));
+    facetsToDisplay = facetsToDisplay.filter((f) => !selectedFacetsMap.has(f.name));
+
+    const selectedFacetsForCategory = $selectedFacets.filter(
+      (facet) => facet.category === facetCategory.name,
     );
+    selectedFacetsForCategory.forEach((facet) => {
+      facet.count = facets.find((f) => f.name === facet.name)?.count || 0;
+    });
+
+    facetsToDisplay.unshift(...selectedFacetsForCategory);
     if (textFilterValue) {
       //Filter Facets by searched text
       const lowerFilterValue = textFilterValue.toLowerCase();