define(["dialogHelper", "loading", "connectionManager", "require", "globalize", "scrollHelper", "layoutManager", "focusManager", "browser", "emby-input", "emby-checkbox", "paper-icon-button-light", "css!./../formdialog", "material-icons", "cardStyle"], function(dialogHelper, loading, connectionManager, require, globalize, scrollHelper, layoutManager, focusManager, browser) {
"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 < length; i++)(value = identifyField[i].value) && ("number" === identifyField[i].type && (value = parseInt(value)), lookupInfo[identifyField[i].getAttribute("data-lookup")] = value);
var hasId = !1,
txtLookupId = page.querySelectorAll(".txtLookupId");
for (i = 0, length = txtLookupId.length; i < length; i++) value = txtLookupId[i].value, value && (hasId = !0), lookupInfo.ProviderIds[txtLookupId[i].getAttribute("data-providerkey")] = value;
if (!hasId && !lookupInfo.Name) return void require(["toast"], function(toast) {
toast(globalize.translate("sharedcomponents#PleaseEnterNameOrId"))
});
currentItem && currentItem.GameSystem && (lookupInfo.GameSystem = currentItem.GameSystem), lookupInfo = {
SearchInfo: lookupInfo
}, currentItem && currentItem.Id ? lookupInfo.ItemId = currentItem.Id : lookupInfo.IncludeDisabledProviders = !0, loading.show();
var apiClient = getApiClient();
apiClient.ajax({
type: "POST",
url: apiClient.getUrl("Items/RemoteSearch/" + currentItemType),
data: JSON.stringify(lookupInfo),
contentType: "application/json",
dataType: "json"
}).then(function(results) {
loading.hide(), showIdentificationSearchResults(page, results)
})
}
function showIdentificationSearchResults(page, results) {
function onSearchImageClick() {
var index = parseInt(this.getAttribute("data-index")),
currentResult = results[index];
null != currentItem ? showIdentifyOptions(page, currentResult) : finishFindNewDialog(page, currentResult)
}
var identificationSearchResults = page.querySelector(".identificationSearchResults");
page.querySelector(".popupIdentifyForm").classList.add("hide"), identificationSearchResults.classList.remove("hide"), page.querySelector(".identifyOptionsForm").classList.add("hide"), page.querySelector(".dialogContentInner").classList.remove("dialog-content-centered");
var i, length, html = "";
for (i = 0, length = results.length; i < length; i++) {
html += getSearchResultHtml(results[i], i)
}
var elem = page.querySelector(".identificationSearchResultList");
elem.innerHTML = html;
var searchImages = elem.querySelectorAll(".card");
for (i = 0, length = searchImages.length; i < length; i++) searchImages[i].addEventListener("click", onSearchImageClick);
layoutManager.tv && focusManager.autoFocus(identificationSearchResults)
}
function finishFindNewDialog(dlg, identifyResult) {
currentSearchResult = identifyResult, hasChanges = !0, loading.hide(), dialogHelper.close(dlg)
}
function showIdentifyOptions(page, identifyResult) {
var identifyOptionsForm = page.querySelector(".identifyOptionsForm");
page.querySelector(".popupIdentifyForm").classList.add("hide"), page.querySelector(".identificationSearchResults").classList.add("hide"), identifyOptionsForm.classList.remove("hide"), page.querySelector("#chkIdentifyReplaceImages").checked = !0, page.querySelector(".dialogContentInner").classList.add("dialog-content-centered"), currentSearchResult = identifyResult;
var lines = [];
lines.push(identifyResult.Name), identifyResult.ProductionYear && lines.push(identifyResult.ProductionYear), identifyResult.GameSystem && lines.push(identifyResult.GameSystem);
var resultHtml = lines.join("
");
if (identifyResult.ImageUrl) {
resultHtml = '