!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.FormieTable=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.static=t.static,this.initTable()}initTable(){const e=this.getRows();this.$field.table=this,this.$addButton=this.$field.querySelector("[data-add-table-row]"),this.$addButton&&(this.minRows=parseInt(this.$addButton.getAttribute("data-min-rows")),this.maxRows=parseInt(this.$addButton.getAttribute("data-max-rows")),this.form.addEventListener(this.$addButton,t("click"),(t=>{this.addRow(t)}))),e&&e.length&&e.forEach((t=>{this.initRow(t)})),this.$field.dispatchEvent(new CustomEvent("init",{bubbles:!0,detail:{table:this}}))}initRow(e){if(!e)return void console.error(e);const s=e.querySelector("[data-remove-table-row]");s&&this.form.addEventListener(s,t("click"),(t=>{this.removeRow(t)})),this.rowCounter++}addRow(t){t.target;const e=this.$addButton.getAttribute("data-add-table-row"),s=document.querySelector(`[data-table-template="${e}"]`),i=this.getNumRows();if(s){if(i>=this.maxRows)return;const t=this.rowCounter,e=s.innerHTML.replace(/__ROW__/g,t),o=document.createElement("tr");o.dataset.tableRow=!0,o.innerHTML=e,this.$field.querySelector("tbody").appendChild(o),setTimeout((()=>{this.updateButton();const t=new CustomEvent("append",{bubbles:!0,detail:{table:this,row:o,form:this.$form}});this.$field.dispatchEvent(t),this.initRow(t.detail.row)}),50)}}removeRow(t){const e=t.target.closest("[data-table-row]");if(e){if(this.getNumRows()<=this.minRows)return;e.parentNode.removeChild(e);const t=new CustomEvent("remove",{bubbles:!0,detail:{table:this,row:e,form:this.$form}});this.$field.dispatchEvent(t),this.updateButton()}}getRows(){return this.$field.querySelectorAll("[data-table-row]")}getNumRows(){return this.getRows().length}updateButton(){var t,e;this.$addButton&&(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")))}}}();