<%_ if (!authenticationTypeOauth2) { _%>
import Vue from 'vue';
<%_ } _%>
<%_ if (!authenticationTypeJwt) { _%>
import axios, { type AxiosPromise } from 'axios';
<%_ } _%>

export default class LoginService {
<%_ if (!authenticationTypeOauth2) { _%>
  private emit: (event: string, ...args: any[]) => void;

  constructor({ emit }: { emit: (event: string, ...args: any[]) => void }) {
    this.emit = emit;
  }

  public openLogin(): void {
    this.emit('bv::show::modal', 'login-page');
  }

  public hideLogin(): void {
    this.emit('bv::hide::modal', 'login-page');
  }
<%_ } else { _%>

  public login(loc: { href: string; hostname: string; pathname: string; port?: string } = window.location) {
    const port = loc.port ? `:${loc.port}` : '';
    let contextPath = loc.pathname;
    if (contextPath.endsWith('accessdenied')) {
      contextPath = contextPath.substring(0, contextPath.indexOf('accessdenied'));
    }
    if (contextPath.endsWith('forbidden')) {
      contextPath = contextPath.substring(0, contextPath.indexOf('forbidden'));
    }
    if (!contextPath.endsWith('/')) {
      contextPath = contextPath + '/';
    }
    // If you have configured multiple OIDC providers, then, you can update this URL to /login.
    // It will show a Spring Security generated login page with links to configured OIDC providers.
    loc.href = `//${loc.hostname}${port}${contextPath}oauth2/authorization/oidc`;
  }
<%_ } _%>
<%_ if (!authenticationTypeJwt) { _%>

  public logout() : AxiosPromise<any> {
    return axios.post('api/logout');
  }
<%_ } _%>
}
