define(["exports","./../emby-apiclient/connectionmanager.js","./../common/globalize.js","./../layoutmanager.js","./../loading/loading.js","./../dialoghelper/dialoghelper.js","./../common/textencoding.js","./../emby-elements/emby-button/emby-button.js","./../emby-elements/emby-select/emby-select.js","./../emby-elements/emby-scroller/emby-scroller.js"],function(_exports,_connectionmanager,_globalize,_layoutmanager,_loading,_dialoghelper,_textencoding,_embyButton,_embySelect,_embyScroller){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,require(["formDialogStyle","css!modules/imageuploader/style.css"]);var currentItem,currentFile,hasChanges=!1;function showAlert(options){Emby.importModule("./modules/common/dialogs/alert.js").then(function(alert){return alert(options)})}function onFileReaderError(evt){switch(_loading.default.hide(),evt.target.error.code){case evt.target.error.NOT_FOUND_ERR:showAlert(_globalize.default.translate("MessageFileReadError"));break;case evt.target.error.ABORT_ERR:break;default:showAlert(_globalize.default.translate("MessageFileReadError"))}}function setFiles(page,files){var reader,theFile,files=(files=function(files){for(var list=[],i=0,length=files.length;i<length;i++)validateImage(files[i])&&list.push(files[i]);return list}(files))[0];files&&files.type.match("image.*")?(currentFile=files,(reader=new FileReader).onerror=onFileReaderError,reader.onloadstart=function(){page.querySelector("#fldUpload").classList.add("hide")},reader.onabort=function(){_loading.default.hide(),console.log("File read cancelled")},reader.onload=(theFile=files,function(e){e=['<img style="max-width:100%;max-height:100%;" src="',e.target.result,'" title="',_textencoding.default.htmlEncode(theFile.name),'"/>'].join("");page.querySelector("#imageOutput").innerHTML=e,page.querySelector("#fldUpload").classList.remove("hide")}),reader.readAsDataURL(files)):(page.querySelector("#imageOutput").innerHTML="",page.querySelector("#fldUpload").classList.add("hide"),currentFile=null)}var supportedImageTypes=["image/png","image/jpg","image/jpeg"];function validateImage(file){return!!supportedImageTypes.includes(file.type)}function onSubmit(e){e.preventDefault();var dlg,imageType,e=currentFile;return e&&validateImage(e)&&(_loading.default.show(),imageType=(dlg=this.closest(".dialog")).querySelector("#selectImageType").value,_connectionmanager.default.getApiClient(currentItem).uploadItemImage(currentItem.Id,imageType,e).then(function(){dlg.querySelector(".uploadImage").value="",_loading.default.hide(),hasChanges=!0,_dialoghelper.default.close(dlg)})),!1}function removeImageType(dlg,type){dlg=dlg.querySelector('option[value="'+type+'"]');dlg&&dlg.parentNode.removeChild(dlg)}function showEditor(options,resolve){options=options||{},require(["text!modules/imageuploader/imageuploader.template.html"],function(template){currentItem=options.item;var page,dialogOptions={removeOnClose:!0},dlg=(_layoutmanager.default.tv?dialogOptions.size="fullscreen":dialogOptions.size="fullscreen-border",_dialoghelper.default.createDialog(dialogOptions));dlg.classList.add("formDialog"),dlg.innerHTML=_globalize.default.translateDocument(template,"sharedcomponents"),dlg.querySelector(".uploadImage").setAttribute("accept",supportedImageTypes.join(",")),"TvChannel"!==currentItem.Type&&(removeImageType(dlg,"LogoLight"),removeImageType(dlg,"LogoLightColor")),dlg.addEventListener("close",function(){_loading.default.hide(),resolve(hasChanges)}),options.imageType||dlg.querySelector(".fldSelectImageType").classList.remove("hide"),_dialoghelper.default.open(dlg),(page=dlg).querySelector("form").addEventListener("submit",onSubmit),page.querySelector(".uploadImage").addEventListener("change",function(){setFiles(page,this.files)}),page.querySelector(".btnBrowse").addEventListener("click",function(){page.querySelector(".uploadImage").click()}),dlg.querySelector("#selectImageType").value=options.imageType||"Primary",dlg.querySelector(".btnCancel").addEventListener("click",function(){_dialoghelper.default.close(dlg)})})}_exports.default={show:function(options){return new Promise(function(resolve,reject){hasChanges=!1,showEditor(options,resolve)})}}});