var Imbox;(()=>{"use strict";var e={601:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.pageInfoMessageType=t.pingIframeMessageType=t.requestSizeMessageType=t.setLanguagePostMessageType=t.validatePostMessageType=t.isValidPostMessageType=t.getFormPostMessageType=t.submitFormPostMessageType=t.setValuePostMessageType=t.allListeningEvents=t.onPongPostMessageType=t.onValidationFailPostMessageType=t.onSizeChangePostMessageType=t.onValueChangePostMessageType=t.onSubmitChangePostMessageType=t.onFetchingChangePostMessageType=t.API_ORIGIN_URL=t.API_URL=void 0,t.API_URL="https://detached-form.imbox.io/prod/app/index.html",t.API_ORIGIN_URL="https://detached-form.imbox.io",t.onFetchingChangePostMessageType="@IMBOX/onFetchingChange",t.onSubmitChangePostMessageType="@IMBOX/onSubmitChange",t.onValueChangePostMessageType="@IMBOX/onValueChange",t.onSizeChangePostMessageType="@IMBOX/onSizeChange",t.onValidationFailPostMessageType="@IMBOX/onValidationFail",t.onPongPostMessageType="@IMBOX/onPong",t.allListeningEvents=[t.onFetchingChangePostMessageType,t.onSubmitChangePostMessageType,t.onValueChangePostMessageType,t.onSizeChangePostMessageType,t.onPongPostMessageType],t.setValuePostMessageType="@IMBOX/setValue",t.submitFormPostMessageType="@IMBOX/submitForm",t.getFormPostMessageType="@IMBOX/getForm",t.isValidPostMessageType="@IMBOX/isValid",t.validatePostMessageType="@IMBOX/validate",t.setLanguagePostMessageType="@IMBOX/setLanguage",t.requestSizeMessageType="@IMBOX/requestSize",t.pingIframeMessageType="@IMBOX/pingIframe",t.pageInfoMessageType="@IMBOX/pageInfo"}},t={};function n(o){var s=t[o];if(void 0!==s)return s.exports;var i=t[o]={exports:{}};return e[o](i,i.exports,n),i.exports}var o={};(()=>{var e=o;Object.defineProperty(e,"__esModule",{value:!0}),e.Form=void 0;var t,s=n(601),i=function(){function e(e,t){var n=this;this._dynamiclyCreatedIframe=!1,this._destroyed=!1,this._connectionIntervalId=null,this._requestQueue=[],this._formIsLoaded=!1,this.destroy=function(){var e,t;window.removeEventListener("message",n._onEvent.bind(n)),null===(e=n._iframe)||void 0===e||e.removeEventListener("load",n._iframeLoadedEvent.bind(n)),n._dynamiclyCreatedIframe&&(null===(t=n._iframe)||void 0===t||t.remove()),null!==n._connectionIntervalId&&clearInterval(n._connectionIntervalId),n._iframe=null,n._destroyed=!0,n._connectionIntervalId=null,n._formIsLoaded=!1},this.setValue=function(e,t){n.setValues([{id:e,value:t}])},this.setValues=function(e){n._isDestroyed()||(Array.isArray(e)?n._sendMessage({type:s.setValuePostMessageType,formId:n._options.formId,payload:e}):console.error("setValues: Argument has to be array"))},this.validate=function(){n._isDestroyed()||n._sendMessage({type:s.validatePostMessageType,formId:n._options.formId})},this.submit=function(){n._isDestroyed()||n._sendMessage({type:s.submitFormPostMessageType,formId:n._options.formId})},this.setLanguage=function(e){n._isDestroyed()||n._sendMessage({type:s.setLanguagePostMessageType,formId:n._options.formId,payload:{languageCode:e}})};var o=document.getElementById(e);if(this._options=t,o){if(this._iframe=o,"IFRAME"!==o.tagName){var i=this._createIframeElement(e,t);o.replaceWith(i),this._iframe=i,this._dynamiclyCreatedIframe=!0}(t.events||t.dynamicHeight)&&window.addEventListener("message",this._onEvent.bind(this),!1),this._iframe.addEventListener("load",this._iframeLoadedEvent.bind(this)),this._pingIframe(),this._requestSize()}else console.error("Could not find (".concat(e,") element in document."))}return e.prototype._createIframeElement=function(e,t){var n=document.createElement("iframe"),o="";return t.articleLanguageCode&&(o+="&articleLanguageCode=".concat(t.articleLanguageCode)),t.articleVersion&&(o+="&articleVersion=".concat(t.articleVersion)),t.articleId&&(o+="&articleId=".concat(t.articleId)),n.src="".concat(s.API_URL,"?widgetId=").concat(t.widgetId,"&formId=").concat(t.formId,"&lang=").concat(t.lang).concat(o),n.setAttribute("id",e),n.setAttribute("frameborder","0"),t.height&&n.setAttribute("height",t.height),t.width&&n.setAttribute("width",t.width),n},e.prototype._onEvent=function(e){var t,n;e.origin===s.API_ORIGIN_URL&&(null===(t=e.data)||void 0===t?void 0:t.formId)===this._options.formId&&s.allListeningEvents.includes(null===(n=e.data)||void 0===n?void 0:n.type)&&(this._handleConnectedEvent(e),this._handleFormStatusEvents(e),this._handleValueChangeEvent(e),this._handleSizeChangeEvent(e))},e.prototype._sendPingMessage=function(){var e,t;null===(t=null===(e=this._iframe)||void 0===e?void 0:e.contentWindow)||void 0===t||t.postMessage({type:s.pingIframeMessageType,formId:this._options.formId},"*")},e.prototype._pingIframe=function(){var e=this;this._sendPingMessage();var t=1;this._connectionIntervalId=setInterval((function(){t+=1,e._isDestroyed()?null!==e._connectionIntervalId&&(clearInterval(e._connectionIntervalId),e._connectionIntervalId=null):(e._sendPingMessage(),600===t&&null!==e._connectionIntervalId&&(clearInterval(e._connectionIntervalId),e._connectionIntervalId=null))}),300)},e.prototype._sendPagePropsMessage=function(){var e,t;null===(t=null===(e=this._iframe)||void 0===e?void 0:e.contentWindow)||void 0===t||t.postMessage({type:s.pageInfoMessageType,formId:this._options.formId,payload:{url:window.location.href}},"*")},e.prototype._handleConnectedEvent=function(e){var t,n,o;(null===(t=e.data)||void 0===t?void 0:t.type)===s.onPongPostMessageType&&(null!==this._connectionIntervalId&&(clearInterval(this._connectionIntervalId),this._connectionIntervalId=null),this._iframeLoadedEvent(),this._sendPagePropsMessage(),this._options.dynamicHeight&&this._setIframeHeight(null===(o=null===(n=e.data)||void 0===n?void 0:n.payload)||void 0===o?void 0:o.height))},e.prototype._handleSizeChangeEvent=function(e){var t,n,o,i,a,d;(null===(t=e.data)||void 0===t?void 0:t.type)===s.onSizeChangePostMessageType&&(this._options.dynamicHeight&&this._setIframeHeight(null===(o=null===(n=e.data)||void 0===n?void 0:n.payload)||void 0===o?void 0:o.height),(null===(i=this._options.events)||void 0===i?void 0:i.onSizeChange)&&(null===(a=this._options.events)||void 0===a||a.onSizeChange(null===(d=e.data)||void 0===d?void 0:d.payload)))},e.prototype._handleValueChangeEvent=function(e){var t,n,o,i;if((null===(t=e.data)||void 0===t?void 0:t.type)===s.onValueChangePostMessageType&&(null===(n=this._options.events)||void 0===n?void 0:n.onValueChange))return null===(o=this._options.events)||void 0===o?void 0:o.onValueChange(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.payload)},e.prototype._handleFormStatusEvents=function(e){var t,n,o,i,a,d,r,l,u,g;if((null===(t=e.data)||void 0===t?void 0:t.type)===s.onFetchingChangePostMessageType||(null===(n=e.data)||void 0===n?void 0:n.type)===s.onSubmitChangePostMessageType)switch(null===(i=null===(o=e.data)||void 0===o?void 0:o.payload)||void 0===i?void 0:i.event){case"fetch-success":this._formIsLoaded=!0,this._runRequestQueue();case"fetch-failed":case"fetching":if(null===(a=this._options.events)||void 0===a?void 0:a.onFetchingChange)return null===(d=this._options.events)||void 0===d?void 0:d.onFetchingChange(null===(r=e.data)||void 0===r?void 0:r.payload);break;case"submit-failed":case"submit-success":case"submitting":if(null===(l=this._options.events)||void 0===l?void 0:l.onFetchingChange)return null===(u=this._options.events)||void 0===u?void 0:u.onSubmitChange(null===(g=e.data)||void 0===g?void 0:g.payload)}},e.prototype._setIframeHeight=function(e){var t;"number"==typeof e&&e&&(null===(t=this._iframe)||void 0===t||t.setAttribute("height",String(e)))},e.prototype._disableScrolBars=function(){var e,t;try{var n=null===(t=null===(e=this._iframe)||void 0===e?void 0:e.contentWindow)||void 0===t?void 0:t.document.querySelector("body");null==n||n.setAttribute("style","overflow: hidden;")}catch(e){}},e.prototype._iframeLoadedEvent=function(){this._options.dynamicHeight&&this._disableScrolBars(),this._options.lang&&this.setLanguage(this._options.lang),this._requestSize()},e.prototype._requestSize=function(){var e,t;null===(t=null===(e=this._iframe)||void 0===e?void 0:e.contentWindow)||void 0===t||t.postMessage({type:s.requestSizeMessageType,formId:this._options.formId},"*")},e.prototype._isDestroyed=function(){return this._destroyed&&console.error("(".concat(this._options.formId,") Form api has manualy been destroyed.")),this._destroyed},e.prototype._sendMessage=function(e){var t,n;(null==this?void 0:this._formIsLoaded)?null===(n=null===(t=this._iframe)||void 0===t?void 0:t.contentWindow)||void 0===n||n.postMessage(e,"*"):null==this||this._requestQueue.push(e)},e.prototype._runRequestQueue=function(){var e=this;this._requestQueue.forEach((function(t){return e._sendMessage(t)})),this._requestQueue=[]},e}();e.Form=i,window.IMBOX_FORMS_AUTO_INITIALIZED=[],t=document.querySelectorAll('iframe[src^="'.concat(s.API_ORIGIN_URL,'"], div[src^="').concat(s.API_ORIGIN_URL,'"]')),Array.from(t).forEach((function(e){if(e instanceof HTMLElement&&"true"===e.dataset.init){var t=new URL(e.getAttribute("src")),n=new URLSearchParams(t.search),o=new i(e.id,{formId:Number(n.get("formId")),widgetId:Number(n.get("widgetId")),lang:n.get("lang")||e.dataset.lang,dynamicHeight:"true"===e.dataset.dynamicheight});window.IMBOX_FORMS_AUTO_INITIALIZED.push(o)}}))})(),Imbox=o})();