import {
  EnvironmentProviders,
  Provider,
  inject,
  provideEnvironmentInitializer,
} from '@angular/core';
import { AuthenticatedInitializerRunner } from './authenticated-initializer';
import { AuthenticationService } from './authentication.service';
import { TokenStore } from './token-store.service';

export const provideAuthentication = (): Array<Provider | EnvironmentProviders> => {
  return [
    TokenStore,
    provideEnvironmentInitializer(() => inject(AuthenticationService)),
    // Starts the authenticated-initializer runner so `provideAuthenticatedInitializer` callbacks
    // fire when the session is established (sign-in or already-valid session on load).
    provideEnvironmentInitializer(() => inject(AuthenticatedInitializerRunner)),
  ];
};
