import { API_URL_ROOT } from "../../../api";

const GOONG_TILES_BASE_URL = "https://tiles.goong.io";

export const API_BASE_URL = normalizeApiBaseUrl(API_URL_ROOT);

export const GOONG_SATELLITE_STYLE_UPSTREAM_URL = `${GOONG_TILES_BASE_URL}/assets/goong_satellite.json`;
export const GOONG_VECTOR_OVERLAY_STYLE_UPSTREAM_URL = `${GOONG_TILES_BASE_URL}/assets/goong_map_web.json`;
export const GOONG_GLYPHS_UPSTREAM_URL = `${GOONG_TILES_BASE_URL}/fonts/{fontstack}/{range}.pbf`;

export const USE_EXTERNAL_BACKGROUND_RASTER = API_BASE_URL.length > 0;

function normalizeApiBaseUrl(rawUrl: string): string {
    return rawUrl.trim().replace(/\/+$/, "");
}

export function stripGoongApiKeyFromUrl(rawUrl: string): string {
    const [basePart, hashPart = ""] = rawUrl.split("#", 2);
    const [pathPart, queryString = ""] = basePart.split("?", 2);
    const sanitizedQuery = queryString
        .split("&")
        .filter((segment) => segment && !segment.toLowerCase().startsWith("api_key="))
        .join("&");

    return `${pathPart}${sanitizedQuery ? `?${sanitizedQuery}` : ""}${hashPart ? `#${hashPart}` : ""}`;
}

export function buildGoongProxyUrl(rawUrl: string): string {
    const sanitizedUrl = stripGoongApiKeyFromUrl(rawUrl);
    const proxyTarget = sanitizedUrl
        .trim()
        .replace(/^https?:\/\//i, "")
        .replace(/^\/+/, "");

    const isMapProxy = !proxyTarget.startsWith("rsapi.goong.io");
    const proxyPrefix = isMapProxy ? `${API_BASE_URL}/map/proxy` : `${API_BASE_URL}/api/proxy`;

    return `${proxyPrefix}/${proxyTarget}`;
}

export const GOONG_GLYPHS_PROXY_URL = buildGoongProxyUrl(GOONG_GLYPHS_UPSTREAM_URL);

export const API_ENDPOINTS = {
    geometries: `${API_BASE_URL}/geometries`,
    entities: `${API_BASE_URL}/entities`,
    wikis: `${API_BASE_URL}/wikis`,
    relations: `${API_BASE_URL}/relations`,
    wikiContent: (id: string) => `${API_BASE_URL}/wikis/content/${id}`,
    // New API uses projects + commits + submissions (JWT-protected).
    authSignin: `${API_BASE_URL}/auth/signin`,
    authRefresh: `${API_BASE_URL}/auth/refresh`,
    authLogout: `${API_BASE_URL}/auth/logout`,
    currentUser: `${API_BASE_URL}/users/current`,
    currentUserProjects: `${API_BASE_URL}/users/current/project`,
    projects: `${API_BASE_URL}/projects`,
    submissions: `${API_BASE_URL}/submissions`,
    battleReplays: `${API_BASE_URL}/battle-replays`,
} as const;
