(()=>{"use strict";const e=window.wp.element,t=window.wp.i18n,r=window.ReactJSXRuntime,o=(0,e.createContext)(),l=({children:e})=>{const l={rollbackLabel:(0,t.__)("Rollback","wp-rollback"),notRollbackable:(0,t.__)("Rollback not available","wp-rollback")};return(0,r.jsx)(o.Provider,{value:l,children:e})},n=({theme:t})=>{const{rollbackLabel:l}=(()=>{const t=(0,e.useContext)(o);if(void 0===t)throw new Error("useUIText must be used within a UITextProvider");return t})(),n=`tools.php?page=${void 0!==window.wpRollbackPro?"wp-rollback-pro":"wp-rollback"}#/rollback/theme/${t}`;return(0,r.jsx)("a",{href:n,className:"button wpr-theme-rollback",children:l})};(new class{constructor(){this.observerConfig={childList:!0,subtree:!0},this.initialized=!1}initialize(){this.initialized||(this.initialized=!0,"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>this.setup()):this.setup())}setup(){this.setupThemeObserver(),this.handleInitialTheme(),this.setupThemeClickHandlers()}handleInitialTheme(){const e=wp.themes=wp.themes||{},t="undefined"!=typeof _wpThemeSettings?_wpThemeSettings:"";if(e.data=t,1===e.data?.themes?.length)return void this.renderThemeRollback(e.data.themes[0].id);const r=this.getThemeFromUrl();r&&this.renderThemeRollback(r)}setupThemeClickHandlers(){document.querySelectorAll(".theme").forEach(e=>{e.addEventListener("click",()=>{setTimeout(()=>{const e=this.getThemeSlug();e&&this.renderThemeRollback(e)},100)})})}setupThemeObserver(){const e=new MutationObserver(e=>{for(const t of e)"childList"===t.type&&t.addedNodes.forEach(e=>{if(this.isThemeOverlay(e)&&!this.isRollbackButtonPresent()){const e=this.getThemeSlug();e&&this.renderThemeRollback(e)}})}),t=document.querySelector(".wrap")||document.body;e.observe(t,this.observerConfig)}isThemeOverlay(e){return e.matches&&e.matches(".theme-overlay")||e.querySelector&&e.querySelector(".theme-overlay")}isRollbackButtonPresent(){return null!==document.querySelector(".wpr-theme-rollback")}getThemeSlug(){let e=this.getThemeFromUrl();if(!e){const t=document.querySelector(".theme-overlay");t?.dataset.theme&&(e=t.dataset.theme)}if(!e){const t=document.querySelector(".theme.active");t?.dataset.slug&&(e=t.dataset.slug)}return e}getThemeFromUrl(){return new URLSearchParams(window.location.search).get("theme")}renderThemeRollback(t){if(!t)return;const o=document.querySelector(".theme-wrap .theme-actions");if(!o)return;let i=document.getElementById("wpr-theme-rollback-container");i&&i.remove(),i=document.createElement("div"),i.id="wpr-theme-rollback-container",o.appendChild(i),(0,e.createRoot)(i).render((0,r.jsx)(l,{children:(0,r.jsx)(n,{theme:t})}))}}).initialize()})();