import {clpp} from "./cl.core.js";let g={};const _ = clpp._;var f=function(window){'use strict';var LA="A persistent DRM session already exists for: ",MA="No DRM configuration.",NA="clpp.persistent",OA="clpp.persistent.SimpleSessionStorage",PA=function(a,b){const c=[];for(const d of a)c.push(b(d));return c},RA=function(a,b,c,d,e,f){a.K=QA;const g=new Map,h=_.An(b);g.set(h,{audioCapabilities:e,videoCapabilities:f,distinctiveIdentifier:_.Ke,persistentState:_.uf,sessionTypes:[_.Pe],label:h,drmInfos:[{keySystem:b,licenseServerUri:c,distinctiveIdentifierRequired:!1,persistentStateRequired:!0,
audioRobustness:"",videoRobustness:"",serverCertificate:d,initData:null,keyIds:null}]});return _.Sn(a,g)},TA=function(a,b){a.K=SA;a.L=[];a.G=!0;return _.Tn(a,b)},UA=function(a,b){return _.x(function*(){const c=yield _.ko(a,b);if(c){var d=[],e=a.h.get(c);e&&a.C&&(e.ic=new _.bn,d.push(e.ic));a.g.debug("Attempting to remove session",b);d.push(c.remove());return Promise.all(d)}a.g.debug("Ignoring attempt to remove missing session",b)})},VA=function(a){a=a.h.keys();a=PA(a,b=>b.sessionId);return Array.from(a)},
WA=function(a,b){const c=_.Tl(a),d=_.tl({drm:a});d.drm=a;a=new _.cp({vb:b,onError:()=>{},Se:()=>{},Re:()=>{},onEvent:()=>{},getConfiguration:()=>d,Qc:()=>null});a.f=c;return a},ZA=function(a,b,c){return _.x(function*(){const d=XA;if(a){var e=a.offlineId;if(0!==e.length){var f=yield d.get(e);if(f)YA.debug(LA,e);else{var g=WA(a,c);try{yield TA(g,b);yield _.io(g);yield _.lo(g);const h=g.getDrmInfo(),k=b.map(l=>({contentType:_.mk(l.video.mimeType,l.video.codecs),robustness:h.videoRobustness}));f={sessionIds:VA(g),
keySystem:h.keySystem,licenseServerUri:h.licenseServerUri,serverCertificate:h.serverCertificate,audioCapabilities:[],videoCapabilities:k,drmConfig:a};yield d.store(e,f)}catch(h){throw YA.error(`Error while persisting DRM session ${e}`),new _.I(1,6,6200,null,h);}finally{yield g.destroy()}}}}else YA.debug(MA)})},SA=1,QA=2,$A=class{constructor(){this.g=new _.S(OA)}store(a,b){return _.x(function*(){{var c=JSON.stringify(b.sessionIds);var d=b.serverCertificate?Array.from(b.serverCertificate):null;d=JSON.stringify(d);
const e=JSON.stringify(b.audioCapabilities),f=JSON.stringify(b.videoCapabilities);c=JSON.stringify({sessionIds:c,keySystem:b.keySystem,licenseServerUri:encodeURIComponent(b.licenseServerUri),serverCertificate:d,audioCapabilities:e,videoCapabilities:f,drmConfig:JSON.stringify(b.drmConfig)})}window.localStorage.setItem(encodeURIComponent(a),btoa(c))})}get(a){const b=this;return _.x(function*(){const c=window.localStorage.getItem(encodeURIComponent(a));if(c)try{{const e=JSON.parse(atob(c)),f=JSON.parse(e.sessionIds),
g=e.audioCapabilities||[],h=e.videoCapabilities||[],k=e.serverCertificate||[],l=e.drmConfig||{};var d={sessionIds:f,keySystem:e.keySystem,licenseServerUri:decodeURIComponent(e.licenseServerUri||""),serverCertificate:k?new Uint8Array(k):null,audioCapabilities:g,videoCapabilities:h,drmConfig:l}}return d}catch(e){b.g.warn(`Error while loading offline items for ${a} from storage: `,e)}return null})}delete(a){return _.x(function*(){window.localStorage.removeItem(encodeURIComponent(a))})}clear(){return _.x(function*(){window.localStorage.clear()})}getName(){return OA}};_.z("clpp.persistent.useStorage",function(a){{var b=["store","get","delete","clear","getName"];const c=[];_.B(a)||(_.ah(a.store)||c.push("store"),_.ah(a.get)||c.push("get"),_.ah(a.delete)||c.push("delete"),_.ah(a.clear)||c.push("clear"),_.ah(a.getName)||c.push("getName"),b=c)}if(0<b.length)throw YA.warn("Invalid session storage implementation"),new _.I(1,6,6202,{missingMethods:b});XA=a});
_.z("clpp.persistent.fetchLicense",function(a,b,...c){return _.x(function*(){var d=XA,e=(l,m)=>new _.I(1,6,6200,l,m);const f=_.Zo(a).shift();if(!f)throw e("No source specified.");if(!b)throw e(MA);var g=b.offlineId;if(typeof g!==_.Nf||!g.length)throw e("Missing offline id.");if(yield d.get(g))YA.debug(LA,g);else{var h=_.tl();d=new _.zr;g=[];for(var k of c){const l=new k;_.wl(l.id())||(l.f(),_.yl(l),g.push(l))}k=null;try{k=yield _.Xs(f.url,d,h.manifest.attemptParameters,f.type||null);k.configure(h.manifest);
const l=yield k.start(f.url,{configurationId:"",networkingEngine:d,onContentSteeringInfoFound:()=>null,filterAllPeriods:()=>{},filterNewPeriod:()=>{},onEvent:()=>{},onError:()=>{}});if(!l.periods.length)throw e(null,new _.I(_.G,4,4014));const m=_.Vs(l.periods);yield ZA(b,m,d)}catch(l){if(l instanceof _.I&&6200===l.code)throw l;throw e(null,l);}finally{for(;g.length;)e=g.shift(),e.a(),_.vl.delete(e.id());e=[];k&&e.push(k.stop());e.push(d.destroy());yield Promise.all(e)}}})});
_.z("clpp.persistent.removeLicense",function(a){return _.x(function*(){var b=XA;if(_.ch(a)&&0!==a.length){var c=null,d=null;try{const e=yield b.get(a);e?(e.drmConfig.delayLicenseRequestUntilPlayed=!0,d=new _.zr,c=WA(e.drmConfig,d),yield RA(c,e.keySystem,e.licenseServerUri,e.serverCertificate,e.audioCapabilities,e.videoCapabilities),yield _.io(c),yield Promise.all(e.sessionIds.map(f=>_.x(function*(){yield UA(c,f);YA.debug("successfully removed session with id",f)}))),yield b.delete(a)):YA.debug("No persistent DRM session found")}catch(e){throw YA.error("Failed to remove session"),
new _.I(1,6,6201,null,e);}finally{b=[],c&&b.push(c.destroy()),d&&b.push(d.destroy()),yield Promise.all(b)}}else YA.warn("Invalid offlineId")})});_.z("clpp.persistent.getSessionStorage",function(){return XA});var XA=new $A,YA=new _.S(NA);_.z("clpp.persistent.PersistentLicenseComponent",class extends _.Av{constructor(){super();this.g=new _.S(NA)}id(){return _.Oe}l(a,b){const c=this;return _.x(function*(){const d=a.getConfiguration().drm;if(d){c.g.debug(`Persisting DRM session ID: ${d.offlineId}`);var e=a.getNetworkEngine();try{yield ZA(d,b,e),a.trigger(new _.F(_.Ld,{offlineId:d.offlineId}))}catch(f){a.onError(f)}}else c.g.debug("No DRM configuration found for loaded source.")})}j(a){const b=this;return _.x(function*(){b.g.debug(`Getting EME session IDs for ${a}`);
const c=XA;try{const d=yield c.get(a);if(d)return d.sessionIds||[];b.g.debug(`No offline session found for ${a}`);return[]}catch(d){return b.g.warn("Error while loading session info from storage"),[]}})}});};f.call(g, window);