((t,e,i,s,o,n)=>{"use strict";let a=!!n.is_gutenberg_editor;if(null!==i.querySelector("#page-builder.themify_write_panel"))a=!1,i.body.classList.remove("themify-gutenberg-editor");else if(!a)return;const r={async init(){if(a&&!i.tfId("tb_canvas_block"))return void e.on("tb_canvas_loaded",(()=>{this.init()}),!0);e.is_builder_active=!0,await Promise.all([t.loadJson(),t.jsModuleLoaded()]),this.render(),"#builder_active"===o.location.hash&&(o.location.hash="",this.scrollToBuilder());let r=i.tfClass("tb_wp_editor_holder")[0],l=s("a",{href:"#",class:"button tb_switch_frontend"},n.i18n.label.switchToFrontendLabel);if(r){l=r.tfTag("a")[0];t.Builder.get().el.tfClass("active_module")[0]&&r.classList.add("tb_active_holder")}else i.querySelector("#postdivrich #wp-content-media-buttons")?.appendChild(l);l?.isConnected&&l.tfOn(e.click,(i=>{i.preventDefault(),e.triggerEvent(t.ToolBar.el.tfClass("switch")[0],i.type)})),t.Registry.trigger(t.Builder.get(),"tb_init"),e.trigger("themify_builder_ready"),e.on("tb_scroll_to_builder",(()=>{this.scrollToBuilder()})),this.t(),t.is_builder_ready=!0},scrollToBuilder(){const s=t.ToolBar.el;i.tfId("page-buildert")?.click(),a?t.Utils.scrollTo(s.getRootNode().host):e.scrollTo(s.getBoundingClientRect().top-(i.tfId("wpadminbar")?.offsetHeight||0))},render(){const e=n.post_ID,s=t.Builder.backendModeHolder(e);a?i.tfId("tb_canvas_block").replaceChildren(s):i.tfId("tb_builder_placeholder").replaceWith(s);const o=t.Helper.correctBuilderData(n.builder_data);ThemifyStyles.init(t.styleData,t.breakpointsReverse,e,n.gutters),new t.Builder(s,o,n.custom_css)},t(){let s=!1;const o=i.querySelectorAll("input#publish,input#save-post,button.editor-post-publish-button__button,.editor-post-save-draft"),n=t=>{if(!1===s){s=!0,t.stopImmediatePropagation(),a||t.preventDefault();const e=t.currentTarget,i=t.type;this.saveCallback(e).finally((()=>{e.click(),setTimeout((()=>{e.tfOn(i,n,{once:!0}),s=!1}),7e3)}))}};for(let t=o.length-1;t>-1;--t)o[t].tfOn(e.click,n,{once:!0});e.on("tb_switch_frontend",(t=>{this.switchFrontend(t)})),setTimeout((()=>{t.Module.loadBackendLightbox("preload")}),1500)},saveCallback:e=>(a?e.setAttribute("aria-disabled","true"):e.classList.add("disabled"),t.ToolBar.save().then((()=>{t.undoManager.reset(),a?e.setAttribute("aria-disabled","false"):e.classList.remove("disabled")}))),switchFrontend(t){let n,r=i.tfId("original_post_status")?.value;n="publish"===r?a?i.tfClass("editor-post-publish-button__button")[0]:i.tfId("publish"):a?i.tfClass("editor-post-save-draft")[0]:i.tfId("save-post"),n&&this.saveCallback(n).then((()=>{if(a){const e=i.tfClass("editor-post-publish-button")[0];e&&new MutationObserver((e=>{const s=e[0].target;setTimeout((()=>{s.classList.contains("is-busy")||i.tfClass("editor-post-saved-state")[0]||(o.location.href=t)}),150)})).observe(e,{attributes:!0})}else{const t=!i.tfId("tb_switch_frontend")&&i.tfId("page-builder");t?.appendChild(s("input",{type:"hidden",id:"tb_switch_frontend",name:"tb_switch_frontend",value:"yes"}))}e.triggerEvent(n,e.click)}))}},l=()=>{if(r.init(),"function"==typeof o.icl_copy_from_original){jQuery.ajaxPrefilter(((s,o)=>{if(o.data&&"string"==typeof o.data&&o.data.includes("icl_ajx_action=copy_from_original")){const a=s.success,r=new URLSearchParams(o.data);s.success=s=>{const o=i.tfId("tb_canvas_block");o&&(o.style.display="none",i.body.appendChild(o)),a(s),o&&(i.querySelector('#editor [data-type="themify-builder/canvas"]')?.replaceChildren(o),o.style.display=""),e.fetch({action:"themify_builder_icl_copy_from_original",source_page_id:r.get("trid"),source_page_lang:r.get("lang")}).then((e=>{e.success&&t.Builder.get().reLoad(e.data,n.post_ID)}))}}}))}};"complete"===i.readyState?l():o.tfOn("load",l,{once:!0,passive:!0})})(tb_app,Themify,document,tb_createElement,window,themifyBuilder);