(function () {
    'use strict';

    // Launch Kerberos request
    $(document).on("portalLoaded", {}, function () {
      if (!window.datas.krbDone) {
        var e;
        e = jQuery.Event("kerberosAttempt");
        $(document).trigger(e);
        if (!e.isDefaultPrevented()) {
          return $.ajax("".concat(portal, "authkrb"), {
            dataType: 'json',
            // Get auth token from success response and post it
            success: function success(data) {
              e = jQuery.Event("kerberosSuccess");
              $(document).trigger(e, [data]);
              if (!e.isDefaultPrevented()) {
                if (data.ajax_auth_token) {
                  $('#lformKerberos').find('input[name="ajax_auth_token"]').attr("value", data.ajax_auth_token);
                }
                return $('#lformKerberos').submit();
              }
            },
            // Case else, will display PE_BADCREDENTIALS or fallback to next auth
            // backend
            error: function error(xhr, status, _error) {
              e = jQuery.Event("kerberosFailure");
              $(document).trigger(e, [xhr, status, _error]);

              // Check if we are in a choice menu
              var authMenu = $('#authMenu');

              // If this is a choice menu, don't submit form
              if (authMenu.length) {
                var msgBox = $('#errormsg');
                var msgBoxContent = '<div class="message message-negative' + ' alert alert-danger" role="alert">';
                // If this is a regular Kerberos authentication error,
                // display the appropriate error message
                if (_error.match(/Unauthorized/i)) {
                  msgBoxContent += '<span trmsg="5">' + translate('PE5') + '</span>';
                }
                // Display generic error message
                else {
                  msgBoxContent += '<span trmsg="24">' + translate('PE24') + '</span>';
                  // If this is an unexpected Kerberos error,
                  // display the error in console
                  console.error("Error while trying Kerberos authentication: " + _error);
                }
                msgBoxContent += '</div>';
                msgBox.html(msgBoxContent);
              }
              // If this is NOT a choice menu, submit form
              else {
                if (!e.isDefaultPrevented()) {
                  return $('#lformKerberos').submit();
                }
              }
            }
          });
        }
      }
    });

})();
