/*!
 * bforge sitepackage v1.0.0 (https://brainforge.at)
 * Copyright 2023-2024 Philip Grussmann
 * Licensed under the GPL-2.0-or-later license
 */
import{debug}from"Debug";import{userInteractionPromise,startup}from"promiseHelpers";class CookieManager{#debug=debug("omCookieManager");#cookieGroups={};#gtmEvents=[];#panelButtons;#openButtons;#cookiePanel;#openCookiePanel=!1;constructor(){this.#init()}start(){if(this.#init(),this.#panelButtons=document.querySelectorAll("[data-omcookie-panel-save]"),this.#openButtons=document.querySelectorAll("[data-omcookie-panel-show]"),this.#cookiePanel=document.querySelector("[data-omcookie-panel]"),null===this.#cookiePanel)this.#openCookiePanel=!1;else{this.#openCookiePanel=!0;var o=this.#getCookie("omCookieConsent");if(null!==o&&0<o.length){var t,i,n,s=document.querySelectorAll("[data-omcookie-panel-grp]");let e="";for(t of o.split(","))"dismiss"===t?this.#openCookiePanel=!1:"1"===(i=t.split("."))[1]&&(this.#enableCookieGroup(i[0]),e+=i[0]+",");for(n of s){if(!(n instanceof HTMLInputElement&&"checked"in n))return;-1!==e.indexOf(n.value)&&(n.checked=!0),-1===o.indexOf(n.value)&&(this.#openCookiePanel=!0)}this.#pushGtmEvents(),this.#triggerPanelEvents(["cookieconsentscriptsloaded"]),document.documentElement.dataset.omCookie="loaded",document.dispatchEvent(new CustomEvent("om-custom-cookie-loaded"))}this.#cookiePanel instanceof HTMLElement&&(!0===this.#openCookiePanel?this.#cookiePanel.classList.add("active"):this.#cookiePanel.classList.remove("active")),this.#panelButtons.forEach(e=>{e.addEventListener("click",()=>{e instanceof HTMLElement&&this.#cookieSave(e.getAttribute("data-omcookie-panel-save"))},{passive:!0})}),this.#openButtons.forEach(e=>{e.addEventListener("click",()=>{null!==this.#cookiePanel&&(this.#cookiePanel.classList.contains("active")?(this.#openCookiePanel=!1,this.#cookiePanel.classList.remove("active")):(this.#openCookiePanel=!0,this.#cookiePanel.classList.add("active")))})})}}#init(){try{document.documentElement.dataset.omCookie="init";var e=JSON.parse(document.getElementById("om-cookie-consent")?.innerHTML??"");"object"==typeof e&&null!==e&&(this.#cookieGroups=e),this.#gtmEvents=[]}catch(e){this.#debug&&console.log("OM Cookie Manager: No Cookie Groups found! Maybe you have forgot to set the page id inside the constants of the extension")}}#getCookie(e){e=document.cookie.match("(^|;) ?"+e+"=([^;]*)(;|$)");return e?e[2]:null}#setCookie(e,o,t){var i=new Date;i.setTime(i.getTime()+864e5*t),document.cookie=e+"="+o+";path=/;expires="+i.toUTCString()+";SameSite=Lax"}#deleteCookie(e){this.#setCookie(e,"",-1)}#cookieSave(e){var o=document.querySelectorAll("[data-omcookie-panel-grp]");let t=this.#getCookie("omCookieConsent");if(null===t||t.length<=0)t="";else for(var i of o)i instanceof HTMLInputElement&&"checked"in i&&(t=t.replace(new RegExp(i.value+"\\S{3}"),""));switch(e){case"all":for(var n of o)n instanceof HTMLInputElement&&"checked"in n&&(this.#enableCookieGroup(n.value),t+=n.value+".1,",n.checked=!0);break;case"save":for(var s of o)s instanceof HTMLInputElement&&"checked"in s&&(!0===s.checked?(this.#enableCookieGroup(s.value),t+=s.value+".1,"):t+=s.value+".0,");break;case"min":for(var a of o)a instanceof HTMLInputElement&&"checked"in a&&(null!==a.getAttribute("data-omcookie-panel-essential")?(this.#enableCookieGroup(a.value),t+=a.value+".1,"):(t+=a.value+".0,",a.checked=!1))}t=t.replace("dismiss",""),t+="dismiss",this.#setCookie("omCookieConsent",t,364),this.#pushGtmEvents(),this.#triggerPanelEvents(["cookieconsentsave","cookieconsentscriptsloaded"]),document.documentElement.dataset.omCookie="loaded",document.dispatchEvent(new CustomEvent("om-custom-cookie-saved")),document.dispatchEvent(new CustomEvent("om-custom-cookie-loaded")),this.#openCookiePanel=!1,null!==this.#cookiePanel&&this.#cookiePanel.classList.remove("active")}#triggerPanelEvents(e){e.forEach(e=>{this.#cookiePanel instanceof HTMLElement&&this.#cookiePanel.dispatchEvent(new CustomEvent(e,{bubbles:!0}))})}#pushGtmEvents(){window.dataLayer=window.dataLayer||[],this.#gtmEvents.forEach(e=>{window.dataLayer.push({event:e})})}#enableCookieGroup(e){if(void 0!==this.#cookieGroups[e]){for(var o in this.#cookieGroups[e])if(this.#cookieGroups[e].hasOwnProperty(o)){var t=this.#cookieGroups[e][o];if("gtm"===o)this.#cookieGroups[e][o]&&this.#gtmEvents.push(this.#cookieGroups[e][o]);else for(var i in t)if(t.hasOwnProperty(i)&&Array.isArray(t[i])){let o="";t[i].forEach(e=>{o+=e});var n=document.createRange(),i="header"===i?document.head:document.body,n=(n.selectNode(i),n.createContextualFragment(o));i.appendChild(n)}}delete this.#cookieGroups[e]}}}let cookieManager=new CookieManager;startup({promise:userInteractionPromise,emitters:[{emitter:document,events:["om-custom-cookie-restarted"]}]},()=>{cookieManager.start()});