class TailwindManager{constructor(e){this.userConfig=e,this.frontData=window.MTDynFrontData,this.data=this.frontData.tailwind,this.staticStylesheet=document.getElementById("mt-tailwind-css")||document.getElementById("mt-tailwind-inline-css"),this.siteWideElement=null,this.pageClassArray=this.data.classCache?JSON.parse(this.data.classCache):[],this.state="IDLE",this.cache={},this.updateQueueTimer=null}async init(){this.createDummyElement(),await this.loadCompiler(),this.setupTailwindConfig()}createDummyElement(){this.siteWideElement=document.createElement("span"),this.siteWideElement.id="mt-site-wide-tailwind",this.siteWideElement.style.display="none !important",document.body.appendChild(this.siteWideElement)}loadCompiler(){return new Promise(((e,t)=>{const s=document.getElementById("amender-script-tailwind-js");if(!s)return console.error("Tailwind Manager: Processor script element not found."),t(new Error("Processor script element not found."));const i=s.getAttribute("src").split("?")[0].replace("main","compiler"),a=document.createElement("script");a.src=`${i}?v=3.3.3`,a.onload=()=>{e()},a.onerror=()=>{t(new Error("Failed to load compiler script."))},document.head.appendChild(a)}))}setupTailwindConfig(){window.tailwind&&window.tailwind.resolveConfig?(window.tailwind.config=window.tailwind.resolveConfig(this.userConfig),"function"==typeof window.startTailwindJIT&&window.startTailwindJIT()):(console.log("Tailwind JIT not ready, retrying config setup..."),setTimeout((()=>this.setupTailwindConfig()),50))}escapeHTML(e){return e?e.replace(/&[^(#\d+;|a-z+;)]/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;"):e}classesChanged(){const e=[];let t=0;for(const s of this.cache.classCache.keys())t>0&&e.push(s),t++;const s=JSON.stringify(e);return this.data.classCache!==s&&(this.data.classCache=s,this.pageClassArray=e,!0)}cssStylesUpdated(e){switch(this.cache.classCache||(this.cache=window.TvrMTTailwind.cache||{}),this.staticStylesheet&&(this.staticStylesheet.remove(),this.staticStylesheet=null),this.state){case"APPLYING_SITEWIDE":this.handleSitewideStylesUpdate(e);break;case"RESETTING":this.state="IDLE";break;default:this.handlePageStylesUpdate(e)}}async handleSitewideStylesUpdate(e){const t={tailwind_actions:"1",update_site_wide_styles:"1",site_wide_slug:"site-wide",site_wide_styles:e};try{await this.postRequest(t)}catch(e){console.error("Failed to update site-wide styles:",e)}let s=0;for(const e of this.cache.classCache.keys())s>0&&!this.pageClassArray.includes(e)&&(this.cache.candidateRuleCache.delete(e),this.cache.classCache.delete(e)),s++;this.state="RESETTING",this.siteWideElement.removeAttribute("class")}postRequest(e){return e.action="mtui",this.frontData.nonce&&(e._wpnonce=this.frontData.nonce),fetch(this.frontData.wp_ajax_url,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams(e)})}handlePageStylesUpdate(e){this.classesChanged()&&(clearTimeout(this.updateQueueTimer),this.updateQueueTimer=setTimeout((async()=>{const t={tailwind_actions:"1",get_site_wide_classes:"1",update_single_page_styles:"1",single_page_slug:`${this.frontData.type}-${this.frontData.id}`,single_page_classes:this.data.classCache,single_page_styles:e,site_wide_slug:"site-wide"};try{const e=await this.postRequest(t),s=await e.text();if(s!==this.data.siteWideCache){this.data.siteWideCache=s;const e=JSON.parse(s),t=this.escapeHTML(Object.keys(e).join(" "));this.state="APPLYING_SITEWIDE",this.siteWideElement.setAttribute("class",t)}}catch(e){console.error("Failed to update page styles:",e)}}),250))}}export default TailwindManager;