import{g as a}from"./request-a636442b.js";class p{constructor({contextElement:t,input:e,requestId:n,...s}){if(!t||!e||!n){console.error("Missing ate least one required params");return}this.options={onLoadingChanged:r=>{},onError:(r,{action:i,path:o,inputs:h})=>{},onGenerate:r=>{},turboStreamTarget:"ai-prompt-request-output-text",...s},this.contextElement=t,this.input=e,this.requestId=n,this.isLoading=!1,this.previousContent="",this.canRevert=!1,this.init()}init(){this.bindTurbo()}bindTurbo(){document.addEventListener("turbo:before-stream-render",t=>{const e=t.target.getAttribute("target");if(e!==this.options.turboStreamTarget)return;const n=t.detail.render;t.detail.render=async s=>{var i,o;await n(s);const r=this.contextElement.querySelector(`#${e}`);r&&(this.input.value=r.value,this.input.dispatchEvent(new Event("input",{bubbles:!0})),this.input.dispatchEvent(new Event("change",{bubbles:!0})),this.setLoadingState(!1),(o=(i=this.options).onGenerate)==null||o.call(i,r.value),this.previousContent&&this.setCanRevert(!0))}})}async generateContent(t,e,n={}){var s,r;if(!e||!t)throw new Error("Action and path are required for content generation");this.setLoadingState(!0),this.setCanRevert(!1),this.previousContent=this.input.value;try{const i=await fetch(e,{method:"POST",headers:{"X-Requested-With":"XMLHttpRequest","Content-Type":"application/json","X-CSRF-Token":a()},body:JSON.stringify({ai_prompt_request:{action:t,request_id:this.requestId,...n}})});if(!i.ok)throw new Error(`AI generation failed: ${i.status}`)}catch(i){this.setLoadingState(!1),(r=(s=this.options).onError)==null||r.call(s,i,{action:t,path:e,inputs:n})}}revertContent(){this.input.value=this.previousContent,this.setCanRevert(!1)}setLoadingState(t){var e,n;this.loading=t,this.contextElement.toggleAttribute("data-ai-loading",t),(n=(e=this.options).onLoadingChanged)==null||n.call(e,t)}setCanRevert(t){this.canRevert=t,this.contextElement.toggleAttribute("data-ai-can-revert",t)}}export{p as A}; //# sourceMappingURL=ai-input-9185a320.js.map