!function(){"use strict";const t=function(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return e||(e=Math.random().toString(36).substr(2,5)),`${t}.${e}`};window.FormieRepeater=class{constructor(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.$form=t.$form,this.form=this.$form.form,this.$field=t.$field,this.disabledClass=this.form.getClasses("disabled"),this.rowCounter=0,this.initRepeater()}initRepeater(){const e=this.getRows();this.$field.repeater=this,this.$addButton=this.$field.querySelector("[data-add-repeater-row]"),this.minRows=parseInt(this.$addButton.getAttribute("data-min-rows")),this.maxRows=parseInt(this.$addButton.getAttribute("data-max-rows")),this.$addButton&&this.form.addEventListener(this.$addButton,t("click"),(t=>{this.addRow()})),e&&e.length&&e.forEach((t=>{this.initRow(t)})),e&&e.length||!this.minRows||this.addMultipleRows(this.minRows),this.$field.dispatchEvent(new CustomEvent("init",{bubbles:!0,detail:{repeater:this}}))}initRow(e){let i=arguments.length>1&&void 0!==arguments[1]&&arguments[1];if(!e)return void console.error(e);const s=e.querySelector("[data-remove-repeater-row]");if(s&&this.form.addEventListener(s,t("click"),(t=>{this.removeRow(t)})),i){const{Formie:t}=this.form.config,i=t.parseFieldConfig(e,this.$form);Object.keys(i).forEach((e=>{i[e].forEach((i=>{t.initJsClass(e,i)}))}))}this.rowCounter++,this.$field.dispatchEvent(new CustomEvent("initRow",{bubbles:!0,detail:{repeater:this,$row:e}})),this.form.triggerEvent("repeater:initRow",{repeater:this,$row:e})}async addRow(){const t=this.$addButton.getAttribute("data-add-repeater-row"),e=document.querySelector(`[data-repeater-template="${t}"]`),i=this.getNumRows();if(e){if(i>=this.maxRows)return;const t=this.rowCounter,s=e.innerHTML.replace(/__ROW__/g,t);let r=document.createElement("div");r.innerHTML=s.trim(),r=r.querySelector("div:first-of-type"),this.$field.querySelector("[data-repeater-rows]").appendChild(r),await new Promise((t=>setTimeout(t,50))),this.updateButton();const o=new CustomEvent("append",{bubbles:!0,detail:{repeater:this,row:r,form:this.$form}});this.$field.dispatchEvent(o),this.initRow(o.detail.row,!0)}}async addMultipleRows(t){for(let e=0;e<t;e++)await this.addRow()}removeRow(t){const e=t.target.closest("[data-repeater-row]");if(e){if(this.getNumRows()<=this.minRows)return;e.parentNode.removeChild(e);const t=new CustomEvent("remove",{bubbles:!0,detail:{repeater:this,row:e,form:this.$form}});this.$field.dispatchEvent(t),this.updateButton()}}getRows(){return this.$field.querySelectorAll("[data-repeater-row]")}getNumRows(){return this.getRows().length}updateButton(){var t,e;this.getNumRows()>=this.maxRows?(t=this.$addButton,e=this.disabledClass,t&&e&&("string"==typeof e&&(e=e.split(" ")),e.forEach((e=>{t.classList.add(e)}))),this.$addButton.setAttribute("disabled","disabled")):(!function(t,e){t&&e&&("string"==typeof e&&(e=e.split(" ")),e.forEach((e=>{t.classList.remove(e)})))}(this.$addButton,this.disabledClass),this.$addButton.removeAttribute("disabled"))}}}();