1import { store, getContext } from '@wordpress/interactivity'; 2import { syncRadioState, computeAccordionLabel, resolveFulfillmentOptionFallback } from '../helpers.js?v=015'; 3 4const { state, actions } = store('dbi/lor', { 5 callbacks: { 6 initProduct() { 7 const context = getContext(); 8 const productId = 9 state.currentVariants[context.productId] || context.productId; 10 const selection = state.productSelections[productId]; 11 12 if (!state.selectedDefaultLocationId) { 13 actions.initUserClosestLocation(); 14 } 15 16 const productData = 17 window['productSalePointsInfo_' + context.parentProductId] || 18 window['productSalePointsInfo_' + context.productId]; 19 20 context.fulfillmentOption = selection?.fulfillmentOption || null; 21 context.locationId = selection?.locationId || null; 22 context.isShipToHomeDisabled = false; 23 context.showFflNotSelected = true; 24 context.fflName = '--'; 25 context.fflCity = '--'; 26 27 if (context.isRestricted || context.isRegulated) { 28 context.isShipToHomeDisabled = true; 29 30 if ( 31 !context.fulfillmentOption || 32 context.fulfillmentOption === 'shipToHome' 33 ) { 34 context.fulfillmentOption = 'shipToFfl'; 35 if (!state.productSelections[productId]) { 36 state.productSelections[productId] = { 37 productId, 38 }; 39 } 40 state.productSelections[productId].fulfillmentOption = 41 'shipToFfl'; 42 } 43 } 44 45 if (productData) { 46 const salePointsData = productData.hasVariations 47 ? productData.variationsSalePoints?.[productId] 48 : productData.productSalePoints; 49 50 if (salePointsData && salePointsData.length) { 51 const savedLocationId = selection?.locationId 52 ? parseInt(selection.locationId, 10) 53 : state.selectedDefaultLocationId; 54 55 const locationHasStock = salePointsData.some( 56 sp => 57 sp.id === savedLocationId && 58 sp.stock > 0 && 59 sp.enabledForLocalPickUp === true, 60 ); 61 62 let resolvedSalePoint; 63 64 if (locationHasStock) { 65 resolvedSalePoint = state.salePoints.find( 66 sp => sp.id === savedLocationId, 67 ); 68 } else { 69 const nextAvailable = salePointsData.find( 70 sp => 71 sp.stock > 0 && 72 sp.enabledForLocalPickUp === true, 73 ); 74 if (nextAvailable) { 75 resolvedSalePoint = state.salePoints.find( 76 sp => sp.id === nextAvailable.id, 77 ); 78 } 79 } 80 81 if (resolvedSalePoint) { 82 context.pickupLocationName = 83 resolvedSalePoint.name || '--'; 84 context.pickupDateMessage = 85 resolvedSalePoint.isOpen 86 ? 'Pick up Today!' 87 : `Pick up by ${resolvedSalePoint.nextOpenDay}`; 88 context.locationId = resolvedSalePoint.id; 89 90 if (!state.productSelections[productId]) { 91 state.productSelections[productId] = { 92 productId, 93 }; 94 } 95 state.productSelections[productId].locationId = 96 resolvedSalePoint.id; 97 } 98 99 const localPickupAvailable = salePointsData.some( 100 sp => 101 sp.stock > 0 && 102 sp.enabledForLocalPickUp === true, 103 );
104 context.isLocalPickupDisabled = !localPickupAvailable; 105 if (!localPickupAvailable) { 106 context.pickupDateMessage = 107 'No Local Pickup Available'; 108 context.pickupLocationName = '--'; 109 } 110 111 const locationsWithStock = salePointsData.filter( 112 sp => 113 sp.stock > 0 && 114 sp.enabledForLocalPickUp === true, 115 ); 116 context.showChangeStore = locationsWithStock.length >= 2; 117 } else { 118 context.isLocalPickupDisabled = true; 119 context.showChangeStore = false; 120 context.pickupDateMessage = 'No Local Pickup Available'; 121 } 122 } 123 124 if (context.fulfillmentOption === 'localPickup' && context.isLocalPickupDisabled) { 125 context.fulfillmentOption = null; 126 } 127 128 if (context.pickupLocationName === '--' && context.locationId && !context.isLocalPickupDisabled) { 129 const sp = state.salePoints.find(s => s.id === context.locationId); 130 if (sp) { 131 context.pickupLocationName = sp.name || '--'; 132 context.pickupDateMessage = sp.isOpen 133 ? 'Pick up Today!' 134 : `Pick up by ${sp.nextOpenDay}`; 135 } 136 } 137 138 if (state.selectedFFL) { 139 let ffl = state.selectedFFL; 140 if (typeof ffl === 'string') { 141 try { 142 ffl = JSON.parse(ffl); 143 } catch (_) { 144 ffl = null; 145 } 146 } 147 if (ffl && ffl.licenseName) { 148 context.showFflNotSelected = false; 149 context.fflName = ffl.licenseName; 150 context.fflCity = `${ffl.premiseCity} - ${ffl.premiseState}`; 151 } 152 } 153 154 if (!context.fulfillmentOption) { 155 context.fulfillmentOption = resolveFulfillmentOptionFallback(context); 156 if (!state.productSelections[productId]) { 157 state.productSelections[productId] = { productId }; 158 } 159 state.productSelections[productId].fulfillmentOption = 160 context.fulfillmentOption; 161 } 162 163 syncRadioState(context); 164 context.accordionLabel = computeAccordionLabel(context); 165 166 const isVariableWithoutVariant = productData?.hasVariations && 167 context.productId === context.parentProductId && 168 !state.currentVariants[context.productId]; 169 170 const el = document.getElementById( 171 `salepoint-fulfillment-options-container-${context.productId}`, 172 ); 173 if (el) { 174 el.dataset.lorHydrated = '1'; 175 176 if (!isVariableWithoutVariant) { 177 if (el.parentElement?.tagName === 'TD') { 178 el.style.display = 'flex'; 179 } else { 180 el.style.display = ''; 181 } 182 } 183 } 184 185 if (el && el.parentElement?.tagName === 'TD' && !el.parentElement.classList.contains('sp-container-td')) { 186 const tableRow = el.closest('tr'); 187 if (tableRow) { 188 const colCount = tableRow.querySelectorAll('td').length; 189 const newRow = document.createElement('tr'); 190 const newCell = document.createElement('td'); 191 newCell.className = 'sp-container-td'; 192 newCell.setAttribute('colspan', String(colCount)); 193 194 el.parentElement.removeChild(el); 195 newCell.appendChild(el); 196 newRow.appendChild(newCell); 197 tableRow.after(newRow); 198 } 199 } 200 }, 201 }, 202});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.