(()=>{var o=class{constructor(t){this.eventSource=t,this.events=new Set}registerEvent(t,e){typeof e=="function"&&(this.events[t]||(this.events[t]=new Set),this.events[t].add(e))}unregisterEvent(t,e){this.events[t]&&this.events[t].delete(e)}fireEvent(t,e){this.events[t]&&this.events[t].forEach(function(s,n){s(e,{eventSource:this.eventSource,eventName:t,data:e})}.bind(this))}};var h=class i{static ___={recipients:new Set};constructor(t){t=typeof t=="string"?{channel:t}:t||{},this.id=("MSG_"+[1e16]).replace(/[018]/g,e=>(e^Math.random()*16>>e/4).toString(16)),this.channels=new Set,this.recipients=new Set,t.channel&&this.subscribe(t.channel),window.opener&&i.___.recipients.add(window.opener),this.events=new o(this),window.addEventListener("message",this.__handleMessage.bind(this))}addRecipient(t){i.___.recipients.add(t)}subscribe(t){t&&typeof t=="string"&&(console.info(`Messenger ${this.id} connected to ${t}`),this.channels.add(t))}unsubscribe(t){this.channels.delete(t)}onMessage(t,e=null){e=typeof e=="string"?[e]:e,e=e&&e.length?e:[],this.events.registerEvent("message",function(s,n,l,d){(n.length===0||n.includes(l.channel))&&s(l.message,l.channel)}.bind(this,t,e))}__handleMessage(t){let e={};try{e=typeof t.data=="string"?JSON.parse(t.data):t.data||{}}catch{}e.format==="messenger"&&(this.send(e.message,e.channel,t.source,!0),this.channels.has(e.channel)&&this.events.fireEvent("message",e))}send(t,e=null,s=null,n=!1){if(e&&!this.channels.has(e)){if(!n)throw`Messenger.send not subscribed to ${e}`}else{var l=e?new Set([e]):this.channels;l.size>0&&l.forEach(function(d,a,_,f){(a===null||a===f)&&this.__sendToWindows(JSON.stringify({format:"messenger",version:1,emiter:this.id,channel:f,message:d}),_)}.bind(this,t,e,s))}}__sendToWindows(t,e){i.___.recipients.forEach(function(s,n){n&&typeof n.postMessage=="function"&&s!==n&&!n.closed&&n.postMessage(t,"*")}.bind(this,e))}};var r=class i{static statics={opened:new Set,openLimit:null,instances:new Set};constructor(t){t=t||{},t.widget=t.widget||{},this.statics={id:("10000000-1000-4000-8000"+-1e11).replace(/[018]/g,e=>(e^Math.random()*16>>e/4).toString(16)),widget:{url:t.widget.url||t.widgetUrl||null,resizable:t.widget.resizable||t.widgetResizable||!0,scrollbars:t.widget.scrollbars||t.widgetScrollbars||!0,dependent:t.widget.dependent||t.widgetDependent||!0,width:t.widget.width||t.widgetWidth||600,height:t.widget.height||t.widgetHeight||400,top:t.widget.top||t.widgetTop||null,left:t.widget.left||t.widgetLeft||null,window:null},defaults:void 0,keepDefaults:!1,closeDetectionInterval:null},this.statics.events=new o(this),this.statics.messenger=new h({channel:this.statics.id}),this.statics.messenger.onMessage(this.___handleMessage.bind(this)),i.statics.instances.add(this),this.___on("widget.ready",this.___postDefaults.bind(this)),window.addEventListener("beforeunload",this.close.bind(this),!0)}static setOpenLimit(t){i.statics.openLimit=t,i.openLimitApply()}static openLimitApply(t=!1){if(i.statics.openLimit>0){var e=i.statics.openLimit,s=i.statics.opened.size+(t?1:0);if(s>e){var n=Array.from(i.statics.opened)[0];n&&(n.close(),i.openLimitApply(t))}}}static invokeAll(t,e){typeof t=="function"?i.statics.instances.forEach(function(s){t(s)}):typeof t=="string"&&i.statics.instances.forEach(function(s){return typeof s?.[t]=="function"?s?.[t](...e):null})}___getId(){return this.statics.id}getId(){return this.___getId()}___on(t,e){this.statics.events.registerEvent(t,e)}on(t,e){this.___on(t,e)}___off(t,e){this.statics.events.unregisterEvent(t,e)}off(t,e){this.___off(t,e)}___fire(t,e,s=null){(s===null||s==="broadcast")&&this.statics.messenger.send({eventName:t,data:e}),(s===null||s==="local")&&this.statics.events.fireEvent(t,e)}fire(t,e,s=null){this.___fire(t,e,s)}___open(t=null,e=void 0){this.close(),i.openLimitApply(!0),t=t||{},t.url=t.url||this.statics.widget.url,t.resizable=t.resizable||this.statics.widget.resizable,t.scrollbars=t.scrollbars||this.statics.widget.scrollbars,t.dependent=t.dependent||this.statics.widget.dependent,t.width=t.width||this.statics.widget.width,t.height=t.height||this.statics.widget.height,t.top=t.top||this.statics.widget.top,t.left=t.left||this.statics.widget.left,t.top=t.top||(screen.height-t.height)/2,t.left=t.left||(screen.width-t.width)/2;var s=[];s.push(`resizable=${t.resizable?"yes":"no"}`),s.push(`scrollbars=${t.scrollbars?"yes":"no"}`),s.push(`dependent=${t.dependent?"yes":"no"}`),t.width&&s.push(`width=${t.width}`),t.height&&s.push(`height=${t.height}`),t.top&&s.push(`top=${t.top}`),t.left&&s.push(`left=${t.left}`);var n=t.url;if(!n)throw"widget url parameter missing";e!==void 0&&(this.statics.defaults=e),this.statics.widget.window=window.open(n,this.statics.id,s.join(",")),this.statics.messenger.addRecipient(this.statics.widget.window),i.statics.opened.add(this),this.___fire("widget.open",null,"local"),this.___startDetectionLoop()}open(t,e=null){this.___open(t,e)}___close(){this.___stopDetectionLoop(),i.statics.opened.delete(this),this.statics.widget.window&&!this.statics.widget.window.closed&&(this.statics.widget.window.close(),this.statics.widget.window=null,this.statics.keepDefaults||(this.statics.defaults=void 0),this.___fire("widget.close",null,"local"))}close(){this.___close()}___handleMessage(t){t.eventName&&this.___fire(t.eventName,t.data,"local")}___postDefaults(){this.statics.defaults&&this.statics.messenger.send({eventName:"client.postDefaults",data:this.statics.defaults})}postDefaults(){this.___postDefaults()}___setDefaults(t){this.statics.defaults=t,this.___postDefaults()}setDefaults(t){this.___setDefaults(t)}___keepDefaults(t){this.statics.keepDefaults=t}keepDefaults(t){this.___keepDefaults(t)}___stopDetectionLoop(){clearInterval(this.statics.closeDetectionInterval),this.statics.closeDetectionInterval=null}___startDetectionLoop(){this.statics.widget.window.closed?(this.___close(),this.___fire("widget.close",null,"local")):this.statics.closeDetectionInterval||(this.statics.closeDetectionInterval=setInterval(this.___startDetectionLoop.bind(this),10))}};var c=class extends r{constructor(t){var e=typeof t=="object"?t:{token:t};super(e),this.token=e.token||null,this.baseURL=e.baseURL!==void 0?e.baseURL:"https://hapidam.com",this.on("select",this.__ckEditorSelect.bind(this))}open(t={}){if(!this.token)throw"Hapidam.open invalid Token";typeof t=="boolean"&&(t=t?{format:"json"}:{});var e={url:`${this.baseURL}/widget/${this.token}${this.__encodeQueryData(t)}`,height:window.screen.height*.8>1080?1080:window.screen.height*.8,width:window.screen.width*.8>1500?1500:window.screen.width*.8};this.___open(e)}openBuilder(t=null,e={}){if(!this.token)throw"Hapidam.open invalid Token";var s={url:`${this.baseURL}/widget-builder/${this.token}${this.__encodeQueryData(e)}`,height:window.screen.height*.8>1080?1080:window.screen.height*.8,width:window.screen.width*.8>1500?1500:window.screen.width*.8};this.___open(s,t)}connect(){var t={url:`${this.baseURL}/widget-connect`,height:window.screen.height*.8>740?740:window.screen.height*.8,width:window.screen.width*.8>600?600:window.screen.width*.8};this.___open(t)}initCKEditor(){return`${this.baseURL}/widget/${this.token}?hapidamClientId=${this.getId()}`}setToken(t){this.token=t}__encodeQueryData(t){let e=[];for(let s in t)e.push(encodeURIComponent(s)+"="+encodeURIComponent(t[s]));return e.length?"?"+e.join("&"):""}__ckEditorSelect(t){if(t=t||{},t.funcNum){let e=null;t.type==="image"&&(e=t.selectedFormat||t.display.native.lg),typeof window?.CKEDITOR?.tools?.callFunction=="function"&&window.CKEDITOR.tools.callFunction(t.funcNum,t.type==="image"?e:t.display.original)}}};window.Hapidam=c;})(); //# sourceMappingURL=/client.js.map