(()=>{(function(){"use strict";function w(e,g,f,p){let r=e.selection.getContent({format:"text"});if(!r){e.windowManager.alert("Please select some text to process.");return}e.setProgressState(!0);try{let t=document.querySelector(".aipkit-tinymce-assistant-button button");t&&(t.disabled=!0,t.dataset._origText=t.textContent,t.textContent="\u23F3 AIP Content Assistant")}catch{}let o=window.aipkit_post_enhancer?.nonce_process_text;if(!o){console.error("AIPKit Enhancer: Nonce not found for text processing."),e.setProgressState(!1),e.windowManager.alert("An error occurred: Security token missing.");return}let d=g.replace("%s",r),h={_ajax_nonce:o,text_to_process:r,final_prompt:d};if(typeof window.aipkit_apiRequest!="function"){console.error("AIPKit Enhancer: aipkit_apiRequest function not available."),e.setProgressState(!1),e.windowManager.alert("An error occurred: API function missing.");return}window.aipkit_apiRequest("aipkit_process_enhancer_text",h).then(t=>{if(!t.text)throw new Error("AI did not return any text.");let u=window.aipkit_post_enhancer||{},n=u.parse_html_formats!==void 0?!!u.parse_html_formats:!0,a=t.text;if(/(^\s{0,3}#{1,6}\s)|(^\s{0,3}[-*+]\s)|(```[\s\S]*?```)|(__|\*\*)|(_|\*)/m.test(a)&&typeof window.aipkit_getMarkdownRenderer=="function")try{let s=window.aipkit_getMarkdownRenderer();s&&(a=s.render(a))}catch(s){console.warn("AIPKit: markdown render failed",s)}if(n){let s=document.createElement("div");s.innerHTML=a;let _=new Set(["STRONG","B","EM","I","U","A","P","BR","H1","H2","H3","H4","H5","H6","UL","OL","LI","BLOCKQUOTE","PRE","CODE"]),m=c=>{if(!c||typeof c!="string")return!1;let l=c.trim();return/^(https?:|mailto:|tel:|#|\/)/i.test(l)||!/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(l)};(function c(l){let x=Array.from(l.childNodes);for(let i of x)if(i.nodeType===Node.ELEMENT_NODE){let k=i.tagName;if(!_.has(k)){for(c(i);i.firstChild;)l.insertBefore(i.firstChild,i);l.removeChild(i);continue}for(let A of Array.from(i.attributes))k==="A"&&A.name.toLowerCase()==="href"&&m(i.getAttribute("href"))||i.removeAttribute(A.name);c(i)}})(s),a=s.innerHTML}else a=a.replace(/<[^>]*>/g,""),a=e.dom.encode(a);let b=window.aipkit_post_enhancer&&window.aipkit_post_enhancer.default_insert_position||"replace",y=(f&&["replace","after","before"].includes(f)?f:null)||b;if(y==="before"){try{e.selection.collapse(!0)}catch{}e.insertContent(a)}else if(y==="after"){try{e.selection.collapse(!1)}catch{}e.insertContent(a)}else e.selection.setContent(a);try{let s=document.getElementById("post_type"),_=s&&s.value?s.value:(document.body.className.match(/post-type-([\w-]+)/)||[])[1]||"post";if(p&&window.localStorage){let m=`aipkit_enhancer_recent_${_}`,c=JSON.parse(localStorage.getItem(m)||"[]").filter(l=>l!==p);c.unshift(p),localStorage.setItem(m,JSON.stringify(c.slice(0,5)))}}catch{}}).catch(t=>{console.error("AIPKit Enhancer: Error during AI processing action.",t),e.windowManager.alert(`An error occurred: ${t.message||"Request failed."}`)}).finally(()=>{e.setProgressState(!1);try{let t=document.querySelector(".aipkit-tinymce-assistant-button button");t&&(t.disabled=!1,t.textContent=t.dataset._origText||"\u270D\uFE0F AIP Content Assistant")}catch{}})}tinymce.PluginManager.add("aipkit_assistant",function(e,g){e.addButton("aipkit_assistant_button",{text:"\u270D\uFE0F AIP Content Assistant",icon:!1,type:"menubutton",classes:"aipkit-tinymce-assistant-button",menu:function(){let p=(window.aipkit_post_enhancer||{}).actions||[],r=[];r.push({text:"\u270D\uFE0F AIP CONTENT ASSISTANT",classes:"aipkit-tinymce-menu-header",disabled:!0}),p.length>0&&r.push({text:"-"});try{let o=document.getElementById("post_type"),d=o&&o.value?o.value:(document.body.className.match(/post-type-([\w-]+)/)||[])[1]||"post",h=JSON.parse(localStorage.getItem(`aipkit_enhancer_recent_${d}`)||"[]"),t={};(p||[]).forEach(n=>{n&&n.id&&(t[n.id]=n)});let u=h.map(n=>t[n]).filter(Boolean);u.length&&(r.push({text:"Recent",classes:"aipkit-tinymce-menu-header",disabled:!0}),u.forEach(n=>{n.label&&n.prompt&&r.push({text:n.label,classes:"aipkit-tinymce-menu-item",onclick:function(){w(e,n.prompt,n.insert_position||null,n.id)}})}),r.push({text:"-"}))}catch{}return p.forEach(o=>{o.label&&o.prompt&&r.push({text:o.label,classes:"aipkit-tinymce-menu-item",onclick:function(){w(e,o.prompt,o.insert_position||null,o.id)}})}),r}()})})})();})();
