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","./../emby-elements/emby-dialogclosebutton/emby-dialogclosebutton.js"],function(_exports,_connectionmanager,_globalize,_layoutmanager,_loading,_dialoghelper,_textencoding,_embyButton,_embySelect,_embyScroller,_embyDialogclosebutton){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,imageIndex,e=currentFile;return e&&validateImage(e)&&(_loading.default.show(),imageType=(dlg=this.closest(".dialog")).querySelector(".selectImageType").value,imageIndex=dlg.imageIndex,_connectionmanager.default.getApiClient(currentItem).uploadItemImage(currentItem.Id,imageType,imageIndex,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},dialogOptions=(_layoutmanager.default.tv?dialogOptions.size="fullscreen":dialogOptions.size="fullscreen-border",_dialoghelper.default.createDialog(dialogOptions));dialogOptions.classList.add("formDialog"),dialogOptions.imageIndex=options.imageIndex,dialogOptions.innerHTML=_globalize.default.translateDocument(template,"sharedcomponents"),dialogOptions.querySelector(".uploadImage").setAttribute("accept",supportedImageTypes.join(",")),"TvChannel"!==currentItem.Type&&(removeImageType(dialogOptions,"LogoLight"),removeImageType(dialogOptions,"LogoLightColor")),dialogOptions.addEventListener("close",function(){_loading.default.hide(),resolve(hasChanges)}),options.imageType||dialogOptions.querySelector(".fldSelectImageType").classList.remove("hide"),_dialoghelper.default.open(dialogOptions),(page=dialogOptions).querySelector("form").addEventListener("submit",onSubmit),page.querySelector(".uploadImage").addEventListener("change",function(){setFiles(page,this.files)}),page.querySelector(".btnBrowse").addEventListener("click",function(){var uploadInput=page.querySelector(".uploadImage");uploadInput.focus(),uploadInput.click()}),dialogOptions.querySelector(".selectImageType").value=options.imageType||"Primary"})}_exports.default={show:function(options){return new Promise(function(resolve,reject){hasChanges=!1,showEditor(options,resolve)})}}});