define([
  "exports",
  "./../modules/emby-elements/emby-scroller/emby-scroller.js",
  "./../modules/emby-elements/emby-button/emby-button.js",
  "./../modules/emby-apiclient/connectionmanager.js",
  "./../modules/common/globalize.js",
  "./../modules/loading/loading.js",
  "./../modules/emby-elements/emby-itemscontainer/emby-itemscontainer.js",
  "./../modules/layoutmanager.js",
  "./../modules/cardbuilder/cardbuilder.js",
  "./../modules/common/itemmanager/itemmanager.js",
], function (
  _exports,
  _embyScroller,
  _embyButton,
  _connectionmanager,
  _globalize,
  _loading,
  _embyItemscontainer,
  _layoutmanager,
  _cardbuilder,
  _itemmanager,
) {
  function getCardOptionsForType(type, items) {
    var options;
    return "BoxSet" === type ||
      "Playlist" === type ||
      "Game" === type ||
      "Book" === type ||
      "PhotoAlbum" === type ||
      "MusicArtist" === type ||
      "Person" === type
      ? {
          shape: "autooverflow",
          fields: ["Name"],
          focusTransformTitleAdjust: !0,
        }
      : "Movie" === type || "Trailer" === type || "Series" === type
      ? {
          shape: "autooverflow",
          fields: ["Name", "ProductionYear"],
          focusTransformTitleAdjust: !0,
        }
      : "Photo" === type
      ? {
          shape: "autooverflow",
          fields: ["Name"],
          focusTransformTitleAdjust: !0,
        }
      : "Audio" === type
      ? {
          shape: "autooverflow",
          preferArtistTitle: !0,
          fields: ["ParentName", "Name"],
          action: "play",
          sideFooter: !0,
          focusTransformTitleAdjust: !0,
        }
      : "TvChannel" === type
      ? {
          shape: "autooverflow",
          fields: ["Name", "CurrentProgramParentName", "CurrentProgramTime"],
          defaultBackground: !0,
          focusTransformTitleAdjust: !0,
        }
      : "Program" === type
      ? ((function (options, type, items) {
          "Program" === type &&
            items.length &&
            items[0].AsSeries &&
            ((options.progress = !1),
            (options.showAirDateTime = !1),
            (options.fields = options.fields
              .filter(isNotName)
              .filter(isNotParentName)
              .filter(isNotAirTime)),
            options.fields.push("ParentNameOrName"));
        })(
          (options = {
            shape: "autooverflow",
            fields: ["ParentName", "Name", "AirTime"],
            showAirDateTime: !0,
            focusTransformTitleAdjust: !0,
          }),
          type,
          items,
        ),
        options)
      : "Tag" === type
      ? {
          shape: "autooverflow",
          fields: ["Name"],
          defaultBackground: !0,
          multiSelect: !1,
          focusTransformTitleAdjust: !0,
        }
      : {
          shape: "autooverflow",
          fields: ["ParentName", "Name"],
          focusTransformTitleAdjust: !0,
        };
  }
  function isNotName(n) {
    return "Name" !== n;
  }
  function isNotParentName(n) {
    return "ParentName" !== n;
  }
  function isNotAirTime(n) {
    return "AirTime" !== n;
  }
  function renderSearchTypes(instance, apiClient, context, value, types) {
    for (
      var searchCategories = context.querySelector(".searchCategories"),
        promises = ((searchCategories.innerHTML = ""), []),
        i = 0,
        length = types.length;
      i < length;
      i++
    ) {
      var type = (type = types[i]).Name || type,
        elem = document.createElement("div"),
        innerHTML =
          ((elem.className = "hide verticalSection albumResults focusable"),
          "Audio" === type &&
            elem.classList.add("verticalSection-extrabottompadding"),
          '<h2 class="sectionTitle sectionTitle-cards focuscontainer-x padded-left padded-right">' +
            _itemmanager.default.getPluralItemTypeName(type) +
            "</h2>"),
        itemsContainerClass = "focuscontainer-x itemsContainer scrollSlider";
      "Audio" === type &&
        (itemsContainerClass += " itemsContainer-sideFooters"),
        (innerHTML =
          (innerHTML +=
            '<div is="emby-scroller" data-horizontal="true" data-focusscroll="true" data-mousewheel="false" class="padded-top-focusscale padded-bottom-focusscale padded-left padded-right">') +
          ('<div is="emby-itemscontainer" class="' +
            itemsContainerClass +
            '"></div>') +
          "</div>"),
        (elem.innerHTML = innerHTML),
        elem.setAttribute("data-focusabletype", "nearest"),
        searchCategories.appendChild(elem),
        promises.push(
          (function (instance, apiClient, query, context, section) {
            return (
              (query.Limit = 16),
              (query.ParentId = instance.options.parentId),
              (function (instance, apiClient, query) {
                (query.Recursive = !0),
                  (query.EnableTotalRecordCount = !1),
                  (query.ImageTypeLimit = 1),
                  (query.Fields = getSearchFields(
                    instance,
                    query.IncludeItemTypes,
                  )),
                  "Program" === query.IncludeItemTypes &&
                    (query.GroupProgramsBySeries = !0);
                "Program" === query.IncludeItemTypes && (query.HasAired = !1);
                return apiClient.getItems(apiClient.getCurrentUserId(), query);
              })(instance, apiClient, query).then(function (result) {
                var items = result.Items || result.SearchHints,
                  items = getCardOptionsForType(query.IncludeItemTypes, items);
                return populateResults(result, context, section, items);
              })
            );
          })(
            instance,
            apiClient,
            { searchTerm: value, IncludeItemTypes: type },
            context,
            elem,
          ),
        );
    }
    return Promise.all(promises);
  }
  function getSearchFields(instance, type) {
    instance =
      instance.options.requestedItemFields +
      ",PrimaryImageAspectRatio,ProductionYear";
    return (
      (type && "Series" !== type) || (instance += ",Status,EndDate"), instance
    );
  }
  function populateResults(result, context, section, cardOptions) {
    (result = result.Items || result.SearchHints),
      (context = (section =
        "string" == typeof section
          ? context.querySelector(section)
          : section).querySelector(".itemsContainer"));
    return (
      _cardbuilder.default.buildCards(
        result,
        Object.assign(
          {
            itemsContainer: context,
            parentContainer: section,
            shape: "autooverflow",
            scalable: !0,
            focusTransformTitleAdjust: !0,
          },
          cardOptions || {},
        ),
      ),
      section
        .querySelector(".emby-scroller")
        .scrollToBeginning({ behavior: "instant" }),
      result
    );
  }
  function SearchResults(options) {
    var elem, instance;
    (this.options = options),
      (elem = options.element),
      (instance = this),
      elem.classList.add("searchResults"),
      instance.search("");
  }
  Object.defineProperty(_exports, "__esModule", { value: !0 }),
    (_exports.default = void 0),
    (SearchResults.prototype.search = function (value) {
      _loading.default.show();
      var apiClient = _connectionmanager.default.getApiClient(
          this.options.serverId,
        ),
        elem = this.options.element,
        searchInfo =
          (elem.querySelector(".noResults").classList.add("hide"),
          { instance: this, searchTerm: value });
      return (function (instance, apiClient, context, value) {
        (value || _layoutmanager.default.tv) &&
          context.querySelector(".searchSuggestions").classList.add("hide");
        var query = {
          SearchTerm: value,
          Recursive: !0,
          EnableTotalRecordCount: !1,
          ImageTypeLimit: 1,
          Limit: 16,
          ParentId: instance.options.parentId,
          Fields: getSearchFields(instance),
          GroupProgramsBySeries: !0,
        };
        return apiClient.getSearchResults(query).then(function (result) {
          var subPromises = [];
          return (
            subPromises.push(
              populateResults(
                result,
                context,
                context.querySelector(".globalResults"),
                {
                  shape: "autooverflow",
                  fields: [
                    "ParentName",
                    "Name",
                    "Type",
                    "CurrentProgramParentName",
                    "AirTime",
                  ],
                  showAirDateTime: !0,
                  lines: 4,
                },
              ),
            ),
            subPromises.push(
              renderSearchTypes(
                instance,
                apiClient,
                context,
                value,
                result.ItemTypes,
              ),
            ),
            Promise.all(subPromises)
          );
        });
      })(this, apiClient, elem, value).then(
        function (itemsResponses) {
          _loading.default.hide();
          var hasResults,
            i,
            length,
            options = this.instance.options;
          if (options) {
            if (
              (itemsResponses[0].length && (hasResults = !0),
              (itemsResponses = itemsResponses[1]).length)
            )
              for (i = 0, length = itemsResponses.length; i < length; i++)
                if (itemsResponses[i].length) {
                  hasResults = !0;
                  break;
                }
            options = options.element.querySelector(".noResults");
            hasResults || !this.searchTerm
              ? options.classList.add("hide")
              : (this.searchTerm.length < 2
                  ? (options.innerHTML = _globalize.default.translate(
                      "TwoSearchCharsRequired",
                    ))
                  : (options.innerHTML = _globalize.default.translate(
                      "NoItemsMatchingFound",
                    )),
                options.classList.remove("hide"));
          }
        }.bind(searchInfo),
        function () {
          _loading.default.hide();
          var options = this.options;
          options &&
            options.element
              .querySelector(".noResults")
              .classList.remove("hide");
        }.bind(this),
      );
    }),
    (SearchResults.prototype.destroy = function () {
      var options = this.options;
      options && options.element.classList.remove("searchFields"),
        (this.options = null);
    }),
    (_exports.default = SearchResults);
});
