var DEBUG=!1;
(function(){var g=function(b){DEBUG&&console.log.apply(console,arguments)},p={method:"GET",async:!0,headers:{},data:null,timeout:3E4,type:"text",onload:function(){},onerror:function(){},ontimeout:null},m=Object.keys(p),l=[],k=!1,b={host:null,authHost:null,lang:"en",session:{accessToken:null,refreshToken:null,userId:null,macAlgorithm:null,macKey:null},request:function(e,c){var d=fetch(),h,n;if(k)l.push({url:e,options:c});else{k=!0;c=c||{};if(e&&("string"===typeof e||e instanceof String)&&0<e.length){if(c&&
"object"===typeof c)for(h=0;h<m.length;h++)void 0===c[m[h]]&&(c[m[h]]=p[m[h]]);d=fetch();g(JSON.stringify([c.method,b.host+"/"+e,c.async]));d.open(c.method,b.host+"/"+e,c.async);d.setRequestHeader("Authorization","Bearer "+b.session.accessToken);d.setRequestHeader("Content-Type","application/json");d.setRequestHeader("Accept-Language",b.lang);if(c.headers&&"object"===typeof c.headers)for(n=Object.keys(c.headers),h=0;h<n.length;h++)d.setRequestHeader(n[h],c.headers[n[h]]);d.responseType=c.type;d.timeout=
c.timeout;d.onreadystatechange=function(){var f=!1;g(this.readyState);g(this.status);if(0===this.readyState)k=!1,0<l.length&&(f=l.shift(),b.request(f.url,f.options));else if(4===this.readyState)if(g(JSON.stringify([this.status,this.responseText])),0===this.status)k=!1,0<l.length&&(f=l.shift(),b.request(f.url,f.options));else if(200===this.status){try{f=JSON.parse(this.responseText)}catch(a){f=!1}try{c.onload(!f,f)}catch(a){if(b.onfail)b.onfail("Exception thrown "+a.message);if(c.onerror)try{c.onerror.call(d)}catch(q){}}k=
!1;0<l.length&&(f=l.shift(),b.request(f.url,f.options))}else if(401===this.status)b.session.refreshToken?b.auth.refreshToken(b.session.refreshToken,function(a){a?(g(a),b.auth.login({password:"",login:""},function(a,d){g(a);g(JSON.stringify(a));g(JSON.stringify(d));if(a)if("invalid_grant"===a.error)b.oncredenitalsrequired(function(){k=!1;b.request(e,c)});else"access_denied"===a.error&&"Account disabled"===a.error_description?b.reload():(k=!1,b.request(e,c));else k=!1,b.request(e,c)})):(g(44444444),
k=!1,b.request(e,c))}):(g(9999999),b.auth.login({password:"",login:""},function(a,d){g(JSON.stringify(a));g(JSON.stringify(d));a?"invalid_grant"===a.error?(g(1111),b.oncredenitalsrequired(function(){k=!1;b.request(e,c)})):"access_denied"===a.error&&"Account disabled"===a.error_description?b.reload():(k=!1,b.request(e,c)):(g(222),k=!1,b.request(e,c))}));else{k=!1;try{c.onload(this.status,this.responseText)}catch(a){if(b.onfail)b.onfail("Exception thrown "+a.message);if(c.onerror)try{c.onerror.call(d)}catch(q){}}0<
l.length&&(f=l.shift(),b.request(f.url,f.options))}};d.onerror=function(){if(c.onerror)try{c.onerror.call(d)}catch(f){}if(b.onfail)b.onfail("Network error "+d.status)};d.ontimeout=function(){if(c.ontimeout)try{c.ontimeout.call(d)}catch(b){}};d.send(c.body||null);return d}return!1}},auth:{login:function(e,c){var d=Math.random().toString(36).substring(2),h=fetch(),k=Math.random().toString(36).substring(2),f=Math.round(new Date/1E3),a=gSTB.GetHashVersion1(k,f),k=a+"."+k+"."+f;g("hash "+a);"false"===
a?setTimeout(function(){b.auth.login(e,c)},5E3):(h.open("POST",b.authHost,!0),h.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d=(a=gSTB.GetUID("device_id",d))?d+"."+a:"",h.onload=function(){var a;g("login responseText "+this.responseText);g("login status "+this.status);if(200===this.status){try{a=JSON.parse(this.responseText)}catch(d){a={}}a.access_token&&a.user_id?(b.session.accessToken=a.access_token,b.session.refreshToken=a.refresh_token,b.session.userId=a.user_id,a.mac_algorithm&&
(b.session.macAlgorithm=a.mac_algorithm),a.mac_key&&(b.session.macKey=a.api.mac_key,b.session.macAlgorithm=a.api.mac_algorithm),a.api&&a.api.v3&&(b.session.macKey=a.api.v3.mac_key,b.session.macAlgorithm=a.api.v3.mac_algorithm),b.saveCallback&&b.saveCallback(),c(!1)):c(a,a)}else c(this.status+" : "+this.responseText,{})},h.onerror=function(){c("onerror "+this.status+" : "+this.responseText,!1)},g("grant_type=password&username="+e.login+"&password="+e.password+"&mac="+gSTB.GetDeviceMacAddress()+"&serial_number="+
gSTB.GetDeviceSerialNumber()+"&stb_type="+gSTB.GetDeviceModelExt()+"&device_id="+gSTB.GetUID()+"&device_id2="+d+"&version="+gSTB.Version()+"&hw_version="+k),h.send("grant_type=password&username="+e.login+"&password="+e.password+"&mac="+gSTB.GetDeviceMacAddress()+"&serial_number="+gSTB.GetDeviceSerialNumber()+"&stb_type="+gSTB.GetDeviceModelExt()+"&device_id="+gSTB.GetUID()+"&device_id2="+d+"&version="+gSTB.Version()+"&hw_version="+k))},refreshToken:function(e,c){var d=fetch(),h=Math.random().toString(36).substring(2),
k=Math.round(new Date/1E3),f=gSTB.GetHashVersion1(h,k),h=f+"."+h+"."+k;g("hash "+f);"false"===f?setTimeout(function(){b.auth.refreshToken(e,c)},5E3):(d.open("POST",b.authHost,!0),d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.onload=function(){var a;g("refresh token status "+this.status);if(200===this.status){try{a=JSON.parse(this.responseText)}catch(d){a={}}g("refresh token responseText "+this.responseText);a.access_token&&a.user_id?(b.session.accessToken=a.access_token,
b.session.refreshToken=a.refresh_token,b.session.userId=a.user_id,a.mac_algorithm&&(b.session.macAlgorithm=a.mac_algorithm),a.mac_key&&(b.session.macKey=a.api.mac_key,b.session.macAlgorithm=a.api.mac_algorithm),a.api&&a.api.v3&&(b.session.macKey=a.api.v3.mac_key,b.session.macAlgorithm=a.api.v3.mac_algorithm),b.saveCallback&&b.saveCallback(),c(!1)):c(a,a)}else c(this.status+" : "+this.responseText,{})},d.onerror=function(){g("onerror");c(!0,{})},g("grant_type=refresh_token&refresh_token="+e+"&hw_version="+
h),d.send("grant_type=refresh_token&refresh_token="+e+"&hw_version="+h))}}};return b})();
