(()=>{"use strict";var e={n:t=>{var s=t&&t.__esModule?()=>t.default:()=>t;return e.d(s,{a:s}),s},d:(t,s)=>{for(var a in s)e.o(s,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:s[a]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)};const t=window.wp.apiFetch;var s=e.n(t);const a=class{constructor(){this.siteUrl=window.location.origin,this.allowedType="om-dam",this.messageListener=this.handlePostMessage.bind(this),this.hasListeners=!1}setFrame(e){this.frame=e}setMultiple(e=!1,t=!1){this.context=e?"multiple":"single",this.append=t}setMediaFrame(e){this.mediaFrame=e}attachListeners(){if(this.hasListeners)return;const e=this;if(this.frame.classList.contains("loaded"))return window.addEventListener("message",e.messageListener),void(this.hasListeners=!0);this.frame.addEventListener("load",(()=>{let t=document.querySelector(".om-dam-loader:not([style])");t&&(t.style.display="none"),this.frame.style.display="",this.frame.classList.add("loaded"),window.addEventListener("message",e.messageListener),this.hasListeners=!0}))}detachListeners(){this.hasListeners&&(window.removeEventListener("message",this.messageListener),this.hasListeners=!1)}handlePostMessage(e){if(this.isValidEvent(e))if("getUrl"!==e.data.action){if("importImages"===e.data.action){if(1>e.data.images.length)return;this.insertImages(e.data.images),this.detachListeners()}}else this.sendSiteUrl()}async insertImages(e){const t=window.optmlMediaModal.routes.insert_images;this.sendMessage({status:"importing"});const a=await s()({method:"POST",path:t,data:{images:e}});if(!a.code||"success"!==a.code)return void this.sendMessage({error:"import"});if(!a.data||1>a.data.length)return void this.sendMessage({error:"importResponse"});const i=this.mediaFrame.state().get("selection");this.append||i.reset();let n=[];Object.values(a.data).forEach(((e,t)=>{wp.media.attachment(e).fetch().then((e=>{n.push(e),n.length===a.data.length&&(this.sendMessage({status:"done"}),i.add(n),this.clickInsertButton())}))}))}clickInsertButton(){document.querySelectorAll(".media-button-select, .media-button-insert, .media-button-gallery").forEach((e=>{e.disabled&&(e.disabled=!1),setTimeout((()=>{this.sendMessage({status:"done"}),e.click()}),250)}))}sendSiteUrl(){this.sendMessage({siteUrl:this.siteUrl,context:this.context})}sendMessage(e={}){this.frame.contentWindow.postMessage({type:"om-dam",...e},"*")}isValidEvent(e){return!!e.data&&!!e.data.type&&this.allowedType===e.data.type&&!!e.data.action}},i=e=>e.extend({omDamMessageHandler:new a,bindHandlers:function(){e.prototype.bindHandlers.apply(this,arguments),this.on("content:render:optimole",this.renderContent,this),this.on("open",this.reattach,this),this.on("toolbar:create:gallery-add",this.setAppend,this),this.on("content:deactivate:optimole",this.detach,this),this.on("reset",this.detach,this),this.on("select",this.detach,this),this.on("close",(()=>{this.toggleModalClass(!1)}),this)},browseRouter:function(t){e.prototype.browseRouter.apply(this,arguments),t.set({optimole:{text:"Optimole",priority:60}})},renderContent:function(){const e=this,t=wp.media.View.extend({template:wp.template("optimole-dam"),className:"om-dam-wrap",toolBar:null,render:function(){return this.$el.html(this.template()),e.omDamMessageHandler.detachListeners(),e.omDamMessageHandler.setFrame(this.$el.find("#om-dam")[0]),e.omDamMessageHandler.setMediaFrame(e),e.omDamMessageHandler.setMultiple(e?.options?.multiple||!1,e.append||!1),e.omDamMessageHandler.attachListeners(),this}});this.damView=new t({controller:this,model:this.state()}),this.toggleModalClass(!0),this.content.set(this.damView)},detach:function(){this.omDamMessageHandler.detachListeners(),this.toggleModalClass(!1)},reattach:function(){if(!this.omDamMessageHandler.frame)return;this.omDamMessageHandler.attachListeners();const{content:e}=this;e.mode&&"optimole"===e.mode()&&this.toggleModalClass(!0)},setAppend:function(){this.append=!0},toggleModalClass:function(e=!0){e?document.body.classList.add("om-dam-modal"):document.body.classList.remove("om-dam-modal")}});window.addEventListener("DOMContentLoaded",(()=>{wp?.media?.view&&(wp.media.view.MediaFrame.Select=i(wp.media.view.MediaFrame.Select),wp.media.view.MediaFrame.Post=i(wp.media.view.MediaFrame.Post))}))})();