"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();__define("pages/embeds/twitch-embed",["lib/tp/twitch-api","lib/tp/superagent","lib/tp/superagent-deps/emitter","lib/tp/superagent-deps/reduce","lib/promise","lib/da/ddt","lib/dom/sigil","lib/data/extend","lib/data/meta","pageload","lib/da/vms","lib/da/deviantart-object","lib/data/init","lib/da/legacy-difi-bridge","lib/tp/eventemitter3","lib/da/dapx","lib/dom/event-listener","lib/dom/event-wrapper"],function(a,b,c,d){var e=a("lib/tp/twitch-api"),f=a("lib/dom/sigil"),g=a("lib/data/extend"),h=a("lib/data/meta"),i=a("lib/da/legacy-difi-bridge"),j=a("lib/da/dapx"),k=a("lib/dom/event-listener"),l=function(){function a(b){var c=this;_classCallCheck(this,a),h.getCachedMetadata(),this.containerNode=b,this.options=h.getGlobal(this.containerNode),this.twitch=new e(this.options.clientId),this.eventSuffix="_twitch_"+this.options.tracking,this.determinePlayerOptions().then(function(a){a.channel&&a.video&&delete a.channel,c.playerNode=document.createElement("div"),c.playerNode.className="twitch-player",c.containerNode.appendChild(c.playerNode),a.video?(c.eventSuffix+="_"+(c.options.channel||a.video)+"_recording",c.containerNode.classList.add("recording")):(c.eventSuffix+="_"+a.channel+"_live",c.containerNode.classList.add("live")),c.twitch.createPlayer(c.playerNode,a),c.trackEvent("view"),c.options.chat&&a.channel&&(c.containerNode.classList.add("chat"),c.chatNode=document.createElement("div"),c.chatNode.className="twitch-chat",c.containerNode.appendChild(c.chatNode),c.twitch.createChat(c.chatNode,a))}),this.bindElninoCTA()}return _createClass(a,[{key:"determinePlayerOptions",value:function(){var a=this;return this.options.video?Promise.resolve(g(this.options)):this.options.channel?this.getLiveStream(this.options.channel).then(function(b){return b?b:a.getLatestRecording(a.options.channel)}).then(function(b){return b||(b={}),g(a.options,b)}):void 0}},{key:"getLiveStream",value:function(a){return this.twitch.api({url:"streams/"+a}).then(function(a){return a&&a.stream?{channel:a.stream.channel.name}:null})}},{key:"getLatestRecording",value:function(a){return this.twitch.api({url:"channels/"+a+"/videos",params:{broadcasts:!0,limit:1,offset:0}}).then(function(a){return a&&a.videos&&a.videos.length>0?{video:a.videos[0]._id}:null})}},{key:"trackEvent",value:function(a){var b=this;this.options.tracking&&(j.sendEvent({event:a+this.eventSuffix}),window.DWait.ready("jms/lib/pubsub.js",function(){window.PubSub.publish("DaGa.track_event_no_debounce",{category:"Twitch",action:a+b.eventSuffix})}))}},{key:"bindElninoCTA",value:function(){var a=f.closest(this.containerNode,"twitch-elnino");a&&k.on("click","twitch-elnino-button",this.trackEvent.bind(this,"cta_click"),!1,a)}}]),a}(),m=[],n=function(){f.find("twitch-embed").forEach(function(a){for(var b in m)if(m[b].node==a)return;m.push({widget:new l(a),node:a})})};i.on("assets.woken",n),n()});