import{k as E,z as L,A as k,aK as x,aL as T}from"./CiQWViEs.js";import{n as H,i as M,a as D,b as F,c as S}from"./CLZfVTF1.js";function P(t,o,i){E(()=>{var r=L(()=>o(t,i==null?void 0:i())||{});if(i&&(r!=null&&r.update)){var a=!1,c={};k(()=>{var n=i();x(n),a&&T(c,n)&&(c=n,r.update(n))}),a=!0}if(r!=null&&r.destroy)return()=>r.destroy()})}function z(t){const o=JSON.parse(t);return o.data&&(o.data=F(o.data,S.decoders)),o}function u(t){return HTMLElement.prototype.cloneNode.call(t)}function R(t,o=H){const i=async({action:a,result:c,reset:n=!0,invalidateAll:s=!0})=>{c.type==="success"&&(n&&HTMLFormElement.prototype.reset.call(t),s&&await M()),(location.origin+location.pathname===a.origin+a.pathname||c.type==="redirect"||c.type==="error")&&await D(c)};async function r(a){var p,b,y;if(((p=a.submitter)!=null&&p.hasAttribute("formmethod")?a.submitter.formMethod:u(t).method)!=="post")return;a.preventDefault();const n=new URL((b=a.submitter)!=null&&b.hasAttribute("formaction")?a.submitter.formAction:u(t).action),s=(y=a.submitter)!=null&&y.hasAttribute("formenctype")?a.submitter.formEnctype:u(t).enctype,d=new FormData(t,a.submitter),f=new AbortController;let m=!1;const w=await o({action:n,cancel:()=>m=!0,controller:f,formData:d,formElement:t,submitter:a.submitter})??i;if(m)return;let l;try{const e=new Headers({accept:"application/json","x-sveltekit-action":"true"});s!=="multipart/form-data"&&e.set("Content-Type",/^(:?application\/x-www-form-urlencoded|text\/plain)$/.test(s)?s:"application/x-www-form-urlencoded");const A=s==="multipart/form-data"?d:new URLSearchParams(d),h=await fetch(n,{method:"POST",headers:e,cache:"no-store",body:A,signal:f.signal});l=z(await h.text()),l.type==="error"&&(l.status=h.status)}catch(e){if((e==null?void 0:e.name)==="AbortError")return;l={type:"error",error:e}}await w({action:n,formData:d,formElement:t,update:e=>i({action:n,result:l,reset:e==null?void 0:e.reset,invalidateAll:e==null?void 0:e.invalidateAll}),result:l})}return HTMLFormElement.prototype.addEventListener.call(t,"submit",r),{destroy(){HTMLFormElement.prototype.removeEventListener.call(t,"submit",r)}}}export{P as a,R as e};
