(()=>{"use strict";const{ConditionItem:t}=JetFormBuilderAbstract;function e(){t.call(this),this.isSupported=function(t){return!!t?.page_state?.length},this.setOptions=function({page_state:t}){this.pageState=t},this.isPassed=function(){const t=this.list?.block?.page?.canSwitch?.current;return"active"===this.pageState&&!t}}e.prototype=Object.create(t.prototype);const s=e,{ReactiveVar:i,createConditionalBlock:n}=JetFormBuilderAbstract,{validateInputs:o,getOffsetTop:r,focusOnInvalidInput:a,populateInputs:c}=JetFormBuilderFunctions,{addAction:h,doAction:u}=JetPlugins.hooks;function d(t,e){this.node=t,this.index=+t.dataset.page,this.offset=+t.dataset.pageOffset,this.state=e,this.inputs=[],this.canSwitch=new i(null),this.isShow=new i(1===this.index),this.autoFocus=window.JetFormBuilderSettings?.auto_focus,this.initialObserveState=!1}d.prototype.observe=function(){this.isLast()||this.observeInputs(),this.canSwitch.make(),this.isShow.make(),this.isShow.watch((()=>{this.isShow.current?this.onShow():this.onHide()})),this.addButtonsListeners(),this.isFirst()&&(this.initialObserveState=!0,this.updateStateAsync().then((()=>{})).catch((()=>{}))),this.updateOffsetByProgress(),h("jet.fb.observe.input.manual","jet-form-builder/page-state",(t=>this.observeInput(t.nodes[0]))),u("jet.fb.multistep.page.init",this)},d.prototype.observeInputs=function(){for(const t of this.node.querySelectorAll("[data-jfb-sync]")){const e=this.observeInput(t);e&&u("jet.fb.multistep.page.observed.input",e,this)}},d.prototype.observeInput=function(t){if(!this.isNodeBelongThis(t)||!t.hasOwnProperty("jfbSync")||t.jfbSync.hasParent())return!1;const e=t.jfbSync;return this.handleInputEnter(e),e.loading.watch((()=>{e.loading.current?this.canSwitch.current=!1:this.updateState()})),e.reporting.restrictions.length?(this.inputs.push(e),e.watchValidity((()=>this.updateState())),e):e},d.prototype.observeConditionalBlocks=function(){if(!this.isLast())for(const t of this.node.querySelectorAll("[data-jfb-conditional]")){if(!this.isNodeBelongThis(t))continue;const e=n(t,this.state.getRoot());for(const t of e.list.getConditions())if(t instanceof s){e.page=this,this.canSwitch.watch((()=>e.list.onChangeRelated()));break}}},d.prototype.onShow=function(){this.node.classList.remove("jet-form-builder-page--hidden"),this.initialObserveState||(this.initialObserveState=!0,this.updateStateAsync().then((()=>{})).catch((()=>{})))},d.prototype.onHide=function(){this.node.classList.add("jet-form-builder-page--hidden")},d.prototype.updateState=function(){for(const t of this.getInputs())if(!t.reporting.validityState.current&&null!==t.reporting.validityState.current)return void(this.canSwitch.current=!1);this.canSwitch.current=!0},d.prototype.updateStateAsync=async function(t=!0){try{await o(this.getInputs(),t),this.canSwitch.current=!0}catch(t){this.canSwitch.current=!1}},d.prototype.addButtonsListeners=function(){const t=this.node.querySelectorAll(".jet-form-builder__next-page, .jet-form-builder__prev-page");for(const e of t){if(!this.isNodeBelongThis(e))continue;const t=e.classList.contains("jet-form-builder__prev-page");e.addEventListener("click",(()=>this.changePage(t)))}},d.prototype.changePage=async function(t){t?this.state.index.current=this.index-1:this.getLockState().current||(await this.updateStateAsync(!1),this.canSwitch.current?this.state.index.current=this.index+1:this.autoFocus&&a(this.getInputs()))},d.prototype.isNodeBelongThis=function(t){const e=t.closest(".jet-form-builder-page");return!!e&&e.isEqualNode(this.node)},d.prototype.getInputs=function(){return c(this.inputs)},d.prototype.getLockState=function(){var t;const e=this.state.getRoot();return(null!==(t=e?.parent?.root?.form)&&void 0!==t?t:e.form).lockState},d.prototype.isLast=function(){return this.state.isLastPage(this)},d.prototype.isFirst=function(){return this.state.isFirstPage(this)},d.prototype.handleInputEnter=function(t){t?.enterKey?.addFilter((()=>{const e=t.root.form;return e?!0===e.canTriggerEnterSubmit&&this.changePage().then((()=>{})).catch((()=>{})):this.changePage().then((()=>{})).catch((()=>{})),!1}))},d.prototype.getOffsetTop=function(){return r(this.node)-this.offset},d.prototype.updateOffsetByProgress=function(){this.state?.progress?.node&&(this.offset+=+this.state.progress.node.clientHeight)};const p=d,l=function(t,e){this.node=t,this.state=e,this.state.index.watch((()=>this.updateItems())),this.updateItems=function(){const{current:t}=this.state.index;for(const e of this.node.children){const s=+e.dataset.page;s<t?this.makePassed(e):t===s?this.makeActive(e):this.makeNotPassed(e)}},this.makeActive=function(t){t.classList.remove("passed-page"),t.classList.add("active-page")},this.makePassed=function(t){t.classList.add("passed-page"),t.classList.remove("active-page")},this.makeNotPassed=function(t){t.classList.remove("active-page","passed-page")}},{ConditionalBlock:f,ReactiveVar:g}=JetFormBuilderAbstract,{doAction:m}=JetPlugins.hooks,b=function(){this.root=null,this.block=null,this.index=null,this.elements=[],this.setScope=function(t){t instanceof f?this.block=t:this.root=t},this.setProgress=function(){this.index=new g(1),this.index.make(),this.index.watch(this.onChangeIndex.bind(this));for(const t of this.getScopeNode().children)t.matches(".jet-form-builder-progress-pages")&&(this.progress=new l(t,this))},this.setPages=function(t){this.elements=t.map((t=>new p(t,this))),this.elements.forEach((t=>t.observe())),this.elements.forEach((t=>t.observeConditionalBlocks()));const{submitter:e}=this.getRoot().getSubmit();e.hasOwnProperty("status")&&e.watchReset((()=>{this.index.current=1}))},this.onChangeIndex=function(){for(const t of this.getPages())t.isShow.current=t.index===this.index.current;window?.jQuery(document)?.trigger("jet-form-builder/switch-page")},this.getCurrentPage=function(){for(const t of this.getPages())if(t.isShow.current)return t;return!1},this.getPages=function(){return this.elements},this.getScopeNode=function(){var t;return null!==(t=this.block?.node)&&void 0!==t?t:this.root.rootNode},this.getRoot=function(){var t;return null!==(t=this.block?.root)&&void 0!==t?t:this.root},this.isLastPage=function(t){return this.elements.at(-1)===t},this.isFirstPage=function(t){return this.elements[0]===t},this.onReady=function(){m("jet.fb.multistep.init",this)}};function S(t){const e=new b;e.setScope(t);const s=[];for(const t of e.getScopeNode().childNodes)t?.classList?.contains("jet-form-builder-page")&&s.push(t);return s.length?(e.setProgress(),e.setPages(s),e):e}const{addAction:y,addFilter:v}=JetPlugins.hooks,{getScrollParent:w}=JetFormBuilderFunctions;y("jet.fb.observe.after","jet-form-builder/multi-step",(function(t){const e=S(t);e.getPages()?.length&&(t.multistep=e,e.onReady())}),15),y("jet.fb.conditional.init","jet-form-builder/multi-step",(function(t){const e=S(t);e.getPages()?.length&&(t.multistep=e,e.onReady())})),v("jet.fb.conditional.types","jet-form-builder/multi-step",(function(t){return[s,...t]})),y("jet.fb.multistep.init","jet-form-builder/multi-step/autoscroll",(function(t){window?.JetFormBuilderSettings?.scroll_on_next&&t.index.watch((()=>{const e=t.getCurrentPage(),s=w(e.node),i=e.getOffsetTop();s?.scrollTo?.({top:i,behavior:"smooth"})}))}))})();