import { bypass } from '../vue/resolver.mjs';
import { resetEnvMode } from '../../../constants/key.mjs';
import { getEnvConfig } from './createModes.mjs';

const normalizePath = (path) => {
  return path.endsWith("/") ? path.slice(0, -1) : path;
};
const isPathExcluded = (path, excludePaths) => {
  if (!excludePaths || excludePaths.length === 0) return false;
  const normalizedPath = normalizePath(path);
  return excludePaths.some((excludePath) => normalizePath(excludePath) === normalizedPath);
};
const API_SERVICE_TYPES = ["/third/gstore", "/third/am", "/guard", "/fe/basic", "/middle", "/product-center"];
const resolveBaseUrl = (mode) => {
  return getEnvConfig(mode).url;
};
const generateProxyConfig = (mode, excludePaths) => {
  const config = {};
  const giimeMode = mode || "development";
  const baseUrl = resolveBaseUrl(giimeMode);
  API_SERVICE_TYPES.forEach((serviceType) => {
    if (isPathExcluded(serviceType, excludePaths)) return;
    config[serviceType] = {
      target: baseUrl,
      changeOrigin: true,
      rewrite: (p) => p.replace(new RegExp(`^${serviceType.replace(/\//g, "\\/")}`), serviceType),
      bypass
    };
  });
  return config;
};
const giimeDevProxy = generateProxyConfig();
const autoGenerateProxy = (env, options) => {
  const proxy = {};
  const giimeMode = env.VITE_GIIME_MODE || "development";
  const proxyTarget = resolveBaseUrl(giimeMode);
  const processedPaths = /* @__PURE__ */ new Set();
  const excludeKeys = options?.excludeKeys || [];
  const excludePaths = options?.excludePaths;
  const disableLocal = options?.disableLocal || false;
  for (const [key, value] of Object.entries(env)) {
    if (!key.startsWith("VITE_")) continue;
    if (excludeKeys.includes(key)) continue;
    if (key === "VITE_BASE_URL") continue;
    const cleanValue = value.replace(/['"]/g, "").trim();
    if (!cleanValue.startsWith("/") || cleanValue === "/") continue;
    if (disableLocal && giimeMode === "development") continue;
    const normalizedPath = normalizePath(cleanValue);
    if (isPathExcluded(normalizedPath, excludePaths)) continue;
    if (processedPaths.has(normalizedPath)) continue;
    proxy[normalizedPath] = {
      target: proxyTarget,
      changeOrigin: true,
      rewrite: (p) => p.replace(new RegExp(`^${normalizedPath.replace(/\//g, "\\/")}`), normalizedPath),
      bypass
    };
    processedPaths.add(normalizedPath);
  }
  return proxy;
};
const giimeDevProxyFn = (env, options) => {
  const mode = env[resetEnvMode];
  return {
    ...generateProxyConfig(mode, options?.excludePaths),
    // 预定义的服务类型配置
    ...autoGenerateProxy(env, options)
    // 自动扫描的配置
  };
};

export { autoGenerateProxy, giimeDevProxy, giimeDevProxyFn };
//# sourceMappingURL=devProxy.mjs.map
