(()=>{"use strict";const t=window.wp.i18n;({dropdowns:{},getTags:t=>window.sugarCalendarBookingsSmartTags?.[t]||{},init(){this.registerPlugin()},registerPlugin(){const s=this;tinymce.PluginManager.add("smarttags",(function(n){n.addButton("smarttags",{text:"",tooltip:(0,t.__)("Smart Tags","sugar-calendar-bookings-scheduling-appointments-lite"),icon:!1,image:`${window.sugarCalendarBookingsPluginUrl}assets/images/icon-tags.svg`,classes:"sugar-bookings-smart-tags-mce-button",onclick:()=>s.toggleDropdown(n)}),n.on("click",(()=>s.closeDropdown(n.id)))}))},toggleDropdown(t){const{id:s}=t;this.dropdowns[s]?this.closeDropdown(s):(Object.keys(this.dropdowns).forEach((t=>this.closeDropdown(t))),this.showDropdown(t))},showDropdown(t){const{id:s}=t,n=this.getTags(s);if(!n||0===Object.keys(n).length)return;const e=this.createDropdownElement(s),o=t=>{e.contains(t.target)||t.target.closest('.mce-btn, button[class*="mce-"]')||this.closeDropdown(s)};this.dropdowns[s]={element:e,editor:t,clickOutsideHandler:o},this.setupDropdownHandlers(e,n,s),document.body.appendChild(e),setTimeout((()=>document.addEventListener("mousedown",o)),100)},createDropdownElement(s){const n=document.querySelector(`#${s}_ifr`)?.closest(".mce-tinymce");if(!n)return null;const{top:e,right:o}=n.getBoundingClientRect(),a=document.createElement("div");return a.id=`smart-tags-dropdown-${s}`,a.className="sugar-bookings-smart-tags-dropdown openDown",a.style.cssText=`\n\t\t\tposition: absolute;\n\t\t\ttop: ${e+window.scrollY+12}px;\n\t\t\tright: ${window.innerWidth-o+window.scrollX+8}px;\n\t\t`,a.innerHTML=`\n\t\t\t<div class="sugar-bookings-smart-tags-title">${(0,t.__)("Smart Tags","sugar-calendar-bookings-scheduling-appointments-lite")}</div>\n\t\t\t<div class="sugar-bookings-smart-tags-search-container">\n\t\t\t\t<input \n\t\t\t\t\ttype="search" \n\t\t\t\t\tclass="sugar-bookings-smart-tags-search-input" \n\t\t\t\t\tplaceholder="${(0,t.__)("Search","sugar-calendar-bookings-scheduling-appointments-lite")}"\n\t\t\t\t\tautocomplete="off"\n\t\t\t\t/>\n\t\t\t</div>\n\t\t\t<ul class="sugar-bookings-smart-tags-list"></ul>\n\t\t`,a},setupDropdownHandlers(t,s,n){const e=t.querySelector(".sugar-bookings-smart-tags-search-input"),o=t.querySelector(".sugar-bookings-smart-tags-list");this.renderTags(o,s),e.addEventListener("input",(t=>{const n=t.target.value.toLowerCase().trim(),e=this.filterTags(s,n);this.renderTags(o,e,n)})),o.addEventListener("click",(t=>{const s=t.target.closest("button[data-tag]");s&&(t.preventDefault(),t.stopPropagation(),this.insertSmartTag(n,s.getAttribute("data-tag")),this.closeDropdown(n))})),e.focus()},filterTags(t,s){if(!s)return t;const n=t=>t.label.toLowerCase().includes(s)||t.value.toLowerCase().includes(s);return Object.keys(t).reduce(((s,e)=>{const o=t[e]?.filter(n);return o?.length&&(s[e]=o),s}),{})},renderTags(s,n,e=""){const o=Object.keys(n).filter((t=>n[t]?.length)).map((t=>`\n\t\t\t\t\t<li data-value="0">\n\t\t\t\t\t\t<span class="sugar-bookings-smart-tags-heading">${t.replace(/_/g," ").split(" ").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join(" ")}</span>\n\t\t\t\t\t</li>\n\t\t\t\t\t${n[t].map((t=>`\n\t\t\t\t\t\t<li class="sugar-bookings-smart-tags-item" data-value="${t.value}">\n\t\t\t\t\t\t\t<button type="button" class="sugar-bookings-smart-tags-button" data-tag="${t.value}">\n\t\t\t\t\t\t\t\t${t.label}\n\t\t\t\t\t\t\t</button>\n\t\t\t\t\t\t</li>\n\t\t\t\t\t`)).join("")}\n\t\t\t\t`)).join("");s.innerHTML=o||(e?`<div class="sugar-bookings-smart-tags-no-results">${(0,t.__)("Sorry, no results found","sugar-calendar-bookings-scheduling-appointments-lite")}</div>`:"")},closeDropdown(t){const s=this.dropdowns[t];s&&(s.element?.remove(),document.removeEventListener("mousedown",s.clickOutsideHandler),delete this.dropdowns[t])},insertSmartTag(t,s){const n=this.dropdowns[t];n?.editor?.insertContent(` {${s}} `)}}).init()})();