(()=>{"use strict";var e={n:t=>{var n=t&&t.__esModule?()=>t.default:()=>t;return e.d(n,{a:n}),n},d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=React;var n=e.n(t);const r=Draftail,a=ReactDOM,o=(e,n)=>{const r=(0,t.useRef)((t=>{e.current&&!e.current.contains(t.target)&&n()}));(0,t.useEffect)((()=>(document.addEventListener("mousedown",r.current),()=>{document.removeEventListener("mousedown",r.current)})),[r])},i=DraftJS;class s extends Error{}const c=(e,t)=>{const n=i.RichUtils.insertSoftNewline(i.EditorState.moveSelectionToEnd(e)),r=n.getCurrentContent(),a=i.Modifier.replaceText(r,i.EditorState.moveSelectionToEnd(n).getSelection(),t);return i.EditorState.push(n,a,"insert-characters")},l=(e,t)=>{const n=e.getCurrentContent(),r=i.Modifier.replaceText(n,(e=>{const t=e.getBlockMap();return new i.SelectionState({anchorKey:t.first().getKey(),anchorOffset:0,focusKey:t.last().getKey(),focusOffset:t.last().getLength()})})(n),t);return i.EditorState.push(e,r,"insert-characters")},u=async(e,t,n,r)=>{const a=e.getCurrentContent().getPlainText();return n(e,await(async(e,t,n)=>{const r=new FormData;return r.append("text",e),r.append("prompt",t.uuid),(async(e,t,n)=>{const r=window.wagtailAI.config.urls,a=await fetch(r.TEXT_COMPLETION,{method:"POST",headers:{[wagtailConfig.CSRF_HEADER_NAME]:wagtailConfig.CSRF_TOKEN},body:t,signal:n}),o=await a.json();if(a.ok)return o.message;throw new s(o.error)})(0,r,n)})(a,t,r.signal))},d=["Processing your query, please wait...","Analyzing your input, just a moment...","Generating a response, hold on...","Thinking, thinking, thinking...","Fetching data, almost there...","Compiling information, please wait...","Crunching numbers, please be patient...","Analyzing data, loading...","Preparing response, please wait...","Interpreting your message, loading..."];function m({cancelHandler:e}){const r=d[Math.floor(Math.random()*d.length)],[a,o]=(0,t.useState)(0);return(0,t.useEffect)((()=>{const e=document.querySelector(".Draftail-Editor")?.parentElement;if(e){const t=new ResizeObserver((e=>{for(const t of e)o(t.contentRect.height)}));return t.observe(e),()=>{t.disconnect()}}}),[]),n().createElement("div",{className:"Draftail-AI-LoadingOverlay",style:{height:a?`${a}px`:"100%"}},n().createElement("span",null,n().createElement("svg",{className:"icon icon-spinner c-spinner","aria-hidden":"true"},n().createElement("use",{href:"#icon-spinner"})),r),n().createElement("button",{onClick:e,className:"button button-secondary"},"Cancel request"))}function g({close:e,onAction:r,aiPrompts:a}){const i=(0,t.useRef)(null);return o(i,e),n().createElement("div",{ref:i,className:"Draftail-AI-ButtonDropdown"},a.map((e=>n().createElement("button",{type:"button",onMouseDown:()=>r(e)},n().createElement("span",null,e.label)," ",e.description))))}window.draftail.registerPlugin({type:"ai",inline:function({getEditorState:e,onChange:o}){const i=window.wagtailAI.config.aiPrompts,s=e(),[d,p]=(0,t.useState)(!1),[f,E]=(0,t.useState)(!1),[w,h]=(0,t.useState)(null),y=(0,t.useRef)(),S=y?.current?y?.current.closest("[data-draftail-editor-wrapper]"):null,v=new AbortController;return n().createElement(n().Fragment,null,n().createElement(r.ToolbarButton,{name:"AI Tools",title:"AI prompts",icon:n().createElement(r.Icon,{icon:d?"#icon-wand-animated":"#icon-wand"}),onClick:()=>E(!f)}),n().createElement("span",{ref:y}),f?n().createElement(g,{close:()=>E(!1),onAction:async e=>{h(null),E(!1),p(!0);try{"append"===e.method?o(await u(s,e,c,v)):o(await u(s,e,l,v))}catch(e){h(e.message)}p(!1)},aiPrompts:i}):null,w&&S?.parentNode?(0,a.createPortal)(n().createElement("div",{className:"w-field__errors"},n().createElement("svg",{className:"icon icon-warning w-field__errors-icon","aria-hidden":"true"},n().createElement("use",{href:"#icon-warning"}))," ",n().createElement("p",{className:"error-message"},w)),S.parentNode.previousElementSibling):null,d&&S?(0,a.createPortal)(n().createElement(m,{cancelHandler:e=>{e.preventDefault(),v.abort(),p(!1)}}),S):null)}},"controls")})();