/*
 * Copyright (c) 2019, FusionAuth, All Rights Reserved
 */
'use strict';

var FusionAuth = FusionAuth || {};
FusionAuth.Admin = FusionAuth.Admin || {};

/**
 * Handles the API key stuff.
 *
 * @constructor
 */
FusionAuth.Admin.APIKeys = function() {
  Prime.Utils.bindAll(this);

  Prime.Document.query('table').each(function(item) {
    new FusionAuth.UI.Listing(item)
        .initialize();
  });

  // Bind the reveal action
  Prime.Document.addDelegatedEventListener('click', '#api-key-reveal-form button', this._handleRevealClick);
};

FusionAuth.Admin.APIKeys.constructor = FusionAuth.Admin.APIKeys;
FusionAuth.Admin.APIKeys.prototype = {

  /* ===================================================================================================================
   * Private methods
   * ===================================================================================================================*/

  _handleRevealClick: function(event) {
    Prime.Utils.stopEvent(event);
    var form = Prime.Document.queryById('api-key-reveal-form');
    new Prime.Ajax.Request(form.getAttribute('action'), 'POST')
        .withDataFromForm(form)
        .withSuccessHandler(this._handleSecretReveal)
        .go();
  },

  _handleSecretReveal: function(xhr) {
    var field = Prime.Document.queryById('api-key-value');
    field.setHTML(xhr.responseText);
  }
};