(()=>{"use strict";const e=window.flexaBlockInlineEditor,t="flexa-fee-editable",n="flexa-fee-active";let r=null,l=!1,i=null,o=null,a=null,s=null;function c(e){i&&(e?i.removeAttribute("hidden"):i.setAttribute("hidden",""))}function d(e,t="info"){o&&(o.textContent=e,o.classList.remove("is-success","is-error"),"success"===t?o.classList.add("is-success"):"error"===t&&o.classList.add("is-error"))}function u(e){r&&(e&&(r.el.innerHTML=r.originalHTML),r.el.contentEditable="false",r.el.classList.remove(n),r.root.classList.remove(n),r=null,l=!1,c(!1))}function f(){if(!r)return"";if(void 0!==r.join&&r.itemEl)return Array.from(r.itemEl.querySelectorAll(r.selector)).map(e=>(e.textContent||"").trim()).filter(e=>""!==e).join(r.join);const e=r.el.cloneNode(!0);return e.querySelectorAll(".flexa-field__required").forEach(e=>e.remove()),e.innerHTML}async function m(){if(!r||!e||l)return;l=!0,d(e.i18n.saving);const t={postId:e.postId,blockId:r.blockId,blockName:r.blockName,attr:r.attr,value:f()};void 0!==r.index&&(t.index=r.index),void 0!==r.subindex&&(t.subindex=r.subindex),void 0!==r.key&&(t.key=r.key);try{const n=await fetch(e.endpoint,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":e.nonce},body:JSON.stringify(t)});if(!n.ok){const e=await n.json().catch(()=>({})),t=e&&e.message||"";throw new Error(t||`HTTP ${n.status}`)}d(e.i18n.saved,"success"),u(!1)}catch(t){l=!1;const n=t instanceof Error?t.message:e.i18n.error;d(`${e.i18n.error}: ${n}`,"error")}}function b(){e&&e.fields&&(i=document.querySelector(".flexa-fee-toolbar"),i&&(o=i.querySelector(".flexa-fee-toolbar__msg"),a=i.querySelector(".flexa-fee-btn--save"),s=i.querySelector(".flexa-fee-btn--cancel"),a?.addEventListener("click",()=>{m()}),s?.addEventListener("click",()=>u(!0))),document.querySelectorAll("[data-flexa-edit-id]").forEach(l=>{const i=l.getAttribute("data-flexa-edit-name")||"",o=e.fields[i];o&&o.forEach(e=>function(e,l){(l.repeat?Array.from(e.querySelectorAll(l.selector)):[e.querySelector(l.selector)].filter(Boolean)).forEach(i=>{if(i.classList.contains(t))return;let o=null;if(l.repeat){if(o=i.closest("[data-flexa-item]"),!o)return;if(l.cell&&!i.hasAttribute("data-flexa-cell"))return}i.classList.add(t),i.addEventListener("click",t=>{r&&r.el===i||(t.preventDefault(),t.stopPropagation(),function(e,t,l,i){if(r&&r.el===e)return;let o,a;if(r&&u(!0),l.repeat&&i){const e=i.getAttribute("data-flexa-item");o=null!==e?parseInt(e,10):void 0}if(l.cell){const t=e.getAttribute("data-flexa-cell");a=null!==t?parseInt(t,10):void 0}r={el:e,root:t,originalHTML:e.innerHTML,blockId:t.getAttribute("data-flexa-edit-id")||"",blockName:t.getAttribute("data-flexa-edit-name")||"",attr:l.attr,index:o,subindex:a,key:l.repeat||l.object?l.key:void 0,selector:l.selector,join:l.join,itemEl:i},e.contentEditable="true",e.classList.add(n),t.classList.add(n),d(""),c(!0),e.focus()}(i,e,l,o))})})}(l,e))}),document.addEventListener("keydown",e=>{r&&("Escape"===e.key?(e.preventDefault(),u(!0)):(e.metaKey||e.ctrlKey)&&"Enter"===e.key&&(e.preventDefault(),m()))}))}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",b):b()})();