define(["connectionManager","globalize","emby-button","browser"],function(connectionManager,globalize,EmbyButton,browser){"use strict";function onClick(e){var button=this,id=button.getAttribute("data-id"),serverId=button.getAttribute("data-serverid"),type=button.getAttribute("data-itemtype"),apiClient=connectionManager.getApiClient(serverId);button.classList.contains("downloadbutton-on")?require(["confirm"],function(confirm){confirm({text:globalize.translate("ConfirmRemoveDownload"),confirmText:globalize.translate("RemoveDownload"),cancelText:globalize.translate("KeepDownload"),primary:"cancel"}).then(function(){apiClient.cancelSyncItems([id]),button.dispatchEvent(new CustomEvent("download-cancel",{cancelable:!1}))})}):require(["syncDialog"],function(syncDialog){syncDialog.showMenu({items:[{Id:id,Type:type,ServerId:serverId}],mode:"download",serverId:serverId}).then(function(){button.dispatchEvent(new CustomEvent("download",{cancelable:!1}))})})}function clearEvents(button){button.removeEventListener("click",onClick)}function bindEvents(button){clearEvents(button),button.addEventListener("click",onClick)}function fetchAndUpdate(button,item){connectionManager.getApiClient(item).getSyncStatus(item.Id).then(function(result){!function(button,syncPercent){var text,icon=button.iconElement;icon||(button.iconElement=button.querySelector("i"),icon=button.iconElement),null!=syncPercent?(button.classList.add("downloadbutton-on"),icon&&icon.classList.add("downloadbutton-icon-on")):(button.classList.remove("downloadbutton-on"),icon&&icon.classList.remove("downloadbutton-icon-on")),100<=(syncPercent||0)?(button.classList.add("downloadbutton-complete"),icon&&icon.classList.add("downloadbutton-icon-complete")):(button.classList.remove("downloadbutton-complete"),icon&&icon.classList.remove("downloadbutton-icon-complete")),text=100<=(syncPercent||0)?globalize.translate("Downloaded"):null!=syncPercent?globalize.translate("Downloading"):globalize.translate("Download");var textElement=button.querySelector(".emby-downloadbutton-downloadtext");textElement&&(textElement.innerHTML=text),button.title=text,button.setAttribute("aria-label",text)}(button,result.Progress)},function(){})}var EmbyDownloadButton=function(){babelHelpers.inherits(EmbyDownloadButton,EmbyButton);var _super=_createSuper(EmbyDownloadButton);function EmbyDownloadButton(){var _this;babelHelpers.classCallCheck(this,EmbyDownloadButton);var self=_this=_super.call(this);return babelHelpers.possibleConstructorReturn(_this,self)}return babelHelpers.createClass(EmbyDownloadButton,[{key:"connectedCallback",value:function(){EmbyButton.prototype.connectedCallback.call(this),function(){var itemId=this.getAttribute("data-id"),serverId=this.getAttribute("data-serverid");itemId&&serverId&&bindEvents(this)}.call(this)}},{key:"disconnectedCallback",value:function(){EmbyButton.prototype.disconnectedCallback.call(this),function(){clearEvents(this),this.iconElement=null}.call(this)}},{key:"setItem",value:function(item){(function(item){item?(this.setAttribute("data-id",item.Id),this.setAttribute("data-serverid",item.ServerId),this.setAttribute("data-itemtype",item.Type),fetchAndUpdate(this,item),bindEvents(this)):(this.removeAttribute("data-id"),this.removeAttribute("data-serverid"),this.removeAttribute("data-itemtype"),clearEvents(this))}).call(this,item)}}]),EmbyDownloadButton}();customElements.define("emby-downloadbutton",EmbyDownloadButton,{extends:"button"})});