(()=>{"use strict";const e="js--vhi-iframe";class t{static sendPreOnLoadAnswer(e){e.contentWindow.postMessage({action:"vh-pre-on-load-answer"},"*")}static sendOnLoadAnswer(e){var t,s;e.contentWindow.postMessage({action:"vh-on-load-answer",accountId:null!=(t=window.accountId)?t:0,lessonId:null!=(s=window.lessonId)?s:0},"*")}static sendGoTo(e){e.contentWindow.postMessage({action:"vh-goto",link:window.location.hash},"*")}static sendGoToTimes(e,t){e.contentWindow.postMessage({action:"go-to-times",times:t},"*")}static sendLessonVideoHashes(e,t){e.contentWindow.postMessage({action:"lesson-video",hashes:t},"*")}static sendNeedPause(e){e.contentWindow.postMessage({action:"need-pause"},"*")}}class s{constructor(e,t){this.playerServers=[],this.waitingVideo=[],this.loadedVideo=[],this.region=t,this.playerServers=e[this.region],this.checkIframeLoaded=this.checkIframeLoaded.bind(this)}startWatch(){setInterval(this.checkIframeLoaded,5e3)}setLoaded(e){this.loadedVideo.includes(e)||this.loadedVideo.push(e)}setWaiting(e){this.waitingVideo.includes(e)||this.waitingVideo.push(e)}isLoaded(e){return this.loadedVideo.includes(e)}isWaiting(e){return this.waitingVideo.includes(e)}checkIframeLoaded(){const e=[];i.getIframesWrappers().forEach((t=>{let s=t.getAttribute("data-file-id")?parseInt(t.getAttribute("data-file-id")):0;if(s&&!e.includes(s))if(e.push(s),this.isLoaded(s)||this.isWaiting(s)){if(!this.isLoaded(s)&&this.isWaiting(s)){const e=window.vhs.bridge.getIframeForWrapper(t);if(!e)return;const s=this.getNextUrl(e.src);s!==e.src&&(e.src=s)}}else this.setWaiting(s)}))}getNextUrl(e){const t=new URL(e);let s="";return this.playerServers.forEach((e=>{e!==t.host?s&&t.host===s&&(t.host=e):s=t.host})),t.toString()}}class i{constructor(){this.playerService=null,this.selector="."+e}sendGoToLinkToIframes(e){/^#goto-\w{32}-[\d\.]+\s?/.test(e)&&(this.getIframes().forEach((e=>{t.sendGoTo(e)})),window.location.hash=window.location.hash.replace(/^#goto-\w{32}-[\d\.]+\s?/,"#goto"))}sendLessonVideoToIframes(e){const s=[];i.getIframesWrappers().forEach((t=>{const i=t.getAttribute("data-video-hash");i!==e.videoHash&&s.push(i)})),s.length&&this.getIframesForHash(e.videoHash).forEach((e=>{t.sendLessonVideoHashes(e,s)}))}sendDocumentLinksToIframes(e,s){const r=[];document.querySelectorAll("a").forEach((t=>{const s=t.getAttribute("href");if(!s||"#goto-"!==s.substr(0,6))return;const i=new RegExp("#goto-"+e+"-(\\d+)\\s?");if(!i.test(s))return;const n=i.exec(s);n&&r.push([1*n[1],t.textContent])})),r.length&&i.getIframesWrappers(s).forEach((e=>{const s=this.getIframeForWrapper(e);s&&t.sendGoToTimes(s,r)}))}getIframeForWrapper(e){return e.querySelector(this.selector)}enterFullScreen(e){i.getIframesWrappers(e.gcFileId).forEach((e=>{e.classList.add("in-fullscreen")}))}cancelFullScreen(e){i.getIframesWrappers(e.gcFileId).forEach((e=>{e.classList.remove("in-fullscreen")}))}sendEventsFromIframes(e){"volume-play"===e.detail.eventType&&this.pauseAllPlayersOnPage(e.detail.gcFileId),i.getIframesWrappers(e.detail.gcFileId).forEach((t=>{const s=new CustomEvent("gc-video-event",{detail:e.detail});t.dispatchEvent(s)}))}pauseAllPlayersOnPage(e){i.getExcludeIframes(e).forEach((e=>{t.sendNeedPause(e)}))}createPlayerService(e,t){this.playerService=new s(e,t)}init(){var e;window.addEventListener("message",(e=>{"vh-pre-onload"===e.data.action?this.onPlayerPreLoad(e.data):"vh-onload"===e.data.action?this.onPlayerLoad(e.data):"vh-onload-lesson-video"===e.data.action?this.sendLessonVideoToIframes(e.data):"vh-video-event"===e.data.action?this.sendEventsFromIframes(e.data):"vh-fullscreen-enter"===e.data.action?this.enterFullScreen(e.data):"vh-fullscreen-cancel"===e.data.action&&this.cancelFullScreen(e.data)})),window.addEventListener("hashchange",(()=>{this.sendGoToLinkToIframes(window.location.hash)})),null==(e=this.playerService)||e.startWatch()}onPlayerPreLoad(e){var s;null==(s=this.playerService)||s.setLoaded(e.gcFileId),setTimeout((()=>{this.getIframesForHash(e.hash).forEach((e=>{t.sendPreOnLoadAnswer(e)}))}),50)}onPlayerLoad(e){new RegExp(e.hash).test(window.location.hash)&&this.sendGoToLinkToIframes(window.location.hash),e.gcFileId&&this.sendDocumentLinksToIframes(e.hash,e.gcFileId),setTimeout((()=>{i.getIframesWrappers(e.gcFileId).forEach((e=>{e.classList.remove("in-fullscreen")})),this.getIframesForHash(e.hash).forEach((e=>{t.sendOnLoadAnswer(e)}))}),50)}getIframes(){return document.querySelectorAll(this.selector)}static getIframesWrappers(e=null){return e?document.querySelectorAll('.js--vhi-root[data-file-id="'+e+'"]'):document.querySelectorAll(".js--vhi-root")}static getExcludeIframes(e){return e?document.querySelectorAll('.js--vhi-root:not([data-file-id="'+e+'"]) iframe'):document.querySelectorAll(".js--vhi-root iframe")}static getIframesWrappersForLesson(e){return document.querySelectorAll('.js--vhi-root[data-lesson-id="'+e+'"]')}getIframesForHash(e){return document.querySelectorAll('.js--vhi-root[data-video-hash="'+e+'"] '+this.selector)}}class r{constructor(){this.aspectRatioIntervalHandles={},this.onMessageReceive=this.onMessageReceive.bind(this)}start(){const e=r.getAllPLayers();0!==e.length&&(window.addEventListener("message",this.onMessageReceive,!1),e.map((e=>{this.aspectRatioIntervalHandles[e.id]=window.setInterval((function(){e&&e.contentWindow.postMessage({msg:"video-info"},"*")}),500)})))}onMessageReceive(e){if("object"!=typeof e.data||"vh"!==e.data.type)return;const t="true"===e.data.is_vertical,s=e.data.video_hash,i=e.data.aspect_ratio,r=document.getElementById("vhi-root-"+s);window.clearInterval(this.aspectRatioIntervalHandles["vhplayeriframe-"+s]),r.setAttribute("style","--video-aspect-ratio: "+i),t&&r.classList.add("vhi-root--vertical")}static getAllPLayers(){return Array.from(document.getElementsByClassName(e))}}var n=(e,t,s)=>new Promise(((i,r)=>{var n=e=>{try{a(s.next(e))}catch(e){r(e)}},o=e=>{try{a(s.throw(e))}catch(e){r(e)}},a=e=>e.done?i(e.value):Promise.resolve(e.value).then(n,o);a((s=s.apply(e,t)).next())}));class o{getJwtForFileHash(e){return n(this,null,(function*(){return(yield fetch("/pl/fileservice/api/jwt-v2?hash="+e)).text()}))}getVideoHashForFile(e){return n(this,null,(function*(){const t=yield fetch("/pl/fileservice/video/get-video-hash?file-hash="+e),s=yield t.json();return s.success?s.video_hash:""}))}getPreviewUrlByHash(e){return n(this,null,(function*(){const t=yield fetch("/pl/fileservice/video/get-preview-url-by-hash?preview-hash="+e),s=yield t.json();return s.success?s.url:""}))}}var a=(e,t,s)=>new Promise(((i,r)=>{var n=e=>{try{a(s.next(e))}catch(e){r(e)}},o=e=>{try{a(s.throw(e))}catch(e){r(e)}},a=e=>e.done?i(e.value):Promise.resolve(e.value).then(n,o);a((s=s.apply(e,t)).next())}));class d{constructor(e){this.domain=e}changeVideoPreview(e,t,s,i){return a(this,null,(function*(){const r=d.getFullUrl(e,"/vh-change-preview/change",{"video-hash":t,jwt:i,"preview-url":encodeURIComponent(s),response:"json"});try{let e=new AbortController;const t=window.setTimeout((()=>e.abort()),3e4),s=yield fetch(r,{signal:e.signal});window.clearTimeout(t);const i=yield s.json();return!(!i.status||"ok"!==i.status)}catch(e){console.error(e)}return!1}))}getLogOfGridChange(e){return a(this,null,(function*(){const t=yield(new o).getJwtForFileHash(e),s=yield fetch(this.getUrl("/api/grid/get_log_of_grid_change?jwt="+t));return(yield s.json()).list}))}getUrl(e,t={}){return d.getFullUrl(this.domain,e,t)}static getFullUrl(e,t,s){let i=window.location.protocol+"//"+e+t+"?";return Object.keys(s).forEach((e=>{i=i+e+"="+s[e]+"&"})),i}}var l=(e,t,s)=>new Promise(((i,r)=>{var n=e=>{try{a(s.next(e))}catch(e){r(e)}},o=e=>{try{a(s.throw(e))}catch(e){r(e)}},a=e=>e.done?i(e.value):Promise.resolve(e.value).then(n,o);a((s=s.apply(e,t)).next())}));class h{constructor(e,t,s,i){this.previewServers=[],this.activePreviewServers=[],this.region=t,this.previewServers=e[this.region],this.vhApiClient=s,this.gcApiClient=i}changeVideoPreviewForHash(e,t){return l(this,null,(function*(){const s=yield this.gcApiClient.getPreviewUrlByHash(t);return!!s&&this.changeVideoPreview(e,s)}))}changeVideoPreview(e,t){return l(this,null,(function*(){const s=yield this.gcApiClient.getVideoHashForFile(e);if(!s)return!1;let i;this.activePreviewServers=[...this.previewServers];do{if(i=this.getRandomDomain(),!i)break;if(yield this.turn(i,e,s,t))return!0}while(this.activePreviewServers.length>0);return window.vhs.observer.sendErrorLog("VideoPreviewService",1,1,JSON.stringify({previewUrl:t,fileHash:e,region:this.region}),s),!1}))}turn(e,t,s,i){return l(this,null,(function*(){const r=yield this.gcApiClient.getJwtForFileHash(t);return yield this.vhApiClient.changeVideoPreview(e,s,i,r)}))}getRandomDomain(){if(!this.activePreviewServers.length)return"";const e=Math.floor(Math.random()*this.activePreviewServers.length),t=this.activePreviewServers[e];return this.activePreviewServers.splice(e,1),t}}const c={staticAssetVersion:parseInt("39"),previewServers:{ru:["v01.getcourse.ru","vh29.gcfiles.net","vh40.gcfiles.net"],eu:["vh29.gcfiles.net","vh40.gcfiles.net"],id:["vh-ops-001.getkurs.id"]},playerServers:{ru:["player02.getcourse.ru","vhapi03.getcourse.ru","vhapi04.getcourse.ru"],eu:["player-ua.gceuproxy.com","cf-api-2.vhcdn.com","player02.getcourse.ru","vh-api2-hel1-dc8.gceuproxy.com"],id:["player02.getcourse.ru"]},vhApiHost:"player02.getcourse.ru",monitoringSaveUrl:"https://v01.getcourse.ru/api/fpm-webbrowser-monitoring/save"};window.vhs={config:c,init:e=>{var t,s;window.vhs.bridge.createPlayerService(c.playerServers,e),window.vhs.bridge.init(),window.vhs.resizer.start(),window.vhs.observer.start(null!=(t=window.accountUserId)?t:0,null!=(s=window.accountId)?s:0)},createPreviewService:e=>new h(c.previewServers,e,window.vhs.vhApiClient,window.vhs.gcApiClient),bridge:new i,resizer:new r,vhApiClient:new d(c.vhApiHost),gcApiClient:new o,observer:new class{constructor(e,t){this.windowErrors=[],this.handleSending=null,this.saveUrl=e,this.staticAssetVersion=t,this.onUnload=this.onUnload.bind(this),this.onWindowError=this.onWindowError.bind(this),this.addSendingToQuery=this.addSendingToQuery.bind(this),this.removeSendingToQuery=this.removeSendingToQuery.bind(this),this.sendDataToServerByTimer=this.sendDataToServerByTimer.bind(this)}start(e,t){this.userId=e,this.accountId=t,this.videoId=0,this.videoHash="",window.addEventListener("unload",this.onUnload),window.addEventListener("error",this.onWindowError,!0),this.addSendingToQuery()}sendErrorLog(e,t,s,i,r){this.videoHash=r,this.windowErrors.push({colNo:s,filename:e,lineNo:t,msg:i}),this.sendDataToServerByTimer(),this.videoHash=""}onWindowError(e){e.message&&(/chrome-extension:\/\//.test(e.filename)||/safari-extension:\/\//.test(e.filename)||/chrome:\/\/internal/.test(e.filename)||/PIPELINE_ERROR_DECODE/.test(e.message)||/AUDIO_RENDERER_ERROR/.test(e.message)||"Script error."==e.message||this.windowErrors.push({colNo:e.colno,filename:e.filename,lineNo:e.lineno,msg:e.message}))}onUnload(){const e=this.getData();if(null===e)return;this.sendData(e,!1).catch((e=>console.error(e)))}sendDataToServerByTimer(){this.removeSendingToQuery();const e=this.getData();null!==e?this.sendData(e,!1).catch((e=>console.error(e))).finally(this.addSendingToQuery):this.addSendingToQuery()}removeSendingToQuery(){this.handleSending&&(window.clearTimeout(this.handleSending),this.handleSending=null)}addSendingToQuery(){this.handleSending=window.setTimeout(this.sendDataToServerByTimer,3e4)}getData(){if(!this.windowErrors.length)return null;const e={userId:this.userId,accountId:this.accountId,videoId:this.videoId,videoHash:this.videoHash,staticAssetVersion:this.staticAssetVersion,windowErrors:[...this.windowErrors],localtime:(new Date).toISOString(),timezone:Intl?Intl.DateTimeFormat().resolvedOptions().timeZone:"",ua:navigator.userAgent};return this.windowErrors.length=0,e}sendDataAsXMLHttpRequest(e,t=!0){return new Promise(((s,i)=>{const r=new XMLHttpRequest;r.open("POST",this.saveUrl,t),r.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),r.onload=()=>{200===r.status?s():i("error-status-code")},r.onerror=()=>i("error-while-request"),r.send(JSON.stringify(e))}))}sendDataWithBeacon(e){return navigator.sendBeacon(this.saveUrl,JSON.stringify(e))?Promise.resolve():Promise.reject("some-error")}sendData(e,t=!0){return void 0!==navigator.sendBeacon?this.sendDataWithBeacon(e):this.sendDataAsXMLHttpRequest(e,t)}}(c.monitoringSaveUrl,c.staticAssetVersion)}})();