{
  "version": 3,
  "sources": ["/cdn/shop/t/95/assets/cart-cross-sell-yy-cleanup.js?cb=20261119"],
  "sourcesContent": ["/**\n * Cart YY add-ons leave with the line that asked for them.\n *\n * A product added from a Cart YY card (cart-cross-sell-yy.js) is an add-on: its\n * line carries the hidden `_pb_upsell_source: cart-crosssell` property. It\n * stays in the cart only while another line still asks for it under the rules\n * in sections/cart-cross-sell-rules.liquid. Example: the Don Pepino sauce stays\n * while a \"Dough Balls\" line is in the cart; when the dough goes (\"\u00D7\", \"\u2212\" to 0\n * or any other removal), the sauce goes too. The discounted pizza follows the\n * same idea through its own rule in cart-subscription-upsell.js.\n *\n *   - Only add-on lines are removed. The same product added from its own page\n *     has no marker and stays.\n *   - An add-on can ask for another add-on (rule chains): one is kept when a\n *     kept line asks for it, starting from the lines that are not add-ons. So\n *     add-ons can never keep each other in the cart on their own.\n *   - Removals are announced to cart-upsell-events.js, so Intelligems does not\n *     count them as the shopper's.\n *\n * Runs whenever the drawer may show a new cart (same triggers as\n * cart-subscription-upsell.js). Reads the shared cart (assets/cart-store.js),\n * which waits for cart writes in flight, so it never removes from a stale cart.\n *\n * Loaded after cart-cross-sell-yy.js, only with the \"Cart YY\" drawer style\n * (snippets/cart-cross-sell.liquid).\n */\n(() => {\n  const rules = window.PocoBeroCrossSellRules;\n  const cartUpsell = window.PocoBeroCartUpsell;\n  if (!rules || !cartUpsell) return;\n\n  // Short debounce: coalesces bursts of drawer DOM updates into one pass.\n  const RUN_DELAY_MS = 30;\n\n  /**\n   * Add-on lines no kept line asks for. Lines that are not add-ons are kept\n   * first; an add-on joins them when a kept line asks for it, until no more join.\n   */\n  const orphanedAddOns = (cart) => {\n    const lines = cartUpsell.renderedCartItems(cart);\n    let kept = lines.filter((item) => !rules.isAddOn(item));\n    let waiting = lines.filter((item) => rules.isAddOn(item));\n    let joining = [];\n    do {\n      const asked = rules.productsAskedBy(kept);\n      joining = waiting.filter((item) => asked.has(Number(item.product_id)));\n      kept = kept.concat(joining);\n      waiting = waiting.filter((item) => !joining.includes(item));\n    } while (joining.length && waiting.length);\n    return waiting;\n  };\n\n  const removeLines = async (lines) => {\n    const updates = Object.fromEntries(lines.map((item) => [item.key, 0]));\n    window.PocoBeroCartEvents?.expectRemoval(Object.keys(updates));\n    const response = await fetch('/cart/update.js', {\n      method: 'POST',\n      headers: { 'Content-Type': 'application/json', Accept: 'application/json' },\n      body: JSON.stringify({ updates })\n    });\n    if (!response.ok) throw new Error(`Add-on cleanup failed: ${response.status}`);\n    document.dispatchEvent(new CustomEvent('update-cart'));\n  };\n\n  let isRunning = false;\n  let runAgain = false;\n  let runTimer;\n\n  /** One pass; a pass asked for while one runs is done right after it. */\n  const run = async () => {\n    if (isRunning) {\n      runAgain = true;\n      return;\n    }\n    isRunning = true;\n    try {\n      if (!orphanedAddOns(await cartUpsell.readCart()).length) return;\n      // The pizza rule may have started its own removal from the same cart:\n      // reading again waits for that write, so the two never overlap.\n      const orphans = orphanedAddOns(await cartUpsell.readCart());\n      if (orphans.length) await removeLines(orphans);\n    } catch (error) {\n      console.error('[Poco Bero] Cart add-on cleanup failed', error);\n    } finally {\n      isRunning = false;\n      if (runAgain) {\n        runAgain = false;\n        schedule();\n      }\n    }\n  };\n\n  const schedule = () => {\n    clearTimeout(runTimer);\n    runTimer = setTimeout(run, RUN_DELAY_MS);\n  };\n\n  document.addEventListener('DOMContentLoaded', schedule);\n  document.addEventListener('update-cart', schedule);\n  document.addEventListener('toggle-cart', schedule);\n\n  // The drawer's own \"\u00D7\" and \"\u2212\" change the cart without an event; its redraw does.\n  const startObserver = () => {\n    const drawer = document.getElementById('cart-drawer');\n    if (drawer) new MutationObserver(schedule).observe(drawer, { childList: true, subtree: true });\n  };\n  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', startObserver);\n  else startObserver();\n})();\n"],
  "mappings": "CA0BC,IAAM,CACL,MAAM,MAAQ,OAAO,uBACf,WAAa,OAAO,mBAC1B,GAAI,CAAC,OAAS,CAAC,WAAY,OAG3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