{
  "version": 3,
  "sources": ["/cdn/shop/t/280/assets/custom-product-bundle.js?h=d8a9a"],
  "sourcesContent": ["\"use strict\";(self.webpackChunk_softlimit_framework=self.webpackChunk_softlimit_framework||[]).push([[\"custom-product-bundle\"],{\"../../../src/_features/ka-dynamic-bundle/scripts/product-bundle.js\":\n/*!**************************************************************************!*\\\n  !*** ../../../src/_features/ka-dynamic-bundle/scripts/product-bundle.js ***!\n  \\**************************************************************************/(__unused_webpack_module,__webpack_exports__,__webpack_require__)=>{eval(\"__webpack_require__.r(__webpack_exports__);\\n/* harmony import */ var Features_scripts_cart__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! Features/scripts/cart */ \\\"./src/_features/scripts/cart.js\\\");\\n/* harmony import */ var Features_scripts_currency__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! Features/scripts/currency */ \\\"./src/_features/scripts/currency.js\\\");\\n/**\\n * @name ProductBundle\\n * @description Handles adding a bundle of products to the cart\\n */\\n\\n\\n\\n\\nif (!customElements.get('custom-product-bundle')) {\\n  customElements.define('custom-product-bundle',\\n    class ProductBundle extends HTMLElement {\\n      connectedCallback() {\\n        this.form = this.querySelector('form[data-bundle-submit]')\\n        this.bundleForms = this.querySelectorAll('custom-product-bundle-item form:not([disabled=\\\"true\\\"])')\\n        this.addBtn = this.querySelector('[data-bundle-add-to-cart]')\\n        this.addBtnPrice = this.addBtn?.querySelector('[data-bundle-atc-price]')\\n        if (!this.form) return\\n        this.handleAdd = this.add.bind(this)\\n        this.form.addEventListener('submit', this.handleAdd)\\n\\n        // get the bundle discount attributes and save them here, then remove from element\\n        this.discount = {\\n          type: this.getAttribute('data-bundle-discount-type'),\\n          baseDiscount: this.getAttribute('data-bundle-discount'),\\n          upgradedDiscount: this.getAttribute('data-bundle-upgraded-discount'),\\n          upgradedDiscountType: this.getAttribute('data-bundle-upgraded-discount-type')\\n        }\\n        this.removeAttribute('data-bundle-discount-type')\\n        this.removeAttribute('data-bundle-discount')\\n        this.removeAttribute('data-bundle-upgraded-discount')\\n        this.removeAttribute('data-bundle-upgraded-discount-type')\\n\\n        // listen for fully selected events to update the add to cart button\\n        this.addEventListener('bundle-group:selection', this.updateAddToCart.bind(this))\\n      }\\n\\n      updateAddToCart(e) {\\n        // if all of our bundle group elements are fully selected, enable the add to cart button\\n        const fullySelected = Array.from(this.querySelectorAll('custom-product-bundle-group'))?.filter(group => group.getAttribute('type') !== 'Auto')?.every(group => group.hasAttribute('fully-selected'))\\n        if (fullySelected) {\\n          this.addBtn?.removeAttribute('disabled')\\n          // update the add to cart button price and show it\\n          this.updatePrice(e)\\n        } else {\\n          this.addBtn?.setAttribute('disabled', '')\\n          // hide the add to cart button price\\n          this.addBtnPrice.innerHTML = ''\\n        }\\n      }\\n\\n      updatePrice(e) {\\n        // get the bundle total price from the event detail\\n        const newPrice = e?.detail?.price\\n        if (!newPrice) return\\n        const bundleTotalPrice = Number(newPrice)\\n        // update the price attribute\\n        this.setAttribute('price', bundleTotalPrice)\\n        const formattedTotalPrice = (0,Features_scripts_currency__WEBPACK_IMPORTED_MODULE_1__.formatMoney)(bundleTotalPrice)\\n        let bundleDiscount = 0\\n\\n        if (!this.discount.baseDiscount && !this.discount.upgradedDiscount) {\\n          // update the add to cart button price and return\\n          this.addBtnPrice.innerHTML = formattedTotalPrice\\n          return\\n        }\\n\\n        // check if we have an upgrade selected (data-bundle-item checked input with data-upgrade-entry attribute)\\n        const upgradeSelected = Array.from(this.querySelectorAll('[data-bundle-item]:checked[data-upgrade-entry]'))?.length > 0\\n\\n        const discount = upgradeSelected ? this.discount.upgradedDiscount : this.discount.baseDiscount\\n        const discountType = upgradeSelected ? this.discount.upgradedDiscountType : this.discount.type\\n\\n        if (discountType === 'Amount') {\\n          // convert the discount amount to a percentage\\n          const percentDiscount = Number(discount * 100) / bundleTotalPrice\\n          if (percentDiscount < 1) bundleDiscount = percentDiscount\\n        } else {\\n          bundleDiscount = discount / 100\\n        }\\n        const discountedPrice = bundleTotalPrice - bundleTotalPrice * bundleDiscount\\n        const formattedDiscountedPrice = (0,Features_scripts_currency__WEBPACK_IMPORTED_MODULE_1__.formatMoney)(discountedPrice)\\n        this.addBtnPrice.innerHTML = `\\n          - <span class=\\\"line-through\\\">${formattedTotalPrice}</span> ${formattedDiscountedPrice}\\n        `\\n      }\\n\\n      // add to cart with bundle parent item from pdp\\n      add(e) {\\n      // prevent form submission\\n        e.preventDefault()\\n\\n        // get the bundle reference property, add date string to make unique\\n        const bundleRef = this.querySelector('[data-bundle-reference]').value\\n        const bundleProperty = `bundle-${bundleRef}-${Date.now()}`\\n        const bundleTitle = this.querySelector('[data-bundle-title]')?.value\\n        const bundleImg = this.querySelector('[data-bundle-image]')?.value\\n        const bundleItems = Array.from(this.querySelectorAll('[data-bundle-item]:checked')).map(item => {\\n          const qty = 1\\n          return {\\n            id: item.value,\\n            qty\\n          }\\n        })\\n\\n        const properties = {}\\n        if (bundleRef) properties['_bundle-reference'] = bundleProperty\\n        if (bundleTitle) properties['_bundle-title'] = bundleTitle\\n        if (bundleImg) properties['_bundle-image'] = bundleImg\\n\\n        // check if we have an upgrade selected (data-bundle-item checked input with data-upgrade-entry attribute)\\n        const upgradeSelected = Array.from(this.querySelectorAll('[data-bundle-item]:checked[data-upgrade-entry]'))?.length > 0\\n\\n        const discount = upgradeSelected ? this.discount.upgradedDiscount : this.discount.baseDiscount\\n        const discountType = upgradeSelected ? this.discount.upgradedDiscountType : this.discount.type\\n\\n        // add discount properties\\n        if (discountType === 'Amount' && discount) {\\n          // convert the amount to a percentage\\n          const bundlePrice = Number(this.getAttribute('price')) / 100\\n          const percentDiscount = (Number(discount) / bundlePrice) * 100\\n          if (percentDiscount < 100) { properties['_bundle-discount'] = percentDiscount }\\n        } else {\\n          if (discount) { properties['_bundle-discount'] = discount }\\n        }\\n\\n        // map items to be added together with parent\\n        const formItems = bundleItems.map(item => {\\n          return {\\n            id: item.id,\\n            properties,\\n            quantity: item.qty,\\n            form_type: 'product'\\n          }\\n        })\\n\\n        // add to cart\\n        Features_scripts_cart__WEBPACK_IMPORTED_MODULE_0__[\\\"default\\\"].add({ form: formItems })\\n      }\\n    }\\n  )\\n}\\n\\n\\n//# sourceURL=webpack://@softlimit/framework/../../../src/_features/ka-dynamic-bundle/scripts/product-bundle.js?\")}}]);"],
  "mappings": "cAAc,KAAK,iCAAiC,KAAK,kCAAkC,CAAC,GAAG,KAAK,CAAC,CAAC,uBAAuB,EAAE,CAAC,qEAGlD,SAAC,wBAAwB,oBAAoB,oBAAsB,CAAC,KAAK,i1NAAi1N,CAAC,CAAC,CAAC,CAAC",
  "names": []
}
