(()=>{"use strict";let t=null,e=null;function o(t,e=24){const n=document.querySelectorAll(".modal.show .ia-suggest-list, .plone-modal .ia-suggest-list, .plone-modal-container .ia-suggest-list");n.length?n.forEach(e=>e.innerHTML=t):e>0&&requestAnimationFrame(()=>o(t,e-1))}function n(t){if(!t)return!1;const e=t.getClientRects?.();return null!==t.offsetParent||e&&e.length>0}function r(o,r){const i=e||t?.dataset.sourceSelector||"#form-widgets-title",s=document.querySelector(i)||t?.querySelector('input[type="text"]');s&&(s.value=o,s.dispatchEvent(new Event("input",{bubbles:!0})),s.dispatchEvent(new Event("change",{bubbles:!0})),s.focus()),function(t){const e=t?.closest(".modal")||document.querySelector(".modal.show")||document.querySelector(".modal");if(!e)return;const o=e.closest(".modal-wrapper");let r=e.querySelector(".modal-close, .plone-modal-close")||e.querySelector('[data-bs-dismiss="modal"], [data-dismiss="modal"]');r&&r.click();try{window.bootstrap?.Modal&&(window.bootstrap.Modal.getInstance(e)||new window.bootstrap.Modal(e)).hide()}catch{}setTimeout(()=>{(e.classList.contains("show")||n(e))&&document.dispatchEvent(new KeyboardEvent("keydown",{key:"Escape",bubbles:!0}))},50),setTimeout(()=>{(e.classList.contains("show")||n(e))&&(e.classList.remove("show"),e.style.display="none",e.setAttribute("aria-hidden","true")),o&&o.parentNode&&o.remove(),(o&&o.parentNode||document.body).querySelectorAll(".modal-backdrop, .plone-modal-backdrop, .plone-modal-overlay, .backdrop, .backdrop-active").forEach(t=>t.remove()),document.body.classList.remove("modal-open"),document.body.style.removeProperty("padding-right"),document.querySelectorAll("[inert]").forEach(t=>t.removeAttribute("inert"))},150)}(r||document.body)}function i(t){const e=t.target.closest(".ia-suggest-item");if(!e)return;"click"!==t.type&&"pointerup"!==t.type||t.stopPropagation();const o=e.getAttribute("data-title")||e.textContent.trim();o&&r(o,e)}document.addEventListener("click",n=>{const r=n.target.closest("a.suggestedtitles-generate-button.pat-plone-modal");if(!r)return;t=r.closest(".suggestedtitles-with-button")||null,e=t?.dataset.sourceSelector||"#form-widgets-title";const i=t?.dataset.slugUrl;if(!i)return;const s=r.getAttribute("href"),a=s?document.querySelector(s):null,c=a?a.querySelector(".ia-suggest-list"):null;c&&(c.textContent="Loading…"),async function(t,e){const o={Accept:"application/json"},n=document.querySelector('input[name="_authenticator"]')?.value||"",r=new URLSearchParams({text:e});try{const e=await fetch(t,{method:"POST",credentials:"same-origin",headers:{...o,"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",...n?{"X-CSRF-TOKEN":n}:{}},body:r.toString()});if(!e.ok)throw new Error(`HTTP ${e.status}`);return await e.json()}catch(n){const r=t+(t.includes("?")?"&":"?")+"text="+encodeURIComponent(e),i=await fetch(r,{credentials:"same-origin",headers:o});if(!i.ok)throw n;return await i.json()}}(i,function(){try{if(window.tinymce){const t=tinymce.get("form-widgets-IRichTextBehavior-text")||tinymce.editors&&tinymce.editors.find(t=>t?.id?.includes("IRichTextBehavior"))||tinymce.activeEditor,e=t?.getContent?.();if(e)return e}}catch{}const t=document.querySelector("#form-widgets-IRichTextBehavior-text")||document.querySelector('textarea[name="form.widgets.IRichTextBehavior.text"]');return t?.value||""}()).then(t=>{const e=function(t){if("string"==typeof t)try{t=JSON.parse(t)}catch{return[t.trim()].filter(Boolean)}if(Array.isArray(t))return Array.from(new Set(t.map(t=>String(t).trim()).filter(Boolean)));if(t&&"object"==typeof t){let e=[];if(Array.isArray(t.suggested_titles))e=t.suggested_titles;else if(Array.isArray(t.titles))e=t.titles;else{const o=Object.values(t).find(t=>Array.isArray(t));o&&(e=o)}return Array.from(new Set(e.map(t=>String(t).trim()).filter(Boolean)))}return[]}(t),n=function(t){if(!t.length)return"<p>No suggestion.</p>";const e=document.createElement("ul");e.className="list-group ia-suggest-ul",e.setAttribute("role","listbox"),e.setAttribute("aria-label","Title suggestions");for(const o of t){const t=document.createElement("li");t.className="list-group-item list-group-item-action ia-suggest-item",t.setAttribute("role","option"),t.setAttribute("tabindex","0"),t.setAttribute("data-title",o),t.textContent=o,e.appendChild(t)}const o=document.createElement("div");return o.appendChild(e),o.innerHTML}(e);c&&(c.innerHTML=n),o(n)}).catch(t=>{const e='<div class="alert alert-warning">Not possible to get suggestions.</div>';c&&(c.innerHTML=e),o(e)})},!0),document.addEventListener("click",i,!0),document.addEventListener("pointerup",i,!0),document.addEventListener("keydown",function(t){if("Enter"!==t.key&&" "!==t.key)return;const e=t.target.closest(".ia-suggest-item");if(!e)return;t.preventDefault(),t.stopPropagation();const o=e.getAttribute("data-title")||e.textContent.trim();o&&r(o,e)},!0),document.addEventListener("hidden.bs.modal",()=>{t=null,e=null},!0)})();