(()=>{"use strict";!function(){const e=new URLSearchParams(window.location.search),t="1"===e.get("gb_overlay_context"),n="1"===e.get("gb_embedded_overlay");if(!t)return;let o=!1,i=!1,a=null===e.get("post")&&window.location.pathname.includes("post-new.php");function r(e){const t=e.target.closest("a[href]");if(!t)return;const n=t.getAttribute("href");if(!n)return;const o=t.closest("#wpadminbar, .wp-admin-bar"),i=t.closest("#adminmenu");if(o||i)return e.preventDefault(),e.stopPropagation(),u(n),l("navigation_attempt",{url:n,type:"admin_navigation"}),!1;c(n,t)||(e.preventDefault(),e.stopPropagation(),u(n),l("navigation_attempt",{url:n}))}function s(e){const t=e.target.action||window.location.href;c(t)||d(t)&&(e.preventDefault(),u(t))}function c(t,n=null){if(!t)return!0;if(t.startsWith("#"))return!0;if(t.startsWith("?")||t.includes("ajax"))return!0;const o=e.get("post");if(o&&t.includes(`post=${o}`))return!0;if(t.includes("admin-ajax.php"))return!0;if(t.includes("/wp-json/"))return!0;if(t.includes("async-upload.php")||t.includes("media-upload.php"))return!0;if(n){const e=["save","update","publish"],t=n.textContent?.toLowerCase()||"",o=n.className?.toLowerCase()||"";if(e.some((e=>t.includes(e)||o.includes(e))))return!0}return!1}function d(e){return!c(e)}function u(e){confirm((o?"You have unsaved changes. This will leave the overlay editor.":"This will leave the overlay editor.")+"\n\nWould you like to open this page in a new tab instead?")&&window.open(e,"_blank")}function l(e,t={}){try{window.parent.postMessage({type:`gb_${e}`,source:"modal_iframe",timestamp:Date.now(),...t},"*")}catch(e){}}function w(){if(i)return;!function(){document.addEventListener("click",r,!0),document.addEventListener("submit",s,!0);try{const e=window.location.assign,t=window.location.replace;e&&"function"==typeof e&&Object.defineProperty(window.location,"assign",{value(t){d(t)?u(t):e.call(window.location,t)},writable:!0,configurable:!0}),t&&"function"==typeof t&&Object.defineProperty(window.location,"replace",{value(e){d(e)?u(e):t.call(window.location,e)},writable:!0,configurable:!0})}catch(e){}const e=history.pushState;history.pushState=function(t,n,o){o&&d(o)?u(o):e.call(history,t,n,o)},window.addEventListener("beforeunload",(function(e){if(o){const t="You have unsaved changes. Are you sure you want to leave?";return e.preventDefault(),t}}));const t=window.open;window.open=function(e,n,o){return t.call(window,e,n,o)}}(),setTimeout(p,100),setTimeout(p,500),setTimeout(p,1e3);const e=function(){if(!window.wp?.data)return;const{select:e,subscribe:t}=window.wp.data;let n=0,i=null,r=!1;const s=t((()=>{try{const t=e("core/editor")?.getCurrentPostId?.(),s=e("core/editor")?.getCurrentPostType?.();if(t&&s){const c=e("core/editor")?.getEditedPostAttribute?.("title")||"",d=e("core/editor")?.getEditedPostAttribute?.("content")||"",u=e("core/editor")?.getEditedPostAttribute?.("meta")||{},w=JSON.stringify({title:c,content:d,meta:u});if(!r)return i=w,void(r=!0);const p=e("core/editor")?.isSavingPost?.(),f=e("core/editor")?.isAutosavingPost?.(),v=e("core/editor")?.getEditedPostAttribute?.("modified");if(!p&&!f&&v&&v!==n){if(n=v,i=w,o&&(o=!1,l("unsaved_changes",{hasChanges:!1})),"gblocks_overlay"===s){l("overlay_saved",{overlayId:t,overlayTitle:c,isNew:a}),a=!1;try{const e=new CustomEvent("gb-overlay-saved-global",{detail:{overlayId:t,overlayTitle:c},bubbles:!0});window.top.dispatchEvent(e)}catch(e){console.error("Error dispatching global event:",e)}}return}if(p||f)return;const m=w!==i;m!==o&&(o=m,l("unsaved_changes",{hasChanges:m}))}}catch(e){}}));return()=>{s&&s()}}();n||function(){const e=document.createElement("div");e.id="gb-overlay-context-indicator",e.innerHTML='\n\t\t\t<span>📱 Overlay Editor</span>\n\t\t\t<button id="gb-overlay-close-btn">Done</button>\n\t\t',document.body.appendChild(e),e.querySelector("#gb-overlay-close-btn").addEventListener("click",f)}(),i=!0,l("iframe_ready",{url:window.location.href,title:document.title}),window.addEventListener("beforeunload",(function(){e&&e()}))}function p(){["#wpadminbar a",".wp-admin-bar a","#wp-admin-bar-wp-logo a","#wp-admin-bar-site-name a","#wp-admin-bar-view a","#wp-admin-bar-new-content a","#adminmenu a","#adminmenu .wp-menu-name"].forEach((e=>{document.querySelectorAll(e).forEach((e=>{e.addEventListener("click",(function(e){e.preventDefault(),e.stopImmediatePropagation();const t=this.getAttribute("href")||this.closest("a")?.getAttribute("href");return t&&u(t),!1}),!0)}))}))}function f(){if(!o||window.confirm("You have unsaved changes. Are you sure you want to close the overlay editor?")){l("close_overlay",{reason:"user_requested",hasUnsavedChanges:o});try{window.close()}catch(e){}}}window.addEventListener("message",(function(e){e.data.type})),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",w):w(),window.wp?.domReady&&window.wp.domReady(w),setTimeout(w,1e3)}()})();