define(["dialogHelper","loading","cardBuilder","connectionManager","require","globalize","scrollHelper","layoutManager","focusManager","emby-input","emby-checkbox","paper-icon-button-light","css!./../formdialog","material-icons","cardStyle"],function(dialogHelper,loading,cardBuilder,connectionManager,require,globalize,scrollHelper,layoutManager,focusManager){"use strict";function getApiClient(){return connectionManager.getApiClient(currentServerId)}function searchForIdentificationResults(page){var i,length,value,lookupInfo={ProviderIds:{}},identifyField=page.querySelectorAll(".identifyField");for(i=0,length=identifyField.length;i");if(identifyResult.ImageUrl){var displayUrl=getSearchImageDisplayUrl(identifyResult.ImageUrl,identifyResult.SearchProviderName);resultHtml='
'+resultHtml+"
"}page.querySelector(".selectedSearchResult").innerHTML=resultHtml,focusManager.focus(identifyOptionsForm.querySelector(".btnSubmit"))}function getSearchResultHtml(result,index){var padderClass,html="",cssClass="card scalableCard",cardBoxCssClass="cardBox visualCardBox";if("Episode"===currentItemType?(cssClass+=" backdropCard backdropCard-scalable",padderClass="cardPadder-backdrop"):"MusicAlbum"===currentItemType||"MusicArtist"===currentItemType?(cssClass+=" squareCard squareCard-scalable",padderClass="cardPadder-square"):(cssClass+=" portraitCard portraitCard-scalable",padderClass="cardPadder-portrait"),layoutManager.tv&&(cssClass+=" card-focusscale",cardBoxCssClass+=" cardBox-focustransform"),cardBoxCssClass+=" card-focuscontent",html+='"}function getSearchImageDisplayUrl(url,provider){var apiClient=getApiClient();return apiClient.getUrl("Items/RemoteSearch/Image",{imageUrl:url,ProviderName:provider})}function submitIdentficationResult(page){loading.show();var options={ReplaceAllImages:page.querySelector("#chkIdentifyReplaceImages").checked},apiClient=getApiClient();apiClient.ajax({type:"POST",url:apiClient.getUrl("Items/RemoteSearch/Apply/"+currentItem.Id,options),data:JSON.stringify(currentSearchResult),contentType:"application/json"}).then(function(){hasChanges=!0,loading.hide(),dialogHelper.close(page)},function(){loading.hide(),dialogHelper.close(page)})}function showIdentificationForm(page,item){var apiClient=getApiClient();apiClient.getJSON(apiClient.getUrl("Items/"+item.Id+"/ExternalIdInfos")).then(function(idList){for(var html="",providerIds=item.ProviderIds||{},i=0,length=idList.length;i';var idLabel=globalize.translate("sharedcomponents#LabelDynamicExternalId").replace("{0}",idInfo.Name);providerIds[idInfo.Key]||"";html+='',html+="
"}page.querySelector("#txtLookupName").value="","Person"===item.Type||"BoxSet"===item.Type?(page.querySelector(".fldLookupYear").classList.add("hide"),page.querySelector("#txtLookupYear").value=""):(page.querySelector(".fldLookupYear").classList.remove("hide"),page.querySelector("#txtLookupYear").value=""),page.querySelector(".identifyProviderIds").innerHTML=html,page.querySelector(".formDialogHeaderTitle").innerHTML=globalize.translate("sharedcomponents#Identify")})}function showEditor(itemId){loading.show(),require(["text!./itemidentifier.template.html"],function(template){var apiClient=getApiClient();apiClient.getItem(apiClient.getCurrentUserId(),itemId).then(function(item){currentItem=item,currentItemType=currentItem.Type;var dialogOptions={size:"medium",removeOnClose:!0,scrollY:!1};layoutManager.tv&&(dialogOptions.size="fullscreen");var dlg=dialogHelper.createDialog(dialogOptions);dlg.classList.add("formDialog"),dlg.classList.add("recordingDialog");var html="";html+=globalize.translateDocument(template,"sharedcomponents"),dlg.innerHTML=html,dlg.addEventListener("close",onDialogClosed),layoutManager.tv&&scrollHelper.centerFocus.on(dlg.querySelector(".formDialogContent"),!1),dialogHelper.open(dlg),dlg.querySelector(".popupIdentifyForm").addEventListener("submit",function(e){return e.preventDefault(),searchForIdentificationResults(dlg),!1}),dlg.querySelector(".identifyOptionsForm").addEventListener("submit",function(e){return e.preventDefault(),submitIdentficationResult(dlg),!1}),dlg.querySelector(".btnCancel").addEventListener("click",function(e){dialogHelper.close(dlg)}),dlg.classList.add("identifyDialog"),showIdentificationForm(dlg,item),loading.hide()})})}function onDialogClosed(){loading.hide(),hasChanges?currentResolve():currentReject()}function showEditorFindNew(itemName,itemYear,itemType,resolveFunc){currentItem=null,currentItemType=itemType,require(["text!./itemidentifier.template.html"],function(template){var dialogOptions={size:"medium",removeOnClose:!0,scrollY:!1};layoutManager.tv&&(dialogOptions.size="fullscreen");var dlg=dialogHelper.createDialog(dialogOptions);dlg.classList.add("formDialog"),dlg.classList.add("recordingDialog");var html="";html+=globalize.translateDocument(template,"sharedcomponents"),dlg.innerHTML=html,layoutManager.tv&&scrollHelper.centerFocus.on(dlg.querySelector(".formDialogContent"),!1),dialogHelper.open(dlg),dlg.querySelector(".btnCancel").addEventListener("click",function(e){dialogHelper.close(dlg)}),dlg.querySelector(".popupIdentifyForm").addEventListener("submit",function(e){return e.preventDefault(),searchForIdentificationResults(dlg),!1}),dlg.addEventListener("close",function(){loading.hide();var foundItem=hasChanges?currentSearchResult:null;resolveFunc(foundItem)}),dlg.classList.add("identifyDialog"),showIdentificationFormFindNew(dlg,itemName,itemYear,itemType)})}function showIdentificationFormFindNew(dlg,itemName,itemYear,itemType){dlg.querySelector("#txtLookupName").value=itemName,"Person"===itemType||"BoxSet"===itemType?(dlg.querySelector(".fldLookupYear").classList.add("hide"),dlg.querySelector("#txtLookupYear").value=""):(dlg.querySelector(".fldLookupYear").classList.remove("hide"),dlg.querySelector("#txtLookupYear").value=itemYear),dlg.querySelector(".formDialogHeaderTitle").innerHTML=globalize.translate("sharedcomponents#Search")}var currentItem,currentItemType,currentServerId,currentResolve,currentReject,currentSearchResult,hasChanges=!1;return{show:function(itemId,serverId){return new Promise(function(resolve,reject){currentResolve=resolve,currentReject=reject,currentServerId=serverId,hasChanges=!1,showEditor(itemId)})},showFindNew:function(itemName,itemYear,itemType,serverId){return new Promise(function(resolve,reject){currentServerId=serverId,hasChanges=!1,showEditorFindNew(itemName,itemYear,itemType,resolve)})}}});