{
  "version": 3,
  "sources": ["/cdn/shop/t/95/assets/cart-protection-after-product.js?cb=20261119"],
  "sourcesContent": ["/**\n * Package Protection after the product (pizza dough pages).\n *\n * The cart drawer (assets/cart-drawer.min.js, compiled, not edited) puts\n * Package Protection in with the first product added to an empty cart, and it\n * does it BEFORE the product: protection POST -> product POST -> GET /cart.js.\n * The drawer stays blank and dimmed until all three finish, so the protection\n * write (about half a second, more on a slow phone) delays the product.\n *\n * This wrapper keeps the drawer's own flow and rules and only moves that one\n * write to the end: product POST -> GET /cart.js (the product shows) ->\n * protection POST -> drawer refresh (the toggle shows ON).\n *\n *   1. It runs the drawer's handler. If the drawer is about to add protection\n *      (its rule: empty cart, protection not in it), that request is held and\n *      the drawer gets a stand-in reply, which it never reads, so it moves on to\n *      the product at once. When the drawer adds no protection (cart not empty,\n *      or protection already there) nothing is held and the watch is dropped.\n *      The hold only happens while the drawer's handler runs synchronously\n *      (today its first request leaves before any await); if a rebuilt drawer\n *      ever sends it later, nothing is held and the page behaves as before.\n *   2. Once the product is in the cart and the drawer has read the cart, the\n *      held request is sent exactly as the drawer built it. A failed product\n *      add sends nothing.\n *   3. `update-cart` makes the drawer read the cart again, so protection shows\n *      as ON. Later adds follow the drawer's rule untouched, so a shopper who\n *      switched protection off keeps it off.\n *\n * Trade-offs of showing the product first:\n *   - Until step 3 the protection toggle reads OFF (about 0.6 s on a fast\n *     connection, 1 to 2 s on a slow phone still loading the page).\n *   - What the shopper does in that gap is not seen by the held request: a tap\n *     on the toggle can leave protection at quantity 2, removing the product\n *     can leave protection alone, and Secure Checkout can go without it.\n *   - One watched add at a time: a newer tap takes over, and only the newer\n *     tap's protection is sent (none if its product add fails).\n *   - `update-cart` also opens the drawer, so it reopens if it was closed.\n *\n * Needs the drawer's handler to exist when this loads; without it the page\n * keeps working as before.\n */\n(() => {\n  const drawerAddToCart = window.addToCartHandler;\n  if (typeof drawerAddToCart !== 'function' || drawerAddToCart.movesProtectionLast) return;\n\n  // Same variant as the drawer's protection line (`je` in cart-drawer.min.js).\n  const PACKAGE_PROTECTION_VARIANT_ID = 45077724594353;\n  const CART_ADD_PATH = '/cart/add.js';\n  const CART_READ_PATH = '/cart.js';\n\n  // Where a watched add is, in the order the drawer sends its requests.\n  const STAGE = Object.freeze({\n    START: 'start', // nothing seen yet\n    HELD: 'held', // protection held, waiting for the product add\n    ADDING: 'adding', // product add in flight\n    READING: 'reading', // product in the cart, waiting for the drawer's cart read\n    DONE: 'done'\n  });\n\n  const pathOf = (input) => {\n    try {\n      const href = input instanceof URL ? input.href : typeof input === 'string' ? input : input?.url;\n      return new URL(href, window.location.origin).pathname;\n    } catch (error) {\n      return '';\n    }\n  };\n\n  const methodOf = (input, init) => String(init?.method || (typeof input === 'object' && input?.method) || 'GET').toUpperCase();\n\n  const isCartAdd = (input, init) => pathOf(input) === CART_ADD_PATH && methodOf(input, init) === 'POST';\n  const isCartRead = (input, init) => pathOf(input) === CART_READ_PATH && methodOf(input, init) === 'GET';\n\n  // The drawer sends protection as JSON; the product goes out as form data.\n  const isProtectionAdd = (init) => {\n    if (typeof init?.body !== 'string') return false;\n    try {\n      const lines = JSON.parse(init.body).items;\n      return Array.isArray(lines) && lines.some((line) => line.id === PACKAGE_PROTECTION_VARIANT_ID);\n    } catch (error) {\n      return false;\n    }\n  };\n\n  const hasProtection = (cart) => cart.items.some((item) => item.variant_id === PACKAGE_PROTECTION_VARIANT_ID);\n  const hasProduct = (cart) => cart.items.some((item) => item.variant_id !== PACKAGE_PROTECTION_VARIANT_ID);\n\n  const standInReply = () => new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } });\n\n  const deferred = () => {\n    let resolve;\n    const promise = new Promise((done) => { resolve = done; });\n    return { promise, resolve };\n  };\n\n  /**\n   * Follows the requests of one drawer add and gives back, as promises, the two\n   * moments to wait for. `send` is the page's own fetch, so the cart store still\n   * sees every write.\n   */\n  const watchDrawerAdd = (send) => {\n    const productAdd = deferred(); // Reply to the product POST, or null if it failed to leave.\n    const cartRead = deferred(); // Reply to the drawer's GET /cart.js, a copy safe to read.\n    let heldRequest = null;\n    let stage = STAGE.START;\n\n    const finish = () => {\n      stage = STAGE.DONE;\n      return null;\n    };\n\n    const holdProtection = (input, init) => {\n      heldRequest = { input, init };\n      stage = STAGE.HELD;\n      return Promise.resolve(standInReply());\n    };\n\n    const followProductAdd = (input, init) => {\n      stage = STAGE.ADDING;\n      const reply = send(input, init);\n      reply.then((response) => {\n        stage = response.ok ? STAGE.READING : STAGE.DONE;\n        productAdd.resolve(response);\n      }, () => {\n        stage = STAGE.DONE;\n        productAdd.resolve(null);\n      });\n      return reply;\n    };\n\n    const followCartRead = (input, init) => {\n      stage = STAGE.DONE;\n      const reply = send(input, init);\n      // Copied before the drawer reads the body.\n      reply.then((response) => cartRead.resolve(response.clone()), () => cartRead.resolve(null));\n      return reply;\n    };\n\n    const offerFirstAdd = (input, init) => {\n      if (!isCartAdd(input, init)) return null;\n      return isProtectionAdd(init) ? holdProtection(input, init) : finish(); // Else: the drawer adds none this time.\n    };\n\n    // A reply when this watch takes the request, null to let it go its way.\n    const offer = (input, init) => {\n      if (stage === STAGE.START) return offerFirstAdd(input, init);\n      if (stage === STAGE.HELD) return isCartAdd(input, init) ? followProductAdd(input, init) : null;\n      if (stage === STAGE.READING) return isCartRead(input, init) ? followCartRead(input, init) : null;\n      return null;\n    };\n\n    return {\n      offer,\n      productAdded: productAdd.promise,\n      cartRead: cartRead.promise,\n      heldRequest: () => heldRequest\n    };\n  };\n\n  const readCart = async (response) => {\n    try {\n      const cart = await response.json();\n      return Array.isArray(cart?.items) ? cart : null;\n    } catch (error) {\n      return null;\n    }\n  };\n\n  /** The product shows first; then protection, only if the drawer's rule still holds. */\n  const addProtectionLast = async (watch, send) => {\n    const productReply = await watch.productAdded;\n    if (!productReply?.ok) return;\n\n    const cartReply = await watch.cartRead;\n    const cart = cartReply?.ok ? await readCart(cartReply) : null;\n    if (!cart || !hasProduct(cart) || hasProtection(cart)) return;\n\n    const { input, init } = watch.heldRequest();\n    try {\n      const reply = await send(input, init);\n      if (reply.ok) document.dispatchEvent(new CustomEvent('update-cart'));\n    } catch (error) {\n      console.warn('[cart-protection] package protection add failed:', error);\n    }\n  };\n\n  /* One page-level hook, inert unless a drawer add is being watched. */\n  const pageFetch = window.fetch.bind(window);\n  let activeWatch = null;\n\n  window.fetch = (input, init) => activeWatch?.offer(input, init) ?? pageFetch(input, init);\n\n  window.addToCartHandler = function addToCartHandler(form) {\n    const watch = watchDrawerAdd(pageFetch);\n    activeWatch = watch;\n    const result = drawerAddToCart(form); // Its first request leaves before this returns.\n    if (watch.heldRequest()) addProtectionLast(watch, pageFetch);\n    else activeWatch = null; // Nothing held: never touch a later request (the shopper's own toggle, for one).\n    return result;\n  };\n  window.addToCartHandler.movesProtectionLast = true;\n})();\n"],
  "mappings": "CAyCC,IAAM,CACL,MAAM,gBAAkB,OAAO,iBAC/B,GAAI,OAAO,iBAAoB,YAAc,gBAAgB,oBAAqB,OAGlF,MAAM,8BAAgC,eAChC,cAAgB,eAChB,eAAiB,WAGjB,MAAQ,OAAO,OAAO,CAC1B,MAAO,QACP,KAAM,OACN,OAAQ,SACR,QAAS,UACT,KAAM,MACR,CAAC,EAEK,OAAU,OAAU,CACxB,GAAI,CACF,MAAM,KAAO,iBAAiB,IAAM,MAAM,KAAO,OAAO,OAAU,SAAW,MAAQ,OAAO,IAC5F,OAAO,IAAI,IAAI,KAAM,OAAO,SAAS,MAAM,EAAE,QAC/C,MAAgB,CACd,MAAO,EACT,CACF,EAEM,SAAW,CAAC,MAAO,OAAS,OAAO,MAAM,QAAW,OAAO,OAAU,UAAY,OAAO,QAAW,KAAK,EAAE,YAAY,EAEtH,UAAY,CAAC,MAAO,OAAS,OAAO,KAAK,IAAM,eAAiB,SAAS,MAAO,IAAI,IAAM,OAC1F,WAAa,CAAC,MAAO,OAAS,OAAO,KAAK,IAAM,gBAAkB,SAAS,MAAO,IAAI,IAAM,MAG5F,gBAAmB,MAAS,CAChC,GAAI,OA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