import{Application,Controller}from"/effortless/stimulus.js";async function fetchSiteInformation(){const e=await fetch("https://www.worldspice.com/a/leitesculinaria");if(!e.ok){const t=`An error has occured: ${e.status}`;throw new Error(t)}return await e.json()}if(window.Stimulus=Application.start(),Stimulus.register("affiliate--effortless",class extends Controller{async initialize(){this.effortlessData=window.effortlessData}connect(){const e=this;if(e.hasProducts="products"in effortlessData&&effortlessData.products.length>0,e.hasKeywordCombos="keyword_combinations"in effortlessData&&effortlessData.keyword_combinations.length>0,e.hasProducts||e.hasKeywordCombos){let t=(e.effortlessData.css_selectors_allowed?e.effortlessData.css_selectors_allowed:"p, blockquote, li").split(",").map((e=>e.trim())),o=(e.effortlessData.css_selectors_ignored?e.effortlessData.css_selectors_ignored:"effortless-ignore, h1, h2, h3, h4, h5, h6, a, caption, nav, form, header, footer, aside, template, style, script, title").split(",").map((e=>e.trim())),s=t.filter((e=>!o.includes(e)));e.nodesToProcess=[],e.nodesWithKeywordMatches=[],e.keywordMatchesForNodeByIndex=[],e.termInserted=[];for(let t of e.element.querySelectorAll(s))null!=t.closest(s)&&null==t.closest(o)&&(t.querySelectorAll(o).forEach((function(e){e.classList.add("effortless-ignore")})),e.nodesToProcess.push(t));e.nodesToProcess.length>0&&e.processNodes()}}processNodes(){const e=this;for(let t of e.nodesToProcess){for(let o of e.effortlessData.products){let s=`https://www.shareasale.com/m-pr.cfm?merchantID=112426&userID=${e.effortlessData.shareasale_affiliate_id}&productID=${o.scid}`;if(o.product)for(let r of o.product)e.searchText(t,r,s,o.scid);if(o.keywords)for(let r of o.keywords)e.searchText(t,r,s,o.scid)}if(e.hasKeywordCombos)for(let o of JSON.parse(e.effortlessData.keyword_combinations))if(o.hasOwnProperty("keyword")&&o.hasOwnProperty("url")){let s=`https://shareasale.com/r.cfm?b=&u=${e.effortlessData.shareasale_affiliate_id}&m=112426&urllink=https://worldspice.com${o.url}&afftrack=`;e.searchText(t,o.keyword,s,o.keyword)}}e.insertIgnoreElements(),e.insertLinks()}searchText(e,t,o,s){const r=this;if(!r.termInserted.includes(s)){let a=new RegExp(" "+t+"(?![a-z])","ig");if(null!=e.textContent.match(a)){let a=r.nodesWithKeywordMatches.indexOf(e);-1==a&&(r.nodesWithKeywordMatches.push(e),a=r.nodesWithKeywordMatches.indexOf(e),r.keywordMatchesForNodeByIndex[a]=[],r.termInserted.push(s)),r.keywordMatchesForNodeByIndex[a].push([t,o])}}}insertIgnoreElements(){const e=this;if(e.effortlessData.disallowed_keywords_array)for(let t of e.effortlessData.disallowed_keywords_array){let o=t.trim();if(o.length>2)for(let t of e.nodesWithKeywordMatches){let s=e.findAllTextNodes(t),r=new RegExp("("+o+")","ig");for(let t of s)e.textNodeReplace(t,r,(function(e){return{name:"effortless-ignore",attrs:{class:"effortless-ignore",rel:"nofollow"},content:e}}))}}}insertLinks(){const e=this;let t=[];for(let[o,s]of e.nodesWithKeywordMatches.entries()){let r=e.findAllTextNodes(s);t[o]=[];for(let s of e.keywordMatchesForNodeByIndex[o]){let a=s[0],l=new RegExp(a+"(?![a-z])","ig"),n=s[1];if(!t[o].includes(a)){for(let t of r)"a"!=t.parentNode.tagName.toLowerCase()&&e.textNodeReplace(t,l,(function(e){return{name:"a",attrs:{href:n,target:"_blank",class:"effortless-affiliate-link",rel:"nofollow"},content:{name:"effortless-affiliate",content:e}}}));t[o].push(a)}}}}findAllTextNodes(e){for(var t=e.ownerDocument.createTreeWalker(e,NodeFilter.SHOW_TEXT),o=[];t.nextNode();){let e=t.currentNode.parentNode;"SCRIPT"!=e.tagName&&"A"!=e.tagName&&null===e.closest(".effortless-ignore")&&o.push(t.currentNode)}return o}textNodeReplace(e,t,o){var s,r=e.parentNode,a=e.nextSibling,l=e.ownerDocument;if(t.global){let r=0;for(;e&&(s=t.exec(e.nodeValue))&&0===r;)r++,t.lastIndex=0,e=handleResult(e,s,o.apply(this,s))}else(s=t.exec(e.nodeValue))&&handleResult(e,s,o.apply(this,s));function handleResult(e,t,o){var s=e.nodeValue;e.nodeValue=s.slice(0,t.index),[].concat(create(r,o)).forEach((function(e){r.insertBefore(e,a)}));var n=s.slice(t.index+t[0].length);return n&&r.insertBefore(l.createTextNode(n),a)}function create(e,t){if(t.map)return t.map((function(t){return create(e,t)}));if("object"==typeof t){var o=l.createElementNS(t.namespaceURI||e.namespaceURI,t.name);if(t.attrs)for(var s in t.attrs)o.setAttribute(s,t.attrs[s]);return t.content&&[].concat(create(o,t.content)).forEach(o.appendChild,o),o}return l.createTextNode(t+"")}}}),"function"==typeof Array.prototype.includes){fetchSiteInformation().then((e=>{var t=new MutationObserver((function(e){e.forEach((function(e){for(let t of e.addedNodes);}))}));let o=e.active;if(!o){let e=window.location.search;e.slice(e.indexOf("?")+1).split("&").map((e=>{const[t,s]=e.split("=");"effortless_devmode"==t&&"true"==s&&(o=!0)}))}if(o){if(e.inline_css){let t=document.createElement("style");t.type="text/css",t.styleSheet?t.styleSheet.cssText=e.inline_css:t.appendChild(document.createTextNode(e.inline_css)),document.getElementsByTagName("head")[0].appendChild(t)}e.disallowed_keywords&&(e.disallowed_keywords_array=e.disallowed_keywords.split(";"));const o=document.querySelector(e.css_container_for_observation);if(o){for(let t of o.querySelectorAll(e.css_selectors_post)){let e="affiliate--effortless";if(t.hasAttribute("data-controller")){e+=` ${t.dataset.controller}`}t.setAttribute("data-controller",e)}window.effortlessData=e,t.observe(o,{childList:!0,subtree:!0})}}}))}