import { isElectron } from "../../../../libs/electron.js";
import { APP_VERSION } from "../../../constants.js";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { canMethodHaveBody } from "@scalar/helpers/http/can-method-have-body";
//#region src/v2/blocks/request-block/helpers/get-default-headers.ts
/** Default Accept header value to accept all response types. */
var DEFAULT_ACCEPT = "*/*";
/**
* Creates a default header object with the override status determined by existing headers.
*
* @param name The header name
* @param defaultValue The header value
* @param existingHeaders Set of existing header names (lowercase) from the operation
* @returns A DefaultHeader object
*/
var createDefaultHeader = (name, defaultValue, existingHeaders) => ({
	name,
	defaultValue,
	isOverridden: existingHeaders.has(name.toLowerCase())
});
/**
* Generates a list of default headers for an OpenAPI operation and HTTP method.
*
* This function intelligently adds standard HTTP headers based on the request context:
* - Content-Type: Added only if the HTTP method supports a request body and the OpenAPI operation
*   defines a request body content type. Uses the selected content type from the operation or the
*   first defined request body content type.
* - Accept: Derived from the 2xx response content types in the spec (joined as a comma-separated list), falling back to a wildcard.
* - User-Agent: Added in Electron environments (desktop app or proxy) to identify the client.
*
* The function respects OpenAPI operation parameters and marks headers as overridden
* if they are explicitly defined in the operation. It also supports hiding disabled
* headers for specific request examples using x-scalar-disable-parameters.
*
* @param method The HTTP method of the operation (GET, POST, etc.)
* @param operation The OpenAPI OperationObject describing the endpoint
* @param exampleKey The current request example key
* @param hideDisabledHeaders If true, filters out headers marked as disabled for this example
* @returns Array of default header objects with their values and override status
*/
var getDefaultHeaders = ({ method, operation, exampleKey, hideDisabledHeaders = false }) => {
	const existingHeaders = new Set(operation.parameters?.filter((param) => getResolvedRef(param).in === "header").map((param) => getResolvedRef(param).name.toLowerCase()) ?? []);
	const disabledHeaders = operation["x-scalar-disable-parameters"]?.["default-headers"]?.[exampleKey] ?? {};
	const headers = [];
	const requestBody = getResolvedRef(operation.requestBody);
	if (canMethodHaveBody(method) && requestBody) {
		const contentType = requestBody["x-scalar-selected-content-type"]?.[exampleKey] ?? Object.keys(requestBody.content ?? {})[0];
		if (contentType && contentType !== "none") headers.push(createDefaultHeader("Content-Type", contentType, existingHeaders));
	}
	const successResponseKey = Object.keys(operation.responses ?? {}).find((k) => k.startsWith("2"));
	const successResponse = successResponseKey ? getResolvedRef(operation.responses[successResponseKey]) : null;
	const acceptValue = Object.keys(successResponse?.content ?? {}).join(", ") || DEFAULT_ACCEPT;
	headers.push(createDefaultHeader("Accept", acceptValue, existingHeaders));
	if (isElectron() && APP_VERSION) headers.push(createDefaultHeader("User-Agent", `Scalar/${APP_VERSION}`, existingHeaders));
	if (hideDisabledHeaders) return headers.filter((header) => disabledHeaders[header.name.toLowerCase()] !== true);
	return headers;
};
//#endregion
export { getDefaultHeaders };

//# sourceMappingURL=get-default-headers.js.map