import { defineConfig, mergeConfig } from "vite";
import {
  commonViteConfig,
  getAllowedHosts,
  hostFromUrl,
} from "../common/config/vite.base";

const buildOutDir =
  process.env.DEVFRONT_BUILD_OUT_DIR ?? "/tmp/baron-sso-devfront-dist";
const devfrontHost = hostFromUrl(process.env.DEVFRONT_URL);

const allowedHosts = getAllowedHosts(
  ["sdev.hmac.kr", "localhost", "172.16.10.176", "127.0.0.1"],
  devfrontHost,
  process.env.DEVFRONT_ALLOWED_HOSTS,
);

export default defineConfig(
  mergeConfig(commonViteConfig, {
    cacheDir:
      process.env.DEVFRONT_VITE_CACHE_DIR ??
      "/tmp/baron-sso-devfront-vite-cache",
    build: {
      outDir: buildOutDir,
    },
    server: {
      host: "127.0.0.1",
      allowedHosts,
      proxy: {
        "/api": {
          target: process.env.API_PROXY_TARGET || "http://localhost:3000",
          changeOrigin: true,
        },
      },
    },
    preview: {
      host: "127.0.0.1",
      port: 5173,
      allowedHosts,
      proxy: {
        "/api": {
          target: process.env.API_PROXY_TARGET || "http://localhost:3000",
          changeOrigin: true,
        },
      },
    },
  }),
);
