!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var i in r)("object"==typeof exports?exports:e)[i]=r[i]}}(Depicter,(()=>(()=>{"use strict";var e={};if((e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})})(e),!window.Depicter)throw new Error("Depicter must be loaded before external layers");if(!window.Depicter.env)throw new Error("Depicter.env is not available. Make sure Depicter is properly initialized.");const{Layer:t,env:r}=window.Depicter;class i extends t{constructor(e,t,i,s,a,o){super(e,t,i,s,a,o),this.type="form",this.nestable=!0,this.frame.classList.add(`${r.prefix}-${this.type}-layer`),this.resetAfterSubmit="false"!==this.element.getAttribute("data-reset-after-submit"),this.recaptchaPromise=this.prepareReCaptcha(),this.element.addEventListener("submit",(e=>{e.preventDefault(),this._onFormSubmit()})),this.isSubmitting=!1}async prepareReCaptcha(){this.recaptchaKey=this.element.querySelector('[name="_g_recaptcha_key"]')?.getAttribute("value"),this.recaptchaKey&&await this.holder.composer.dependencyManager.loadScript(`https://www.google.com/recaptcha/api.js?render=${this.recaptchaKey}`)}async _onFormSubmit(){if(this.isSubmitting)return;if(this.recaptchaPromise&&await this.recaptchaPromise,this.recaptchaKey)try{const e=await window.grecaptcha.execute(this.recaptchaKey,{action:"submit"}),t=this.element.querySelector('[name="_g_recaptcha_token"]');t?t.setAttribute("value",e):this.element.append(`<input type="hidden" name="_g_recaptcha_token" value="${e}" />`)}catch(e){this.element.querySelector('[name="_g_recaptcha_token"]')?.remove();const t=document.createElement("input");t.setAttribute("type","hidden"),t.setAttribute("name","_g_recaptcha_error"),t.setAttribute("value",e?.message||e.toString()),this.element.append(t)}const e=new FormData(this.element),t=this.element.querySelector('[type="submit"]');t?.classList.add("depicter-is-loading");const r=["token","csrf","auth","password","secret","key","depicter-csrf","action"];if(document.referrer){const t=new URL(document.referrer);r.forEach((e=>{t.searchParams.has(e)&&t.searchParams.delete(e)})),e.append("referrerURL",t.toString())}const i=new URL(window.location.href);r.forEach((e=>{i.searchParams.has(e)&&i.searchParams.delete(e)})),e.append("pageURL",i.toString()),this.isSubmitting=!0,await this._sendForm(e),t?.classList.remove("depicter-is-loading"),this.isSubmitting=!1}async _sendForm(e){this.isSubmitting=!0;const t=this.element.getAttribute("action");try{this.trigger("formSubmit",[this]);const r=await fetch(t,{method:"POST",body:e}),i=await r.json();i.success?(this.resetAfterSubmit&&this.element.reset(),this.trigger("formSubmitted",[this,i]),this.composer.trigger("formSubmitted",[this,i])):(this.trigger("formSubmitError",[this,i]),this.composer.trigger("formSubmitError",[this,i]))}catch(e){this.trigger("formSubmitError",[this,e]),this.composer.trigger("formSubmitError",[this,e]),console.error(e)}}}return window.Depicter.externalLayers.form=i,window.Depicter.externalLayers["survey:form"]=i,e})()));