import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var mz="No surface found. Can not attach frame!",nz="SIMID:Media:timeupdate",oz="Starting creative",pz=function(a,b,c){return{type:b,sessionId:a.h,messageId:a.l++,timestamp:Date.now(),args:c}},sz=function(a,b){return new Promise((c,d)=>{new qz(a.j,c,d,b);rz(a,b)})},tz=function(a){const b=pz(a,"SIMID:Player:startCreative",void 0);sz(a,b).then(()=>{var c=a.f;c.g.info(oz);c.m.trigger(new _.F(_.Ef));a.a.style.opacity="1"}).catch(c=>{a.unload();a.f.g.error("Error while starting creative",
c)})},vz=function(a,b){_.x(function*(){a.h=b.sessionId;uz(a,b);try{{var c=a.f;const f=c.getSurface(),g=c.getContainer();var d={environmentData:{videoDimensions:{x:0,y:0,height:g.clientHeight,width:g.clientWidth},creativeDimensions:{x:0,y:0,height:g.clientHeight,width:g.clientWidth},fullscreen:f.isFullscreen(),fullscreenAllowed:!0,variableDurationAllowed:!1,skippableState:"notSkippable",version:"1.1",skipoffset:"",siteUrl:window.location.toString(),appId:"",useragent:"",deviceId:"",muted:c.m.isMuted()||
!1,volume:c.m.getVolume()||0,navigationSupport:"adHandles",closeButtonSupport:"adHandles",nonlinearDuration:c.f.duration},creativeData:{adParameters:c.f.adParameters||"",clickThruUri:"",clickThruUrl:""}}}{const f=pz(a,"SIMID:Player:init",d);var e=sz(a,f)}yield e;a.g.info("Player Init acknowledged");a.f.g.info("SIMID Player initialized")}catch(f){a.g.error("Error while initializing",f),a.f.g.error("Error while initializing SIMID player: ",f),a.unload()}})},uz=function(a,b){rz(a,pz(a,"resolve",{messageId:b.messageId,
value:void 0}))},rz=function(a,b){a.h?(b.type!==nz&&a.g.info(`Posting message [${b.messageId}] '${b.type}':`,b.args),a.a.contentWindow&&a.a.contentWindow.postMessage(JSON.stringify(b),"*")):a.g.warn(`WARNING: no session available to send message '${b.type}'`)},wz=function(a,b,c){return new ResizeObserver(()=>{if(a.a.h){var d=a.a,e={videoDimensions:{x:0,y:0,height:b.clientHeight,width:b.clientWidth},creativeDimensions:{x:0,y:0,height:b.clientHeight,width:b.clientWidth},fullscreen:c.isFullscreen()};
e=pz(d,"SIMID:Player:resize",e);rz(d,e)}})},xz=function(a,b){a.g.info("SIMID Session created");a.a=b},yz=function(a){a.g.info("Stopping creative");a.m.getSurface().getMedia().style.transition=".5s";setTimeout(()=>{a.m.getSurface().getMedia().style.transition=""},500);a.m.getSurface().getMedia().style.left="";a.m.getSurface().getMedia().style.top="";a.m.getSurface().getMedia().style.width="";a.m.getSurface().getMedia().style.height="";a.a.a.style.left="";a.a.a.style.top="";a.a.a.style.width="";a.a.a.style.height=
"";a.h=!1;a.m.trigger(new _.F(_.Ff))},zz=function(a,b){var c=a.getContainer();a=c.clientWidth;c=c.clientHeight;return{top:`${b.y/c*100}%`,left:`${b.x/a*100}%`,width:`${b.width/a*100}%`,height:`${b.height/c*100}%`}},Az=class{constructor(a,b){this.f=a;this.w=b;this.h="";this.l=0;this.j=new Map;this.a=document.createElement("iframe");this.a.style.opacity="0";a=this.w.style;a.className?this.a.classList.add(a.className):(this.a.style.border=_.Be,this.a.style.backgroundColor="transparent",this.a.style.transition=
"opacity .5s",this.a.style.transitionDelay=".5s",this.a.style.pointerEvents=_.Be,this.a.style.overflow=_.$d);this.a.classList.add(_.Yc);this.o=c=>{if(c.source===this.a.contentWindow&&(c=(c=c.data)?typeof c===_.Nf?JSON.parse(c):c:null))if(c.type)switch(this.g.info(`Received message [${c.messageId}] '${c.type}':`,c.args),c.type){case "createSession":vz(this,c);break;case "resolve":c=c.args;var d=this.j.get(c.messageId);d&&d.resolve(c);break;case "reject":c=c.args;(d=this.j.get(c.messageId))&&d.reject(c);
break;case "SIMID:Creative:requestResize":d=this.f;var e=c.args;d.g.info("Creative requested resize",e);if(d.m&&d.m.getSurface()&&d.m.getSurface().getMedia()&&e.mediaDimensions){d.h?d.m.getSurface().getMedia().style.transition="":(d.m.getSurface().getMedia().style.transition="left .5s, top .5s,width .5s, height .5s",d.h=!0);const f=zz(d,e.mediaDimensions);d.m.getSurface().getMedia().style.left=f.left;d.m.getSurface().getMedia().style.top=f.top;d.m.getSurface().getMedia().style.width=f.width;d.m.getSurface().getMedia().style.height=
f.height}e.creativeDimensions&&(e=zz(d,e.creativeDimensions),d.a.a.style.left=e.left,d.a.a.style.top=e.top,d.a.a.style.width=e.width,d.a.a.style.height=e.height);uz(this,c)}else this.g.warn("WARNING: received message without 'type' property")};this.g=new _.S("clpp.simid.SimidPlayer");xz(this.f,this)}load(a){window.addEventListener(_.re,this.o);this.h="";this.l=0;const b=this.f.url();this.g.info(`Loading "${b}" as src of iFrame`);this.a.src=b;this.g.info("Attaching iFrame");a.appendChild(this.a)}unload(){yz(this.f);
this.h=this.a.src="";this.l=0;this.a.style.opacity="0";window.removeEventListener(_.re,this.o);this.a.parentElement&&this.a.parentElement.removeChild(this.a)}},qz=class{constructor(a,b,c,d,e){e=void 0===e?-1:e;this.f=a;this.l=b;this.j=c;this.h=d;this.a=null;this.f.set(d.messageId,this);0<=e&&(this.a=setTimeout(()=>{this.resolve()},e))}resolve(a){null!==this.a&&(clearTimeout(this.a),this.a=null);this.f.delete(this.h.messageId);this.l(a)}reject(a){null!==this.a&&(clearTimeout(this.a),this.a=null);this.f.delete(this.h.messageId);
this.j(a)}};var Cz=class extends _.Pv{constructor(){super();this.g=new _.S("clpp.simid.Plugin");this.h=this.a=this.m=null;this.l=[];this.j=null;this.w=this.A.bind(this);this.f=null;this.o=!1}id(){return"simid"}onPlayerCreated(a){this.g.info("New player created");this.a&&(this.a.unload(),this.a=null);this.m=a;this.h&&this.h.release();this.h=new Bz(a)}onContentWillLoad(a){var b=a.getConfiguration();if(b.simid&&b.simid.creatives&&0!==b.simid.creatives.length){this.l=b.simid.creatives;this.g.info(`Setting up SIMID plugin for ${this.l.length} creatives`);
var c=this.m.getSurface();c?(this.g.info("Creating new session"),this.a=new Az(this.h,b.simid),b=c.getContainer(),this.j=wz(this,b,c),this.j.observe(b),a.on(_.Zf,this.w)):this.g.warn(mz)}}A(){var a=this.m.getPosition();a:if(this.f&&a<this.f.startTime+this.f.duration)var b=this.f;else{for(b=0;b<this.l.length;b++){var c=this.l[b];const d=c.startTime+c.duration;if(a>=c.startTime-(c.preload||5)&&a<d){b=c;break a}}b=null}b?b===this.f?(b=b.startTime,this.a&&(c=a>=b,!this.o&&c&&(tz(this.a),this.o=!0,this.g.info(oz)),
c&&(c=this.a,rz(c,pz(c,nz,{currentTime:a-b}))))):((a=this.m.getSurface())||this.g.warn(mz),a=a.getContainer(),this.f=b,this.o=!1,this.g.info("Loading session"),this.h.f=b,this.a.load(a)):this.f&&(this.a.unload(),this.f=null,this.o=!1)}onPlayerWillRelease(a){super.onPlayerWillRelease(a);this.m.getSurface()&&(this.j&&(this.j.disconnect(),this.j=null),a.off(_.Zf,this.w),this.f&&(this.a.unload(),this.f=this.h.f=null))}},Bz=class{constructor(a){this.m=a;this.a=null;this.g=new _.S("clpp.simid.Loader");
this.h=!1;this.f=null}release(){this.g.info("Releasing loader")}getContainer(){const a=this.getSurface().getContainer();if(!a)throw this.g.error("No player container found. Can not create SIMID init data"),new _.I(1,10,10100,"No player container found");return a}getSurface(){const a=this.m.getSurface();if(!a)throw this.g.error("No player surface found. Can not create SIMID init data"),new _.I(1,10,10100,"No surface");return a}bg(){return this.a}url(){return this.f?this.f.url:""}};_.Ir(new class{create(){return new Cz}});};f.call(g, window);