function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
 * Copyright 2017 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
// keycloak.js downloaded in index.ftl
export class KeycloakService {
  constructor() {}

  static get Instance() {
    return this.instance;
  }
  /**
   * Configure and initialize the Keycloak adapter.
   *
   * @param configOptions Optionally, a path to keycloak.json, or an object containing
   *                      url, realm, and clientId.
   * @param adapterOptions Optional initiaization options.  See javascript adapter docs
   *                       for details.
   * @returns {Promise<T>}
   */


  static init(configOptions, initOptions = {}) {
    KeycloakService.keycloakAuth = Keycloak(configOptions);
    return new Promise((resolve, reject) => {
      KeycloakService.keycloakAuth.init(initOptions).success(() => {
        resolve();
      }).error(errorData => {
        reject(errorData);
      });
    });
  }

  authenticated() {
    return KeycloakService.keycloakAuth.authenticated ? KeycloakService.keycloakAuth.authenticated : false;
  }

  login(options) {
    KeycloakService.keycloakAuth.login(options);
  }

  logout(redirectUri) {
    KeycloakService.keycloakAuth.logout({
      redirectUri: redirectUri
    });
  }

  account() {
    KeycloakService.keycloakAuth.accountManagement();
  }

  authServerUrl() {
    const authServerUrl = KeycloakService.keycloakAuth.authServerUrl;
    return authServerUrl.charAt(authServerUrl.length - 1) === '/' ? authServerUrl : authServerUrl + '/';
  }

  realm() {
    return KeycloakService.keycloakAuth.realm;
  }

  getToken() {
    return new Promise((resolve, reject) => {
      if (KeycloakService.keycloakAuth.token) {
        KeycloakService.keycloakAuth.updateToken(5).success(() => {
          resolve(KeycloakService.keycloakAuth.token);
        }).error(() => {
          reject('Failed to refresh token');
        });
      } else {
        reject('Not loggen in');
      }
    });
  }

}

_defineProperty(KeycloakService, "keycloakAuth", keycloak);

_defineProperty(KeycloakService, "instance", new KeycloakService());
//# sourceMappingURL=keycloak.service.js.map