var FPF_Repeater=function(){function e(){this.repeaterWrapper=".fpf-repeater-field",this.init()}var t=e.prototype;return t.init=function(){e.initDragManager(),this.initEvents()},t.initEvents=function(){document.addEventListener("click",function(e){this.handleAdd(e),this.handleRemove(e)}.bind(this))},t.handleAdd=function(e){if(e.target.closest(".fpf-repeater-field-add-item-btn")){e.preventDefault();var t=e.target.closest(this.repeaterWrapper),r=t.querySelector(".template");if(r=r.cloneNode(!0),(r=this.replaceTemplateIDs(t,r)).classList.remove("template"),r.querySelectorAll(".fpf-colorpicker-item.no-render").forEach(function(e){e.classList.remove("no-render")}),r.querySelectorAll(".fpf-datepicker-item.no-render").forEach(function(e){e.classList.remove("no-render")}),r.innerHTML=r.innerHTML.replace(/data-showon-initialised/g,""),t.querySelectorAll(".items .fpf-repeater-item").length&&e.target.closest(".fpf-repeater-item")?e.target.closest(".fpf-repeater-item").parentNode.insertBefore(r,e.target.closest(".fpf-repeater-item").nextSibling):t.querySelector(".items").append(r),"function"==typeof FPF_Conditionize&&new FPF_Conditionize(r),"function"==typeof FPF_Colorpicker&&new FPF_Colorpicker,"function"==typeof FPF_Datepicker&&new FPF_Datepicker,"function"==typeof FPF_Textarea)new FPF_Textarea(r).init();var a={repeater_name:t.getAttribute("data-name"),initial_open:!0,repeater_item:r};r.repeater_data=a;var i=new CustomEvent("fpf_repeater_add",{detail:a,cancelable:!0});document.dispatchEvent(i),e.preventDefault()}},e.initDragManager=function(){var e=document.querySelectorAll(".fpf-repeater-field:not(.initialized)");if(e){var t=e,r=Array.isArray(t),a=0;for(t=r?t:t[Symbol.iterator]();;){var i;if(r){if(a>=t.length)break;i=t[a++]}else{if((a=t.next()).done)break;i=a.value}var n=i;new Sortable(n.querySelector(".items"),{animation:150,handle:".fpf-repeater-field-move-item-btn"}),n.classList.add("initialized")}}},t.handleRemove=function(e){var t=e.target.closest(".fpf-repeater-field-remove-item-btn");t&&(t.classList.contains("no-confirm")||confirm(fpf_js_object.FPF_ARE_YOU_SURE_YOU_WANT_TO_DELETE_THIS_ITEM))&&(e.target.closest(".fpf-repeater-item").remove(),e.preventDefault())},t.replaceTemplateIDs=function(e,t){var r=this.getNewRepeaterItemID(e);t.querySelectorAll("[name]").forEach(function(e){var t=e.getAttribute("name");t=t.replace("[ITEM_ID]","["+r+"]"),e.setAttribute("name",t)}),t.querySelectorAll("[data-name]").forEach(function(e){var t=e.getAttribute("data-name");t=t.replace("[ITEM_ID]","["+r+"]"),e.setAttribute("data-name",t)}),t.querySelectorAll("[data-showon]").forEach(function(e){var t=e.getAttribute("data-showon");t=t.replace(/\[ITEM_ID\]/g,"["+r+"]"),e.setAttribute("data-showon",t),e.classList.remove("fpf-run-condition")}),t.querySelectorAll("[for]").forEach(function(e){var t=e.getAttribute("for");t=t.replace(/\[ITEM_ID\]/g,"["+r+"]"),e.setAttribute("for",t)}),t.querySelectorAll("[id]").forEach(function(e){var t=e.getAttribute("id");t=t.replace(/\[ITEM_ID\]/g,"["+r+"]"),e.setAttribute("id",t)});var a=t.getAttribute("data-item-id");return a=a.replace(/\[ITEM_ID\]/g,r),t.setAttribute("data-item-id",a),t},t.getNewRepeaterItemID=function(e){var t=e.querySelectorAll(".items .fpf-repeater-item"),r=0;return t.forEach(function(e){var t=parseInt(e.getAttribute("data-item-id"));r<t&&(r=t)}),0===r?1:r+1},e}();FPF_Helper.onReady(function(){new FPF_Repeater});

