import type { EnvironmentProviders, Provider } from '@angular/core';
import { TRANSLOCO_SCOPE } from '@jsverse/transloco';
import { provideApollo as provideApolloLibrary } from 'apollo-angular';
import { apolloFactory } from './apollo.factory';
import { auroraGraphqlAuthHandlerFactory } from './handlers/aurora-graphql-auth.handler';
import { auroraGraphqlErrorHandlerFactory } from './handlers/aurora-graphql-error.handler';
import { GRAPHQL_AUTH_HANDLER, GRAPHQL_ERROR_HANDLER } from './handlers/graphql.tokens';

export const provideApollo = (): Array<Provider | EnvironmentProviders> => [
  provideApolloLibrary(() => apolloFactory()),
  // Transparent token refresh is a framework default: wire the DI-aware auth handler so any app
  // using provideApollo() gets refresh-on-401 (single-flight, retry-once) without wiring it.
  { provide: GRAPHQL_AUTH_HANDLER, useFactory: auroraGraphqlAuthHandlerFactory },
  // Default GraphQL error presentation (Spartan sonner toasts). Apps may override
  // GRAPHQL_ERROR_HANDLER to present errors differently.
  { provide: GRAPHQL_ERROR_HANDLER, useFactory: auroraGraphqlErrorHandlerFactory },
  // apollo Error Translations
  {
    provide: TRANSLOCO_SCOPE,
    useValue: 'error',
    multi: true,
  },
];
