import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';

import { API_BASE_URL } from '../config/api.tokens';

/** Префиксы путей, которые НЕ проксируются через API base URL (статические ассеты). */
const STATIC_ASSET_PREFIXES = ['/svg/', '/fonts/', '/images/'];

export const apiBaseUrlInterceptor: HttpInterceptorFn = (req, next) => {
  const base = inject(API_BASE_URL);
  if (/^https?:\/\//i.test(req.url)) {
    return next(req);
  }
  const path = req.url.startsWith('/') ? req.url : `/${req.url}`;
  if (STATIC_ASSET_PREFIXES.some((p) => path.startsWith(p))) {
    return next(req);
  }
  return next(req.clone({ url: `${base}${path}` }));
};
