import { combineUrlAndPath } from "@scalar/helpers/url/merge-urls";
//#region src/v2/helpers/get-servers.ts
/**
* Retrieves and processes servers from an OpenAPI document.
*
* This function handles several scenarios:
* 1. No servers provided - creates a default server from document URL or fallback
* 2. Invalid server configurations - filters them out with warnings
* 3. Relative URLs - resolves them to absolute URLs using available base URLs
*
* @param servers - Array of OpenAPI server objects from the document
* @param options - Configuration options for server processing
* @returns Array of validated Server entities
*/
function getServers(servers, options = {}) {
	if (!servers?.length) {
		const fallbackServer = createFallbackServer(options);
		return fallbackServer ? [fallbackServer] : [];
	}
	if (!Array.isArray(servers)) return [];
	const validServers = servers.map((server) => processServerObject(server, options));
	if (validServers.length === 0) {
		const fallbackServer = createFallbackServer(options);
		return fallbackServer ? [fallbackServer] : [];
	}
	return validServers;
}
/**
* Extracts the base URL (protocol + hostname) from a document URL.
* Returns undefined if the URL is invalid.
*/
function extractBaseUrlFromDocumentUrl(documentUrl) {
	try {
		const url = new URL(documentUrl);
		const port = url.port ? `:${url.port}` : "";
		return `${url.protocol}//${url.hostname}${port}`;
	} catch {
		return;
	}
}
/**
* Gets the fallback URL from window.location.origin if available.
*/
function getFallbackUrl() {
	if (typeof window === "undefined" || typeof window?.location?.origin !== "string") return;
	return window.location.origin;
}
/**
* Creates a server object from a URL string, with error handling.
*/
function createServerFromUrl(url) {
	return { url };
}
/**
* Creates a default server using the document URL as the base.
*/
function createDefaultServerFromDocumentUrl(documentUrl) {
	const baseUrl = extractBaseUrlFromDocumentUrl(documentUrl);
	if (!baseUrl) return;
	return createServerFromUrl(baseUrl);
}
/**
* Creates a default server using the fallback URL (window.location.origin).
*/
function createDefaultServerFromFallback() {
	const fallbackUrl = getFallbackUrl();
	if (!fallbackUrl) return;
	return createServerFromUrl(fallbackUrl);
}
/**
* Resolves a relative server URL to an absolute URL using available base URLs.
* Uses a priority system: baseServerURL > documentUrl > fallbackUrl.
*/
function resolveRelativeServerUrl(serverUrl, options) {
	const { baseServerUrl: baseServerURL, documentUrl } = options;
	if (baseServerURL) return combineUrlAndPath(baseServerURL, serverUrl);
	if (documentUrl) {
		const baseUrl = extractBaseUrlFromDocumentUrl(documentUrl);
		if (baseUrl) return combineUrlAndPath(baseUrl, serverUrl);
	}
	const fallbackUrl = getFallbackUrl();
	if (fallbackUrl) return combineUrlAndPath(fallbackUrl, serverUrl);
	return serverUrl;
}
/**
* Processes a single server object, handling validation and URL resolution.
*/
function processServerObject(server, options) {
	if (server.url?.startsWith("/")) server.url = resolveRelativeServerUrl(server.url, options);
	return server;
}
/**
* Creates a fallback server when no valid servers are available.
* Uses document URL first, then fallback URL.
*/
function createFallbackServer(options) {
	if (options.documentUrl) {
		const defaultServer = createDefaultServerFromDocumentUrl(options.documentUrl);
		if (defaultServer) return defaultServer;
	}
	return createDefaultServerFromFallback();
}
//#endregion
export { getServers };

//# sourceMappingURL=get-servers.js.map