(function (){
'use strict';
const { __ }=window.wp?.i18n||{};
const { subscribe, select }=window.wp?.data||{};
const updateNamespace=bos4wBlocksCartPlans?.updateNamespace||'bos4w-cart-plans';
const sanitizeKeyForClass=(key)=>
String(key||'')
.toLowerCase()
.replace(/[^a-z0-9_-]+/g, '-')
.slice(0, 80);
const hasPlans=(cartItem)=>
!! cartItem?.extensions?.bos4w?.plans?.length;
const getCartState=()=> {
const cartStore=window.wc?.wcBlocksData?.cartStore;
if(cartStore){
const cart=window.wp?.data?.select(cartStore )?.getCartData?.();
if(cart&&Array.isArray(cart.items) ){
return { items: cart.items };}}
const legacy=window.wp?.data?.select?.('wc/store/cart');
if(legacy?.getCartItems){
return { items: legacy.getCartItems()||[] };}
return { items: [] };};
const getCartRows=()=> {
const selectors=[
'.wc-block-cart-items__row',
'.wc-block-cart-items__item',
'.wc-block-cart-item',
];
for(const sel of selectors){
const nodes=document.querySelectorAll(sel);
if(nodes&&nodes.length) return Array.from(nodes);
}
return [];
};
const getRowCartItemKey=(row)=> {
if(! row) return '';
const direct =
row.getAttribute?.('data-cart-item-key') ||
row.getAttribute?.('data-wc-cart-item-key') ||
row.getAttribute?.('data-wc-cart-item') ||
'';
if(direct) return String(direct);
const removeEl =
row.querySelector?.('a[href*="remove_item="]') ||
row.querySelector?.('a[href*="remove-item="]') ||
row.querySelector?.('button[name="remove_item"]') ||
null;
const href=removeEl?.getAttribute?.('href')||'';
if(href){
try {
const url=new URL(href, window.location.href);
const key =
url.searchParams.get('remove_item') ||
url.searchParams.get('remove-item') ||
'';
if(key) return key;
} catch(e){
}
const m=href.match(/(?:remove_item|remove-item)=([^&]+)/);
if(m?.[ 1 ]) return decodeURIComponent(m[ 1 ]);
}
const any=row.querySelector?.('[data-cart-item-key],[data-wc-cart-item-key]');
if(any){
return (
any.getAttribute('data-cart-item-key') ||
any.getAttribute('data-wc-cart-item-key') ||
''
);
}
return '';
};
const getExtensionCartUpdate=()=> window.wc?.blocksCheckout?.extensionCartUpdate;
const getProcessErrorResponse=()=> window.wc?.wcBlocksData?.processErrorResponse;
const decodeHtml=(input)=> {
const html=String(input ?? '');
if(! html) return '';
const el=document.createElement('textarea');
el.innerHTML=html;
return el.value;
};
const buildRadios=(cartItem)=> {
const ext=cartItem.extensions?.bos4w;
if(! ext?.plans?.length) return null;
const wrapper=document.createElement('div');
wrapper.className='bos4w-blocks-cart-plans';
const title=document.createElement('div');
title.className='bos4w-blocks-cart-plans__title';
title.textContent=__('Choose a plan', 'bos4w')||'Choose a plan';
wrapper.appendChild(title);
const selected=ext.selected||null;
const name=`bos4w_blocks_plan_${ sanitizeKeyForClass(cartItem.key) }`;
ext.plans.forEach(( plan)=> {
const label=document.createElement('label');
label.className='bos4w-blocks-cart-plans__option';
const input=document.createElement('input');
input.type='radio';
input.name=name;
input.value=plan.value;
input.checked=selected ? plan.value===selected:! plan.is_subscription;
input.addEventListener('change', ()=> {
const extensionCartUpdate=getExtensionCartUpdate();
if(typeof extensionCartUpdate!=='function') return;
input.disabled=true;
wrapper.setAttribute('aria-busy', 'true');
extensionCartUpdate( {
namespace: updateNamespace,
data: {
cart_item_key: cartItem.key,
selected_plan: plan.value,
},
}).catch(( err)=> {
const processErrorResponse=getProcessErrorResponse();
if(typeof processErrorResponse==='function'){
processErrorResponse(err);
}
console.warn('BOS4W cart plan update failed', err);
}).finally(()=> {
input.disabled=false;
wrapper.removeAttribute('aria-busy');
queueInject();
});
});
const text=document.createElement('span');
text.className='bos4w-blocks-cart-plans__label';
text.textContent=decodeHtml(plan.label);
label.appendChild(input);
label.appendChild(text);
wrapper.appendChild(label);
});
return wrapper;
};
const inject=()=> {
const { items }=getCartState();
if(! items.length) return;
const rows=getCartRows();
if(! rows.length) return;
const rowsByKey=new Map();
rows.forEach(( row)=> {
const key=getRowCartItemKey(row);
if(key) rowsByKey.set(key, row);
});
items.forEach(( item, index)=> {
if(! hasPlans(item) ) return;
const row=rowsByKey.get(item.key)||rows[ index ];
if(! row) return;
const priceContainer =
row.querySelector('.wc-block-cart-item__prices') ||
row.querySelector('.wc-block-components-product-price' )?.parentElement ||
row;
if(! priceContainer) return;
if(priceContainer.querySelector('.bos4w-blocks-cart-plans') ) return;
const radios=buildRadios(item);
if(! radios) return;
priceContainer.appendChild(radios);
});
};
let injectQueued=false;
const queueInject=()=> {
if(injectQueued) return;
injectQueued=true;
setTimeout(()=> {
injectQueued=false;
inject();
}, 50);
};
try {
const mo=new MutationObserver(queueInject);
mo.observe(document.body, { childList: true, subtree: true });
} catch(e){
}
let lastSignature='';
subscribe?.(()=> {
const { items }=getCartState();
const sig=items
.map(( it)=> `${ it.key||'' }:${ it.extensions?.bos4w?.selected||'' }`)
.join('|');
if(sig!==lastSignature){
lastSignature=sig;
queueInject();
}});
if(document.readyState==='loading'){
document.addEventListener('DOMContentLoaded', inject);
}else{
inject();
}})();