/**
 * =====================================================
 * Layer rules for the current page.
 * =====================================================
 */

window.OLAYER_RULES = [];

/**
 * =====================================================
 * Layer script bundle.
 * =====================================================
 */

(function(){function e(e){document.querySelectorAll(`script[type="application/ld+json"]`).forEach(e=>{let t=e.textContent;(e.getAttribute(`data-schema`)===`article`||t&&t.includes(`"@type":"Article"`))&&e.remove()});let t=document.createElement(`script`);t.setAttribute(`type`,`application/ld+json`),t.setAttribute(`data-schema`,`article`),t.textContent=e.schemaJson,document.head.appendChild(t),console.debug(`Added Article schema`,{schema:JSON.parse(e.schemaJson)})}function t(e){let t=document.querySelector(`link[rel="${e.rel}"]`);if(t)t.setAttribute(`href`,e.value),console.debug(`Updated ${e.rel} link to: ${e.value}`,{element:t});else{let t=document.createElement(`link`);t.setAttribute(`rel`,e.rel),t.setAttribute(`href`,e.value),document.head.insertAdjacentElement(`afterbegin`,t),console.debug(`Created new ${e.rel} link with value: ${e.value}`,{element:t})}}function n(e){document.querySelectorAll(`script[type="application/ld+json"]`).forEach(e=>{let t=e.textContent;(e.getAttribute(`data-schema`)===`faq`||t&&t.includes(`"@type":"FAQPage"`))&&e.remove()});let t=document.createElement(`script`);t.setAttribute(`type`,`application/ld+json`),t.setAttribute(`data-schema`,`faq`),t.textContent=e.schemaJson,document.head.appendChild(t),console.debug(`Added FAQ Page schema`,{schema:JSON.parse(e.schemaJson)})}function r(e){let t=document.querySelector(`img[src="${e.url}"]`);t instanceof HTMLImageElement&&(t.setAttribute(e.attribute,e.value),console.debug(`Applied alt text to image: ${e.value}`,{element:t}))}function i(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}var a=new Set([`A`,`B`,`BLOCKQUOTE`,`BUTTON`,`CODE`,`H1`,`H2`,`H3`,`H4`,`H5`,`H6`,`I`,`INPUT`,`NAV`,`OPTION`,`PRE`,`SCRIPT`,`SELECT`,`STRONG`,`STYLE`,`TABLE`,`TEXTAREA`]);function o(e,t){let n=e.parentElement;for(;n&&n!==t;){if(a.has(n.tagName))return!0;n=n.parentElement}return!1}function s(e){let t=document.body,n=RegExp(`\\b${i(e.text)}\\b`,`gi`),r=[],a=document.createTreeWalker(t,NodeFilter.SHOW_TEXT,null),s;for(;s=a.nextNode();)s instanceof Text&&!o(s,t)&&r.push(s);r.forEach(t=>{let r=t.textContent;if(!r||!n.test(r))return;n.lastIndex=0;let i=document.createDocumentFragment(),a=0,o;for(;(o=n.exec(r))!==null;){let t=o.index;t>a&&i.appendChild(document.createTextNode(r.slice(a,t)));let n=document.createElement(e.tag);e.attributes&&Object.entries(e.attributes).forEach(([e,t])=>{n.setAttribute(e,t)}),n.textContent=o[0],i.appendChild(n),a=t+o[0].length}a<r.length&&i.appendChild(document.createTextNode(r.slice(a))),t.parentNode?.replaceChild(i,t),console.debug(`Created link(s) for text: "${e.text}"`,{element:i})})}function c(e){let t=document.querySelector(`meta[name="${e.name}"]`);if(t)t.setAttribute(`content`,e.value),console.debug(`Updated meta ${e.name} to: ${e.value}`,{element:t});else{let t=document.createElement(`meta`);t.setAttribute(`name`,e.name),t.setAttribute(`content`,e.value),document.head.insertAdjacentElement(`afterbegin`,t),console.debug(`Created new meta ${e.name} with value: ${e.value}`,{element:t})}}function l(e){let t=document.querySelector(`title`);t instanceof HTMLTitleElement&&(t.textContent=e.value,console.debug(`Modified page title to: ${e.value}`,{element:t}))}var u=window.OLAYER_RULES||[];function d(i){try{switch(i.action){case`page-title`:l(i);break;case`image-alt`:r(i);break;case`meta-description`:c(i);break;case`canonical`:t(i);break;case`faq-page`:n(i);break;case`article`:e(i);break;case`internal-links`:s(i);break;default:console.warn(`Unknown rule type: ${i.action}`)}}catch(e){console.error(`Failed to apply rule:`,e,i)}}function f(){console.log(`Initializing OLayer...`),u.forEach(d);let e=new MutationObserver(e=>{e.forEach(e=>{e.type===`childList`&&u.forEach(e=>{(e.action===`image-alt`||e.action===`internal-links`)&&d(e)})})});return e.observe(document.documentElement,{childList:!0,subtree:!0}),console.log(`Observer set up`),e}f(),console.log(`OLayer initialized`)})();