import { HttpInterceptorFn, provideHttpClient, withInterceptors } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { Config } from '@core/config';
import { AvConfiguration } from '@generated/swagger/availability-api';
import { CatConfiguration } from '@generated/swagger/cat-search-api';
import { CheckoutConfiguration } from '@generated/swagger/checkout-api';
import { CrmConfiguration } from '@generated/swagger/crm-api';
import { EisConfiguration } from '@generated/swagger/eis-api';
import { IsaConfiguration } from '@generated/swagger/isa-api';
import { OmsConfiguration } from '@generated/swagger/oms-api';
import { PrintConfiguration } from '@generated/swagger/print-api';
import { RemiConfiguration } from '@generated/swagger/inventory-api';
import { WwsConfiguration } from '@generated/swagger/wws-api';

export function createConfigurationFactory(name: string) {
  return function (config: Config): { rootUrl: string } {
    return config.get(`@swagger/${name}`);
  };
}

const serviceWorkerInterceptor: HttpInterceptorFn = (req, next) => {
  return next(req.clone({ setHeaders: { 'ngsw-bypass': 'true' } }));
};

@NgModule({
  providers: [
    provideHttpClient(withInterceptors([serviceWorkerInterceptor])),
    { provide: AvConfiguration, useFactory: createConfigurationFactory('av'), deps: [Config] },
    { provide: CatConfiguration, useFactory: createConfigurationFactory('cat'), deps: [Config] },
    { provide: CheckoutConfiguration, useFactory: createConfigurationFactory('checkout'), deps: [Config] },
    { provide: CrmConfiguration, useFactory: createConfigurationFactory('crm'), deps: [Config] },
    { provide: EisConfiguration, useFactory: createConfigurationFactory('eis'), deps: [Config] },
    { provide: IsaConfiguration, useFactory: createConfigurationFactory('isa'), deps: [Config] },
    { provide: OmsConfiguration, useFactory: createConfigurationFactory('oms'), deps: [Config] },
    { provide: PrintConfiguration, useFactory: createConfigurationFactory('print'), deps: [Config] },
    { provide: RemiConfiguration, useFactory: createConfigurationFactory('remi'), deps: [Config] },
    { provide: WwsConfiguration, useFactory: createConfigurationFactory('wws'), deps: [Config] },
  ],
})
export class AppSwaggerModule {}
