import {Injectable} from '@angular/core';
import {Inject} from '@angular/core';
import {DOCUMENT} from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  // check if we have auth at load, or redirect to the given app url
  app_check_url: string = "https://example.com/check";
  app_auth_url: string = "https://example.com/auth";
  auth_data: any = null;
  private window: WindowProxy & typeof globalThis | null;

  constructor(@Inject(DOCUMENT) private document: Document) {
    this.window = this.document.defaultView;
    this.checkAuthData()
  }

  checkAuthData() {
    if (this.auth_data) {

      fetch(this.app_auth_url).then(resp => {
        if (resp.status !== 200) {

          this.redirectToAuth()
        }
      })
    }
  }

  redirectToAuth() {
    this.window?.open(this.app_auth_url)
  }
}
