@final-commerce/common 2.13.0-staging2.1 → 2.13.0-staging2.2

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.
package/dist/index.mjs CHANGED
@@ -1669,13 +1669,14 @@ function compositeItemUnavailable(item, rows) {
1669
1669
  }
1670
1670
  return compositeVariantInStock(variantId, item.quantity, outlet) ? null : "out_of_stock";
1671
1671
  }
1672
- function compositeMakeable(parts, rows) {
1672
+ function compositeMakeable(parts, rows, inner = /* @__PURE__ */ new Map()) {
1673
1673
  const { outlet } = rows;
1674
1674
  if (!outlet) throw new Error("compositeMakeable counts at one outlet: rows.outlet is required");
1675
- const required = parts.map((part, partIndex) => ({ ...part, partIndex })).filter((part) => part.min > 0);
1675
+ const own = parts.map((part, partIndex) => ({ ...part, partIndex })).filter((part) => part.min > 0);
1676
1676
  const isBookable = (variantId) => rows.bookableProductIds.has(rows.variantProducts.get(String(variantId)) ?? "");
1677
- if (required.some((part) => part.items.some((item) => item.variantId && isBookable(item.variantId))))
1677
+ if (own.some((part) => part.items.some((item) => item.variantId && isBookable(item.variantId))))
1678
1678
  return { byDate: true };
1679
+ const required = own.flatMap((part) => withInnerParts(part, inner, rows, /* @__PURE__ */ new Set()));
1679
1680
  const optionsOf = (item) => (item.categoryId ? rows.categoryVariants.get(String(item.categoryId)) ?? [] : [String(item.variantId)]).flatMap(
1680
1681
  (variantId) => {
1681
1682
  const reason = compositeItemUnavailable({ variantId, quantity: item.quantity }, rows);
@@ -1684,7 +1685,7 @@ function compositeMakeable(parts, rows) {
1684
1685
  const stock = outlet.poolStock.get(shelf.pool);
1685
1686
  const onHand = shelf.unresolved || stock === void 0 ? 0 : stock === null || shelf.backorder ? null : stock;
1686
1687
  const perPick = shelf.unit ? toBase(item.quantity, shelf.unit) : item.quantity;
1687
- return [{ itemId: item.id, pool: shelf.pool, perPick, onHand }];
1688
+ return [{ itemId: item.id, variantId, pool: shelf.pool, perPick, onHand }];
1688
1689
  }
1689
1690
  );
1690
1691
  const counted = required.map((part) => ({ ...part, options: part.items.flatMap(optionsOf) }));
@@ -1702,28 +1703,31 @@ function compositeMakeable(parts, rows) {
1702
1703
  reserved.set(option.pool, (reserved.get(option.pool) ?? 0) + taken * option.perPick);
1703
1704
  wanted -= taken;
1704
1705
  }
1705
- if (wanted > 0) return part.partIndex;
1706
+ if (wanted > 0) return part;
1706
1707
  }
1707
1708
  return void 0;
1708
1709
  };
1709
1710
  const whyRows = [];
1710
1711
  const rowPools = /* @__PURE__ */ new Map();
1712
+ const rowOf = /* @__PURE__ */ new Map();
1711
1713
  const makes = (has, uses) => has === null ? null : Math.floor(has / uses);
1712
1714
  for (const part of counted) {
1713
1715
  const [option] = part.options;
1714
1716
  if (part.options.length === 1) {
1715
1717
  const uses = option.perPick * part.min;
1716
- const row = whyRows.find((candidate) => rowPools.get(candidate) === option.pool);
1717
- if (!row) {
1718
+ const row2 = whyRows.find((candidate) => rowPools.get(candidate) === option.pool);
1719
+ if (!row2) {
1718
1720
  const created = { itemIds: [option.itemId], uses, has: option.onHand, makes: makes(option.onHand, uses) };
1719
1721
  rowPools.set(created, option.pool);
1722
+ rowOf.set(part, created);
1720
1723
  whyRows.push(created);
1721
1724
  continue;
1722
1725
  }
1723
- row.itemIds.push(option.itemId);
1724
- row.uses += uses;
1725
- if (option.onHand === null) row.has = null;
1726
- row.makes = makes(row.has, row.uses);
1726
+ rowOf.set(part, row2);
1727
+ if (!row2.itemIds.includes(option.itemId)) row2.itemIds.push(option.itemId);
1728
+ row2.uses += uses;
1729
+ if (option.onHand === null) row2.has = null;
1730
+ row2.makes = makes(row2.has, row2.uses);
1727
1731
  continue;
1728
1732
  }
1729
1733
  const perPool = /* @__PURE__ */ new Map();
@@ -1731,13 +1735,19 @@ function compositeMakeable(parts, rows) {
1731
1735
  perPool.set(candidate.pool, Math.max(perPool.get(candidate.pool) ?? 0, picksLeft(candidate, /* @__PURE__ */ new Map())));
1732
1736
  const total = [...perPool.values()].reduce((sum, picks) => sum + picks, 0);
1733
1737
  const has = total === Infinity ? null : total;
1734
- whyRows.push({
1735
- itemIds: part.items.map((item) => item.id),
1738
+ const row = {
1739
+ itemIds: [...new Set(part.items.map((item) => item.id))],
1736
1740
  partIndex: part.partIndex,
1737
1741
  uses: part.min,
1738
1742
  has,
1739
1743
  makes: makes(has, part.min)
1740
- });
1744
+ };
1745
+ rowOf.set(part, row);
1746
+ whyRows.push(row);
1747
+ }
1748
+ for (const row of new Set(counted.filter((part) => part.inner).map((part) => rowOf.get(part)))) {
1749
+ const rowParts = counted.filter((part) => rowOf.get(part) === row);
1750
+ row.variantIds = [...new Set(rowParts.flatMap((part) => part.options.map((option) => option.variantId)))];
1741
1751
  }
1742
1752
  if (counted.every((part) => part.options.some((option) => option.onHand === null)))
1743
1753
  return { count: null, rows: whyRows };
@@ -1753,12 +1763,29 @@ function compositeMakeable(parts, rows) {
1753
1763
  else high = mid - 1;
1754
1764
  }
1755
1765
  const short = shortAt(low + 1);
1756
- const shortPart = counted.find((part) => part.partIndex === short);
1757
- const limitedBy = whyRows.findIndex(
1758
- (row) => shortPart?.options.length === 1 ? row.partIndex === void 0 && row.itemIds.includes(shortPart.options[0].itemId) : row.partIndex === short
1759
- );
1766
+ const limitedBy = short ? whyRows.indexOf(rowOf.get(short)) : -1;
1760
1767
  return { count: low, rows: whyRows, ...limitedBy >= 0 ? { limitedBy } : {} };
1761
1768
  }
1769
+ function withInnerParts(part, inner, rows, seen) {
1770
+ const [item] = part.items;
1771
+ const variantId = String(item?.variantId);
1772
+ const innerParts = part.items.length === 1 && item.variantId ? inner.get(variantId) : void 0;
1773
+ const reason = innerParts && compositeItemUnavailable({ variantId, quantity: item.quantity }, rows);
1774
+ if (!innerParts || seen.has(variantId) || reason && reason !== "out_of_stock") return [part];
1775
+ return innerParts.filter((innerPart) => innerPart.min > 0).flatMap(
1776
+ (innerPart) => withInnerParts(
1777
+ {
1778
+ ...part,
1779
+ inner: true,
1780
+ min: part.min * item.quantity * innerPart.min,
1781
+ items: innerPart.items.map((innerItem) => ({ ...innerItem, id: item.id }))
1782
+ },
1783
+ inner,
1784
+ rows,
1785
+ new Set(seen).add(variantId)
1786
+ )
1787
+ );
1788
+ }
1762
1789
  var poolOf = (line) => line.stockVariantId || line.variantId;
1763
1790
  function poolDemand(lines) {
1764
1791
  const demand = /* @__PURE__ */ new Map();