@misiki/gocommerce-connector 0.28.0 → 0.29.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.
@@ -1574,7 +1574,23 @@ function attributesFrom(p) {
1574
1574
  return Object.keys(out).length ? out : void 0;
1575
1575
  }
1576
1576
  var counts = (values) => Object.fromEntries((values ?? []).map((v) => [v.value, v.count]));
1577
- function toStorefrontFacets(f) {
1577
+ function categoryFacets(counted, tree) {
1578
+ const byId = new Map(tree.map((c) => [c.id, c]));
1579
+ const totals = /* @__PURE__ */ new Map();
1580
+ for (const { id: id2, count } of counted) {
1581
+ for (let c = byId.get(id2); c; c = c.parent_id === null ? void 0 : byId.get(c.parent_id)) {
1582
+ totals.set(c.id, (totals.get(c.id) ?? 0) + count);
1583
+ }
1584
+ }
1585
+ const kept = tree.filter((c) => totals.has(c.id)).sort((a, b) => a.position - b.position || a.id - b.id);
1586
+ const parentOf = (c) => c.parent_id !== null && byId.has(c.parent_id) ? c.parent_id : null;
1587
+ const under = (parent) => kept.filter((c) => parentOf(c) === parent).flatMap((c) => [{ name: c.title, slug: c.slug, count: totals.get(c.id), depth: c.depth }, ...under(c.id)]);
1588
+ return [
1589
+ ...under(null),
1590
+ ...counted.filter((c) => !byId.has(c.id)).map((c) => ({ name: c.title, slug: c.slug, count: c.count, depth: 0 }))
1591
+ ];
1592
+ }
1593
+ function toStorefrontFacets(f, tree = []) {
1578
1594
  if (!f) return { priceStat: {}, categories: [], tags: [], allFilters: {} };
1579
1595
  const allFilters = {};
1580
1596
  const group = (key, values) => {
@@ -1585,7 +1601,7 @@ function toStorefrontFacets(f) {
1585
1601
  for (const [handle, values] of Object.entries(f.attributes ?? {})) group(`attributes.${handle}`, values);
1586
1602
  return {
1587
1603
  priceStat: f.price ? { min: toMajor(f.price.min), max: toMajor(f.price.max) } : {},
1588
- categories: (f.categories ?? []).map((c) => ({ name: c.title, slug: c.slug, count: c.count })),
1604
+ categories: categoryFacets(f.categories ?? [], tree),
1589
1605
  tags: (f.tags ?? []).map((t) => ({ name: t.value, count: t.count })),
1590
1606
  allFilters
1591
1607
  };
@@ -1618,13 +1634,14 @@ var SearchService = class extends CatalogService {
1618
1634
  productTypes: listParam(p, "product_type"),
1619
1635
  facets: true
1620
1636
  });
1637
+ const tree = res.facets?.categories?.length ? await this.facts.categories().catch(() => []) : [];
1621
1638
  return {
1622
1639
  data: res.data,
1623
1640
  count: res.count,
1624
1641
  totalPages: res.noOfPage,
1625
1642
  page: res.page,
1626
1643
  categoryHierarchy: res.categoryHierarchy,
1627
- facets: toStorefrontFacets(res.facets)
1644
+ facets: toStorefrontFacets(res.facets, tree)
1628
1645
  };
1629
1646
  } catch (err) {
1630
1647
  if (err?.status === 404) return this.emptyResult();