(function(wc_order_attribution){
'use strict';
const params=wc_order_attribution.params;
const $=document.querySelector.bind(document);
const propertyAccessor=(obj, path)=> path.split('.').reduce(( acc, part)=> acc&&acc[ part ], obj);
const returnNull=()=> null;
const stringifyFalsyInputValue=(value)=> value===null||value===undefined ? '':value;
const CHECKOUT_STORE_KEY='wc/store/checkout';
wc_order_attribution.getAttributionData=function(){
const accessor=params.allowTracking&&isSbjsAvailable() ? propertyAccessor:returnNull;
const getter=isSbjsAvailable() ? sbjs.get:{};
const entries=Object.entries(wc_order_attribution.fields)
.map(( [ key, property ])=> [ key, accessor(getter, property) ]);
return Object.fromEntries(entries);
}
function removeDuplicateInputGroups(){
document.querySelectorAll('wc-order-attribution-inputs').forEach(( group, index)=> {
if(index > 0){
group.remove();
}});
}
function updateFormValues(values){
removeDuplicateInputGroups();
for(const element of document.querySelectorAll('wc-order-attribution-inputs') ){
element.values=values;
}};
function updateCheckoutBlockData(values){
if(window.wp &&
window.wp.data &&
window.wp.data.dispatch &&
window.wc &&
window.wc.wcBlocksData
){
window.wp.data
.dispatch(window.wc.wcBlocksData.CHECKOUT_STORE_KEY)
.setExtensionData('woocommerce/order-attribution',
values,
true
);
}}
function isSbjsAvailable(){
return typeof sbjs!=='undefined';
}
wc_order_attribution.setOrderTracking=function(allow){
params.allowTracking=allow;
if(! allow){
removeTrackingCookies();
}else if(! isSbjsAvailable()){
return;
}else{
sbjs.init({
lifetime: Number(params.lifetime),
session_length: Number(params.session),
base64: Boolean(params.base64),
timezone_offset: '0',
});
}
const values=wc_order_attribution.getAttributionData();
updateFormValues(values);
updateCheckoutBlockData(values);
}
function removeTrackingCookies(){
const domain=window.location.hostname;
const sbCookies=[
'sbjs_current',
'sbjs_current_add',
'sbjs_first',
'sbjs_first_add',
'sbjs_session',
'sbjs_udata',
'sbjs_migrations',
'sbjs_promo'
];
sbCookies.forEach(( name)=> {
document.cookie=`${name}=; path=/; max-age=-999; domain=.${domain};`;
});
}
wc_order_attribution.setOrderTracking(params.allowTracking);
function eventuallyInitializeCheckoutBlock(){
if(window.wp&&window.wp.data&&typeof window.wp.data.subscribe==='function'
){
const unsubscribe=window.wp.data.subscribe(function (){
unsubscribe();
updateCheckoutBlockData(wc_order_attribution.getAttributionData());
}, CHECKOUT_STORE_KEY);
}};
if(document.readyState==="loading"){
document.addEventListener("DOMContentLoaded", eventuallyInitializeCheckoutBlock);
}else{
eventuallyInitializeCheckoutBlock();
}
window.customElements.define('wc-order-attribution-inputs', class extends HTMLElement {
constructor(){
super();
this._fieldNames=Object.keys(wc_order_attribution.fields);
if(this.hasOwnProperty('_values') ){
let values=this.values;
delete this.values;
this.values=values||{};}}
connectedCallback(){
this.innerHTML='';
const inputs=new DocumentFragment();
for(const fieldName of this._fieldNames){
const input=document.createElement('input');
input.type='hidden';
input.name=`${params.prefix}${fieldName}`;
input.value=stringifyFalsyInputValue(( this.values&&this.values[ fieldName ])||'');
inputs.appendChild(input);
}
this.appendChild(inputs);
}
set values(values){
this._values=values;
if(this.isConnected){
for(const fieldName of this._fieldNames){
const input=this.querySelector(`input[name="${params.prefix}${fieldName}"]`);
if(input){
input.value=stringifyFalsyInputValue(this.values[ fieldName ]);
}else{
console.warn(`Field "${fieldName}" not found. ` +
`Most likely, the '<wc-order-attribution-inputs>' element was manipulated.`
);
}}
}}
get values(){
return this._values;
}});
}(window.wc_order_attribution) );