define([
  "exports",
  "./../modules/common/globalize.js",
  "./../modules/loading/loading.js",
  "./../modules/tabbedview/basetab.js",
  "./../modules/formhelper.js",
  "./../modules/emby-elements/emby-checkbox/emby-checkbox.js",
  "./../modules/emby-elements/emby-button/emby-button.js",
  "./../modules/emby-elements/emby-button/paper-icon-button-light.js",
  "./../modules/emby-elements/emby-select/emby-select.js",
  "./../modules/emby-elements/emby-scroller/emby-scroller.js",
  "./../modules/emby-elements/emby-itemscontainer/emby-itemscontainer.js",
], function (
  _exports,
  _globalize,
  _loading,
  _basetab,
  _formhelper,
  _embyCheckbox,
  _embyButton,
  _paperIconButtonLight,
  _embySelect,
  _embyScroller,
  _embyItemscontainer,
) {
  function loadDeleteFolders(view, user, mediaFolders) {
    ApiClient.getJSON(
      ApiClient.getUrl("Channels", { SupportsMediaDeletion: !0 }),
    ).then(function (channelsResult) {
      for (
        var select, evt, html = "", i = 0, length = mediaFolders.length;
        i < length;
        i++
      ) {
        var folder = mediaFolders[i],
          folderId = folder.Guid || folder.Id;
        html +=
          '<label><input type="checkbox" is="emby-checkbox" class="chkFolder" data-id="' +
          folderId +
          '" ' +
          (user.Policy.EnableContentDeletion ||
          -1 !== user.Policy.EnableContentDeletionFromFolders.indexOf(folderId)
            ? ' checked="checked"'
            : "") +
          "><span>" +
          folder.Name +
          "</span></label>";
      }
      for (i = 0, length = channelsResult.Items.length; i < length; i++) {
        var _folder = channelsResult.Items[i],
          _folderId = _folder.Guid || _folder.Id;
        html +=
          '<label><input type="checkbox" is="emby-checkbox" class="chkFolder" data-id="' +
          _folderId +
          '" ' +
          (user.Policy.EnableContentDeletion ||
          -1 !== user.Policy.EnableContentDeletionFromFolders.indexOf(_folderId)
            ? ' checked="checked"'
            : "") +
          "><span>" +
          _folder.Name +
          "</span></label>";
      }
      (view.querySelector(".deleteAccess").innerHTML = html),
        (view.querySelector(".chkEnableDeleteAllFolders").checked =
          user.Policy.EnableContentDeletion),
        (select = view.querySelector(".chkEnableDeleteAllFolders")),
        (evt = document.createEvent("HTMLEvents")).initEvent("change", !1, !0),
        select.dispatchEvent(evt);
    });
  }
  function loadUser(view, user) {
    ApiClient.getJSON(ApiClient.getUrl("Auth/Providers")).then(
      function (providers) {
        !(function (view, user, providers) {
          1 < providers.length && !user.Policy.IsAdministrator
            ? view
                .querySelector(".fldSelectLoginProvider")
                .classList.remove("hide")
            : view
                .querySelector(".fldSelectLoginProvider")
                .classList.add("hide");
          var currentProviderId = user.Policy.AuthenticationProviderId;
          view.querySelector(".selectLoginProvider").innerHTML = providers.map(
            function (provider) {
              var selected =
                provider.Id === currentProviderId || providers.length < 2
                  ? " selected"
                  : "";
              return (
                '<option value="' +
                provider.Id +
                '"' +
                selected +
                ">" +
                provider.Name +
                "</option>"
              );
            },
          );
        })(view, user, providers);
      },
    ),
      ApiClient.getJSON(
        ApiClient.getUrl("Library/MediaFolders", { IsHidden: !1 }),
      ).then(function (folders) {
        loadDeleteFolders(view, user, folders.Items);
      }),
      user.Policy.IsDisabled
        ? view.querySelector(".disabledUserBanner").classList.remove("hide")
        : view.querySelector(".disabledUserBanner").classList.add("hide"),
      view
        .querySelector(".lnkEditUserPreferences")
        .setAttribute(
          "href",
          "settings?userId=" + user.Id + "&serverId=" + user.ServerId,
        ),
      (view.querySelector(".txtUserName").value = user.Name),
      (view.querySelector(".txtConnectUserName").value =
        user.ConnectUserName || ""),
      (view.querySelector(".chkIsAdmin").checked = user.Policy.IsAdministrator),
      (view.querySelector(".chkDisabled").checked = user.Policy.IsDisabled),
      (view.querySelector(".chkIsHidden").checked = user.Policy.IsHidden),
      (view.querySelector(".chkIsHiddenRemotely").checked =
        user.Policy.IsHiddenRemotely || !1),
      (view.querySelector(".chkHideFromUnusedDevices").checked =
        user.Policy.IsHiddenFromUnusedDevices || !0),
      (view.querySelector(".chkRemoteControlSharedDevices").checked =
        user.Policy.EnableSharedDeviceControl),
      (view.querySelector(".chkEnableRemoteControlOtherUsers").checked =
        user.Policy.EnableRemoteControlOfOtherUsers),
      (view.querySelector(".chkEnableDownloading").checked =
        user.Policy.EnableContentDownloading),
      (view.querySelector(".chkEnableSubtitleDownloading").checked =
        user.Policy.EnableSubtitleDownloading || !1),
      (view.querySelector(".chkEnableSubtitleManagement").checked =
        user.Policy.EnableSubtitleManagement || !1),
      (view.querySelector(".chkManageLiveTv").checked =
        user.Policy.EnableLiveTvManagement),
      (view.querySelector(".chkEnableLiveTvAccess").checked =
        user.Policy.EnableLiveTvAccess),
      (view.querySelector(".chkEnableMediaPlayback").checked =
        user.Policy.EnableMediaPlayback),
      (view.querySelector(".chkEnableAudioPlaybackTranscoding").checked =
        user.Policy.EnableAudioPlaybackTranscoding),
      (view.querySelector(".chkEnableVideoPlaybackTranscoding").checked =
        user.Policy.EnableVideoPlaybackTranscoding),
      (view.querySelector(".chkEnableVideoPlaybackRemuxing").checked =
        user.Policy.EnablePlaybackRemuxing),
      (view.querySelector(".chkRemoteAccess").checked =
        null == user.Policy.EnableRemoteAccess ||
        user.Policy.EnableRemoteAccess),
      (view.querySelector(".chkAllowChangeProfile").checked =
        user.Policy.EnableUserPreferenceAccess),
      (view.querySelector(".chkEnableSyncTranscoding").checked =
        user.Policy.EnableSyncTranscoding),
      (view.querySelector(".chkEnableConversion").checked =
        user.Policy.EnableMediaConversion || !1),
      (view.querySelector(".chkEnableSharing").checked =
        user.Policy.EnablePublicSharing),
      (view.querySelector(".txtRemoteClientBitrateLimit").value =
        user.Policy.RemoteClientBitrateLimit / 1e6 || ""),
      (view.querySelector(".selectStreamLimit").value =
        user.Policy.SimultaneousStreamLimit || "0");
    for (
      var chkDynamicFeatures = view.querySelectorAll(".chkDynamicFeature"),
        restrictedFeatures = user.Policy.RestrictedFeatures || [],
        i = 0,
        length = chkDynamicFeatures.length;
      i < length;
      i++
    )
      chkDynamicFeatures[i].checked = !restrictedFeatures.includes(
        chkDynamicFeatures[i].getAttribute("data-id"),
      );
    user.Policy.IsAdministrator && !user.Policy.IsDisabled
      ? view.querySelector(".fldIsEnabled").classList.add("hide")
      : view.querySelector(".fldIsEnabled").classList.remove("hide"),
      _loading.default.hide();
  }
  function onSubmit(e) {
    var instance = this,
      view = this.view;
    return (
      _loading.default.show(),
      getUser(instance).then(function (result) {
        !(function (instance, user, view) {
          (user.Name = view.querySelector(".txtUserName").value),
            (user.Policy.IsAdministrator =
              view.querySelector(".chkIsAdmin").checked),
            (user.Policy.IsHidden = view.querySelector(".chkIsHidden").checked),
            (user.Policy.IsHiddenRemotely = view.querySelector(
              ".chkIsHiddenRemotely",
            ).checked),
            (user.Policy.IsHiddenFromUnusedDevices = view.querySelector(
              ".chkHideFromUnusedDevices",
            ).checked),
            (user.Policy.IsDisabled =
              view.querySelector(".chkDisabled").checked),
            (user.Policy.EnableRemoteControlOfOtherUsers = view.querySelector(
              ".chkEnableRemoteControlOtherUsers",
            ).checked),
            (user.Policy.EnableLiveTvManagement =
              view.querySelector(".chkManageLiveTv").checked),
            (user.Policy.EnableLiveTvAccess = view.querySelector(
              ".chkEnableLiveTvAccess",
            ).checked),
            (user.Policy.EnableSharedDeviceControl = view.querySelector(
              ".chkRemoteControlSharedDevices",
            ).checked),
            (user.Policy.EnableMediaPlayback = view.querySelector(
              ".chkEnableMediaPlayback",
            ).checked),
            (user.Policy.EnableAudioPlaybackTranscoding = view.querySelector(
              ".chkEnableAudioPlaybackTranscoding",
            ).checked),
            (user.Policy.EnableVideoPlaybackTranscoding = view.querySelector(
              ".chkEnableVideoPlaybackTranscoding",
            ).checked),
            (user.Policy.EnablePlaybackRemuxing = view.querySelector(
              ".chkEnableVideoPlaybackRemuxing",
            ).checked),
            (user.Policy.EnableSubtitleDownloading = view.querySelector(
              ".chkEnableSubtitleDownloading",
            ).checked),
            (user.Policy.EnableSubtitleManagement = view.querySelector(
              ".chkEnableSubtitleManagement",
            ).checked),
            (user.Policy.EnableContentDownloading = view.querySelector(
              ".chkEnableDownloading",
            ).checked),
            (user.Policy.EnableSyncTranscoding = view.querySelector(
              ".chkEnableSyncTranscoding",
            ).checked),
            (user.Policy.EnableMediaConversion = view.querySelector(
              ".chkEnableConversion",
            ).checked),
            (user.Policy.EnablePublicSharing =
              view.querySelector(".chkEnableSharing").checked),
            (user.Policy.EnableRemoteAccess =
              view.querySelector(".chkRemoteAccess").checked),
            (user.Policy.RemoteClientBitrateLimit = parseInt(
              1e6 *
                parseFloat(
                  view.querySelector(".txtRemoteClientBitrateLimit").value ||
                    "0",
                ),
            )),
            (user.Policy.AuthenticationProviderId = view.querySelector(
              ".selectLoginProvider",
            ).value),
            (user.Policy.SimultaneousStreamLimit =
              view.querySelector(".selectStreamLimit").value),
            (user.Policy.EnableUserPreferenceAccess = view.querySelector(
              ".chkAllowChangeProfile",
            ).checked),
            (user.Policy.EnableContentDeletion = view.querySelector(
              ".chkEnableDeleteAllFolders",
            ).checked),
            (user.Policy.EnableContentDeletionFromFolders = user.Policy
              .EnableContentDeletion
              ? []
              : Array.prototype.filter
                  .call(view.querySelectorAll(".chkFolder"), function (c) {
                    return c.checked;
                  })
                  .map(function (c) {
                    return c.getAttribute("data-id");
                  })),
            (user.Policy.RestrictedFeatures = Array.prototype.filter
              .call(view.querySelectorAll(".chkDynamicFeature"), function (c) {
                return !c.checked;
              })
              .map(function (c) {
                return c.getAttribute("data-id");
              })),
            ApiClient.updateUser(user).then(function () {
              ApiClient.updateUserPolicy(user.Id, user.Policy).then(
                function () {
                  !(function (instance, user) {
                    _loading.default.hide();
                    var currentConnectUsername =
                        instance.currentConnectUsername || "",
                      enteredConnectUsername = instance.view.querySelector(
                        ".txtConnectUserName",
                      ).value;
                    currentConnectUsername === enteredConnectUsername
                      ? _formhelper.default.handleConfigurationSavedResponse()
                      : require(["connectHelper"], function (connectHelper) {
                          connectHelper
                            .updateUserLink(
                              ApiClient,
                              user,
                              enteredConnectUsername,
                            )
                            .then(function () {
                              loadData(instance);
                            });
                        });
                  })(instance, user);
                },
              );
            });
        })(instance, result, view);
      }),
      e.preventDefault(),
      e.stopPropagation(),
      !1
    );
  }
  function getUser(instance) {
    instance = instance.params.userId;
    return ApiClient.getUser(instance, !1);
  }
  function getFeatureHtml(feature) {
    var html = "";
    return (
      html +
      "<label>" +
      ('<input type="checkbox" is="emby-checkbox" class="' +
        (feature.className || "chkDynamicFeature") +
        '" data-id="' +
        feature.Id +
        '" />') +
      ("<span>" + feature.Name + "</span>") +
      "</label>"
    );
  }
  function showFeature(feature) {
    return !feature.Id.includes(".");
  }
  function loadData(instance, options) {
    _loading.default.show(),
      getUser(instance).then(function (user) {
        (instance.currentConnectUsername = user.ConnectUserName),
          ApiClient.getFeatures({ FeatureType: "User" }).then(
            function (features) {
              !(function (view, features) {
                features.unshift(
                  {
                    Id: "livetv",
                    Name: _globalize.default.translate("LiveTV"),
                    className: "chkEnableLiveTvAccess",
                  },
                  {
                    Id: "livetv_manage",
                    Name: _globalize.default.translate(
                      "LiveTVRecordingManagement",
                    ),
                    className: "chkManageLiveTv",
                  },
                ),
                  (view.querySelector(".featureList").innerHTML = features
                    .filter(showFeature)
                    .map(getFeatureHtml)
                    .join(""));
              })(instance.view, features),
                loadUser(instance.view, user),
                null != options && options.autoFocus && instance.autoFocus();
            },
          );
      });
  }
  function InfoTab(view, params, options) {
    _basetab.default.apply(this, arguments),
      (this.apiClient = ApiClient),
      (function (view) {
        for (
          var html =
              '<option value="0">' +
              _globalize.default.translate("Unlimited") +
              "</option>",
            i = 1;
          i <= 50;
          i++
        )
          html += '<option value="' + i + '">' + i + "</option>";
        view.querySelector(".selectStreamLimit").innerHTML = html;
      })(view),
      (view.querySelector(".streamLimitPremiereInfo").innerHTML =
        _globalize.default.translate(
          "FeatureRequiresEmbyPremiere",
          '<a href="https://emby.media/premiere" data-preset="premiereinfo" is="emby-linkbutton" type="button" class="button-link">',
          "</a>",
        )),
      view
        .querySelector(".editUserProfileForm")
        .addEventListener("submit", onSubmit.bind(this)),
      (view.querySelector(".sharingHelp").innerHTML =
        _globalize.default.translate("OptionAllowLinkSharingHelp", 30)),
      view
        .querySelector(".chkEnableDeleteAllFolders", view)
        .addEventListener("change", function () {
          this.checked
            ? view.querySelector(".deleteAccess").classList.add("hide")
            : view.querySelector(".deleteAccess").classList.remove("hide");
        }),
      ApiClient.getServerConfiguration().then(function (config) {
        config.EnableRemoteAccess
          ? view.querySelector(".fldRemoteAccess").classList.remove("hide")
          : view.querySelector(".fldRemoteAccess").classList.add("hide");
      });
    for (
      var userId = params.userId,
        btns = view.querySelectorAll(".userEditTabButton"),
        i = 0,
        length = btns.length;
      i < length;
      i++
    )
      btns[i].href =
        btns[i].getAttribute("data-href") +
        "?userId=" +
        userId +
        "&serverId=" +
        ApiClient.serverId();
  }
  Object.defineProperty(_exports, "__esModule", { value: !0 }),
    (_exports.default = void 0),
    Object.assign(InfoTab.prototype, _basetab.default.prototype),
    (InfoTab.prototype.onResume = function (options) {
      _basetab.default.prototype.onResume.apply(this, arguments),
        loadData(this, options);
    }),
    (InfoTab.prototype.destroy = function () {
      _basetab.default.prototype.destroy.apply(this, arguments),
        (this.apiClient = null);
    }),
    (_exports.default = InfoTab);
});
