import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { ApiConfigService } from '../core/config/api-config.service';

export const apiBaseUrlInterceptor: HttpInterceptorFn = (req, next) => {
  const apiConfig = inject(ApiConfigService);

  if (!req.url.startsWith('/api')) {
    return next(req);
  }

  const resolvedUrl = apiConfig.toApiUrl(req.url);
  if (resolvedUrl === req.url) {
    return next(req);
  }

  return next(req.clone({ url: resolvedUrl }));
};