define(["connectionManager","serverNotifications","events","globalize","emby-button","browser"],function(connectionManager,serverNotifications,events,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 _connectedCallback(){var itemId=this.getAttribute("data-id"),serverId=this.getAttribute("data-serverid");itemId&&serverId&&bindEvents(this)}function _disconnectedCallback(){clearEvents(this),this.iconElement=null}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,result.Progress)},function(){})}function _setItem(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))}if(window.customElements){var EmbyDownloadButton=function(_EmbyButton){function EmbyDownloadButton(){var _this;babelHelpers.classCallCheck(this,EmbyDownloadButton);var self=_this=babelHelpers.possibleConstructorReturn(this,babelHelpers.getPrototypeOf(EmbyDownloadButton).call(this));return babelHelpers.possibleConstructorReturn(_this,self)}return babelHelpers.inherits(EmbyDownloadButton,_EmbyButton),babelHelpers.createClass(EmbyDownloadButton,[{key:"connectedCallback",value:function(){browser.customBuiltInElements||EmbyButton.prototype.connectedCallback.call(this),_connectedCallback.call(this)}},{key:"disconnectedCallback",value:function(){browser.customBuiltInElements||EmbyButton.prototype.disconnectedCallback.call(this),_disconnectedCallback.call(this)}},{key:"setItem",value:function(item){_setItem.call(this,item)}}]),EmbyDownloadButton}(EmbyButton);customElements.define("emby-downloadbutton",EmbyDownloadButton,{extends:"button"})}else if(document.registerElement){var EmbyDownloadButtonPrototype=Object.create(EmbyButton);EmbyDownloadButtonPrototype.createdCallback=function(){EmbyButton.createdCallback&&EmbyButton.createdCallback.call(this)},EmbyDownloadButtonPrototype.attachedCallback=function(){EmbyButton.attachedCallback&&EmbyButton.attachedCallback.call(this),_connectedCallback.call(this)},EmbyDownloadButtonPrototype.detachedCallback=function(){EmbyButton.detachedCallback&&EmbyButton.detachedCallback.call(this),_disconnectedCallback.call(this)},EmbyDownloadButtonPrototype.setItem=_setItem,document.registerElement("emby-downloadbutton",{prototype:EmbyDownloadButtonPrototype,extends:"button"})}});